]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bsys.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / lib / bsys.c
index 1c0f40d7f4fd2ad6d3e6a66df3eb1fe574648e7e..ba14452c0e0ae7c2fbe4bd5cf1e176470dd014a2 100644 (file)
@@ -1,14 +1,14 @@
 /*
  * Miscellaneous Bacula memory and thread safe routines
  *   Generally, these are interfaces to system or standard
- *   library routines. 
- * 
+ *   library routines.
+ *
  *  Bacula utility functions are in util.c
  *
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-2004 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
 #include <grp.h>
 #endif
 
+static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
+
+/*
+ * 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 usec)
+{
+   struct timespec timeout;
+   struct timeval tv;
+   struct timezone tz;
+   int stat;
+
+   timeout.tv_sec = sec;
+   timeout.tv_nsec = usec * 1000;
+
+#ifdef HAVE_NANOSLEEP
+   stat = nanosleep(&timeout, NULL);
+   if (!(stat < 0 && errno == ENOSYS)) {
+      return stat;
+   }
+   /* If we reach here it is because nanosleep is not supported by the OS */
+#endif
+
+   /* Do it the old way */
+   gettimeofday(&tv, &tz);
+   timeout.tv_nsec += tv.tv_usec * 1000;
+   timeout.tv_sec += tv.tv_sec;
+   while (timeout.tv_nsec >= 1000000000) {
+      timeout.tv_nsec -= 1000000000;
+      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);
+   if (stat != 0) {
+      berrno be;
+      Dmsg2(200, "pthread_cond_timedwait stat=%d ERR=%s\n", stat,
+        be.strerror(stat));
+   }
+   V(timer_mutex);
+   return stat;
+}
+
 /*
  * Guarantee that the string is properly terminated */
 char *bstrncpy(char *dest, const char *src, int maxlen)
@@ -45,6 +94,16 @@ char *bstrncpy(char *dest, const char *src, int maxlen)
    return dest;
 }
 
+/*
+ * Guarantee that the string is properly terminated */
+char *bstrncpy(char *dest, POOL_MEM &src, int maxlen)
+{
+   strncpy(dest, src.c_str(), maxlen-1);
+   dest[maxlen-1] = 0;
+   return dest;
+}
+
+
 char *bstrncat(char *dest, const char *src, int maxlen)
 {
    strncat(dest, src, maxlen-1);
@@ -52,6 +111,14 @@ char *bstrncat(char *dest, const char *src, int maxlen)
    return dest;
 }
 
+char *bstrncat(char *dest, POOL_MEM &src, int maxlen)
+{
+   strncat(dest, src.c_str(), maxlen-1);
+   dest[maxlen-1] = 0;
+   return dest;
+}
+
+
 
 #ifndef DEBUG
 void *bmalloc(size_t size)
@@ -108,36 +175,15 @@ void *bcalloc (size_t size1, size_t size2)
 /*
  * Implement snprintf
  */
-int bsnprintf(char *str, int32_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);
-   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);
+   len = bvsnprintf(str, size, 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
 }
 
 /*
@@ -153,15 +199,16 @@ int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap)
 
 #else
 
-   int len;
+   int len, buflen;
    char *buf;
-   buf = get_memory(BIG_BUF);
+   buflen = size > BIG_BUF ? size : BIG_BUF;
+   buf = get_memory(buflen);
    len = vsprintf(buf, format, ap);
-   if (len >= BIG_BUF) {
+   if (len >= buflen) {
       Emsg0(M_ABORT, 0, _("Buffer overflow.\n"));
    }
-   memcpy(str, buf, size);
-   str[size-1] = 0;
+   memcpy(str, buf, len);
+   str[len] = 0;               /* len excludes the null */
    free_memory(buf);
    return len;
 #endif
@@ -172,14 +219,16 @@ int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap)
 struct tm *localtime_r(const time_t *timep, struct tm *tm)
 {
     static pthread_mutex_t mutex;
-    static int first = 1;
-    struct tm *ltm;
+    static bool first = true;
+    struct tm *ltm,
 
     if (first) {
        pthread_mutex_init(&mutex, NULL);
-       first = 0;
+       first = false;
     }
+
     P(mutex);
+
     ltm = localtime(timep);
     if (ltm) {
        memcpy(tm, ltm, sizeof(struct tm));
@@ -222,13 +271,13 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
 #endif
 #endif /* HAVE_READDIR_R */
 
-#ifdef xxxxxxxxxx_STRERROR_R
-int strerror_r(int errnum, char *buf, size_t bufsiz)
+
+int bstrerror(int errnum, char *buf, size_t bufsiz)
 {
     static pthread_mutex_t mutex;
     static int first = 1;
     int stat = 0;
-    char *msg;
+    const char *msg;
 
     if (first) {
        pthread_mutex_init(&mutex, NULL);
@@ -245,7 +294,6 @@ int strerror_r(int errnum, char *buf, size_t bufsiz)
     V(mutex);
     return stat;
 }
-#endif /* HAVE_STRERROR_R */
 
 /*
  * These are mutex routines that do error checking
@@ -264,7 +312,7 @@ void _p(char *file, int line, pthread_mutex_t *m)
       } else {
          e_msg(file, line, M_ERROR, 0, _("Possible mutex deadlock resolved.\n"));
       }
-        
+
    }
 }
 
@@ -283,6 +331,19 @@ void _v(char *file, int line, pthread_mutex_t *m)
 }
 #endif /* DEBUG_MUTEX */
 
+#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
@@ -300,10 +361,10 @@ void create_pid_file(char *dir, const char *progname, int port)
    struct stat statp;
 
    Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
-   if (stat(mp_chr(fname), &statp) == 0) {
+   if (stat(fname, &statp) == 0) {
       /* File exists, see what we have */
       *pidbuf = 0;
-      if ((pidfd = open(mp_chr(fname), O_RDONLY|O_BINARY, 0)) < 0 || 
+      if ((pidfd = open(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));
@@ -314,10 +375,10 @@ void create_pid_file(char *dir, const char *progname, int port)
               progname, oldpid, fname);
       }
       /* He is not alive, so take over file ownership */
-      unlink(mp_chr(fname));                 /* remove stale pid file */
+      unlink(fname);                 /* remove stale pid file */
    }
    /* Create new pid file */
-   if ((pidfd = open(mp_chr(fname), O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0640)) >= 0) {
+   if ((pidfd = open(fname, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0640)) >= 0) {
       len = sprintf(pidbuf, "%d\n", (int)getpid());
       write(pidfd, pidbuf, len);
       close(pidfd);
@@ -344,7 +405,7 @@ int delete_pid_file(char *dir, const char *progname, int port)
    }
    del_pid_file_ok = FALSE;
    Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
-   unlink(mp_chr(fname));
+   unlink(fname);
    free_pool_memory(fname);
 #endif
    return 1;
@@ -357,7 +418,7 @@ struct s_state_hdr {
    uint64_t reserved[20];
 };
 
-static struct s_state_hdr state_hdr = { 
+static struct s_state_hdr state_hdr = {
    "Bacula State\n",
    3,
    0
@@ -377,18 +438,18 @@ void read_state_file(char *dir, const char *progname, int port)
    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", 
+   if ((sfd = open(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", 
+      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", 
+      Dmsg2(010, "Bad hdr version. Wanted %d got %d\n",
         state_hdr.version, hdr.version);
    }
    hdr.id[13] = 0;
@@ -415,7 +476,7 @@ void write_state_file(char *dir, const char *progname, int port)
 
    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) {
+   if ((sfd = open(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;
@@ -426,14 +487,14 @@ void write_state_file(char *dir, const char *progname, int port)
    }
 // 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);   
+   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)) {
-      Dmsg1(000, "Write final hdr error: ERR=%s\n", strerror(errno));
+      Pmsg1(000, "Write final hdr error: ERR=%s\n", strerror(errno));
    }
 // Dmsg1(010, "rewrote header = %d\n", sizeof(state_hdr));
 bail_out:
@@ -478,60 +539,16 @@ void drop(char *uid, char *gid)
       }
    }
 #endif
-         
-}
 
-static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
-
-/*
- * 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 usec)
-{
-   struct timespec timeout;
-   struct timeval tv;
-   struct timezone tz;
-   int stat;
-
-   timeout.tv_sec = sec;
-   timeout.tv_nsec = usec * 1000;
-
-#ifdef HAVE_NANOSLEEP
-   stat = nanosleep(&timeout, NULL);
-   if (!(stat < 0 && errno == ENOSYS)) {
-      return stat;                  
-   }
-   /* If we reach here it is because nanosleep is not supported by the OS */
-#endif
-
-   /* Do it the old way */
-   gettimeofday(&tv, &tz);
-   timeout.tv_nsec += tv.tv_usec * 1000;
-   timeout.tv_sec += tv.tv_sec;
-   while (timeout.tv_nsec >= 1000000000) {
-      timeout.tv_nsec -= 1000000000;
-      timeout.tv_sec++;
-   }
-
-   Dmsg1(200, "pthread_cond_timedwait sec=%d\n", timeout.tv_sec);
-   /* 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);
-   V(timer_mutex);
-   return stat;
 }
 
+
 /* BSDI does not have this.  This is a *poor* simulation */
 #ifndef HAVE_STRTOLL
 long long int
 strtoll(const char *ptr, char **endptr, int base)
 {
-   return (long long int)strtod(ptr, endptr);  
+   return (long long int)strtod(ptr, endptr);
 }
 #endif
 
@@ -543,7 +560,7 @@ strtoll(const char *ptr, char **endptr, int base)
 char *bfgets(char *s, int size, FILE *fd)
 {
    char *p = s;
-   int ch;      
+   int ch;
    *p = 0;
    for (int i=0; i < size-1; i++) {
       do {
@@ -559,6 +576,17 @@ 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;
       }