]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bsys.c
Restore JobIds in right order + Tru64 porting
[bacula/bacula] / bacula / src / lib / bsys.c
index 5a39359605ca39c5fa36e155313a12a775677bd7..bb36376c16301ff81c75406975a0a87716506423 100644 (file)
@@ -8,7 +8,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -66,7 +66,7 @@ void *bmalloc(size_t size)
 }
 #endif
 
-void *b_malloc(char *file, int line, size_t size)
+void *b_malloc(const char *file, int line, size_t size)
 {
   void *buf;
 
@@ -108,42 +108,21 @@ void *bcalloc (size_t size1, size_t size2)
 /*
  * Implement snprintf
  */
-int bsnprintf(char *str, size_t size, const  char  *fmt,  ...) 
+int bsnprintf(char *str, int32_t size, const char *fmt,  ...) 
 {
-#ifdef HAVE_VSNPRINTF
    va_list   arg_ptr;
    int len;
 
    va_start(arg_ptr, fmt);
-   len = vsnprintf(str, size, fmt, arg_ptr);
+   len = bvsnprintf(str, size, fmt, arg_ptr);
    va_end(arg_ptr);
-   str[size-1] = 0;
-   return len;
-
-#else
-
-   va_list   arg_ptr;
-   int len;
-   char *buf;
-
-   buf = get_memory(BIG_BUF);
-   va_start(arg_ptr, fmt);
-   len = vsprintf(buf, fmt, arg_ptr);
-   va_end(arg_ptr);
-   if (len >= BIG_BUF) {
-      Emsg0(M_ABORT, 0, _("Buffer overflow.\n"));
-   }
-   memcpy(str, buf, size);
-   str[size-1] = 0;
-   free_memory(buf);
    return len;
-#endif
 }
 
 /*
  * Implement vsnprintf()
  */
-int bvsnprintf(char *str, size_t size, const char  *format, va_list ap)
+int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap)
 {
 #ifdef HAVE_VSNPRINTF
    int len;
@@ -190,7 +169,7 @@ struct tm *localtime_r(const time_t *timep, struct tm *tm)
 #endif /* HAVE_LOCALTIME_R */
 
 #ifndef HAVE_READDIR_R
-
+#ifndef HAVE_WIN32
 #include <dirent.h>
 
 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
@@ -217,8 +196,9 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
     }
     V(mutex);
     return stat;
-}
 
+}
+#endif
 #endif /* HAVE_READDIR_R */
 
 #ifdef xxxxxxxxxx_STRERROR_R
@@ -282,16 +262,29 @@ void _v(char *file, int line, pthread_mutex_t *m)
 }
 #endif /* DEBUG_MUTEX */
 
-#ifndef HAVE_CYGWIN
+#ifdef DEBUG_MEMSET
+/* These routines are not normally turned on */
+#undef memset
+void b_memset(const char *file, int line, void *mem, int val, size_t num)
+{
+   /* Testing for 2000 byte zero at beginning of Volume block */
+   if (num > 1900 && num < 3000) {
+      Pmsg3(000, "Memset for %d bytes at %s:%d\n", (int)num, file, line);
+   }
+   memset(mem, val, num);
+}
+#endif
+
+#if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
 static int del_pid_file_ok = FALSE;
 #endif
 
 /*
  * Create a standard "Unix" pid file.
  */
-void create_pid_file(char *dir, char *progname, int port)
+void create_pid_file(char *dir, const char *progname, int port)
 {
-#ifndef HAVE_CYGWIN
+#if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
    int pidfd, len;
    int oldpid;
    char  pidbuf[20];
@@ -302,7 +295,7 @@ void create_pid_file(char *dir, char *progname, int port)
    if (stat(mp_chr(fname), &statp) == 0) {
       /* File exists, see what we have */
       *pidbuf = 0;
-      if ((pidfd = open(mp_chr(fname), O_RDONLY)) < 0 || 
+      if ((pidfd = open(mp_chr(fname), O_RDONLY|O_BINARY, 0)) < 0 || 
           read(pidfd, &pidbuf, sizeof(pidbuf)) < 0 ||
            sscanf(pidbuf, "%d", &oldpid) != 1) {
          Emsg2(M_ERROR_TERM, 0, _("Cannot open pid file. %s ERR=%s\n"), fname, strerror(errno));
@@ -316,7 +309,7 @@ void create_pid_file(char *dir, char *progname, int port)
       unlink(mp_chr(fname));                 /* remove stale pid file */
    }
    /* Create new pid file */
-   if ((pidfd = open(mp_chr(fname), O_CREAT | O_TRUNC | O_WRONLY, 0644)) >= 0) {
+   if ((pidfd = open(mp_chr(fname), O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0640)) >= 0) {
       len = sprintf(pidbuf, "%d\n", (int)getpid());
       write(pidfd, pidbuf, len);
       close(pidfd);
@@ -328,12 +321,13 @@ void create_pid_file(char *dir, char *progname, int port)
 #endif
 }
 
+
 /*
  * Delete the pid file if we created it
  */
-int delete_pid_file(char *dir, char *progname, int port)
+int delete_pid_file(char *dir, const char *progname, int port)
 {
-#ifndef HAVE_CYGWIN
+#if !defined(HAVE_CYGWIN)  && !defined(HAVE_WIN32)
    POOLMEM *fname = get_pool_memory(PM_FNAME);
 
    if (!del_pid_file_ok) {
@@ -348,6 +342,100 @@ int delete_pid_file(char *dir, char *progname, int port)
    return 1;
 }
 
+struct s_state_hdr {
+   char id[14];
+   int32_t version;
+   uint64_t last_jobs_addr;
+   uint64_t reserved[20];
+};
+
+static struct s_state_hdr state_hdr = { 
+   "Bacula State\n",
+   3,
+   0
+};
+
+/*
+ * Open and read the state file for the daemon
+ */
+void read_state_file(char *dir, const char *progname, int port)
+{
+   int sfd;
+   ssize_t stat;
+   POOLMEM *fname = get_pool_memory(PM_FNAME);
+   struct s_state_hdr hdr;
+   int hdr_size = sizeof(hdr);
+
+   Mmsg(&fname, "%s/%s.%d.state", dir, progname, port);
+   /* If file exists, see what we have */
+// Dmsg1(10, "O_BINARY=%d\n", O_BINARY);
+   if ((sfd = open(mp_chr(fname), O_RDONLY|O_BINARY, 0)) < 0) {
+      Dmsg3(010, "Could not open state file. sfd=%d size=%d: ERR=%s\n", 
+                   sfd, sizeof(hdr), strerror(errno));
+          goto bail_out;
+   }
+   if ((stat=read(sfd, &hdr, hdr_size)) != hdr_size) {
+      Dmsg4(010, "Could not read state file. sfd=%d stat=%d size=%d: ERR=%s\n", 
+                   sfd, (int)stat, hdr_size, strerror(errno));
+      goto bail_out;
+   }
+   if (hdr.version != state_hdr.version) {
+      Dmsg2(010, "Bad hdr version. Wanted %d got %d\n", 
+        state_hdr.version, hdr.version);
+   }
+   hdr.id[13] = 0;
+   if (strcmp(hdr.id, state_hdr.id) != 0) {
+      Dmsg0(000, "State file header id invalid.\n");
+      goto bail_out;
+   }
+// Dmsg1(010, "Read header of %d bytes.\n", sizeof(hdr));
+   read_last_jobs_list(sfd, hdr.last_jobs_addr);
+bail_out:
+   if (sfd >= 0) {
+      close(sfd);
+   }
+   free_pool_memory(fname);
+}
+
+/*
+ * Write the state file
+ */
+void write_state_file(char *dir, const char *progname, int port)
+{
+   int sfd;
+   POOLMEM *fname = get_pool_memory(PM_FNAME);
+
+   Mmsg(&fname, "%s/%s.%d.state", dir, progname, port);
+   /* Create new state file */
+   if ((sfd = open(mp_chr(fname), O_CREAT|O_WRONLY|O_BINARY, 0640)) < 0) {
+      Dmsg2(000, _("Could not create state file. %s ERR=%s\n"), fname, strerror(errno));
+      Emsg2(M_ERROR, 0, _("Could not create state file. %s ERR=%s\n"), fname, strerror(errno));
+      goto bail_out;
+   }
+   if (write(sfd, &state_hdr, sizeof(state_hdr)) != sizeof(state_hdr)) {
+      Dmsg1(000, "Write hdr error: ERR=%s\n", strerror(errno));
+      goto bail_out;
+   }
+// Dmsg1(010, "Wrote header of %d bytes\n", sizeof(state_hdr));
+   state_hdr.last_jobs_addr = sizeof(state_hdr);
+   state_hdr.reserved[0] = write_last_jobs_list(sfd, state_hdr.last_jobs_addr);   
+// Dmsg1(010, "write last job end = %d\n", (int)state_hdr.reserved[0]);
+   if (lseek(sfd, 0, SEEK_SET) < 0) {
+      Dmsg1(000, "lseek error: ERR=%s\n", strerror(errno));
+      goto bail_out;
+   }  
+   if (write(sfd, &state_hdr, sizeof(state_hdr)) != sizeof(state_hdr)) {
+      Pmsg1(000, "Write final hdr error: ERR=%s\n", strerror(errno));
+   }
+// Dmsg1(010, "rewrote header = %d\n", sizeof(state_hdr));
+bail_out:
+   if (sfd >= 0) {
+      close(sfd);
+   }
+   free_pool_memory(fname);
+}
+
+
 /*
  * Drop to privilege new userid and new gid if non-NULL
  */
@@ -389,12 +477,12 @@ static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
 
 /*
- * This routine will sleep (sec, msec).  Note, however, that if a 
+ * This routine will sleep (sec, microsec).  Note, however, that if a 
  *   signal occurs, it will return early.  It is up to the caller
  *   to recall this routine if he/she REALLY wants to sleep the
  *   requested time.
  */
-int bmicrosleep(time_t sec, long msec)
+int bmicrosleep(time_t sec, long usec)
 {
    struct timespec timeout;
    struct timeval tv;
@@ -402,7 +490,7 @@ int bmicrosleep(time_t sec, long msec)
    int stat;
 
    timeout.tv_sec = sec;
-   timeout.tv_nsec = msec * 1000;
+   timeout.tv_nsec = usec * 1000;
 
 #ifdef HAVE_NANOSLEEP
    stat = nanosleep(&timeout, NULL);
@@ -421,11 +509,14 @@ int bmicrosleep(time_t sec, long msec)
       timeout.tv_sec++;
    }
 
-   Dmsg1(200, "pthread_cond_timedwait sec=%d\n", timeout.tv_sec);
+   Dmsg2(200, "pthread_cond_timedwait sec=%d usec=%d\n", sec, usec);
    /* Note, this unlocks mutex during the sleep */
    P(timer_mutex);
    stat = pthread_cond_timedwait(&timer, &timer_mutex, &timeout);
-   Dmsg1(200, "pthread_cond_timedwait stat=%d\n", stat);
+   if (stat != 0) {
+      Dmsg2(200, "pthread_cond_timedwait stat=%d ERR=%s\n", stat,
+        strerror(stat));
+   }
    V(timer_mutex);
    return stat;
 }
@@ -443,7 +534,7 @@ strtoll(const char *ptr, char **endptr, int base)
  * Bacula's implementation of fgets(). The difference is that it handles
  *   being interrupted by a signal (e.g. a SIGCHLD).
  */
-#undef bfgets
+#undef fgetc
 char *bfgets(char *s, int size, FILE *fd)
 {
    char *p = s;
@@ -463,9 +554,32 @@ char *bfgets(char *s, int size, FILE *fd)
       }
       *p++ = ch;
       *p = 0;
+      if (ch == '\r') { /* Support for Mac/Windows file format */
+        ch = fgetc(fd);
+         if (ch == '\n') { /* Windows (\r\n) */
+           *p++ = ch;
+           *p = 0;
+        }
+         else { /* Mac (\r only) */
+           ungetc(ch, fd); /* Push next character back to fd */
+        }
+        break;
+      }      
       if (ch == '\n') {
         break;
       }
    }
    return s;
 }
+
+/*
+ * Make a "unique" filename.  It is important that if
+ *   called again with the same "what" that the result
+ *   will be identical. This allows us to use the file
+ *   without saving its name, and re-generate the name
+ *   so that it can be deleted.
+ */
+void make_unique_filename(POOLMEM **name, int Id, char *what)
+{
+   Mmsg(name, "%s/%s.%s.%d.tmp", working_directory, my_name, what, Id);
+}