1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "path-util.h"
11 #include "string-table.h"
12 #include "syslog-util.h"
13 #include "unit-name.h"
15 int syslog_parse_priority(const char **p
, int *priority
, bool with_facility
) {
16 int a
= 0, b
= 0, c
= 0;
27 end
= strchr(*p
, '>');
35 c
= undecchar((*p
)[1]);
37 b
= undecchar((*p
)[1]);
38 c
= undecchar((*p
)[2]);
40 a
= undecchar((*p
)[1]);
41 b
= undecchar((*p
)[2]);
42 c
= undecchar((*p
)[3]);
46 if (a
< 0 || b
< 0 || c
< 0 ||
47 (!with_facility
&& (a
|| b
|| c
> 7)))
51 *priority
= a
*100 + b
*10 + c
;
53 *priority
= (*priority
& LOG_FACMASK
) | c
;
59 static const char *const log_facility_unshifted_table
[LOG_NFACILITIES
] = {
60 [LOG_FAC(LOG_KERN
)] = "kern",
61 [LOG_FAC(LOG_USER
)] = "user",
62 [LOG_FAC(LOG_MAIL
)] = "mail",
63 [LOG_FAC(LOG_DAEMON
)] = "daemon",
64 [LOG_FAC(LOG_AUTH
)] = "auth",
65 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
66 [LOG_FAC(LOG_LPR
)] = "lpr",
67 [LOG_FAC(LOG_NEWS
)] = "news",
68 [LOG_FAC(LOG_UUCP
)] = "uucp",
69 [LOG_FAC(LOG_CRON
)] = "cron",
70 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
71 [LOG_FAC(LOG_FTP
)] = "ftp",
72 [LOG_FAC(LOG_LOCAL0
)] = "local0",
73 [LOG_FAC(LOG_LOCAL1
)] = "local1",
74 [LOG_FAC(LOG_LOCAL2
)] = "local2",
75 [LOG_FAC(LOG_LOCAL3
)] = "local3",
76 [LOG_FAC(LOG_LOCAL4
)] = "local4",
77 [LOG_FAC(LOG_LOCAL5
)] = "local5",
78 [LOG_FAC(LOG_LOCAL6
)] = "local6",
79 [LOG_FAC(LOG_LOCAL7
)] = "local7"
82 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted
, int, LOG_FAC(~0));
84 bool log_facility_unshifted_is_valid(int facility
) {
85 return facility
>= 0 && facility
<= LOG_FAC(~0);
88 static const char *const log_level_table
[] = {
89 [LOG_EMERG
] = "emerg",
90 [LOG_ALERT
] = "alert",
93 [LOG_WARNING
] = "warning",
94 [LOG_NOTICE
] = "notice",
99 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level
, int, LOG_DEBUG
);
101 bool log_level_is_valid(int level
) {
102 return level
>= 0 && level
<= LOG_DEBUG
;
105 /* The maximum size for a log namespace length. This is the file name size limit 255 minus the size of a
106 * formatted machine ID minus a separator char */
107 #define LOG_NAMESPACE_MAX (NAME_MAX - (SD_ID128_STRING_MAX - 1) - 1)
109 bool log_namespace_name_valid(const char *s
) {
110 /* Let's make sure the namespace fits in a filename that is prefixed with the machine ID and a dot
111 * (so that /var/log/journal/<machine-id>.<namespace> can be created based on it). Also make sure it
112 * is suitable as unit instance name, and does not contain fishy characters. */
114 if (!filename_is_valid(s
))
117 if (strlen(s
) > LOG_NAMESPACE_MAX
)
120 if (!unit_instance_is_valid(s
))
123 if (!string_is_safe(s
))
126 /* Let's avoid globbing for now */
127 if (string_is_glob(s
))