/src/sudo/lib/util/logpri.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 1999-2005, 2007-2019  | 
5  |  |  *  Todd C. Miller <Todd.Miller@sudo.ws>  | 
6  |  |  *  | 
7  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
8  |  |  * purpose with or without fee is hereby granted, provided that the above  | 
9  |  |  * copyright notice and this permission notice appear in all copies.  | 
10  |  |  *  | 
11  |  |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
12  |  |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
13  |  |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
14  |  |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
15  |  |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
16  |  |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
17  |  |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
18  |  |  *  | 
19  |  |  * Sponsored in part by the Defense Advanced Research Projects  | 
20  |  |  * Agency (DARPA) and Air Force Research Laboratory, Air Force  | 
21  |  |  * Materiel Command, USAF, under agreement number F39502-99-1-0512.  | 
22  |  |  */  | 
23  |  |  | 
24  |  | #include <config.h>  | 
25  |  |  | 
26  |  | #include <string.h>  | 
27  |  | #include <syslog.h>  | 
28  |  |  | 
29  |  | #include <sudo_compat.h>  | 
30  |  | #include <sudo_debug.h>  | 
31  |  | #include <sudo_util.h>  | 
32  |  |  | 
33  |  | /*  | 
34  |  |  * For converting between syslog numbers and strings.  | 
35  |  |  */  | 
36  |  | struct strmap { | 
37  |  |     const char *name;  | 
38  |  |     int num;  | 
39  |  | };  | 
40  |  |  | 
41  |  | static const struct strmap priorities[] = { | 
42  |  |   { "alert",  LOG_ALERT }, | 
43  |  |   { "crit", LOG_CRIT }, | 
44  |  |   { "debug",  LOG_DEBUG }, | 
45  |  |   { "emerg",  LOG_EMERG }, | 
46  |  |   { "err",  LOG_ERR }, | 
47  |  |   { "info", LOG_INFO }, | 
48  |  |   { "notice", LOG_NOTICE }, | 
49  |  |   { "warning",  LOG_WARNING }, | 
50  |  |   { "none", -1 }, | 
51  |  |   { NULL,   -1 } | 
52  |  | };  | 
53  |  |  | 
54  |  | bool  | 
55  |  | sudo_str2logpri_v1(const char *str, int *logpri)  | 
56  | 135k  | { | 
57  | 135k  |     const struct strmap *pri;  | 
58  | 135k  |     debug_decl(sudo_str2logpri, SUDO_DEBUG_UTIL);  | 
59  |  |  | 
60  | 534k  |     for (pri = priorities; pri->name != NULL; pri++) { | 
61  | 534k  |   if (strcmp(str, pri->name) == 0) { | 
62  | 135k  |       *logpri = pri->num;  | 
63  | 135k  |       debug_return_bool(true);  | 
64  | 135k  |   }  | 
65  | 534k  |     }  | 
66  | 64  |     debug_return_bool(false);  | 
67  | 64  | }  | 
68  |  |  | 
69  |  | const char *  | 
70  |  | sudo_logpri2str_v1(int num)  | 
71  | 3.90k  | { | 
72  | 3.90k  |     const struct strmap *pri;  | 
73  | 3.90k  |     debug_decl(sudo_logpri2str, SUDO_DEBUG_UTIL);  | 
74  |  |  | 
75  | 15.6k  |     for (pri = priorities; pri->name != NULL; pri++) { | 
76  | 15.6k  |   if (pri->num == num)  | 
77  | 3.90k  |       break;  | 
78  | 15.6k  |     }  | 
79  | 3.90k  |     debug_return_const_str(pri->name);  | 
80  | 3.90k  | }  |