]> git.gag.com Git - debian/amanda/blobdiff - common-src/debug.c
Imported Upstream version 2.5.0p2
[debian/amanda] / common-src / debug.c
index ced7bc1cd1ee9f720ce43f214a5d8a7fc8c0494f..66d31bd42c409247d4e8a5c9f9b847be6703a64c 100644 (file)
  *                        University of Maryland at College Park
  */
 /*
- * $Id: debug.c,v 1.17.4.3.4.3.2.9 2003/01/04 17:46:09 martinea Exp $
+ * $Id: debug.c,v 1.36 2006/01/12 01:57:05 paddy_s Exp $
  *
  * debug log subroutines
  */
 
 #include "amanda.h"
-#include "clock.h"
 #include "util.h"
 #include "arglist.h"
+#include "clock.h"
 
 #ifndef AMANDA_DBGDIR
 #  define AMANDA_DBGDIR                AMANDA_TMPDIR
@@ -54,7 +54,7 @@ static pid_t debug_prefix_pid = 0;
 /*
  * Format and write a debug message to the process debug file.
  */
-printf_arglist_function(void debug_printf, char *, format)
+printf_arglist_function(void debug_printf, const char *, format)
 {
     va_list argp;
     int save_errno;
@@ -100,37 +100,32 @@ get_debug_name(t, n)
     if(n == 0) {
        number[0] = '\0';
     } else {
-       ap_snprintf(number, sizeof(number), "%03d", n - 1);
+       snprintf(number, sizeof(number), "%03d", n - 1);
     }
     result = vstralloc(get_pname(), ".", ts, number, ".debug", NULL);
     amfree(ts);
     return result;
 }
 
-void debug_open()
+static char *dbgdir = NULL;
+static time_t curtime;
+
+static void debug_setup_1()
 {
-    time_t curtime;
-    int saved_debug;
-    char *dbgdir = NULL;
+    struct passwd *pwent;
+    char *pname;
+    size_t pname_len;
     char *e = NULL;
     char *s = NULL;
-    char *dbfilename = NULL;
     DIR *d;
     struct dirent *entry;
-    char *pname;
-    size_t pname_len;
     int do_rename;
     char *test_name = NULL;
     size_t test_name_len;
-    int fd = -1;
-    int i;
     size_t d_name_len;
-    int fd_close[MIN_DB_FD+1];
-    struct passwd *pwent;
     struct stat sbuf;
-
-    pname = get_pname();
-    pname_len = strlen(pname);
+    char *dbfilename = NULL;
+    int i;
 
     if(client_uid == (uid_t) -1 && (pwent = getpwnam(CLIENT_LOGIN)) != NULL) {
        client_uid = pwent->pw_uid;
@@ -138,9 +133,13 @@ void debug_open()
        endpwent();
     }
 
+    pname = get_pname();
+    pname_len = strlen(pname);
+
     /*
      * Create the debug directory if it does not yet exist.
      */
+    amfree(dbgdir);
     dbgdir = stralloc2(AMANDA_DBGDIR, "/");
     if(mkpdir(dbgdir, 02700, client_uid, client_gid) == -1) {
         error("create debug directory \"%s\": %s",
@@ -211,25 +210,22 @@ void debug_open()
     amfree(s);
     amfree(test_name);
     closedir(d);
+}
+
+static void debug_setup_2(s, fd, notation)
+    char *s;
+    int fd;
+    char *notation;
+{
+    int saved_debug;
+    int i;
+    int fd_close[MIN_DB_FD+1];
 
-    /*
-     * Create the new file.
-     */
-    for(i = 0;
-       (dbfilename = get_debug_name(curtime, i)) != NULL
-       && (s = newvstralloc(s, dbgdir, dbfilename, NULL)) != NULL
-       && (fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0600)) < 0;
-       i++, free(dbfilename)) {}
-    if(dbfilename == NULL) {
-       error("cannot create %s debug file", get_pname());
-    }
-    amfree(dbfilename);
     amfree(db_filename);
     db_filename = s;
     s = NULL;
     (void) chown(db_filename, client_uid, client_gid);
     amfree(dbgdir);
-
     /*
      * Move the file descriptor up high so it stays out of the way
      * of other processing, e.g. sendbackup.
@@ -244,15 +240,102 @@ void debug_open()
     }
     db_file = fdopen(db_fd, "a");
 
+    if (notation) {
+       /*
+        * Make the first debug log file entry.
+        */
+       saved_debug = debug; debug = 1;
+       debug_printf("%s: debug %d pid %ld ruid %ld euid %ld: %s at %s",
+                    get_pname(), saved_debug, (long)getpid(),
+                    (long)getuid(), (long)geteuid(),
+                    notation,
+                    ctime(&curtime));
+       debug = saved_debug;
+    }
+}
+
+void debug_open()
+{
+    char *dbfilename = NULL;
+    int fd = -1;
+    int i;
+    char *s = NULL;
+    mode_t mask;
+
     /*
-     * Make the first debug log file entry.
+     * Do initial setup.
      */
-    saved_debug = debug; debug = 1;
-    debug_printf("%s: debug %d pid %ld ruid %ld euid %ld: start at %s",
-                pname, saved_debug, (long)getpid(),
-                (long)getuid(), (long)geteuid(),
-                ctime(&curtime));
-    debug = saved_debug;
+    debug_setup_1();
+
+    /*
+     * Create the new file with a unique sequence number.
+     */
+    mask = umask(0037); /* Allow the group read bit through */
+    for(i = 0; fd < 0; i++) {
+        if ((dbfilename = get_debug_name(curtime, i)) == NULL) {
+           error("Cannot create %s debug file", get_pname());
+            /* NOTREACHED */
+        }
+
+        if ((s = newvstralloc(s, dbgdir, dbfilename, NULL)) == NULL) {
+           error("Cannot allocate %s debug file name memory", get_pname());
+            /* NOTREACHED */
+        }
+        amfree(dbfilename);
+
+        if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
+            if (errno != EEXIST) {
+                error("Cannot create %s debug file: %s",
+                       get_pname(), strerror(errno));
+                /* NOTREACHED */
+            }
+            amfree(s);
+        }
+    }
+    (void)umask(mask); /* Restore mask */
+
+    /*
+     * Finish setup.
+     *
+     * Note: we release control of the string 's' points to.
+     */
+    debug_setup_2(s, fd, "start");
+}
+
+void debug_reopen(dbfilename, notation)
+    char *dbfilename;
+    char *notation;
+{
+    char *s = NULL;
+    int fd = -1;
+
+    if (dbfilename == NULL) {
+       return;
+    }
+
+    /*
+     * Do initial setup.
+     */
+    debug_setup_1();
+
+    /*
+     * Reopen the file.
+     */
+    if (*dbfilename == '/') {
+       s = stralloc(dbfilename);
+    } else {
+       s = newvstralloc(s, dbgdir, dbfilename, NULL);
+    }
+    if ((fd = open(s, O_RDWR|O_APPEND)) < 0) {
+       error("cannot reopen %s debug file %s", get_pname(), dbfilename);
+    }
+
+    /*
+     * Finish setup.
+     *
+     * Note: we release control of the string 's' points to.
+     */
+    debug_setup_2(s, fd, notation);
 }
 
 void debug_close()
@@ -315,26 +398,31 @@ void set_debug_prefix_pid(p)
 char *debug_prefix(suffix)
     char *suffix;
 {
+    int save_errno;
     static char *s = NULL;
     char debug_pid[NUM_STR_SIZE];
 
+    save_errno = errno;
     s = newvstralloc(s, get_pname(), suffix, NULL);
     if (debug_prefix_pid != (pid_t) 0) {
-       ap_snprintf(debug_pid, sizeof(debug_pid),
-                   "%ld",
-                   (long) debug_prefix_pid);
+       snprintf(debug_pid, sizeof(debug_pid),
+                "%ld",
+                (long) debug_prefix_pid);
        s = newvstralloc(s, s, "[", debug_pid, "]", NULL);
     }
+    errno = save_errno;
     return s;
 }
 
 char *debug_prefix_time(suffix)
     char *suffix;
 {
+    int save_errno;
     static char *s = NULL;
     char *t1;
     char *t2;
 
+    save_errno = errno;
     if (clock_is_running()) {
        t1 = ": time ";
        t2 = walltime_str(curclock());
@@ -344,6 +432,7 @@ char *debug_prefix_time(suffix)
 
     s = newvstralloc(s, debug_prefix(suffix), t1, t2, NULL);
 
+    errno = save_errno;
     return s;
 }
 #endif