2 * Copyright (c) 2015-19 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include "frr_pthread.h"
27 #include "zlog_targets.h"
29 /* these allocations are intentionally left active even when doing full exit
30 * cleanup, in order to keep the logging subsystem fully functional until the
35 DEFINE_MGROUP_ACTIVEATEXIT(LOG
, "logging subsystem")
37 DEFINE_MTYPE_STATIC(LOG
, LOG_FD
, "log file target")
38 DEFINE_MTYPE_STATIC(LOG
, LOG_FD_NAME
, "log file name")
39 DEFINE_MTYPE_STATIC(LOG
, LOG_FD_ROTATE
, "log file rotate helper")
40 DEFINE_MTYPE_STATIC(LOG
, LOG_SYSL
, "syslog target")
43 struct zlog_target zt
;
45 atomic_uint_fast32_t fd
;
50 struct rcu_head_close head_close
;
53 static const char * const prionames
[] = {
54 [LOG_EMERG
] = "emergencies: ",
55 [LOG_ALERT
] = "alerts: ",
56 [LOG_CRIT
] = "critical: ",
57 [LOG_ERR
] = "errors: ",
58 [LOG_WARNING
] = "warnings: ",
59 [LOG_NOTICE
] = "notifications: ",
60 [LOG_INFO
] = "informational: ",
61 [LOG_DEBUG
] = "debugging: ",
64 void zlog_fd(struct zlog_target
*zt
, struct zlog_msg
*msgs
[], size_t nmsgs
)
66 struct zlt_fd
*zte
= container_of(zt
, struct zlt_fd
, zt
);
68 size_t i
, textlen
, iovpos
= 0;
69 size_t niov
= MIN(4 * nmsgs
+ 1, IOV_MAX
);
70 struct iovec iov
[niov
];
71 /* "\nYYYY-MM-DD HH:MM:SS.NNNNNNNNN+ZZ:ZZ " = 37 chars */
73 char ts_buf
[TS_LEN
* nmsgs
], *ts_pos
= ts_buf
;
75 fd
= atomic_load_explicit(&zte
->fd
, memory_order_relaxed
);
77 for (i
= 0; i
< nmsgs
; i
++) {
78 struct zlog_msg
*msg
= msgs
[i
];
79 int prio
= zlog_msg_prio(msg
);
81 if (prio
> zt
->prio_min
)
84 iov
[iovpos
].iov_base
= ts_pos
;
87 ts_pos
+= zlog_msg_ts(msg
, ts_pos
, sizeof(ts_buf
) - 1
89 ZLOG_TS_LEGACY
| zte
->ts_subsec
);
91 iov
[iovpos
].iov_len
= ts_pos
- (char *)iov
[iovpos
].iov_base
;
95 if (zte
->record_priority
) {
96 iov
[iovpos
].iov_base
= (char *)prionames
[prio
];
97 iov
[iovpos
].iov_len
= strlen(iov
[iovpos
].iov_base
);
102 iov
[iovpos
].iov_base
= zlog_prefix
;
103 iov
[iovpos
].iov_len
= zlog_prefixsz
;
107 iov
[iovpos
].iov_base
= (char *)zlog_msg_text(msg
, &textlen
);
108 iov
[iovpos
].iov_len
= textlen
;
112 if (ts_buf
+ sizeof(ts_buf
) - ts_pos
< TS_LEN
114 || array_size(iov
) - iovpos
< 5) {
115 iov
[iovpos
].iov_base
= (char *)"\n";
116 iov
[iovpos
].iov_len
= 1;
120 writev(fd
, iov
, iovpos
);
130 static void zlog_fd_sigsafe(struct zlog_target
*zt
, const char *text
,
133 struct zlt_fd
*zte
= container_of(zt
, struct zlt_fd
, zt
);
137 iov
[0].iov_base
= (char *)prionames
[LOG_CRIT
];
138 iov
[0].iov_len
= zte
->record_priority
? strlen(iov
[0].iov_base
) : 0;
140 iov
[1].iov_base
= zlog_prefix
;
141 iov
[1].iov_len
= zlog_prefixsz
;
143 iov
[2].iov_base
= (char *)text
;
144 iov
[2].iov_len
= len
;
146 iov
[3].iov_base
= (char *)"\n";
149 fd
= atomic_load_explicit(&zte
->fd
, memory_order_relaxed
);
151 writev(fd
, iov
, array_size(iov
));
158 void zlog_file_init(struct zlog_cfg_file
*zcf
)
160 memset(zcf
, 0, sizeof(*zcf
));
161 zcf
->prio_min
= ZLOG_DISABLED
;
163 pthread_mutex_init(&zcf
->cfg_mtx
, NULL
);
166 static void zlog_file_target_free(struct zlt_fd
*zlt
)
171 rcu_close(&zlt
->head_close
, zlt
->fd
);
172 rcu_free(MTYPE_LOG_FD
, zlt
, zt
.rcu_head
);
175 void zlog_file_fini(struct zlog_cfg_file
*zcf
)
179 struct zlog_target
*zt
;
181 zt
= zlog_target_replace(&zcf
->active
->zt
, NULL
);
182 ztf
= container_of(zt
, struct zlt_fd
, zt
);
183 zlog_file_target_free(ztf
);
185 XFREE(MTYPE_LOG_FD_NAME
, zcf
->filename
);
186 pthread_mutex_destroy(&zcf
->cfg_mtx
);
189 static bool zlog_file_cycle(struct zlog_cfg_file
*zcf
)
191 struct zlog_target
*zt
, *old
;
192 struct zlt_fd
*zlt
= NULL
;
197 if (zcf
->prio_min
== ZLOG_DISABLED
)
202 else if (zcf
->filename
)
203 fd
= open(zcf
->filename
,
204 O_WRONLY
| O_APPEND
| O_CREAT
| O_CLOEXEC
215 zt
= zlog_target_clone(MTYPE_LOG_FD
, &zcf
->active
->zt
,
217 zlt
= container_of(zt
, struct zlt_fd
, zt
);
220 zlt
->record_priority
= zcf
->record_priority
;
221 zlt
->ts_subsec
= zcf
->ts_subsec
;
223 zlt
->zt
.prio_min
= zcf
->prio_min
;
224 zlt
->zt
.logfn
= zcf
->zlog_wrap
? zcf
->zlog_wrap
: zlog_fd
;
225 zlt
->zt
.logfn_sigsafe
= zlog_fd_sigsafe
;
228 old
= zlog_target_replace(&zcf
->active
->zt
, &zlt
->zt
);
231 zlog_file_target_free(container_of(old
, struct zlt_fd
, zt
));
236 void zlog_file_set_other(struct zlog_cfg_file
*zcf
)
238 frr_with_mutex(&zcf
->cfg_mtx
) {
239 zlog_file_cycle(zcf
);
243 bool zlog_file_set_filename(struct zlog_cfg_file
*zcf
, const char *filename
)
245 frr_with_mutex(&zcf
->cfg_mtx
) {
246 XFREE(MTYPE_LOG_FD_NAME
, zcf
->filename
);
247 zcf
->filename
= XSTRDUP(MTYPE_LOG_FD_NAME
, filename
);
250 return zlog_file_cycle(zcf
);
255 bool zlog_file_set_fd(struct zlog_cfg_file
*zcf
, int fd
)
257 frr_with_mutex(&zcf
->cfg_mtx
) {
261 XFREE(MTYPE_LOG_FD_NAME
, zcf
->filename
);
264 return zlog_file_cycle(zcf
);
269 struct rcu_close_rotate
{
270 struct rcu_head_close head_close
;
271 struct rcu_head head_self
;
274 bool zlog_file_rotate(struct zlog_cfg_file
*zcf
)
276 struct rcu_close_rotate
*rcr
;
279 frr_with_mutex(&zcf
->cfg_mtx
) {
280 if (!zcf
->active
|| !zcf
->filename
)
283 fd
= open(zcf
->filename
,
284 O_WRONLY
| O_APPEND
| O_CREAT
| O_CLOEXEC
| O_NOCTTY
,
289 fd
= atomic_exchange_explicit(&zcf
->active
->fd
,
291 memory_order_relaxed
);
294 rcr
= XCALLOC(MTYPE_LOG_FD_ROTATE
, sizeof(*rcr
));
295 rcu_close(&rcr
->head_close
, fd
);
296 rcu_free(MTYPE_LOG_FD_ROTATE
, rcr
, head_self
);
301 /* fixed crash logging */
303 static struct zlt_fd zlog_crashlog
;
305 static void zlog_crashlog_sigsafe(struct zlog_target
*zt
, const char *text
,
308 static int crashlog_fd
= -1;
310 if (crashlog_fd
== -1) {
312 crashlog_fd
= openat(zlog_tmpdirfd
, "crashlog",
313 O_WRONLY
| O_APPEND
| O_CREAT
,
320 if (crashlog_fd
== -2)
323 zlog_crashlog
.fd
= crashlog_fd
;
324 zlog_fd_sigsafe(&zlog_crashlog
.zt
, text
, len
);
327 /* this is used for assert failures (they don't need AS-Safe logging) */
328 static void zlog_crashlog_plain(struct zlog_target
*zt
, struct zlog_msg
*msgs
[],
334 for (i
= 0; i
< nmsgs
; i
++) {
335 if (zlog_msg_prio(msgs
[i
]) > zt
->prio_min
)
338 text
= zlog_msg_text(msgs
[i
], &len
);
339 zlog_crashlog_sigsafe(zt
, text
, len
);
343 static void zlog_crashlog_init(void)
345 zlog_crashlog
.zt
.prio_min
= LOG_CRIT
;
346 zlog_crashlog
.zt
.logfn
= zlog_crashlog_plain
;
347 zlog_crashlog
.zt
.logfn_sigsafe
= zlog_crashlog_sigsafe
;
348 zlog_crashlog
.fd
= -1;
350 zlog_target_replace(NULL
, &zlog_crashlog
.zt
);
353 /* fixed logging for test/auxiliary programs */
355 static struct zlt_fd zlog_aux_stdout
;
356 static bool zlog_is_aux
;
358 static int zlt_aux_init(const char *prefix
, int prio_min
)
362 zlog_aux_stdout
.zt
.prio_min
= prio_min
;
363 zlog_aux_stdout
.zt
.logfn
= zlog_fd
;
364 zlog_aux_stdout
.zt
.logfn_sigsafe
= zlog_fd_sigsafe
;
365 zlog_aux_stdout
.fd
= STDOUT_FILENO
;
367 zlog_target_replace(NULL
, &zlog_aux_stdout
.zt
);
372 static int zlt_init(const char *progname
, const char *protoname
,
373 unsigned short instance
, uid_t uid
, gid_t gid
)
375 openlog(progname
, LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
379 static int zlt_fini(void)
385 /* fixed startup logging to stderr */
387 static struct zlt_fd zlog_startup_stderr
;
389 __attribute__((_CONSTRUCTOR(450))) static void zlog_startup_init(void)
391 zlog_startup_stderr
.zt
.prio_min
= LOG_WARNING
;
392 zlog_startup_stderr
.zt
.logfn
= zlog_fd
;
393 zlog_startup_stderr
.zt
.logfn_sigsafe
= zlog_fd_sigsafe
;
394 zlog_startup_stderr
.fd
= STDERR_FILENO
;
396 zlog_target_replace(NULL
, &zlog_startup_stderr
.zt
);
398 hook_register(zlog_aux_init
, zlt_aux_init
);
399 hook_register(zlog_init
, zlt_init
);
400 hook_register(zlog_fini
, zlt_fini
);
403 void zlog_startup_end(void)
405 static bool startup_ended
= false;
409 startup_ended
= true;
411 zlog_target_replace(&zlog_startup_stderr
.zt
, NULL
);
416 /* until here, crashlogs go to stderr */
417 zlog_crashlog_init();
423 struct zlog_target zt
;
428 static void zlog_syslog(struct zlog_target
*zt
, struct zlog_msg
*msgs
[],
432 struct zlt_syslog
*zte
= container_of(zt
, struct zlt_syslog
, zt
);
434 for (i
= 0; i
< nmsgs
; i
++) {
435 if (zlog_msg_prio(msgs
[i
]) > zt
->prio_min
)
438 syslog(zlog_msg_prio(msgs
[i
]) | zte
->syslog_facility
, "%s",
439 zlog_msg_text(msgs
[i
], NULL
));
444 #define _PATH_LOG "/dev/log"
447 static void zlog_syslog_sigsafe(struct zlog_target
*zt
, const char *text
,
450 static int syslog_fd
= -1;
456 if (syslog_fd
== -1) {
457 syslog_fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
458 if (syslog_fd
>= 0) {
459 struct sockaddr_un sa
;
460 socklen_t salen
= sizeof(sa
);
462 sa
.sun_family
= AF_UNIX
;
463 strlcpy(sa
.sun_path
, _PATH_LOG
, sizeof(sa
.sun_path
));
464 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
465 salen
= sa
.sun_len
= SUN_LEN(&sa
);
467 if (connect(syslog_fd
, (struct sockaddr
*)&sa
, salen
)) {
473 /* /dev/log could be a fifo instead of a socket */
474 if (syslog_fd
== -1) {
475 syslog_fd
= open(_PATH_LOG
, O_WRONLY
| O_NOCTTY
);
485 /* note zlog_prefix includes trailing ": ", need to cut off 2 chars */
486 hdrlen
= snprintfrr(hdr
, sizeof(hdr
), "<%d>%.*s[%ld]: ", LOG_CRIT
,
487 zlog_prefixsz
> 2 ? (int)(zlog_prefixsz
- 2) : 0,
488 zlog_prefix
, (long)getpid());
490 iov
[0].iov_base
= hdr
;
491 iov
[0].iov_len
= hdrlen
;
493 iov
[1].iov_base
= (char *)text
;
494 iov
[1].iov_len
= len
;
496 writev(syslog_fd
, iov
, array_size(iov
));
500 static pthread_mutex_t syslog_cfg_mutex
= PTHREAD_MUTEX_INITIALIZER
;
501 static struct zlt_syslog
*zlt_syslog
;
502 static int syslog_facility
= LOG_DAEMON
;
503 static int syslog_prio_min
= ZLOG_DISABLED
;
505 void zlog_syslog_set_facility(int facility
)
507 struct zlog_target
*newztc
;
508 struct zlt_syslog
*newzt
;
510 frr_with_mutex(&syslog_cfg_mutex
) {
511 if (facility
== syslog_facility
)
513 syslog_facility
= facility
;
515 if (syslog_prio_min
== ZLOG_DISABLED
)
518 newztc
= zlog_target_clone(MTYPE_LOG_SYSL
, &zlt_syslog
->zt
,
520 newzt
= container_of(newztc
, struct zlt_syslog
, zt
);
521 newzt
->syslog_facility
= syslog_facility
;
523 zlog_target_free(MTYPE_LOG_SYSL
,
524 zlog_target_replace(&zlt_syslog
->zt
,
531 int zlog_syslog_get_facility(void)
533 frr_with_mutex(&syslog_cfg_mutex
) {
534 return syslog_facility
;
539 void zlog_syslog_set_prio_min(int prio_min
)
541 struct zlog_target
*newztc
;
542 struct zlt_syslog
*newzt
= NULL
;
544 frr_with_mutex(&syslog_cfg_mutex
) {
545 if (prio_min
== syslog_prio_min
)
547 syslog_prio_min
= prio_min
;
549 if (syslog_prio_min
!= ZLOG_DISABLED
) {
550 newztc
= zlog_target_clone(MTYPE_LOG_SYSL
,
553 newzt
= container_of(newztc
, struct zlt_syslog
, zt
);
554 newzt
->zt
.prio_min
= prio_min
;
555 newzt
->zt
.logfn
= zlog_syslog
;
556 newzt
->zt
.logfn_sigsafe
= zlog_syslog_sigsafe
;
557 newzt
->syslog_facility
= syslog_facility
;
560 zlog_target_free(MTYPE_LOG_SYSL
,
561 zlog_target_replace(&zlt_syslog
->zt
,
568 int zlog_syslog_get_prio_min(void)
570 frr_with_mutex(&syslog_cfg_mutex
) {
571 return syslog_prio_min
;