7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering/***
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering This file is part of systemd.
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering Copyright 2010 Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering systemd is free software; you can redistribute it and/or modify it
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering under the terms of the GNU Lesser General Public License as published by
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering (at your option) any later version.
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering systemd is distributed in the hope that it will be useful, but
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering Lesser General Public License for more details.
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering You should have received a copy of the GNU Lesser General Public License
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering***/
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <string.h>
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering#include <syslog.h>
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering#include "hexdecoct.h"
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include "macro.h"
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering#include "string-table.h"
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering#include "syslog-util.h"
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poetteringint syslog_parse_priority(const char **p, int *priority, bool with_facility) {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering int a = 0, b = 0, c = 0;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering int k;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering assert(p);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering assert(*p);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering assert(priority);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering if ((*p)[0] != '<')
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return 0;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering if (!strchr(*p, '>'))
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return 0;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering if ((*p)[2] == '>') {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering c = undecchar((*p)[1]);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering k = 3;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering } else if ((*p)[3] == '>') {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering b = undecchar((*p)[1]);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering c = undecchar((*p)[2]);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering k = 4;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering } else if ((*p)[4] == '>') {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering a = undecchar((*p)[1]);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering b = undecchar((*p)[2]);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering c = undecchar((*p)[3]);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering k = 5;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering } else
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return 0;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering if (a < 0 || b < 0 || c < 0 ||
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering (!with_facility && (a || b || c > 7)))
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return 0;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering if (with_facility)
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering *priority = a*100 + b*10 + c;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering else
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering *priority = (*priority & LOG_FACMASK) | c;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering *p += k;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return 1;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering}
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poetteringstatic const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_KERN)] = "kern",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_USER)] = "user",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_MAIL)] = "mail",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_DAEMON)] = "daemon",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_AUTH)] = "auth",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_SYSLOG)] = "syslog",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LPR)] = "lpr",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_NEWS)] = "news",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_UUCP)] = "uucp",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_CRON)] = "cron",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_FTP)] = "ftp",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL0)] = "local0",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL1)] = "local1",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL2)] = "local2",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL3)] = "local3",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL4)] = "local4",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL5)] = "local5",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL6)] = "local6",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_FAC(LOG_LOCAL7)] = "local7"
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering};
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poetteringbool log_facility_unshifted_is_valid(int facility) {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return facility >= 0 && facility <= LOG_FAC(~0);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering}
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poetteringstatic const char *const log_level_table[] = {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_EMERG] = "emerg",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_ALERT] = "alert",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_CRIT] = "crit",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_ERR] = "err",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_WARNING] = "warning",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_NOTICE] = "notice",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_INFO] = "info",
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering [LOG_DEBUG] = "debug"
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering};
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poetteringbool log_level_is_valid(int level) {
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering return level >= 0 && level <= LOG_DEBUG;
7ccbd1ae843d77275f2c542582a9a80e5e058a70Lennart Poettering}