1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2015-19 David Lamparter, for NetDEF, Inc.
13 #include "frr_pthread.h"
16 #include "zlog_targets.h"
18 /* these allocations are intentionally left active even when doing full exit
19 * cleanup, in order to keep the logging subsystem fully functional until the
23 DEFINE_MGROUP_ACTIVEATEXIT(LOG
, "logging subsystem");
25 DEFINE_MTYPE_STATIC(LOG
, LOG_FD
, "log file target");
26 DEFINE_MTYPE_STATIC(LOG
, LOG_FD_NAME
, "log file name");
27 DEFINE_MTYPE_STATIC(LOG
, LOG_FD_ROTATE
, "log file rotate helper");
28 DEFINE_MTYPE_STATIC(LOG
, LOG_SYSL
, "syslog target");
31 struct zlog_target zt
;
33 atomic_uint_fast32_t fd
;
38 struct rcu_head_close head_close
;
41 static const char * const prionames
[] = {
42 [LOG_EMERG
] = "emergencies: ",
43 [LOG_ALERT
] = "alerts: ",
44 [LOG_CRIT
] = "critical: ",
45 [LOG_ERR
] = "errors: ",
46 [LOG_WARNING
] = "warnings: ",
47 [LOG_NOTICE
] = "notifications: ",
48 [LOG_INFO
] = "informational: ",
49 [LOG_DEBUG
] = "debugging: ",
52 void zlog_fd(struct zlog_target
*zt
, struct zlog_msg
*msgs
[], size_t nmsgs
)
54 struct zlt_fd
*zte
= container_of(zt
, struct zlt_fd
, zt
);
56 size_t i
, textlen
, iovpos
= 0;
57 size_t niov
= MIN(4 * nmsgs
+ 1, IOV_MAX
);
58 struct iovec iov
[niov
];
59 /* "\nYYYY-MM-DD HH:MM:SS.NNNNNNNNN+ZZ:ZZ " = 37 chars */
61 char ts_buf
[TS_LEN
* nmsgs
], *ts_pos
= ts_buf
;
63 fd
= atomic_load_explicit(&zte
->fd
, memory_order_relaxed
);
65 for (i
= 0; i
< nmsgs
; i
++) {
66 struct zlog_msg
*msg
= msgs
[i
];
67 int prio
= zlog_msg_prio(msg
);
69 if (prio
<= zt
->prio_min
) {
73 .len
= sizeof(ts_buf
),
76 iov
[iovpos
].iov_base
= ts_pos
;
77 zlog_msg_ts(msg
, &fbuf
,
78 ZLOG_TS_LEGACY
| zte
->ts_subsec
);
83 ts_pos
- (char *)iov
[iovpos
].iov_base
;
87 if (zte
->record_priority
) {
88 iov
[iovpos
].iov_base
= (char *)prionames
[prio
];
90 strlen(iov
[iovpos
].iov_base
);
95 iov
[iovpos
].iov_base
= zlog_prefix
;
96 iov
[iovpos
].iov_len
= zlog_prefixsz
;
100 iov
[iovpos
].iov_base
=
101 (char *)zlog_msg_text(msg
, &textlen
);
102 iov
[iovpos
].iov_len
= textlen
+ 1;
107 /* conditions that trigger writing:
108 * - out of space for more timestamps/headers
109 * - this being the last message in the batch
110 * - not enough remaining iov entries
112 if (iovpos
> 0 && (ts_buf
+ sizeof(ts_buf
) - ts_pos
< TS_LEN
114 || array_size(iov
) - iovpos
< 5)) {
115 writev(fd
, iov
, iovpos
);
125 static void zlog_fd_sigsafe(struct zlog_target
*zt
, const char *text
,
128 struct zlt_fd
*zte
= container_of(zt
, struct zlt_fd
, zt
);
132 iov
[0].iov_base
= (char *)prionames
[LOG_CRIT
];
133 iov
[0].iov_len
= zte
->record_priority
? strlen(iov
[0].iov_base
) : 0;
135 iov
[1].iov_base
= zlog_prefix
;
136 iov
[1].iov_len
= zlog_prefixsz
;
138 iov
[2].iov_base
= (char *)text
;
139 iov
[2].iov_len
= len
;
141 iov
[3].iov_base
= (char *)"\n";
144 fd
= atomic_load_explicit(&zte
->fd
, memory_order_relaxed
);
146 writev(fd
, iov
, array_size(iov
));
153 void zlog_file_init(struct zlog_cfg_file
*zcf
)
155 memset(zcf
, 0, sizeof(*zcf
));
156 zcf
->prio_min
= ZLOG_DISABLED
;
158 pthread_mutex_init(&zcf
->cfg_mtx
, NULL
);
161 static void zlog_file_target_free(struct zlt_fd
*zlt
)
166 rcu_close(&zlt
->head_close
, zlt
->fd
);
167 rcu_free(MTYPE_LOG_FD
, zlt
, zt
.rcu_head
);
170 void zlog_file_fini(struct zlog_cfg_file
*zcf
)
174 struct zlog_target
*zt
;
176 zt
= zlog_target_replace(&zcf
->active
->zt
, NULL
);
177 ztf
= container_of(zt
, struct zlt_fd
, zt
);
178 zlog_file_target_free(ztf
);
180 XFREE(MTYPE_LOG_FD_NAME
, zcf
->filename
);
181 pthread_mutex_destroy(&zcf
->cfg_mtx
);
184 static bool zlog_file_cycle(struct zlog_cfg_file
*zcf
)
186 struct zlog_target
*zt
, *old
;
187 struct zlt_fd
*zlt
= NULL
;
192 if (zcf
->prio_min
== ZLOG_DISABLED
)
197 else if (zcf
->filename
)
198 fd
= open(zcf
->filename
,
199 O_WRONLY
| O_APPEND
| O_CREAT
| O_CLOEXEC
210 zt
= zlog_target_clone(MTYPE_LOG_FD
, &zcf
->active
->zt
,
212 zlt
= container_of(zt
, struct zlt_fd
, zt
);
215 zlt
->record_priority
= zcf
->record_priority
;
216 zlt
->ts_subsec
= zcf
->ts_subsec
;
218 zlt
->zt
.prio_min
= zcf
->prio_min
;
219 zlt
->zt
.logfn
= zcf
->zlog_wrap
? zcf
->zlog_wrap
: zlog_fd
;
220 zlt
->zt
.logfn_sigsafe
= zlog_fd_sigsafe
;
223 old
= zlog_target_replace(zcf
->active
? &zcf
->active
->zt
: NULL
,
224 zlt
? &zlt
->zt
: NULL
);
227 zlog_file_target_free(container_of_null(old
, struct zlt_fd
, zt
));
232 void zlog_file_set_other(struct zlog_cfg_file
*zcf
)
234 frr_with_mutex (&zcf
->cfg_mtx
) {
235 zlog_file_cycle(zcf
);
239 bool zlog_file_set_filename(struct zlog_cfg_file
*zcf
, const char *filename
)
241 frr_with_mutex (&zcf
->cfg_mtx
) {
242 XFREE(MTYPE_LOG_FD_NAME
, zcf
->filename
);
243 zcf
->filename
= XSTRDUP(MTYPE_LOG_FD_NAME
, filename
);
246 return zlog_file_cycle(zcf
);
252 bool zlog_file_set_fd(struct zlog_cfg_file
*zcf
, int fd
)
254 frr_with_mutex (&zcf
->cfg_mtx
) {
258 XFREE(MTYPE_LOG_FD_NAME
, zcf
->filename
);
261 return zlog_file_cycle(zcf
);
267 struct rcu_close_rotate
{
268 struct rcu_head_close head_close
;
269 struct rcu_head head_self
;
272 bool zlog_file_rotate(struct zlog_cfg_file
*zcf
)
274 struct rcu_close_rotate
*rcr
;
277 frr_with_mutex (&zcf
->cfg_mtx
) {
278 if (!zcf
->active
|| !zcf
->filename
)
281 fd
= open(zcf
->filename
,
282 O_WRONLY
| O_APPEND
| O_CREAT
| O_CLOEXEC
| O_NOCTTY
,
287 fd
= atomic_exchange_explicit(&zcf
->active
->fd
,
289 memory_order_relaxed
);
292 rcr
= XCALLOC(MTYPE_LOG_FD_ROTATE
, sizeof(*rcr
));
293 rcu_close(&rcr
->head_close
, fd
);
294 rcu_free(MTYPE_LOG_FD_ROTATE
, rcr
, head_self
);
299 /* fixed crash logging */
301 static struct zlt_fd zlog_crashlog
;
303 static void zlog_crashlog_sigsafe(struct zlog_target
*zt
, const char *text
,
306 static int crashlog_fd
= -1;
308 if (crashlog_fd
== -1) {
310 crashlog_fd
= openat(zlog_tmpdirfd
, "crashlog",
311 O_WRONLY
| O_APPEND
| O_CREAT
,
318 if (crashlog_fd
== -2)
321 zlog_crashlog
.fd
= crashlog_fd
;
322 zlog_fd_sigsafe(&zlog_crashlog
.zt
, text
, len
);
325 /* this is used for assert failures (they don't need AS-Safe logging) */
326 static void zlog_crashlog_plain(struct zlog_target
*zt
, struct zlog_msg
*msgs
[],
332 for (i
= 0; i
< nmsgs
; i
++) {
333 if (zlog_msg_prio(msgs
[i
]) > zt
->prio_min
)
336 text
= zlog_msg_text(msgs
[i
], &len
);
337 zlog_crashlog_sigsafe(zt
, text
, len
);
341 static void zlog_crashlog_init(void)
343 zlog_crashlog
.zt
.prio_min
= LOG_CRIT
;
344 zlog_crashlog
.zt
.logfn
= zlog_crashlog_plain
;
345 zlog_crashlog
.zt
.logfn_sigsafe
= zlog_crashlog_sigsafe
;
346 zlog_crashlog
.fd
= -1;
348 zlog_target_replace(NULL
, &zlog_crashlog
.zt
);
351 /* fixed logging for test/auxiliary programs */
353 static struct zlt_fd zlog_aux_stdout
;
354 static bool zlog_is_aux
;
356 static int zlt_aux_init(const char *prefix
, int prio_min
)
360 zlog_aux_stdout
.zt
.prio_min
= prio_min
;
361 zlog_aux_stdout
.zt
.logfn
= zlog_fd
;
362 zlog_aux_stdout
.zt
.logfn_sigsafe
= zlog_fd_sigsafe
;
363 zlog_aux_stdout
.fd
= STDOUT_FILENO
;
365 zlog_target_replace(NULL
, &zlog_aux_stdout
.zt
);
370 static int zlt_init(const char *progname
, const char *protoname
,
371 unsigned short instance
, uid_t uid
, gid_t gid
)
373 openlog(progname
, LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
377 static int zlt_fini(void)
383 /* fixed startup logging to stderr */
385 static struct zlt_fd zlog_startup_stderr
;
387 __attribute__((_CONSTRUCTOR(450))) static void zlog_startup_init(void)
389 zlog_startup_stderr
.zt
.prio_min
= LOG_WARNING
;
390 zlog_startup_stderr
.zt
.logfn
= zlog_fd
;
391 zlog_startup_stderr
.zt
.logfn_sigsafe
= zlog_fd_sigsafe
;
392 zlog_startup_stderr
.fd
= STDERR_FILENO
;
394 zlog_target_replace(NULL
, &zlog_startup_stderr
.zt
);
396 hook_register(zlog_aux_init
, zlt_aux_init
);
397 hook_register(zlog_init
, zlt_init
);
398 hook_register(zlog_fini
, zlt_fini
);
401 void zlog_startup_end(void)
403 static bool startup_ended
= false;
407 startup_ended
= true;
409 zlog_target_replace(&zlog_startup_stderr
.zt
, NULL
);
414 /* until here, crashlogs go to stderr */
415 zlog_crashlog_init();
421 struct zlog_target zt
;
426 static void zlog_syslog(struct zlog_target
*zt
, struct zlog_msg
*msgs
[],
430 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 text
= zlog_msg_text(msgs
[i
], &text_len
);
439 syslog(zlog_msg_prio(msgs
[i
]) | zte
->syslog_facility
, "%.*s",
440 (int)text_len
, text
);
445 #define _PATH_LOG "/dev/log"
448 static void zlog_syslog_sigsafe(struct zlog_target
*zt
, const char *text
,
451 static int syslog_fd
= -1;
457 if (syslog_fd
== -1) {
458 syslog_fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
459 if (syslog_fd
>= 0) {
460 struct sockaddr_un sa
;
461 socklen_t salen
= sizeof(sa
);
463 sa
.sun_family
= AF_UNIX
;
464 strlcpy(sa
.sun_path
, _PATH_LOG
, sizeof(sa
.sun_path
));
465 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
466 salen
= sa
.sun_len
= SUN_LEN(&sa
);
468 if (connect(syslog_fd
, (struct sockaddr
*)&sa
, salen
)) {
474 /* /dev/log could be a fifo instead of a socket */
475 if (syslog_fd
== -1) {
476 syslog_fd
= open(_PATH_LOG
, O_WRONLY
| O_NOCTTY
);
486 /* note zlog_prefix includes trailing ": ", need to cut off 2 chars */
487 hdrlen
= snprintfrr(hdr
, sizeof(hdr
), "<%d>%.*s[%ld]: ", LOG_CRIT
,
488 zlog_prefixsz
> 2 ? (int)(zlog_prefixsz
- 2) : 0,
489 zlog_prefix
, (long)getpid());
491 iov
[0].iov_base
= hdr
;
492 iov
[0].iov_len
= hdrlen
;
494 iov
[1].iov_base
= (char *)text
;
495 iov
[1].iov_len
= len
;
497 writev(syslog_fd
, iov
, array_size(iov
));
501 static pthread_mutex_t syslog_cfg_mutex
= PTHREAD_MUTEX_INITIALIZER
;
502 static struct zlt_syslog
*zlt_syslog
;
503 static int syslog_facility
= LOG_DAEMON
;
504 static int syslog_prio_min
= ZLOG_DISABLED
;
506 void zlog_syslog_set_facility(int facility
)
508 struct zlog_target
*newztc
;
509 struct zlt_syslog
*newzt
;
511 frr_with_mutex (&syslog_cfg_mutex
) {
512 if (facility
== syslog_facility
)
514 syslog_facility
= facility
;
516 if (syslog_prio_min
== ZLOG_DISABLED
)
519 newztc
= zlog_target_clone(MTYPE_LOG_SYSL
, &zlt_syslog
->zt
,
521 newzt
= container_of(newztc
, struct zlt_syslog
, zt
);
522 newzt
->syslog_facility
= syslog_facility
;
524 zlog_target_free(MTYPE_LOG_SYSL
,
525 zlog_target_replace(&zlt_syslog
->zt
,
532 int zlog_syslog_get_facility(void)
534 frr_with_mutex (&syslog_cfg_mutex
) {
535 return syslog_facility
;
541 void zlog_syslog_set_prio_min(int prio_min
)
543 struct zlog_target
*newztc
;
544 struct zlt_syslog
*newzt
= NULL
;
546 frr_with_mutex (&syslog_cfg_mutex
) {
547 if (prio_min
== syslog_prio_min
)
549 syslog_prio_min
= prio_min
;
551 if (syslog_prio_min
!= ZLOG_DISABLED
) {
552 newztc
= zlog_target_clone(MTYPE_LOG_SYSL
,
555 newzt
= container_of(newztc
, struct zlt_syslog
, zt
);
556 newzt
->zt
.prio_min
= prio_min
;
557 newzt
->zt
.logfn
= zlog_syslog
;
558 newzt
->zt
.logfn_sigsafe
= zlog_syslog_sigsafe
;
559 newzt
->syslog_facility
= syslog_facility
;
562 zlog_target_free(MTYPE_LOG_SYSL
,
563 zlog_target_replace(&zlt_syslog
->zt
,
570 int zlog_syslog_get_prio_min(void)
572 frr_with_mutex (&syslog_cfg_mutex
) {
573 return syslog_prio_min
;