blob: ec9586457b2741fcb6caff9a2a20ec70e9b79545 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajsc3324c62004-12-09 17:26:31 +00002 * $Id: log.c,v 1.19 2004/12/09 17:26:31 ajs Exp $
ajs274a4a42004-12-07 15:39:31 +00003 *
4 * Logging of zebra
paul718e3742002-12-13 20:15:29 +00005 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
24
25#include <zebra.h>
26
27#include "log.h"
28#include "memory.h"
29#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000030#ifndef SUNOS_5
31#include <sys/un.h>
32#endif
paul718e3742002-12-13 20:15:29 +000033
34struct zlog *zlog_default = NULL;
35
36const char *zlog_proto_names[] =
37{
38 "NONE",
39 "DEFAULT",
40 "ZEBRA",
41 "RIP",
42 "BGP",
43 "OSPF",
44 "RIPNG",
45 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000046 "ISIS",
paul718e3742002-12-13 20:15:29 +000047 "MASC",
48 NULL,
49};
50
51const char *zlog_priority[] =
52{
53 "emergencies",
54 "alerts",
55 "critical",
56 "errors",
57 "warnings",
58 "notifications",
59 "informational",
60 "debugging",
61 NULL,
62};
63
64
65
66/* For time string format. */
67#define TIME_BUF 27
68
69/* Utility routine for current time printing. */
70static void
71time_print (FILE *fp)
72{
73 int ret;
74 char buf [TIME_BUF];
75 time_t clock;
76 struct tm *tm;
77
78 time (&clock);
79 tm = localtime (&clock);
80
81 ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm);
82 if (ret == 0) {
83 zlog_warn ("strftime error");
84 }
85
86 fprintf (fp, "%s ", buf);
87}
88
89/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +000090static void
91vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +000092{
93 /* If zlog is not specified, use default one. */
94 if (zl == NULL)
95 zl = zlog_default;
96
97 /* When zlog_default is also NULL, use stderr for logging. */
98 if (zl == NULL)
99 {
100 time_print (stderr);
101 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000102 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000103 fprintf (stderr, "\n");
104 fflush (stderr);
105
106 /* In this case we return at here. */
107 return;
108 }
109
paul718e3742002-12-13 20:15:29 +0000110 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000111 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000112 {
113 va_list ac;
114 va_copy(ac, args);
115 vsyslog (priority|zlog_default->facility, format, ac);
116 va_end(ac);
117 }
paul718e3742002-12-13 20:15:29 +0000118
119 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000120 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000121 {
ajsd246bd92004-11-23 17:35:08 +0000122 va_list ac;
paul718e3742002-12-13 20:15:29 +0000123 time_print (zl->fp);
hassob04c6992004-10-04 19:10:31 +0000124 if (zl->record_priority)
125 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000126 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000127 va_copy(ac, args);
128 vfprintf (zl->fp, format, ac);
129 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000130 fprintf (zl->fp, "\n");
131 fflush (zl->fp);
132 }
133
134 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000135 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000136 {
ajsd246bd92004-11-23 17:35:08 +0000137 va_list ac;
paul718e3742002-12-13 20:15:29 +0000138 time_print (stdout);
hassob04c6992004-10-04 19:10:31 +0000139 if (zl->record_priority)
140 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000141 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000142 va_copy(ac, args);
143 vfprintf (stdout, format, ac);
144 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000145 fprintf (stdout, "\n");
146 fflush (stdout);
147 }
148
paul718e3742002-12-13 20:15:29 +0000149 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000150 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
151 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
152 zlog_proto_names[zl->protocol], format, args);
paul718e3742002-12-13 20:15:29 +0000153}
154
ajs59a06a92004-11-23 18:19:14 +0000155static char *
156str_append(char *dst, int len, const char *src)
157{
158 while ((len-- > 0) && *src)
159 *dst++ = *src++;
160 return dst;
161}
162
163static char *
164num_append(char *s, int len, u_long x)
165{
166 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000167 char *t;
ajs59a06a92004-11-23 18:19:14 +0000168
ajs7d149b82004-11-28 23:00:01 +0000169 if (!x)
170 return str_append(s,len,"0");
171 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000172 while (x && (t > buf))
173 {
174 *--t = '0'+(x % 10);
175 x /= 10;
176 }
177 return str_append(s,len,t);
178}
179
ajsc3324c62004-12-09 17:26:31 +0000180#ifdef HAVE_GLIBC_BACKTRACE
181
182/* This function is used only in zlog_backtrace_sigsafe when glibc
183 backtraces are available. */
ajs7d149b82004-11-28 23:00:01 +0000184static char *
185hex_append(char *s, int len, u_long x)
186{
187 char buf[30];
188 char *t;
189
190 if (!x)
191 return str_append(s,len,"0");
192 *(t = &buf[sizeof(buf)-1]) = '\0';
193 while (x && (t > buf))
194 {
195 u_int cc = (x % 16);
196 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
197 x /= 16;
198 }
199 return str_append(s,len,t);
200}
201
ajsc3324c62004-12-09 17:26:31 +0000202#endif /* HAVE_GLIBC_BACKTRACE */
203
ajs7d149b82004-11-28 23:00:01 +0000204static int syslog_fd = -1;
205
206/* Needs to be enhanced to support Solaris. */
207static int
208syslog_connect(void)
209{
210#ifdef SUNOS_5
211 return -1;
212#else
213 int fd;
214 char *s;
215 struct sockaddr_un addr;
216
217 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
218 return -1;
219 addr.sun_family = AF_UNIX;
220#ifdef _PATH_LOG
221#define SYSLOG_SOCKET_PATH _PATH_LOG
222#else
223#define SYSLOG_SOCKET_PATH "/dev/log"
224#endif
225 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
226#undef SYSLOG_SOCKET_PATH
227 *s = '\0';
228 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
229 {
230 close(fd);
231 return -1;
232 }
233 return fd;
234#endif
235}
236
237static void
238syslog_sigsafe(int priority, const char *msg, size_t msglen)
239{
240 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
241 char *s;
242
243 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
244 return;
245
246#define LOC s,buf+sizeof(buf)-s
247 s = buf;
248 s = str_append(LOC,"<");
249 s = num_append(LOC,priority);
250 s = str_append(LOC,">");
251 /* forget about the timestamp, too difficult in a signal handler */
252 s = str_append(LOC,zlog_default->ident);
253 if (zlog_default->syslog_options & LOG_PID)
254 {
255 s = str_append(LOC,"[");
256 s = num_append(LOC,getpid());
257 s = str_append(LOC,"]");
258 }
259 s = str_append(LOC,": ");
260 s = str_append(LOC,msg);
261 write(syslog_fd,buf,s-buf);
262#undef LOC
263}
264
265/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000266void
267zlog_signal(int signo, const char *action)
268{
269 time_t now;
270 char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
271 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000272 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000273#define LOC s,buf+sizeof(buf)-s
274
275 time(&now);
276 if (zlog_default)
277 {
278 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
279 *s++ = ':';
280 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000281 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000282 }
283 s = str_append(LOC,"Received signal ");
284 s = num_append(LOC,signo);
285 s = str_append(LOC," at ");
286 s = num_append(LOC,now);
287 s = str_append(LOC,"; ");
288 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000289 if (s < buf+sizeof(buf))
290 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000291
ajs274a4a42004-12-07 15:39:31 +0000292 /* N.B. implicit priority is most severe */
293#define PRI LOG_EMERG
294
ajs59a06a92004-11-23 18:19:14 +0000295#define DUMP(FP) write(fileno(FP),buf,s-buf);
296 if (!zlog_default)
297 DUMP(stderr)
298 else
299 {
ajs274a4a42004-12-07 15:39:31 +0000300 if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
ajs59a06a92004-11-23 18:19:14 +0000301 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000302 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs59a06a92004-11-23 18:19:14 +0000303 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000304 /* Remove trailing '\n' for monitor and syslog */
305 *--s = '\0';
306 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
307 vty_log_fixed(buf,s-buf);
308 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
309 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000310 }
311#undef DUMP
312
ajs274a4a42004-12-07 15:39:31 +0000313 zlog_backtrace_sigsafe(PRI);
314#undef PRI
ajs063ee522004-11-26 18:11:14 +0000315#undef LOC
316}
ajs59a06a92004-11-23 18:19:14 +0000317
ajs063ee522004-11-26 18:11:14 +0000318/* Log a backtrace using only async-signal-safe functions.
319 Needs to be enhanced to support syslog logging. */
320void
ajs48d6c692004-11-26 20:52:59 +0000321zlog_backtrace_sigsafe(int priority)
ajs063ee522004-11-26 18:11:14 +0000322{
323#ifdef HAVE_GLIBC_BACKTRACE
324 void *array[20];
325 int size;
326 char buf[100];
327 char *s;
328#define LOC s,buf+sizeof(buf)-s
329
ajs063ee522004-11-26 18:11:14 +0000330 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
331 ((size_t)size > sizeof(array)/sizeof(array[0])))
332 return;
333 s = buf;
334 s = str_append(LOC,"Backtrace for ");
335 s = num_append(LOC,size);
336 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000337
338#define DUMP(FP) { \
339 write(fileno(FP),buf,s-buf); \
340 backtrace_symbols_fd(array, size, fileno(FP)); \
341}
342
343 if (!zlog_default)
344 DUMP(stderr)
345 else
346 {
ajs274a4a42004-12-07 15:39:31 +0000347 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
348 zlog_default->fp)
ajs063ee522004-11-26 18:11:14 +0000349 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000350 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs063ee522004-11-26 18:11:14 +0000351 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000352 /* Remove trailing '\n' for monitor and syslog */
353 *--s = '\0';
354 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
355 vty_log_fixed(buf,s-buf);
356 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
357 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
358 {
359 int i;
360 /* Just print the function addresses. */
361 for (i = 0; i < size; i++)
362 {
363 s = buf;
364 s = str_append(LOC,"[bt ");
365 s = num_append(LOC,i);
366 s = str_append(LOC,"] 0x");
367 s = hex_append(LOC,(u_long)(array[i]));
368 *s = '\0';
369 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
370 vty_log_fixed(buf,s-buf);
371 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000372 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000373 }
374 }
ajs59a06a92004-11-23 18:19:14 +0000375 }
376#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000377#undef LOC
ajs063ee522004-11-26 18:11:14 +0000378#endif /* HAVE_GLIBC_BACKTRACE */
379}
380
381void
382zlog_backtrace(int priority)
383{
384#ifndef HAVE_GLIBC_BACKTRACE
385 zlog(NULL, priority, "No backtrace available on this platform.");
386#else
387 void *array[20];
388 int size, i;
389 char **strings;
390
391 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
392 ((size_t)size > sizeof(array)/sizeof(array[0])))
393 {
394 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
395 "(valid range is between 1 and %u)",
396 size, sizeof(array)/sizeof(array[0]));
397 return;
398 }
399 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
400 if (!(strings = backtrace_symbols(array, size)))
401 {
402 zlog_err("Cannot get backtrace symbols (out of memory?)");
403 for (i = 0; i < size; i++)
404 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
405 }
406 else
407 {
408 for (i = 0; i < size; i++)
409 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
410 free(strings);
411 }
412#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000413}
414
paul718e3742002-12-13 20:15:29 +0000415void
416zlog (struct zlog *zl, int priority, const char *format, ...)
417{
ajsd246bd92004-11-23 17:35:08 +0000418 va_list args;
paul718e3742002-12-13 20:15:29 +0000419
ajsd246bd92004-11-23 17:35:08 +0000420 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000421 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000422 va_end (args);
paul718e3742002-12-13 20:15:29 +0000423}
424
ajsd246bd92004-11-23 17:35:08 +0000425#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
426void \
427FUNCNAME(const char *format, ...) \
428{ \
429 va_list args; \
430 va_start(args, format); \
431 vzlog (NULL, PRIORITY, format, args); \
432 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000433}
434
ajsd246bd92004-11-23 17:35:08 +0000435ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000436
ajsd246bd92004-11-23 17:35:08 +0000437ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000438
ajsd246bd92004-11-23 17:35:08 +0000439ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000440
ajsd246bd92004-11-23 17:35:08 +0000441ZLOG_FUNC(zlog_notice, LOG_NOTICE)
442
443ZLOG_FUNC(zlog_debug, LOG_DEBUG)
444
445#undef ZLOG_FUNC
446
447#define PLOG_FUNC(FUNCNAME,PRIORITY) \
448void \
449FUNCNAME(struct zlog *zl, const char *format, ...) \
450{ \
451 va_list args; \
452 va_start(args, format); \
453 vzlog (zl, PRIORITY, format, args); \
454 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000455}
456
ajsd246bd92004-11-23 17:35:08 +0000457PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000458
ajsd246bd92004-11-23 17:35:08 +0000459PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000460
ajsd246bd92004-11-23 17:35:08 +0000461PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000462
ajsd246bd92004-11-23 17:35:08 +0000463PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000464
ajsd246bd92004-11-23 17:35:08 +0000465PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000466
ajsd246bd92004-11-23 17:35:08 +0000467#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000468
ajscee3df12004-11-24 17:14:49 +0000469void
470_zlog_assert_failed (const char *assertion, const char *file,
471 unsigned int line, const char *function)
472{
473 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
474 assertion,file,line,(function ? function : "?"));
ajs274a4a42004-12-07 15:39:31 +0000475 zlog_backtrace(LOG_EMERG);
ajscee3df12004-11-24 17:14:49 +0000476 abort();
477}
478
paul718e3742002-12-13 20:15:29 +0000479
480/* Open log stream */
481struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000482openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000483 int syslog_flags, int syslog_facility)
484{
485 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000486 u_int i;
paul718e3742002-12-13 20:15:29 +0000487
ajs274a4a42004-12-07 15:39:31 +0000488 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000489
490 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000491 zl->protocol = protocol;
492 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000493 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000494
ajs274a4a42004-12-07 15:39:31 +0000495 /* Set default logging levels. */
496 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
497 zl->maxlvl[i] = ZLOG_DISABLED;
498 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
499 zl->default_lvl = LOG_DEBUG;
500
paul718e3742002-12-13 20:15:29 +0000501 openlog (progname, syslog_flags, zl->facility);
502
503 return zl;
504}
505
506void
507closezlog (struct zlog *zl)
508{
509 closelog();
510 fclose (zl->fp);
511
512 XFREE (MTYPE_ZLOG, zl);
513}
514
515/* Called from command.c. */
516void
ajs274a4a42004-12-07 15:39:31 +0000517zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000518{
519 if (zl == NULL)
520 zl = zlog_default;
521
ajs274a4a42004-12-07 15:39:31 +0000522 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000523}
524
525int
ajs274a4a42004-12-07 15:39:31 +0000526zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000527{
528 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000529 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000530
531 /* There is opend file. */
532 zlog_reset_file (zl);
533
534 /* Set default zl. */
535 if (zl == NULL)
536 zl = zlog_default;
537
538 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000539 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000540 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000541 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000542 if (fp == NULL)
543 return 0;
paul718e3742002-12-13 20:15:29 +0000544
545 /* Set flags. */
546 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000547 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000548 zl->fp = fp;
549
550 return 1;
551}
552
553/* Reset opend file. */
554int
555zlog_reset_file (struct zlog *zl)
556{
557 if (zl == NULL)
558 zl = zlog_default;
559
paul718e3742002-12-13 20:15:29 +0000560 if (zl->fp)
561 fclose (zl->fp);
562 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000563 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000564
565 if (zl->filename)
566 free (zl->filename);
567 zl->filename = NULL;
568
569 return 1;
570}
571
572/* Reopen log file. */
573int
574zlog_rotate (struct zlog *zl)
575{
ajs274a4a42004-12-07 15:39:31 +0000576 int level;
paul718e3742002-12-13 20:15:29 +0000577
578 if (zl == NULL)
579 zl = zlog_default;
580
581 if (zl->fp)
582 fclose (zl->fp);
583 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000584 level = zl->maxlvl[ZLOG_DEST_FILE];
585 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000586
587 if (zl->filename)
588 {
gdtaa593d52003-12-22 20:15:53 +0000589 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000590 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000591
592 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000593 zl->fp = fopen (zl->filename, "a");
594 save_errno = errno;
595 umask(oldumask);
596 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000597 {
ajs274a4a42004-12-07 15:39:31 +0000598 zlog_err("Log rotate failed: cannot open file %s for append: %s",
599 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000600 return -1;
601 }
ajs274a4a42004-12-07 15:39:31 +0000602 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000603 }
604
605 return 1;
606}
607
paul718e3742002-12-13 20:15:29 +0000608/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000609const char *
paul718e3742002-12-13 20:15:29 +0000610lookup (struct message *mes, int key)
611{
612 struct message *pnt;
613
614 for (pnt = mes; pnt->key != 0; pnt++)
615 if (pnt->key == key)
616 return pnt->str;
617
618 return "";
619}
620
621/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000622 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000623const char *
paul718e3742002-12-13 20:15:29 +0000624mes_lookup (struct message *meslist, int max, int index)
625{
626 if (index < 0 || index >= max)
627 {
628 zlog_err ("message index out of bound: %d", max);
629 return NULL;
630 }
631 return meslist[index].str;
632}
ajsca359762004-11-19 23:40:16 +0000633
634/* Wrapper around strerror to handle case where it returns NULL. */
635const char *
636safe_strerror(int errnum)
637{
638 const char *s = strerror(errnum);
639 return (s != NULL) ? s : "Unknown error";
640}