2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: debug.c,v 1.40 2006/07/26 11:49:32 martinea Exp $
37 #include "timestamp.h"
40 #ifdef HAVE_GLIBC_BACKTRACE
44 /* Minimum file descriptor on which to keep the debug file. This is intended
45 * to keep the descriptor "out of the way" of other processing. It's not clear
46 * that this is required any longer, but it doesn't hurt anything.
50 /* information on the current debug file */
51 static int db_fd = 2; /* file descriptor (default stderr) */
52 static FILE *db_file = NULL; /* stdio stream */
53 static char *db_name = NULL; /* unqualified filename */
54 static char *db_filename = NULL; /* fully qualified pathname */
56 /* directory containing debug file, including trailing slash */
57 static char *dbgdir = NULL;
59 /* time debug log was opened (timestamp of the file) */
60 static time_t open_time;
62 /* storage for global variables */
63 int error_exit_status = 1;
65 /* static function prototypes */
66 static char *get_debug_name(time_t t, int n);
67 static void debug_unlink_old(void);
68 static void debug_setup_1(char *config, char *subdir);
69 static void debug_setup_2(char *s, int fd, char *annotation);
70 static char *msg_timestamp(void);
72 static void debug_logging_handler(const gchar *log_domain,
73 GLogLevelFlags log_level,
76 static void debug_setup_logging(void);
78 /* By default, do not suppress tracebacks */
79 static gboolean do_suppress_error_traceback = FALSE;
81 /* configured amanda_log_handlers */
82 static GSList *amanda_log_handlers = NULL;
85 * Generate a debug file name. The name is based on the program name,
86 * followed by a timestamp, an optional sequence number, and ".debug".
89 * @param n: sequence number between 1 and 1000; if zero, no sequence number
97 char number[NUM_STR_SIZE];
101 if(n < 0 || n > 1000) {
104 ts = get_timestamp_from_time(t);
108 g_snprintf(number, SIZEOF(number), "%03d", n - 1);
110 result = vstralloc(get_pname(), ".", ts, number, ".debug", NULL);
115 /* Call this to suppress tracebacks on error() or g_critical(). This is used
116 * when a critical error is indicated in perl, and the traceback will not be
119 suppress_error_traceback(void)
121 do_suppress_error_traceback = 1;
124 /* A GLogFunc to handle g_log calls. This function assumes that user_data
125 * is either NULL or a pointer to one of the debug_* configuration variables
126 * in conffile.c, indicating whether logging for this log domain is enabled.
128 * @param log_domain: the log domain, or NULL for general logging
129 * @param log_level: level, fatality, and recursion flags
130 * @param message: the message to log
131 * @param user_pointer: unused
134 debug_logging_handler(const gchar *log_domain G_GNUC_UNUSED,
135 GLogLevelFlags log_level,
136 const gchar *message,
137 gpointer user_data G_GNUC_UNUSED)
139 GLogLevelFlags maxlevel;
140 char *levprefix = NULL;
141 pcontext_t context = get_pcontext();
143 /* glib allows a message to have multiple levels, so calculate the "worst"
145 if (log_level & G_LOG_LEVEL_ERROR) {
146 maxlevel = G_LOG_LEVEL_ERROR;
147 levprefix = _("error (fatal): ");
148 } else if (log_level & G_LOG_LEVEL_CRITICAL) {
149 maxlevel = G_LOG_LEVEL_CRITICAL;
150 levprefix = _("critical (fatal): ");
151 } else if (log_level & G_LOG_LEVEL_WARNING) {
152 maxlevel = G_LOG_LEVEL_WARNING;
153 levprefix = _("warning: ");
154 } else if (log_level & G_LOG_LEVEL_MESSAGE) {
155 maxlevel = G_LOG_LEVEL_MESSAGE;
156 levprefix = _("message: ");
157 } else if (log_level & G_LOG_LEVEL_INFO) {
158 maxlevel = G_LOG_LEVEL_INFO;
159 levprefix = _("info: ");
161 maxlevel = G_LOG_LEVEL_DEBUG;
162 levprefix = ""; /* no level displayed for debugging */
165 /* scriptutil context doesn't do any logging except for critical
166 * and error levels */
167 if (context != CONTEXT_SCRIPTUTIL) {
168 /* convert the highest level to a string and dbprintf it */
169 debug_printf("%s%s\n", levprefix, message);
172 if (amanda_log_handlers) {
173 GSList *iter = amanda_log_handlers;
175 amanda_log_handler_t *hdlr = (amanda_log_handler_t *)iter->data;
176 hdlr(maxlevel, message);
177 iter = g_slist_next(iter);
180 /* call the appropriate handlers, based on the context */
181 amanda_log_stderr(maxlevel, message);
182 if (context == CONTEXT_DAEMON)
183 amanda_log_syslog(maxlevel, message);
186 /* error and critical levels have special handling */
187 if (log_level & (G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL)) {
188 #ifdef HAVE_GLIBC_BACKTRACE
189 /* try logging a traceback to the debug log */
190 if (!do_suppress_error_traceback && db_fd != -1) {
193 naddrs = backtrace(stack, sizeof(stack)/sizeof(*stack));
194 backtrace_symbols_fd(stack, naddrs, db_fd);
199 if (log_level & G_LOG_LEVEL_CRITICAL)
200 exit(error_exit_status);
203 g_assert_not_reached();
207 /* Install our handler into the glib log handling system.
210 debug_setup_logging(void)
212 /* g_error and g_critical should be fatal, although the log handler
213 * takes care of this anyway */
214 g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
216 /* set up handler (g_log_set_default_handler is new in glib-2.6, and
217 * hence not useable here) */
218 g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
219 debug_logging_handler, NULL);
223 add_amanda_log_handler(amanda_log_handler_t *hdlr)
225 amanda_log_handlers = g_slist_append(amanda_log_handlers, (gpointer)hdlr);
229 amanda_log_syslog(GLogLevelFlags log_level, const gchar *message)
231 int priority = LOG_ERR;
233 case G_LOG_LEVEL_ERROR:
234 case G_LOG_LEVEL_CRITICAL:
238 case G_LOG_LEVEL_WARNING:
240 priority = LOG_WARNING;
249 openlog(get_pname(), LOG_PID, LOG_DAEMON);
251 openlog(get_pname(), LOG_PID, 0);
253 syslog(priority, "%s", message);
259 amanda_log_stderr(GLogLevelFlags log_level, const gchar *message)
262 case G_LOG_LEVEL_ERROR:
263 case G_LOG_LEVEL_CRITICAL:
264 g_fprintf(stderr, "%s: %s\n", get_pname(), message);
273 amanda_log_null(GLogLevelFlags log_level G_GNUC_UNUSED, const gchar *message G_GNUC_UNUSED)
277 /* Set the global dbgdir according to 'config' and 'subdir'
279 * The global open_time is set to the current time, and used to delete
282 * @param config: configuration or NULL
283 * @param subdir: subdirectory (server, client, etc.) or NULL
286 debug_setup_1(char *config, char *subdir)
288 char *sane_config = NULL;
291 * Create the debug directory if it does not yet exist.
295 sane_config = sanitise_filename(config);
296 if (sane_config && subdir)
297 dbgdir = vstralloc(AMANDA_DBGDIR, "/", subdir, "/", sane_config,
299 else if (sane_config)
300 dbgdir = vstralloc(AMANDA_DBGDIR, "/", sane_config, "/", NULL);
302 dbgdir = vstralloc(AMANDA_DBGDIR, "/", subdir, "/", NULL);
304 dbgdir = stralloc2(AMANDA_DBGDIR, "/");
305 if(mkpdir(dbgdir, 0700, get_client_uid(), get_client_gid()) == -1) {
306 error(_("create debug directory \"%s\": %s"),
307 dbgdir, strerror(errno));
316 * Clean out old debug files. We also rename files with old style
317 * names (XXX.debug or XXX.$PID.debug) into the new name format.
318 * We assume no system has 17 digit PID-s :-) and that there will
319 * not be a conflict between an old and new name.
322 debug_unlink_old(void)
328 struct dirent *entry;
331 size_t test_name_len;
333 char *dbfilename = NULL;
338 assert(dbgdir != NULL);
340 memset(&sbuf, 0, SIZEOF(sbuf));
343 pname_len = strlen(pname);
345 if((d = opendir(dbgdir)) == NULL) {
346 error(_("open debug directory \"%s\": %s"),
347 dbgdir, strerror(errno));
350 test_name = get_debug_name(open_time - (getconf_int(CNF_DEBUG_DAYS) * 24 * 60 * 60), 0);
351 test_name_len = strlen(test_name);
352 while((entry = readdir(d)) != NULL) {
353 if(is_dot_or_dotdot(entry->d_name)) {
356 d_name_len = strlen(entry->d_name);
357 if(strncmp(entry->d_name, pname, pname_len) != 0
358 || entry->d_name[pname_len] != '.'
360 || strcmp(entry->d_name + d_name_len - 6, ".debug") != 0) {
361 continue; /* not one of our debug files */
363 e = newvstralloc(e, dbgdir, entry->d_name, NULL);
364 if(d_name_len < test_name_len) {
366 * Create a "pretend" name based on the last modification
367 * time. This name will be used to decide if the real name
368 * should be removed. If not, it will be used to rename the
371 if(stat(e, &sbuf) != 0) {
372 continue; /* ignore errors */
375 dbfilename = get_debug_name((time_t)sbuf.st_mtime, 0);
378 dbfilename = newstralloc(dbfilename, entry->d_name);
381 if(strcmp(dbfilename, test_name) < 0) {
382 (void) unlink(e); /* get rid of old file */
387 while(dbfilename != NULL
388 && (s = newvstralloc(s, dbgdir, dbfilename, NULL)) != NULL
389 && rename(e, s) != 0 && errno != ENOENT) {
391 dbfilename = get_debug_name((time_t)sbuf.st_mtime, ++i);
393 if(dbfilename == NULL) {
394 error(_("cannot rename old debug file \"%s\""), entry->d_name);
406 /* Given an already-opened debug file, set the file's ownership
407 * appropriately, move its file descriptor above MIN_DB_FD, and
408 * add an initial log entry to the file.
410 * This function records the file's identity in the globals
411 * db_filename, db_fd, and db_file. It does *not* set db_name.
412 * db_file is not set if fd is -1
414 * This function uses the global 'open_time', which is set by
417 * @param s: the filename of the debug file; string should be malloc'd,
418 * and should *not* be freed by the caller.
419 * @param fd: the descriptor connected to the debug file, or -1 if
420 * no decriptor moving should take place.
421 * @param annotation: an extra string to include in the initial
431 int fd_close[MIN_DB_FD+1];
437 /* If we're root, change the ownership of the debug files. If we're not root,
438 * this would either be redundant or an error. */
439 if (geteuid() == 0) {
440 if (chown(db_filename, get_client_uid(), get_client_gid()) < 0) {
441 dbprintf(_("chown(%s, %d, %d) failed: %s"),
442 db_filename, (int)get_client_uid(), (int)get_client_gid(), strerror(errno));
447 * Move the file descriptor up high so it stays out of the way
448 * of other processing, e.g. sendbackup.
453 while((db_fd = dup(fd)) < MIN_DB_FD) {
454 fd_close[i++] = db_fd;
459 db_file = fdopen(db_fd, "a");
464 * Make the first debug log file entry.
466 debug_printf(_("pid %ld ruid %ld euid %ld version %s: %s at %s"),
468 (long)getuid(), (long)geteuid(),
475 /* Get current GMT time and return a message timestamp.
476 * Used for g_printf calls to logs and such. The return value
477 * is to a static buffer, so it should be used immediately.
479 * @returns: timestamp
484 static char timestamp[128];
489 ctime_r(&curtime, timestamp);
490 r = strchr(timestamp, '\n');
498 * ---- public functions
504 debug_setup_logging();
506 /* the scriptutil context does not create a debug log, since such
507 * processes are invoked many times.
509 if (get_pcontext() != CONTEXT_SCRIPTUTIL) {
510 debug_open(get_ptype());
515 debug_open(char *subdir)
522 /* set up logging while we're here */
523 debug_setup_logging();
525 /* set 'dbgdir' and clean out old debug files */
526 debug_setup_1(NULL, subdir);
529 * Create the new file with a unique sequence number.
531 mask = (mode_t)umask((mode_t)0037); /* Allow the group read bit through */
533 /* iteratate through sequence numbers until we find one that
534 * is not already in use */
535 for(i = 0; fd < 0; i++) {
537 if ((db_name = get_debug_name(open_time, i)) == NULL) {
538 error(_("Cannot create debug file name in %d tries."), i);
542 if ((s = newvstralloc(s, dbgdir, db_name, NULL)) == NULL) {
543 error(_("Cannot allocate debug file name memory"));
547 if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
548 if (errno != EEXIST) {
549 error(_("Cannot create debug file \"%s\": %s"),
556 (void)umask(mask); /* Restore mask */
561 * Note: we release control of the string 's' points to.
563 debug_setup_2(s, fd, "start");
574 if (dbfilename == NULL) {
578 /* set 'dbgdir' and clean out old debug files */
579 debug_setup_1(NULL, NULL);
584 if (*dbfilename == '/') {
585 s = stralloc(dbfilename);
587 s = newvstralloc(s, dbgdir, dbfilename, NULL);
589 if ((fd = open(s, O_RDWR|O_APPEND)) < 0) {
590 error(_("cannot reopen debug file %s"), dbfilename);
597 * Note: we release control of the string 's' points to.
599 debug_setup_2(s, fd, annotation);
615 if (get_pcontext() == CONTEXT_SCRIPTUTIL) {
619 /* Remove old log from source directory */
621 /* set 'dbgdir' and clean out old debug files */
622 debug_setup_1(config, subdir);
623 /* Remove old log from destination directory */
626 s = newvstralloc(s, dbgdir, db_name, NULL);
628 if (strcmp(db_filename, s) == 0) {
633 mask = (mode_t)umask((mode_t)0037);
635 #if defined(__CYGWIN__)
637 * On cygwin, rename will not overwrite an existing file nor
638 * will it rename a file that is open for writing...
640 * Rename file directly. Expect failure if file already exists
641 * or is open by another user.
645 while (rename(db_filename, s) < 0) {
646 if (errno != EEXIST) {
648 * If the failure was not due to the target file name already
649 * existing then we have bigger issues at hand so we keep
652 dbprintf(_("Cannot rename \"%s\" to \"%s\": %s\n"),
653 db_filename, s, strerror(errno));
654 s = newvstralloc(s, db_filename, NULL);
660 * Files already exists:
661 * Continue searching for a unique file name that will work.
664 if ((db_name = get_debug_name(open_time, i++)) == NULL) {
665 dbprintf(_("Cannot create unique debug file name"));
668 s = newvstralloc(s, dbgdir, db_name, NULL);
672 * We need to close and reopen the original file handle to
673 * release control of the original debug file name.
675 if ((fd = open(s, O_WRONLY|O_APPEND, 0640)) >= 0) {
677 * We can safely close the the original log file
678 * since we now have a new working handle.
686 /* check if a file with the same name already exists. */
687 if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
688 for(i = 0; fd < 0; i++) {
690 if ((db_name = get_debug_name(open_time, i)) == NULL) {
691 dbprintf(_("Cannot create debug file"));
695 s = newvstralloc(s, dbgdir, db_name, NULL);
696 if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
697 if (errno != EEXIST) {
698 dbprintf(_("Cannot create debug file: %s"),
708 if (rename(db_filename, s) == -1) {
709 dbprintf(_("Cannot rename \"%s\" to \"%s\": %s\n"),
710 db_filename, s, strerror(errno));
716 (void)umask(mask); /* Restore mask */
720 * Note: we release control of the string 's' points to.
722 debug_setup_2(s, fd, "rename");
730 if (get_pcontext() == CONTEXT_SCRIPTUTIL) {
737 debug_printf(_("pid %ld finish time %s"), (long)getpid(), ctime(&curtime));
739 if(db_file && fclose(db_file) == EOF) {
740 int save_errno = errno;
742 db_file = NULL; /* prevent recursion */
743 g_fprintf(stderr, _("close debug file: %s"), strerror(save_errno));
753 * Format and write a debug message to the process debug file.
755 printf_arglist_function(void debug_printf, const char *, format)
761 * It is common in the code to call dbprintf to write out
762 * syserrno(errno) and then turn around and try to do something else
763 * with errno (e.g. g_printf() or log()), so we make sure errno goes
764 * back out with the same value it came in with.
769 /* handle the default (stderr) if debug_open hasn't been called yet */
770 if(db_file == NULL && db_fd == 2) {
773 if(db_file != NULL) {
777 if (db_file != stderr)
778 prefix = g_strdup_printf("%s: %s:", msg_timestamp(), get_pname());
780 prefix = g_strdup_printf("%s:", get_pname());
781 arglist_start(argp, format);
782 text = g_strdup_vprintf(format, argp);
784 fprintf(db_file, "%s %s", prefix, text);
811 debug_dup_stderr_to_debug(void)
813 if(db_fd != -1 && db_fd != STDERR_FILENO)
815 if(dup2(db_fd, STDERR_FILENO) != STDERR_FILENO)
817 error(_("can't redirect stderr to the debug file: %d, %s"), db_fd, strerror(errno));
818 g_assert_not_reached();