blob: fcf7c5d5829e97654a90393adc4a4e9eb012d9ce [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs40abf232005-01-12 17:27:27 +00002 * $Id: log.c,v 1.21 2005/01/12 17:27:27 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
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
ajs40abf232005-01-12 17:27:27 +0000261zlog_signal(int signo, const char *action, siginfo_t *siginfo,
262 void *program_counter)
ajs59a06a92004-11-23 18:19:14 +0000263{
264 time_t now;
ajs40abf232005-01-12 17:27:27 +0000265 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000266 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000267 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000268#define LOC s,buf+sizeof(buf)-s
269
270 time(&now);
271 if (zlog_default)
272 {
273 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
274 *s++ = ':';
275 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000276 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000277 }
278 s = str_append(LOC,"Received signal ");
279 s = num_append(LOC,signo);
280 s = str_append(LOC," at ");
281 s = num_append(LOC,now);
ajs40abf232005-01-12 17:27:27 +0000282 s = str_append(LOC," (si_addr 0x");
283 s = hex_append(LOC,(u_long)(siginfo->si_addr));
284 if (program_counter)
285 {
286 s = str_append(LOC,", PC 0x");
287 s = hex_append(LOC,(u_long)program_counter);
288 }
289 s = str_append(LOC,"); ");
ajs59a06a92004-11-23 18:19:14 +0000290 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000291 if (s < buf+sizeof(buf))
292 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000293
ajs274a4a42004-12-07 15:39:31 +0000294 /* N.B. implicit priority is most severe */
ajs3378d202004-12-10 22:43:17 +0000295#define PRI LOG_ERR
ajs274a4a42004-12-07 15:39:31 +0000296
ajs59a06a92004-11-23 18:19:14 +0000297#define DUMP(FP) write(fileno(FP),buf,s-buf);
298 if (!zlog_default)
299 DUMP(stderr)
300 else
301 {
ajs274a4a42004-12-07 15:39:31 +0000302 if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
ajs59a06a92004-11-23 18:19:14 +0000303 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000304 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs59a06a92004-11-23 18:19:14 +0000305 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000306 /* Remove trailing '\n' for monitor and syslog */
307 *--s = '\0';
308 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
309 vty_log_fixed(buf,s-buf);
310 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
311 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000312 }
313#undef DUMP
314
ajs274a4a42004-12-07 15:39:31 +0000315 zlog_backtrace_sigsafe(PRI);
316#undef PRI
ajs063ee522004-11-26 18:11:14 +0000317#undef LOC
318}
ajs59a06a92004-11-23 18:19:14 +0000319
ajs063ee522004-11-26 18:11:14 +0000320/* Log a backtrace using only async-signal-safe functions.
321 Needs to be enhanced to support syslog logging. */
322void
ajs48d6c692004-11-26 20:52:59 +0000323zlog_backtrace_sigsafe(int priority)
ajs063ee522004-11-26 18:11:14 +0000324{
325#ifdef HAVE_GLIBC_BACKTRACE
326 void *array[20];
327 int size;
328 char buf[100];
329 char *s;
330#define LOC s,buf+sizeof(buf)-s
331
ajs063ee522004-11-26 18:11:14 +0000332 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
333 ((size_t)size > sizeof(array)/sizeof(array[0])))
334 return;
335 s = buf;
336 s = str_append(LOC,"Backtrace for ");
337 s = num_append(LOC,size);
338 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000339
340#define DUMP(FP) { \
341 write(fileno(FP),buf,s-buf); \
342 backtrace_symbols_fd(array, size, fileno(FP)); \
343}
344
345 if (!zlog_default)
346 DUMP(stderr)
347 else
348 {
ajs274a4a42004-12-07 15:39:31 +0000349 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
350 zlog_default->fp)
ajs063ee522004-11-26 18:11:14 +0000351 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000352 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs063ee522004-11-26 18:11:14 +0000353 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000354 /* Remove trailing '\n' for monitor and syslog */
355 *--s = '\0';
356 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
357 vty_log_fixed(buf,s-buf);
358 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
359 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
360 {
361 int i;
362 /* Just print the function addresses. */
363 for (i = 0; i < size; i++)
364 {
365 s = buf;
366 s = str_append(LOC,"[bt ");
367 s = num_append(LOC,i);
368 s = str_append(LOC,"] 0x");
369 s = hex_append(LOC,(u_long)(array[i]));
370 *s = '\0';
371 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
372 vty_log_fixed(buf,s-buf);
373 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000374 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000375 }
376 }
ajs59a06a92004-11-23 18:19:14 +0000377 }
378#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000379#undef LOC
ajs063ee522004-11-26 18:11:14 +0000380#endif /* HAVE_GLIBC_BACKTRACE */
381}
382
383void
384zlog_backtrace(int priority)
385{
386#ifndef HAVE_GLIBC_BACKTRACE
387 zlog(NULL, priority, "No backtrace available on this platform.");
388#else
389 void *array[20];
390 int size, i;
391 char **strings;
392
393 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
394 ((size_t)size > sizeof(array)/sizeof(array[0])))
395 {
396 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
397 "(valid range is between 1 and %u)",
398 size, sizeof(array)/sizeof(array[0]));
399 return;
400 }
401 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
402 if (!(strings = backtrace_symbols(array, size)))
403 {
404 zlog_err("Cannot get backtrace symbols (out of memory?)");
405 for (i = 0; i < size; i++)
406 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
407 }
408 else
409 {
410 for (i = 0; i < size; i++)
411 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
412 free(strings);
413 }
414#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000415}
416
paul718e3742002-12-13 20:15:29 +0000417void
418zlog (struct zlog *zl, int priority, const char *format, ...)
419{
ajsd246bd92004-11-23 17:35:08 +0000420 va_list args;
paul718e3742002-12-13 20:15:29 +0000421
ajsd246bd92004-11-23 17:35:08 +0000422 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000423 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000424 va_end (args);
paul718e3742002-12-13 20:15:29 +0000425}
426
ajsd246bd92004-11-23 17:35:08 +0000427#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
428void \
429FUNCNAME(const char *format, ...) \
430{ \
431 va_list args; \
432 va_start(args, format); \
433 vzlog (NULL, PRIORITY, format, args); \
434 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000435}
436
ajsd246bd92004-11-23 17:35:08 +0000437ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000438
ajsd246bd92004-11-23 17:35:08 +0000439ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000440
ajsd246bd92004-11-23 17:35:08 +0000441ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000442
ajsd246bd92004-11-23 17:35:08 +0000443ZLOG_FUNC(zlog_notice, LOG_NOTICE)
444
445ZLOG_FUNC(zlog_debug, LOG_DEBUG)
446
447#undef ZLOG_FUNC
448
449#define PLOG_FUNC(FUNCNAME,PRIORITY) \
450void \
451FUNCNAME(struct zlog *zl, const char *format, ...) \
452{ \
453 va_list args; \
454 va_start(args, format); \
455 vzlog (zl, PRIORITY, format, args); \
456 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000457}
458
ajsd246bd92004-11-23 17:35:08 +0000459PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000460
ajsd246bd92004-11-23 17:35:08 +0000461PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000462
ajsd246bd92004-11-23 17:35:08 +0000463PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000464
ajsd246bd92004-11-23 17:35:08 +0000465PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000466
ajsd246bd92004-11-23 17:35:08 +0000467PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000468
ajsd246bd92004-11-23 17:35:08 +0000469#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000470
ajscee3df12004-11-24 17:14:49 +0000471void
472_zlog_assert_failed (const char *assertion, const char *file,
473 unsigned int line, const char *function)
474{
475 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
476 assertion,file,line,(function ? function : "?"));
ajs3378d202004-12-10 22:43:17 +0000477 zlog_backtrace(LOG_ERR);
ajscee3df12004-11-24 17:14:49 +0000478 abort();
479}
480
paul718e3742002-12-13 20:15:29 +0000481
482/* Open log stream */
483struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000484openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000485 int syslog_flags, int syslog_facility)
486{
487 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000488 u_int i;
paul718e3742002-12-13 20:15:29 +0000489
ajs274a4a42004-12-07 15:39:31 +0000490 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000491
492 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000493 zl->protocol = protocol;
494 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000495 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000496
ajs274a4a42004-12-07 15:39:31 +0000497 /* Set default logging levels. */
498 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
499 zl->maxlvl[i] = ZLOG_DISABLED;
500 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
501 zl->default_lvl = LOG_DEBUG;
502
paul718e3742002-12-13 20:15:29 +0000503 openlog (progname, syslog_flags, zl->facility);
504
505 return zl;
506}
507
508void
509closezlog (struct zlog *zl)
510{
511 closelog();
512 fclose (zl->fp);
513
514 XFREE (MTYPE_ZLOG, zl);
515}
516
517/* Called from command.c. */
518void
ajs274a4a42004-12-07 15:39:31 +0000519zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000520{
521 if (zl == NULL)
522 zl = zlog_default;
523
ajs274a4a42004-12-07 15:39:31 +0000524 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000525}
526
527int
ajs274a4a42004-12-07 15:39:31 +0000528zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000529{
530 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000531 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000532
533 /* There is opend file. */
534 zlog_reset_file (zl);
535
536 /* Set default zl. */
537 if (zl == NULL)
538 zl = zlog_default;
539
540 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000541 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000542 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000543 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000544 if (fp == NULL)
545 return 0;
paul718e3742002-12-13 20:15:29 +0000546
547 /* Set flags. */
548 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000549 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000550 zl->fp = fp;
551
552 return 1;
553}
554
555/* Reset opend file. */
556int
557zlog_reset_file (struct zlog *zl)
558{
559 if (zl == NULL)
560 zl = zlog_default;
561
paul718e3742002-12-13 20:15:29 +0000562 if (zl->fp)
563 fclose (zl->fp);
564 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000565 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000566
567 if (zl->filename)
568 free (zl->filename);
569 zl->filename = NULL;
570
571 return 1;
572}
573
574/* Reopen log file. */
575int
576zlog_rotate (struct zlog *zl)
577{
ajs274a4a42004-12-07 15:39:31 +0000578 int level;
paul718e3742002-12-13 20:15:29 +0000579
580 if (zl == NULL)
581 zl = zlog_default;
582
583 if (zl->fp)
584 fclose (zl->fp);
585 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000586 level = zl->maxlvl[ZLOG_DEST_FILE];
587 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000588
589 if (zl->filename)
590 {
gdtaa593d52003-12-22 20:15:53 +0000591 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000592 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000593
594 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000595 zl->fp = fopen (zl->filename, "a");
596 save_errno = errno;
597 umask(oldumask);
598 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000599 {
ajs274a4a42004-12-07 15:39:31 +0000600 zlog_err("Log rotate failed: cannot open file %s for append: %s",
601 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000602 return -1;
603 }
ajs274a4a42004-12-07 15:39:31 +0000604 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000605 }
606
607 return 1;
608}
609
paul718e3742002-12-13 20:15:29 +0000610/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000611const char *
paul718e3742002-12-13 20:15:29 +0000612lookup (struct message *mes, int key)
613{
614 struct message *pnt;
615
616 for (pnt = mes; pnt->key != 0; pnt++)
617 if (pnt->key == key)
618 return pnt->str;
619
620 return "";
621}
622
623/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000624 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000625const char *
paul718e3742002-12-13 20:15:29 +0000626mes_lookup (struct message *meslist, int max, int index)
627{
628 if (index < 0 || index >= max)
629 {
630 zlog_err ("message index out of bound: %d", max);
631 return NULL;
632 }
633 return meslist[index].str;
634}
ajsca359762004-11-19 23:40:16 +0000635
636/* Wrapper around strerror to handle case where it returns NULL. */
637const char *
638safe_strerror(int errnum)
639{
640 const char *s = strerror(errnum);
641 return (s != NULL) ? s : "Unknown error";
642}