-
Notifications
You must be signed in to change notification settings - Fork 909
/
log_systemd_mod.c
216 lines (183 loc) · 5.45 KB
/
log_systemd_mod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright (C) 2015 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-journal.h>
#include "../../core/sr_module.h"
#include "../../core/dprint.h"
#include "../../core/ut.h"
#include "../../core/mod_fix.h"
#include "../../core/kemi.h"
#include "journal_send.h"
MODULE_VERSION
static int _lc_log_systemd = 0;
void _lc_core_log_systemd(int lpriority, const char *format, ...);
static int mod_init(void);
static int child_init(int);
static void mod_destroy(void);
static int w_sd_journal_print(struct sip_msg *msg, char *lev, char *txt);
static int w_sd_journal_send_xavp(struct sip_msg *msg, char *xname, char *);
static cmd_export_t cmds[] = {
{"sd_journal_print", (cmd_function)w_sd_journal_print, 2,
fixup_spve_spve, 0, ANY_ROUTE},
{"sd_journal_send_xavp", (cmd_function)w_sd_journal_send_xavp, 1,
fixup_spve_spve, 0, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}};
static param_export_t params[] = {{0, 0, 0}};
struct module_exports exports = {
"log_systemd", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* cmd (cfg function) exports */
params, /* param exports */
0, /* RPC method exports */
0, /* pseudo-variables exports */
0, /* response handling function */
mod_init, /* module init function */
child_init, /* per-child init function */
mod_destroy /* module destroy function */
};
/**
* init module function
*/
static int mod_init(void)
{
return 0;
}
/**
* @brief Initialize async module children
*/
static int child_init(int rank)
{
return 0;
}
/**
* destroy module function
*/
static void mod_destroy(void)
{
}
/**
*
*/
static int ki_sd_journal_print(sip_msg_t *msg, str *slev, str *stxt)
{
int ilev;
/* one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING,
* LOG_NOTICE, LOG_INFO, LOG_DEBUG, as defined in syslog.h, see syslog(3) */
ilev = LOG_DEBUG;
if(slev->len == 9 && strncasecmp(slev->s, "LOG_EMERG", slev->len) == 0) {
ilev = LOG_EMERG;
} else if(slev->len == 9
&& strncasecmp(slev->s, "LOG_ALERT", slev->len) == 0) {
ilev = LOG_ALERT;
} else if(slev->len == 8
&& strncasecmp(slev->s, "LOG_CRIT", slev->len) == 0) {
ilev = LOG_CRIT;
} else if(slev->len == 7
&& strncasecmp(slev->s, "LOG_ERR", slev->len) == 0) {
ilev = LOG_ERR;
} else if(slev->len == 11
&& strncasecmp(slev->s, "LOG_WARNING", slev->len) == 0) {
ilev = LOG_WARNING;
} else if(slev->len == 10
&& strncasecmp(slev->s, "LOG_NOTICE", slev->len) == 0) {
ilev = LOG_NOTICE;
} else if(slev->len == 8
&& strncasecmp(slev->s, "LOG_INFO", slev->len) == 0) {
ilev = LOG_INFO;
}
sd_journal_print(ilev, "%.*s", stxt->len, stxt->s);
return 1;
}
static int w_sd_journal_print(struct sip_msg *msg, char *lev, char *txt)
{
str slev;
str stxt;
if(fixup_get_svalue(msg, (gparam_t *)lev, &slev) != 0) {
LM_ERR("unable to get level parameter\n");
return -1;
}
if(fixup_get_svalue(msg, (gparam_t *)txt, &stxt) != 0) {
LM_ERR("unable to get text parameter\n");
return -1;
}
return ki_sd_journal_print(msg, &slev, &stxt);
}
#define LC_LOG_MSG_MAX_SIZE 16384
void _lc_core_log_systemd(int lpriority, const char *format, ...)
{
va_list arglist;
char obuf[LC_LOG_MSG_MAX_SIZE];
int n;
int priority;
/* Return on MASKed log priorities */
priority = LOG_PRI(lpriority);
va_start(arglist, format);
n = 0;
n += vsnprintf(obuf + n, LC_LOG_MSG_MAX_SIZE - n, format, arglist);
va_end(arglist);
sd_journal_print(priority, "%.*s", n, obuf);
}
static int w_sd_journal_send_xavp(struct sip_msg *msg, char *xname, char *foo)
{
str sxname;
if(fixup_get_svalue(msg, (gparam_t *)xname, &sxname) != 0) {
LM_ERR("unable to get xname parameter\n");
return -1;
}
return k_sd_journal_send_xavp(&sxname);
}
static int ki_sd_journal_send_xavp(sip_msg_t *msg, str *xname)
{
return k_sd_journal_send_xavp(xname);
}
/**
*
*/
/* clang-format off */
static sr_kemi_t sr_kemi_log_systemd_exports[] = {
{ str_init("log_systemd"), str_init("sd_journal_print"),
SR_KEMIP_INT, ki_sd_journal_print,
{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
},
{ str_init("log_systemd"), str_init("sd_journal_send_xvap"),
SR_KEMIP_INT, ki_sd_journal_send_xavp,
{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
},
{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
};
/* clang-format on */
int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
if(_km_log_engine_type == 0)
return 0;
if(strcasecmp(_km_log_engine_type, "systemd") != 0)
return 0;
km_log_func_set(&_lc_core_log_systemd);
_lc_log_systemd = 1;
sr_kemi_modules_add(sr_kemi_log_systemd_exports);
return 0;
}