blob: a639745dbf53658860ddec19d40e22855c02c95c [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs239c26f2005-01-17 15:22:28 +00002 * $Id: log.c,v 1.22 2005/01/17 15:22:28 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
ajs239c26f2005-01-17 15:22:28 +0000315 zlog_backtrace_sigsafe(PRI, program_counter);
ajs274a4a42004-12-07 15:39:31 +0000316#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
ajs239c26f2005-01-17 15:22:28 +0000323zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000324{
325#ifdef HAVE_GLIBC_BACKTRACE
ajs239c26f2005-01-17 15:22:28 +0000326 static const char pclabel[] = "Program counter: ";
ajs063ee522004-11-26 18:11:14 +0000327 void *array[20];
328 int size;
329 char buf[100];
330 char *s;
331#define LOC s,buf+sizeof(buf)-s
332
ajs063ee522004-11-26 18:11:14 +0000333 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
334 ((size_t)size > sizeof(array)/sizeof(array[0])))
335 return;
336 s = buf;
337 s = str_append(LOC,"Backtrace for ");
338 s = num_append(LOC,size);
339 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000340
341#define DUMP(FP) { \
ajs239c26f2005-01-17 15:22:28 +0000342 if (program_counter) \
343 { \
344 write(fileno(FP),pclabel,sizeof(pclabel)-1); \
345 backtrace_symbols_fd(&program_counter, 1, fileno(FP)); \
346 } \
ajs59a06a92004-11-23 18:19:14 +0000347 write(fileno(FP),buf,s-buf); \
348 backtrace_symbols_fd(array, size, fileno(FP)); \
349}
350
351 if (!zlog_default)
352 DUMP(stderr)
353 else
354 {
ajs274a4a42004-12-07 15:39:31 +0000355 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
356 zlog_default->fp)
ajs063ee522004-11-26 18:11:14 +0000357 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000358 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs063ee522004-11-26 18:11:14 +0000359 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000360 /* Remove trailing '\n' for monitor and syslog */
361 *--s = '\0';
362 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
363 vty_log_fixed(buf,s-buf);
364 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
365 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
366 {
367 int i;
368 /* Just print the function addresses. */
369 for (i = 0; i < size; i++)
370 {
371 s = buf;
372 s = str_append(LOC,"[bt ");
373 s = num_append(LOC,i);
374 s = str_append(LOC,"] 0x");
375 s = hex_append(LOC,(u_long)(array[i]));
376 *s = '\0';
377 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
378 vty_log_fixed(buf,s-buf);
379 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000380 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000381 }
382 }
ajs59a06a92004-11-23 18:19:14 +0000383 }
384#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000385#undef LOC
ajs063ee522004-11-26 18:11:14 +0000386#endif /* HAVE_GLIBC_BACKTRACE */
387}
388
389void
390zlog_backtrace(int priority)
391{
392#ifndef HAVE_GLIBC_BACKTRACE
393 zlog(NULL, priority, "No backtrace available on this platform.");
394#else
395 void *array[20];
396 int size, i;
397 char **strings;
398
399 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
400 ((size_t)size > sizeof(array)/sizeof(array[0])))
401 {
402 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
403 "(valid range is between 1 and %u)",
404 size, sizeof(array)/sizeof(array[0]));
405 return;
406 }
407 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
408 if (!(strings = backtrace_symbols(array, size)))
409 {
410 zlog_err("Cannot get backtrace symbols (out of memory?)");
411 for (i = 0; i < size; i++)
412 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
413 }
414 else
415 {
416 for (i = 0; i < size; i++)
417 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
418 free(strings);
419 }
420#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000421}
422
paul718e3742002-12-13 20:15:29 +0000423void
424zlog (struct zlog *zl, int priority, const char *format, ...)
425{
ajsd246bd92004-11-23 17:35:08 +0000426 va_list args;
paul718e3742002-12-13 20:15:29 +0000427
ajsd246bd92004-11-23 17:35:08 +0000428 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000429 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000430 va_end (args);
paul718e3742002-12-13 20:15:29 +0000431}
432
ajsd246bd92004-11-23 17:35:08 +0000433#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
434void \
435FUNCNAME(const char *format, ...) \
436{ \
437 va_list args; \
438 va_start(args, format); \
439 vzlog (NULL, PRIORITY, format, args); \
440 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000441}
442
ajsd246bd92004-11-23 17:35:08 +0000443ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000444
ajsd246bd92004-11-23 17:35:08 +0000445ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000446
ajsd246bd92004-11-23 17:35:08 +0000447ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000448
ajsd246bd92004-11-23 17:35:08 +0000449ZLOG_FUNC(zlog_notice, LOG_NOTICE)
450
451ZLOG_FUNC(zlog_debug, LOG_DEBUG)
452
453#undef ZLOG_FUNC
454
455#define PLOG_FUNC(FUNCNAME,PRIORITY) \
456void \
457FUNCNAME(struct zlog *zl, const char *format, ...) \
458{ \
459 va_list args; \
460 va_start(args, format); \
461 vzlog (zl, PRIORITY, format, args); \
462 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000463}
464
ajsd246bd92004-11-23 17:35:08 +0000465PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000466
ajsd246bd92004-11-23 17:35:08 +0000467PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000468
ajsd246bd92004-11-23 17:35:08 +0000469PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000470
ajsd246bd92004-11-23 17:35:08 +0000471PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000472
ajsd246bd92004-11-23 17:35:08 +0000473PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000474
ajsd246bd92004-11-23 17:35:08 +0000475#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000476
ajscee3df12004-11-24 17:14:49 +0000477void
478_zlog_assert_failed (const char *assertion, const char *file,
479 unsigned int line, const char *function)
480{
481 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
482 assertion,file,line,(function ? function : "?"));
ajs3378d202004-12-10 22:43:17 +0000483 zlog_backtrace(LOG_ERR);
ajscee3df12004-11-24 17:14:49 +0000484 abort();
485}
486
paul718e3742002-12-13 20:15:29 +0000487
488/* Open log stream */
489struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000490openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000491 int syslog_flags, int syslog_facility)
492{
493 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000494 u_int i;
paul718e3742002-12-13 20:15:29 +0000495
ajs274a4a42004-12-07 15:39:31 +0000496 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000497
498 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000499 zl->protocol = protocol;
500 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000501 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000502
ajs274a4a42004-12-07 15:39:31 +0000503 /* Set default logging levels. */
504 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
505 zl->maxlvl[i] = ZLOG_DISABLED;
506 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
507 zl->default_lvl = LOG_DEBUG;
508
paul718e3742002-12-13 20:15:29 +0000509 openlog (progname, syslog_flags, zl->facility);
510
511 return zl;
512}
513
514void
515closezlog (struct zlog *zl)
516{
517 closelog();
518 fclose (zl->fp);
519
520 XFREE (MTYPE_ZLOG, zl);
521}
522
523/* Called from command.c. */
524void
ajs274a4a42004-12-07 15:39:31 +0000525zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000526{
527 if (zl == NULL)
528 zl = zlog_default;
529
ajs274a4a42004-12-07 15:39:31 +0000530 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000531}
532
533int
ajs274a4a42004-12-07 15:39:31 +0000534zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000535{
536 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000537 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000538
539 /* There is opend file. */
540 zlog_reset_file (zl);
541
542 /* Set default zl. */
543 if (zl == NULL)
544 zl = zlog_default;
545
546 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000547 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000548 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000549 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000550 if (fp == NULL)
551 return 0;
paul718e3742002-12-13 20:15:29 +0000552
553 /* Set flags. */
554 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000555 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000556 zl->fp = fp;
557
558 return 1;
559}
560
561/* Reset opend file. */
562int
563zlog_reset_file (struct zlog *zl)
564{
565 if (zl == NULL)
566 zl = zlog_default;
567
paul718e3742002-12-13 20:15:29 +0000568 if (zl->fp)
569 fclose (zl->fp);
570 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000571 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000572
573 if (zl->filename)
574 free (zl->filename);
575 zl->filename = NULL;
576
577 return 1;
578}
579
580/* Reopen log file. */
581int
582zlog_rotate (struct zlog *zl)
583{
ajs274a4a42004-12-07 15:39:31 +0000584 int level;
paul718e3742002-12-13 20:15:29 +0000585
586 if (zl == NULL)
587 zl = zlog_default;
588
589 if (zl->fp)
590 fclose (zl->fp);
591 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000592 level = zl->maxlvl[ZLOG_DEST_FILE];
593 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000594
595 if (zl->filename)
596 {
gdtaa593d52003-12-22 20:15:53 +0000597 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000598 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000599
600 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000601 zl->fp = fopen (zl->filename, "a");
602 save_errno = errno;
603 umask(oldumask);
604 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000605 {
ajs274a4a42004-12-07 15:39:31 +0000606 zlog_err("Log rotate failed: cannot open file %s for append: %s",
607 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000608 return -1;
609 }
ajs274a4a42004-12-07 15:39:31 +0000610 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000611 }
612
613 return 1;
614}
615
paul718e3742002-12-13 20:15:29 +0000616/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000617const char *
paul718e3742002-12-13 20:15:29 +0000618lookup (struct message *mes, int key)
619{
620 struct message *pnt;
621
622 for (pnt = mes; pnt->key != 0; pnt++)
623 if (pnt->key == key)
624 return pnt->str;
625
626 return "";
627}
628
629/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000630 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000631const char *
paul718e3742002-12-13 20:15:29 +0000632mes_lookup (struct message *meslist, int max, int index)
633{
634 if (index < 0 || index >= max)
635 {
636 zlog_err ("message index out of bound: %d", max);
637 return NULL;
638 }
639 return meslist[index].str;
640}
ajsca359762004-11-19 23:40:16 +0000641
642/* Wrapper around strerror to handle case where it returns NULL. */
643const char *
644safe_strerror(int errnum)
645{
646 const char *s = strerror(errnum);
647 return (s != NULL) ? s : "Unknown error";
648}