blob: b68896cac8ae66f89681d613be50dae6c1944f05 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
2 * $Id: log.c,v 1.17 2004/12/07 15:39:32 ajs Exp $
3 *
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
ajs7d149b82004-11-28 23:00:01 +0000180static char *
181hex_append(char *s, int len, u_long x)
182{
183 char buf[30];
184 char *t;
185
186 if (!x)
187 return str_append(s,len,"0");
188 *(t = &buf[sizeof(buf)-1]) = '\0';
189 while (x && (t > buf))
190 {
191 u_int cc = (x % 16);
192 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
193 x /= 16;
194 }
195 return str_append(s,len,t);
196}
197
198static int syslog_fd = -1;
199
200/* Needs to be enhanced to support Solaris. */
201static int
202syslog_connect(void)
203{
204#ifdef SUNOS_5
205 return -1;
206#else
207 int fd;
208 char *s;
209 struct sockaddr_un addr;
210
211 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
212 return -1;
213 addr.sun_family = AF_UNIX;
214#ifdef _PATH_LOG
215#define SYSLOG_SOCKET_PATH _PATH_LOG
216#else
217#define SYSLOG_SOCKET_PATH "/dev/log"
218#endif
219 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
220#undef SYSLOG_SOCKET_PATH
221 *s = '\0';
222 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
223 {
224 close(fd);
225 return -1;
226 }
227 return fd;
228#endif
229}
230
231static void
232syslog_sigsafe(int priority, const char *msg, size_t msglen)
233{
234 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
235 char *s;
236
237 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
238 return;
239
240#define LOC s,buf+sizeof(buf)-s
241 s = buf;
242 s = str_append(LOC,"<");
243 s = num_append(LOC,priority);
244 s = str_append(LOC,">");
245 /* forget about the timestamp, too difficult in a signal handler */
246 s = str_append(LOC,zlog_default->ident);
247 if (zlog_default->syslog_options & LOG_PID)
248 {
249 s = str_append(LOC,"[");
250 s = num_append(LOC,getpid());
251 s = str_append(LOC,"]");
252 }
253 s = str_append(LOC,": ");
254 s = str_append(LOC,msg);
255 write(syslog_fd,buf,s-buf);
256#undef LOC
257}
258
259/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000260void
261zlog_signal(int signo, const char *action)
262{
263 time_t now;
264 char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
265 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000266 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000267#define LOC s,buf+sizeof(buf)-s
268
269 time(&now);
270 if (zlog_default)
271 {
272 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
273 *s++ = ':';
274 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000275 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000276 }
277 s = str_append(LOC,"Received signal ");
278 s = num_append(LOC,signo);
279 s = str_append(LOC," at ");
280 s = num_append(LOC,now);
281 s = str_append(LOC,"; ");
282 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000283 if (s < buf+sizeof(buf))
284 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000285
ajs274a4a42004-12-07 15:39:31 +0000286 /* N.B. implicit priority is most severe */
287#define PRI LOG_EMERG
288
ajs59a06a92004-11-23 18:19:14 +0000289#define DUMP(FP) write(fileno(FP),buf,s-buf);
290 if (!zlog_default)
291 DUMP(stderr)
292 else
293 {
ajs274a4a42004-12-07 15:39:31 +0000294 if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
ajs59a06a92004-11-23 18:19:14 +0000295 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000296 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs59a06a92004-11-23 18:19:14 +0000297 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000298 /* Remove trailing '\n' for monitor and syslog */
299 *--s = '\0';
300 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
301 vty_log_fixed(buf,s-buf);
302 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
303 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000304 }
305#undef DUMP
306
ajs274a4a42004-12-07 15:39:31 +0000307 zlog_backtrace_sigsafe(PRI);
308#undef PRI
ajs063ee522004-11-26 18:11:14 +0000309#undef LOC
310}
ajs59a06a92004-11-23 18:19:14 +0000311
ajs063ee522004-11-26 18:11:14 +0000312/* Log a backtrace using only async-signal-safe functions.
313 Needs to be enhanced to support syslog logging. */
314void
ajs48d6c692004-11-26 20:52:59 +0000315zlog_backtrace_sigsafe(int priority)
ajs063ee522004-11-26 18:11:14 +0000316{
317#ifdef HAVE_GLIBC_BACKTRACE
318 void *array[20];
319 int size;
320 char buf[100];
321 char *s;
322#define LOC s,buf+sizeof(buf)-s
323
ajs063ee522004-11-26 18:11:14 +0000324 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
325 ((size_t)size > sizeof(array)/sizeof(array[0])))
326 return;
327 s = buf;
328 s = str_append(LOC,"Backtrace for ");
329 s = num_append(LOC,size);
330 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000331
332#define DUMP(FP) { \
333 write(fileno(FP),buf,s-buf); \
334 backtrace_symbols_fd(array, size, fileno(FP)); \
335}
336
337 if (!zlog_default)
338 DUMP(stderr)
339 else
340 {
ajs274a4a42004-12-07 15:39:31 +0000341 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
342 zlog_default->fp)
ajs063ee522004-11-26 18:11:14 +0000343 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000344 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs063ee522004-11-26 18:11:14 +0000345 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000346 /* Remove trailing '\n' for monitor and syslog */
347 *--s = '\0';
348 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
349 vty_log_fixed(buf,s-buf);
350 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
351 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
352 {
353 int i;
354 /* Just print the function addresses. */
355 for (i = 0; i < size; i++)
356 {
357 s = buf;
358 s = str_append(LOC,"[bt ");
359 s = num_append(LOC,i);
360 s = str_append(LOC,"] 0x");
361 s = hex_append(LOC,(u_long)(array[i]));
362 *s = '\0';
363 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
364 vty_log_fixed(buf,s-buf);
365 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000366 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000367 }
368 }
ajs59a06a92004-11-23 18:19:14 +0000369 }
370#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000371#undef LOC
ajs063ee522004-11-26 18:11:14 +0000372#endif /* HAVE_GLIBC_BACKTRACE */
373}
374
375void
376zlog_backtrace(int priority)
377{
378#ifndef HAVE_GLIBC_BACKTRACE
379 zlog(NULL, priority, "No backtrace available on this platform.");
380#else
381 void *array[20];
382 int size, i;
383 char **strings;
384
385 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
386 ((size_t)size > sizeof(array)/sizeof(array[0])))
387 {
388 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
389 "(valid range is between 1 and %u)",
390 size, sizeof(array)/sizeof(array[0]));
391 return;
392 }
393 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
394 if (!(strings = backtrace_symbols(array, size)))
395 {
396 zlog_err("Cannot get backtrace symbols (out of memory?)");
397 for (i = 0; i < size; i++)
398 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
399 }
400 else
401 {
402 for (i = 0; i < size; i++)
403 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
404 free(strings);
405 }
406#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000407}
408
paul718e3742002-12-13 20:15:29 +0000409void
410zlog (struct zlog *zl, int priority, const char *format, ...)
411{
ajsd246bd92004-11-23 17:35:08 +0000412 va_list args;
paul718e3742002-12-13 20:15:29 +0000413
ajsd246bd92004-11-23 17:35:08 +0000414 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000415 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000416 va_end (args);
paul718e3742002-12-13 20:15:29 +0000417}
418
ajsd246bd92004-11-23 17:35:08 +0000419#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
420void \
421FUNCNAME(const char *format, ...) \
422{ \
423 va_list args; \
424 va_start(args, format); \
425 vzlog (NULL, PRIORITY, format, args); \
426 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000427}
428
ajsd246bd92004-11-23 17:35:08 +0000429ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000430
ajsd246bd92004-11-23 17:35:08 +0000431ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000432
ajsd246bd92004-11-23 17:35:08 +0000433ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000434
ajsd246bd92004-11-23 17:35:08 +0000435ZLOG_FUNC(zlog_notice, LOG_NOTICE)
436
437ZLOG_FUNC(zlog_debug, LOG_DEBUG)
438
439#undef ZLOG_FUNC
440
441#define PLOG_FUNC(FUNCNAME,PRIORITY) \
442void \
443FUNCNAME(struct zlog *zl, const char *format, ...) \
444{ \
445 va_list args; \
446 va_start(args, format); \
447 vzlog (zl, PRIORITY, format, args); \
448 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000449}
450
ajsd246bd92004-11-23 17:35:08 +0000451PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000452
ajsd246bd92004-11-23 17:35:08 +0000453PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000454
ajsd246bd92004-11-23 17:35:08 +0000455PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000456
ajsd246bd92004-11-23 17:35:08 +0000457PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000458
ajsd246bd92004-11-23 17:35:08 +0000459PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000460
ajsd246bd92004-11-23 17:35:08 +0000461#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000462
ajscee3df12004-11-24 17:14:49 +0000463void
464_zlog_assert_failed (const char *assertion, const char *file,
465 unsigned int line, const char *function)
466{
467 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
468 assertion,file,line,(function ? function : "?"));
ajs274a4a42004-12-07 15:39:31 +0000469 zlog_backtrace(LOG_EMERG);
ajscee3df12004-11-24 17:14:49 +0000470 abort();
471}
472
paul718e3742002-12-13 20:15:29 +0000473
474/* Open log stream */
475struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000476openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000477 int syslog_flags, int syslog_facility)
478{
479 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000480 u_int i;
paul718e3742002-12-13 20:15:29 +0000481
ajs274a4a42004-12-07 15:39:31 +0000482 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000483
484 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000485 zl->protocol = protocol;
486 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000487 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000488
ajs274a4a42004-12-07 15:39:31 +0000489 /* Set default logging levels. */
490 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
491 zl->maxlvl[i] = ZLOG_DISABLED;
492 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
493 zl->default_lvl = LOG_DEBUG;
494
paul718e3742002-12-13 20:15:29 +0000495 openlog (progname, syslog_flags, zl->facility);
496
497 return zl;
498}
499
500void
501closezlog (struct zlog *zl)
502{
503 closelog();
504 fclose (zl->fp);
505
506 XFREE (MTYPE_ZLOG, zl);
507}
508
509/* Called from command.c. */
510void
ajs274a4a42004-12-07 15:39:31 +0000511zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000512{
513 if (zl == NULL)
514 zl = zlog_default;
515
ajs274a4a42004-12-07 15:39:31 +0000516 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000517}
518
519int
ajs274a4a42004-12-07 15:39:31 +0000520zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000521{
522 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000523 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000524
525 /* There is opend file. */
526 zlog_reset_file (zl);
527
528 /* Set default zl. */
529 if (zl == NULL)
530 zl = zlog_default;
531
532 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000533 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000534 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000535 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000536 if (fp == NULL)
537 return 0;
paul718e3742002-12-13 20:15:29 +0000538
539 /* Set flags. */
540 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000541 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000542 zl->fp = fp;
543
544 return 1;
545}
546
547/* Reset opend file. */
548int
549zlog_reset_file (struct zlog *zl)
550{
551 if (zl == NULL)
552 zl = zlog_default;
553
paul718e3742002-12-13 20:15:29 +0000554 if (zl->fp)
555 fclose (zl->fp);
556 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000557 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000558
559 if (zl->filename)
560 free (zl->filename);
561 zl->filename = NULL;
562
563 return 1;
564}
565
566/* Reopen log file. */
567int
568zlog_rotate (struct zlog *zl)
569{
ajs274a4a42004-12-07 15:39:31 +0000570 int level;
paul718e3742002-12-13 20:15:29 +0000571
572 if (zl == NULL)
573 zl = zlog_default;
574
575 if (zl->fp)
576 fclose (zl->fp);
577 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000578 level = zl->maxlvl[ZLOG_DEST_FILE];
579 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000580
581 if (zl->filename)
582 {
gdtaa593d52003-12-22 20:15:53 +0000583 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000584 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000585
586 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000587 zl->fp = fopen (zl->filename, "a");
588 save_errno = errno;
589 umask(oldumask);
590 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000591 {
ajs274a4a42004-12-07 15:39:31 +0000592 zlog_err("Log rotate failed: cannot open file %s for append: %s",
593 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000594 return -1;
595 }
ajs274a4a42004-12-07 15:39:31 +0000596 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000597 }
598
599 return 1;
600}
601
paul718e3742002-12-13 20:15:29 +0000602/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000603const char *
paul718e3742002-12-13 20:15:29 +0000604lookup (struct message *mes, int key)
605{
606 struct message *pnt;
607
608 for (pnt = mes; pnt->key != 0; pnt++)
609 if (pnt->key == key)
610 return pnt->str;
611
612 return "";
613}
614
615/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000616 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000617const char *
paul718e3742002-12-13 20:15:29 +0000618mes_lookup (struct message *meslist, int max, int index)
619{
620 if (index < 0 || index >= max)
621 {
622 zlog_err ("message index out of bound: %d", max);
623 return NULL;
624 }
625 return meslist[index].str;
626}
ajsca359762004-11-19 23:40:16 +0000627
628/* Wrapper around strerror to handle case where it returns NULL. */
629const char *
630safe_strerror(int errnum)
631{
632 const char *s = strerror(errnum);
633 return (s != NULL) ? s : "Unknown error";
634}