blob: 64d576a2fa09100deb2954cd1be2bf8f99dcc825 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
gdtad572392004-12-09 14:53:32 +00002 * $Id: log.c,v 1.18 2004/12/09 14:53:32 gdt 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
gdtad572392004-12-09 14:53:32 +0000180/*
181 * XXX warning: `hex_append' defined but not used
182 * Apparently this is used only if HAVE_GLIBC_BACKTRACE is defined.
183 */
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
202static int syslog_fd = -1;
203
204/* Needs to be enhanced to support Solaris. */
205static int
206syslog_connect(void)
207{
208#ifdef SUNOS_5
209 return -1;
210#else
211 int fd;
212 char *s;
213 struct sockaddr_un addr;
214
215 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
216 return -1;
217 addr.sun_family = AF_UNIX;
218#ifdef _PATH_LOG
219#define SYSLOG_SOCKET_PATH _PATH_LOG
220#else
221#define SYSLOG_SOCKET_PATH "/dev/log"
222#endif
223 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
224#undef SYSLOG_SOCKET_PATH
225 *s = '\0';
226 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
227 {
228 close(fd);
229 return -1;
230 }
231 return fd;
232#endif
233}
234
235static void
236syslog_sigsafe(int priority, const char *msg, size_t msglen)
237{
238 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
239 char *s;
240
241 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
242 return;
243
244#define LOC s,buf+sizeof(buf)-s
245 s = buf;
246 s = str_append(LOC,"<");
247 s = num_append(LOC,priority);
248 s = str_append(LOC,">");
249 /* forget about the timestamp, too difficult in a signal handler */
250 s = str_append(LOC,zlog_default->ident);
251 if (zlog_default->syslog_options & LOG_PID)
252 {
253 s = str_append(LOC,"[");
254 s = num_append(LOC,getpid());
255 s = str_append(LOC,"]");
256 }
257 s = str_append(LOC,": ");
258 s = str_append(LOC,msg);
259 write(syslog_fd,buf,s-buf);
260#undef LOC
261}
262
263/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000264void
265zlog_signal(int signo, const char *action)
266{
267 time_t now;
268 char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
269 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000270 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000271#define LOC s,buf+sizeof(buf)-s
272
273 time(&now);
274 if (zlog_default)
275 {
276 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
277 *s++ = ':';
278 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000279 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000280 }
281 s = str_append(LOC,"Received signal ");
282 s = num_append(LOC,signo);
283 s = str_append(LOC," at ");
284 s = num_append(LOC,now);
285 s = str_append(LOC,"; ");
286 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000287 if (s < buf+sizeof(buf))
288 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000289
ajs274a4a42004-12-07 15:39:31 +0000290 /* N.B. implicit priority is most severe */
291#define PRI LOG_EMERG
292
ajs59a06a92004-11-23 18:19:14 +0000293#define DUMP(FP) write(fileno(FP),buf,s-buf);
294 if (!zlog_default)
295 DUMP(stderr)
296 else
297 {
ajs274a4a42004-12-07 15:39:31 +0000298 if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
ajs59a06a92004-11-23 18:19:14 +0000299 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000300 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs59a06a92004-11-23 18:19:14 +0000301 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000302 /* Remove trailing '\n' for monitor and syslog */
303 *--s = '\0';
304 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
305 vty_log_fixed(buf,s-buf);
306 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
307 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000308 }
309#undef DUMP
310
ajs274a4a42004-12-07 15:39:31 +0000311 zlog_backtrace_sigsafe(PRI);
312#undef PRI
ajs063ee522004-11-26 18:11:14 +0000313#undef LOC
314}
ajs59a06a92004-11-23 18:19:14 +0000315
ajs063ee522004-11-26 18:11:14 +0000316/* Log a backtrace using only async-signal-safe functions.
317 Needs to be enhanced to support syslog logging. */
318void
ajs48d6c692004-11-26 20:52:59 +0000319zlog_backtrace_sigsafe(int priority)
ajs063ee522004-11-26 18:11:14 +0000320{
321#ifdef HAVE_GLIBC_BACKTRACE
322 void *array[20];
323 int size;
324 char buf[100];
325 char *s;
326#define LOC s,buf+sizeof(buf)-s
327
ajs063ee522004-11-26 18:11:14 +0000328 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
329 ((size_t)size > sizeof(array)/sizeof(array[0])))
330 return;
331 s = buf;
332 s = str_append(LOC,"Backtrace for ");
333 s = num_append(LOC,size);
334 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000335
336#define DUMP(FP) { \
337 write(fileno(FP),buf,s-buf); \
338 backtrace_symbols_fd(array, size, fileno(FP)); \
339}
340
341 if (!zlog_default)
342 DUMP(stderr)
343 else
344 {
ajs274a4a42004-12-07 15:39:31 +0000345 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
346 zlog_default->fp)
ajs063ee522004-11-26 18:11:14 +0000347 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000348 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs063ee522004-11-26 18:11:14 +0000349 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000350 /* Remove trailing '\n' for monitor and syslog */
351 *--s = '\0';
352 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
353 vty_log_fixed(buf,s-buf);
354 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
355 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
356 {
357 int i;
358 /* Just print the function addresses. */
359 for (i = 0; i < size; i++)
360 {
361 s = buf;
362 s = str_append(LOC,"[bt ");
363 s = num_append(LOC,i);
364 s = str_append(LOC,"] 0x");
365 s = hex_append(LOC,(u_long)(array[i]));
366 *s = '\0';
367 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
368 vty_log_fixed(buf,s-buf);
369 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000370 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000371 }
372 }
ajs59a06a92004-11-23 18:19:14 +0000373 }
374#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000375#undef LOC
ajs063ee522004-11-26 18:11:14 +0000376#endif /* HAVE_GLIBC_BACKTRACE */
377}
378
379void
380zlog_backtrace(int priority)
381{
382#ifndef HAVE_GLIBC_BACKTRACE
383 zlog(NULL, priority, "No backtrace available on this platform.");
384#else
385 void *array[20];
386 int size, i;
387 char **strings;
388
389 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
390 ((size_t)size > sizeof(array)/sizeof(array[0])))
391 {
392 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
393 "(valid range is between 1 and %u)",
394 size, sizeof(array)/sizeof(array[0]));
395 return;
396 }
397 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
398 if (!(strings = backtrace_symbols(array, size)))
399 {
400 zlog_err("Cannot get backtrace symbols (out of memory?)");
401 for (i = 0; i < size; i++)
402 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
403 }
404 else
405 {
406 for (i = 0; i < size; i++)
407 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
408 free(strings);
409 }
410#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000411}
412
paul718e3742002-12-13 20:15:29 +0000413void
414zlog (struct zlog *zl, int priority, const char *format, ...)
415{
ajsd246bd92004-11-23 17:35:08 +0000416 va_list args;
paul718e3742002-12-13 20:15:29 +0000417
ajsd246bd92004-11-23 17:35:08 +0000418 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000419 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000420 va_end (args);
paul718e3742002-12-13 20:15:29 +0000421}
422
ajsd246bd92004-11-23 17:35:08 +0000423#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
424void \
425FUNCNAME(const char *format, ...) \
426{ \
427 va_list args; \
428 va_start(args, format); \
429 vzlog (NULL, PRIORITY, format, args); \
430 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000431}
432
ajsd246bd92004-11-23 17:35:08 +0000433ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000434
ajsd246bd92004-11-23 17:35:08 +0000435ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000436
ajsd246bd92004-11-23 17:35:08 +0000437ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000438
ajsd246bd92004-11-23 17:35:08 +0000439ZLOG_FUNC(zlog_notice, LOG_NOTICE)
440
441ZLOG_FUNC(zlog_debug, LOG_DEBUG)
442
443#undef ZLOG_FUNC
444
445#define PLOG_FUNC(FUNCNAME,PRIORITY) \
446void \
447FUNCNAME(struct zlog *zl, const char *format, ...) \
448{ \
449 va_list args; \
450 va_start(args, format); \
451 vzlog (zl, PRIORITY, format, args); \
452 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000453}
454
ajsd246bd92004-11-23 17:35:08 +0000455PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000456
ajsd246bd92004-11-23 17:35:08 +0000457PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000458
ajsd246bd92004-11-23 17:35:08 +0000459PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000460
ajsd246bd92004-11-23 17:35:08 +0000461PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000462
ajsd246bd92004-11-23 17:35:08 +0000463PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000464
ajsd246bd92004-11-23 17:35:08 +0000465#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000466
ajscee3df12004-11-24 17:14:49 +0000467void
468_zlog_assert_failed (const char *assertion, const char *file,
469 unsigned int line, const char *function)
470{
471 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
472 assertion,file,line,(function ? function : "?"));
ajs274a4a42004-12-07 15:39:31 +0000473 zlog_backtrace(LOG_EMERG);
ajscee3df12004-11-24 17:14:49 +0000474 abort();
475}
476
paul718e3742002-12-13 20:15:29 +0000477
478/* Open log stream */
479struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000480openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000481 int syslog_flags, int syslog_facility)
482{
483 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000484 u_int i;
paul718e3742002-12-13 20:15:29 +0000485
ajs274a4a42004-12-07 15:39:31 +0000486 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000487
488 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000489 zl->protocol = protocol;
490 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000491 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000492
ajs274a4a42004-12-07 15:39:31 +0000493 /* Set default logging levels. */
494 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
495 zl->maxlvl[i] = ZLOG_DISABLED;
496 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
497 zl->default_lvl = LOG_DEBUG;
498
paul718e3742002-12-13 20:15:29 +0000499 openlog (progname, syslog_flags, zl->facility);
500
501 return zl;
502}
503
504void
505closezlog (struct zlog *zl)
506{
507 closelog();
508 fclose (zl->fp);
509
510 XFREE (MTYPE_ZLOG, zl);
511}
512
513/* Called from command.c. */
514void
ajs274a4a42004-12-07 15:39:31 +0000515zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000516{
517 if (zl == NULL)
518 zl = zlog_default;
519
ajs274a4a42004-12-07 15:39:31 +0000520 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000521}
522
523int
ajs274a4a42004-12-07 15:39:31 +0000524zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000525{
526 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000527 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000528
529 /* There is opend file. */
530 zlog_reset_file (zl);
531
532 /* Set default zl. */
533 if (zl == NULL)
534 zl = zlog_default;
535
536 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000537 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000538 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000539 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000540 if (fp == NULL)
541 return 0;
paul718e3742002-12-13 20:15:29 +0000542
543 /* Set flags. */
544 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000545 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000546 zl->fp = fp;
547
548 return 1;
549}
550
551/* Reset opend file. */
552int
553zlog_reset_file (struct zlog *zl)
554{
555 if (zl == NULL)
556 zl = zlog_default;
557
paul718e3742002-12-13 20:15:29 +0000558 if (zl->fp)
559 fclose (zl->fp);
560 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000561 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000562
563 if (zl->filename)
564 free (zl->filename);
565 zl->filename = NULL;
566
567 return 1;
568}
569
570/* Reopen log file. */
571int
572zlog_rotate (struct zlog *zl)
573{
ajs274a4a42004-12-07 15:39:31 +0000574 int level;
paul718e3742002-12-13 20:15:29 +0000575
576 if (zl == NULL)
577 zl = zlog_default;
578
579 if (zl->fp)
580 fclose (zl->fp);
581 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000582 level = zl->maxlvl[ZLOG_DEST_FILE];
583 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000584
585 if (zl->filename)
586 {
gdtaa593d52003-12-22 20:15:53 +0000587 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000588 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000589
590 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000591 zl->fp = fopen (zl->filename, "a");
592 save_errno = errno;
593 umask(oldumask);
594 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000595 {
ajs274a4a42004-12-07 15:39:31 +0000596 zlog_err("Log rotate failed: cannot open file %s for append: %s",
597 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000598 return -1;
599 }
ajs274a4a42004-12-07 15:39:31 +0000600 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000601 }
602
603 return 1;
604}
605
paul718e3742002-12-13 20:15:29 +0000606/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000607const char *
paul718e3742002-12-13 20:15:29 +0000608lookup (struct message *mes, int key)
609{
610 struct message *pnt;
611
612 for (pnt = mes; pnt->key != 0; pnt++)
613 if (pnt->key == key)
614 return pnt->str;
615
616 return "";
617}
618
619/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000620 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000621const char *
paul718e3742002-12-13 20:15:29 +0000622mes_lookup (struct message *meslist, int max, int index)
623{
624 if (index < 0 || index >= max)
625 {
626 zlog_err ("message index out of bound: %d", max);
627 return NULL;
628 }
629 return meslist[index].str;
630}
ajsca359762004-11-19 23:40:16 +0000631
632/* Wrapper around strerror to handle case where it returns NULL. */
633const char *
634safe_strerror(int errnum)
635{
636 const char *s = strerror(errnum);
637 return (s != NULL) ? s : "Unknown error";
638}