]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/syslog-util.c
util: introduce memcmp_safe()
[thirdparty/systemd.git] / src / basic / syslog-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <string.h>
4 #include <syslog.h>
5
6 #include "hexdecoct.h"
7 #include "macro.h"
8 #include "string-table.h"
9 #include "syslog-util.h"
10
11 int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
12 int a = 0, b = 0, c = 0;
13 int k;
14
15 assert(p);
16 assert(*p);
17 assert(priority);
18
19 if ((*p)[0] != '<')
20 return 0;
21
22 if (!strchr(*p, '>'))
23 return 0;
24
25 if ((*p)[2] == '>') {
26 c = undecchar((*p)[1]);
27 k = 3;
28 } else if ((*p)[3] == '>') {
29 b = undecchar((*p)[1]);
30 c = undecchar((*p)[2]);
31 k = 4;
32 } else if ((*p)[4] == '>') {
33 a = undecchar((*p)[1]);
34 b = undecchar((*p)[2]);
35 c = undecchar((*p)[3]);
36 k = 5;
37 } else
38 return 0;
39
40 if (a < 0 || b < 0 || c < 0 ||
41 (!with_facility && (a || b || c > 7)))
42 return 0;
43
44 if (with_facility)
45 *priority = a*100 + b*10 + c;
46 else
47 *priority = (*priority & LOG_FACMASK) | c;
48
49 *p += k;
50 return 1;
51 }
52
53 static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
54 [LOG_FAC(LOG_KERN)] = "kern",
55 [LOG_FAC(LOG_USER)] = "user",
56 [LOG_FAC(LOG_MAIL)] = "mail",
57 [LOG_FAC(LOG_DAEMON)] = "daemon",
58 [LOG_FAC(LOG_AUTH)] = "auth",
59 [LOG_FAC(LOG_SYSLOG)] = "syslog",
60 [LOG_FAC(LOG_LPR)] = "lpr",
61 [LOG_FAC(LOG_NEWS)] = "news",
62 [LOG_FAC(LOG_UUCP)] = "uucp",
63 [LOG_FAC(LOG_CRON)] = "cron",
64 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
65 [LOG_FAC(LOG_FTP)] = "ftp",
66 [LOG_FAC(LOG_LOCAL0)] = "local0",
67 [LOG_FAC(LOG_LOCAL1)] = "local1",
68 [LOG_FAC(LOG_LOCAL2)] = "local2",
69 [LOG_FAC(LOG_LOCAL3)] = "local3",
70 [LOG_FAC(LOG_LOCAL4)] = "local4",
71 [LOG_FAC(LOG_LOCAL5)] = "local5",
72 [LOG_FAC(LOG_LOCAL6)] = "local6",
73 [LOG_FAC(LOG_LOCAL7)] = "local7"
74 };
75
76 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
77
78 bool log_facility_unshifted_is_valid(int facility) {
79 return facility >= 0 && facility <= LOG_FAC(~0);
80 }
81
82 static const char *const log_level_table[] = {
83 [LOG_EMERG] = "emerg",
84 [LOG_ALERT] = "alert",
85 [LOG_CRIT] = "crit",
86 [LOG_ERR] = "err",
87 [LOG_WARNING] = "warning",
88 [LOG_NOTICE] = "notice",
89 [LOG_INFO] = "info",
90 [LOG_DEBUG] = "debug"
91 };
92
93 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
94
95 bool log_level_is_valid(int level) {
96 return level >= 0 && level <= LOG_DEBUG;
97 }