2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * Miscellaneous Bacula memory and thread safe routines
18 * Generally, these are interfaces to system or standard
21 * Bacula utility functions are in util.c
27 #include "lib/bregex.h"
32 static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
38 POOLMEM *quote_string(POOLMEM *snew, const char *old)
49 for (i=0; old[i]; i++) {
70 * Quote a where (list of addresses separated by spaces)
72 POOLMEM *quote_where(POOLMEM *snew, const char *old)
83 for (i=0; old[i]; i++) {
109 * This routine is a somewhat safer unlink in that it
110 * allows you to run a regex on the filename before
111 * excepting it. It also requires the file to be in
112 * the working directory.
114 int safer_unlink(const char *pathname, const char *regx)
119 const int nmatch = 30;
120 regmatch_t pmatch[nmatch];
123 /* Name must start with working directory */
124 if (strncmp(pathname, working_directory, strlen(working_directory)) != 0) {
125 Pmsg1(000, "Safe_unlink excluded: %s\n", pathname);
129 /* Compile regex expression */
130 rc = regcomp(&preg1, regx, REG_EXTENDED);
132 regerror(rc, &preg1, prbuf, sizeof(prbuf));
133 Pmsg2(000, _("safe_unlink could not compile regex pattern \"%s\" ERR=%s\n"),
138 /* Unlink files that match regexes */
139 if (regexec(&preg1, pathname, nmatch, pmatch, 0) == 0) {
140 Dmsg1(100, "safe_unlink unlinking: %s\n", pathname);
141 rtn = unlink(pathname);
143 Pmsg2(000, "safe_unlink regex failed: regex=%s file=%s\n", regx, pathname);
151 * This routine will sleep (sec, microsec). Note, however, that if a
152 * signal occurs, it will return early. It is up to the caller
153 * to recall this routine if he/she REALLY wants to sleep the
156 int bmicrosleep(int32_t sec, int32_t usec)
158 struct timespec timeout;
163 timeout.tv_sec = sec;
164 timeout.tv_nsec = usec * 1000;
166 #ifdef HAVE_NANOSLEEP
167 stat = nanosleep(&timeout, NULL);
168 if (!(stat < 0 && errno == ENOSYS)) {
171 /* If we reach here it is because nanosleep is not supported by the OS */
174 /* Do it the old way */
175 gettimeofday(&tv, &tz);
176 timeout.tv_nsec += tv.tv_usec * 1000;
177 timeout.tv_sec += tv.tv_sec;
178 while (timeout.tv_nsec >= 1000000000) {
179 timeout.tv_nsec -= 1000000000;
183 Dmsg2(200, "pthread_cond_timedwait sec=%d usec=%d\n", sec, usec);
184 /* Note, this unlocks mutex during the sleep */
186 stat = pthread_cond_timedwait(&timer, &timer_mutex, &timeout);
189 Dmsg2(200, "pthread_cond_timedwait stat=%d ERR=%s\n", stat,
197 * Guarantee that the string is properly terminated */
198 char *bstrncpy(char *dest, const char *src, int maxlen)
200 strncpy(dest, src, maxlen-1);
206 * Guarantee that the string is properly terminated */
207 char *bstrncpy(char *dest, POOL_MEM &src, int maxlen)
209 strncpy(dest, src.c_str(), maxlen-1);
215 * Note: Here the maxlen is the maximum length permitted
216 * stored in dest, while on Unix systems, it is the maximum characters
217 * that may be copied from src.
219 char *bstrncat(char *dest, const char *src, int maxlen)
221 int len = strlen(dest);
222 if (len < maxlen-1) {
223 strncpy(dest+len, src, maxlen-len-1);
230 * Note: Here the maxlen is the maximum length permitted
231 * stored in dest, while on Unix systems, it is the maximum characters
232 * that may be copied from src.
234 char *bstrncat(char *dest, POOL_MEM &src, int maxlen)
236 int len = strlen(dest);
237 if (len < maxlen-1) {
238 strncpy(dest+len, src.c_str(), maxlen-len-1);
245 * Allows one or both pointers to be NULL
247 bool bstrcmp(const char *s1, const char *s2)
249 if (s1 == s2) return true;
250 if (s1 == NULL || s2 == NULL) return false;
251 return strcmp(s1, s2) == 0;
255 * Allows one or both pointers to be NULL
257 bool bstrcasecmp(const char *s1, const char *s2)
259 if (s1 == s2) return true;
260 if (s1 == NULL || s2 == NULL) return false;
261 return strcasecmp(s1, s2) == 0;
266 * Get character length of UTF-8 string
269 * U-00000000 - U-0000007F: 0xxxxxxx
270 * U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
271 * U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
272 * U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
273 * U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
274 * U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
276 int cstrlen(const char *str)
278 uint8_t *p = (uint8_t *)str;
284 if ((*p & 0xC0) != 0xC0) {
289 if ((*p & 0xD0) == 0xC0) {
294 if ((*p & 0xF0) == 0xD0) {
299 if ((*p & 0xF8) == 0xF0) {
304 if ((*p & 0xFC) == 0xF8) {
309 if ((*p & 0xFE) == 0xFC) {
314 p++; /* Shouln't get here but must advance */
322 void *bmalloc(size_t size)
327 buf = sm_malloc(file, line, size);
333 Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), be.bstrerror());
339 void *b_malloc(const char *file, int line, size_t size)
344 buf = sm_malloc(file, line, size);
350 e_msg(file, line, M_ABORT, 0, _("Out of memory: ERR=%s\n"), be.bstrerror());
356 void bfree(void *buf)
359 sm_free(__FILE__, __LINE__, buf);
365 void *brealloc (void *buf, size_t size)
368 buf = sm_realloc(__FILE__, __LINE__, buf, size);
370 buf = realloc(buf, size);
374 Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), be.bstrerror());
380 void *bcalloc(size_t size1, size_t size2)
384 buf = calloc(size1, size2);
387 Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), be.bstrerror());
392 /* Code now in src/lib/bsnprintf.c */
393 #ifndef USE_BSNPRINTF
399 int bsnprintf(char *str, int32_t size, const char *fmt, ...)
404 va_start(arg_ptr, fmt);
405 len = bvsnprintf(str, size, fmt, arg_ptr);
411 * Implement vsnprintf()
413 int bvsnprintf(char *str, int32_t size, const char *format, va_list ap)
415 #ifdef HAVE_VSNPRINTF
417 len = vsnprintf(str, size, format, ap);
425 buflen = size > BIG_BUF ? size : BIG_BUF;
426 buf = get_memory(buflen);
427 len = vsprintf(buf, format, ap);
429 Emsg0(M_ABORT, 0, _("Buffer overflow.\n"));
431 memcpy(str, buf, len);
432 str[len] = 0; /* len excludes the null */
437 #endif /* USE_BSNPRINTF */
439 #ifndef HAVE_LOCALTIME_R
441 struct tm *localtime_r(const time_t *timep, struct tm *tm)
443 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
447 ltm = localtime(timep);
449 memcpy(tm, ltm, sizeof(struct tm));
452 return ltm ? tm : NULL;
454 #endif /* HAVE_LOCALTIME_R */
456 #ifndef HAVE_READDIR_R
460 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
462 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
468 ndir = readdir(dirp);
471 memcpy(entry, ndir, sizeof(struct dirent));
472 strcpy(entry->d_name, ndir->d_name);
482 #endif /* HAVE_READDIR_R */
485 int b_strerror(int errnum, char *buf, size_t bufsiz)
487 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
493 msg = strerror(errnum);
495 msg = _("Bad errno");
498 bstrncpy(buf, msg, bufsiz);
504 /* These routines are not normally turned on */
506 void b_memset(const char *file, int line, void *mem, int val, size_t num)
508 /* Testing for 2000 byte zero at beginning of Volume block */
509 if (num > 1900 && num < 3000) {
510 Pmsg3(000, _("Memset for %d bytes at %s:%d\n"), (int)num, file, line);
512 memset(mem, val, num);
516 #if !defined(HAVE_WIN32)
517 static int del_pid_file_ok = FALSE;
521 * Create a standard "Unix" pid file.
523 void create_pid_file(char *dir, const char *progname, int port)
525 #if !defined(HAVE_WIN32)
529 POOLMEM *fname = get_pool_memory(PM_FNAME);
532 Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
533 if (stat(fname, &statp) == 0) {
534 /* File exists, see what we have */
536 if ((pidfd = open(fname, O_RDONLY|O_BINARY, 0)) < 0 ||
537 read(pidfd, &pidbuf, sizeof(pidbuf)) < 0 ||
538 sscanf(pidbuf, "%d", &oldpid) != 1) {
540 Emsg2(M_ERROR_TERM, 0, _("Cannot open pid file. %s ERR=%s\n"), fname,
543 /* Some OSes (IRIX) don't bother to clean out the old pid files after a crash, and
544 * since they use a deterministic algorithm for assigning PIDs, we can have
545 * pid conflicts with the old PID file after a reboot.
546 * The intent the following code is to check if the oldpid read from the pid
547 * file is the same as the currently executing process's pid,
548 * and if oldpid == getpid(), skip the attempt to
549 * kill(oldpid,0), since the attempt is guaranteed to succeed,
550 * but the success won't actually mean that there is an
551 * another Bacula process already running.
552 * For more details see bug #797.
554 if ((oldpid != (int)getpid()) && (kill(oldpid, 0) != -1 || errno != ESRCH)) {
555 Emsg3(M_ERROR_TERM, 0, _("%s is already running. pid=%d\nCheck file %s\n"),
556 progname, oldpid, fname);
558 /* He is not alive, so take over file ownership */
559 unlink(fname); /* remove stale pid file */
561 /* Create new pid file */
562 if ((pidfd = open(fname, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0640)) >= 0) {
563 len = sprintf(pidbuf, "%d\n", (int)getpid());
564 write(pidfd, pidbuf, len);
566 del_pid_file_ok = TRUE; /* we created it so we can delete it */
569 Emsg2(M_ERROR_TERM, 0, _("Could not open pid file. %s ERR=%s\n"), fname,
572 free_pool_memory(fname);
578 * Delete the pid file if we created it
580 int delete_pid_file(char *dir, const char *progname, int port)
582 #if !defined(HAVE_WIN32)
583 POOLMEM *fname = get_pool_memory(PM_FNAME);
585 if (!del_pid_file_ok) {
586 free_pool_memory(fname);
589 del_pid_file_ok = FALSE;
590 Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
592 free_pool_memory(fname);
600 uint64_t last_jobs_addr;
601 uint64_t reserved[20];
604 static struct s_state_hdr state_hdr = {
611 * Open and read the state file for the daemon
613 void read_state_file(char *dir, const char *progname, int port)
618 POOLMEM *fname = get_pool_memory(PM_FNAME);
619 struct s_state_hdr hdr;
620 int hdr_size = sizeof(hdr);
622 Mmsg(&fname, "%s/%s.%d.state", dir, progname, port);
623 /* If file exists, see what we have */
624 // Dmsg1(10, "O_BINARY=%d\n", O_BINARY);
625 if ((sfd = open(fname, O_RDONLY|O_BINARY)) < 0) {
627 Dmsg3(010, "Could not open state file. sfd=%d size=%d: ERR=%s\n",
628 sfd, (int)sizeof(hdr), be.bstrerror());
631 if ((stat=read(sfd, &hdr, hdr_size)) != hdr_size) {
633 Dmsg4(010, "Could not read state file. sfd=%d stat=%d size=%d: ERR=%s\n",
634 sfd, (int)stat, hdr_size, be.bstrerror());
637 if (hdr.version != state_hdr.version) {
638 Dmsg2(010, "Bad hdr version. Wanted %d got %d\n",
639 state_hdr.version, hdr.version);
643 if (strcmp(hdr.id, state_hdr.id) != 0) {
644 Dmsg0(000, "State file header id invalid.\n");
647 // Dmsg1(010, "Read header of %d bytes.\n", sizeof(hdr));
648 if (!read_last_jobs_list(sfd, hdr.last_jobs_addr)) {
659 free_pool_memory(fname);
663 * Write the state file
665 static pthread_mutex_t state_mutex = PTHREAD_MUTEX_INITIALIZER;
667 void write_state_file(char *dir, const char *progname, int port)
671 POOLMEM *fname = get_pool_memory(PM_FNAME);
673 P(state_mutex); /* Only one job at a time can call here */
674 Mmsg(&fname, "%s/%s.%d.state", dir, progname, port);
675 /* Create new state file */
677 if ((sfd = open(fname, O_CREAT|O_WRONLY|O_BINARY, 0640)) < 0) {
679 Dmsg2(000, "Could not create state file. %s ERR=%s\n", fname, be.bstrerror());
680 Emsg2(M_ERROR, 0, _("Could not create state file. %s ERR=%s\n"), fname, be.bstrerror());
683 if (write(sfd, &state_hdr, sizeof(state_hdr)) != sizeof(state_hdr)) {
685 Dmsg1(000, "Write hdr error: ERR=%s\n", be.bstrerror());
688 // Dmsg1(010, "Wrote header of %d bytes\n", sizeof(state_hdr));
689 state_hdr.last_jobs_addr = sizeof(state_hdr);
690 state_hdr.reserved[0] = write_last_jobs_list(sfd, state_hdr.last_jobs_addr);
691 // Dmsg1(010, "write last job end = %d\n", (int)state_hdr.reserved[0]);
692 if (lseek(sfd, 0, SEEK_SET) < 0) {
694 Dmsg1(000, "lseek error: ERR=%s\n", be.bstrerror());
697 if (write(sfd, &state_hdr, sizeof(state_hdr)) != sizeof(state_hdr)) {
699 Pmsg1(000, _("Write final hdr error: ERR=%s\n"), be.bstrerror());
703 // Dmsg1(010, "rewrote header = %d\n", sizeof(state_hdr));
712 free_pool_memory(fname);
716 /* BSDI does not have this. This is a *poor* simulation */
719 strtoll(const char *ptr, char **endptr, int base)
721 return (long long int)strtod(ptr, endptr);
726 * Bacula's implementation of fgets(). The difference is that it handles
727 * being interrupted by a signal (e.g. a SIGCHLD).
730 char *bfgets(char *s, int size, FILE *fd)
735 for (int i=0; i < size-1; i++) {
739 } while (ch == EOF && ferror(fd) && (errno == EINTR || errno == EAGAIN));
749 if (ch == '\r') { /* Support for Mac/Windows file format */
751 if (ch != '\n') { /* Mac (\r only) */
752 (void)ungetc(ch, fd); /* Push next character back to fd */
765 * Bacula's implementation of fgets(). The difference is that it handles
766 * being interrupted by a signal (e.g. a SIGCHLD) and it has a
767 * different calling sequence which implements input lines of
768 * up to a million characters.
770 char *bfgets(POOLMEM *&s, FILE *fd)
777 soft_max = sizeof_pool_memory(s) - 10;
782 } while (ch == EOF && ferror(fd) && (errno == EINTR || errno == EAGAIN));
792 if (soft_max > 1000000) {
795 s = check_pool_memory_size(s, soft_max+10000);
796 soft_max = sizeof_pool_memory(s) - 10;
800 if (ch == '\r') { /* Support for Mac/Windows file format */
802 if (ch != '\n') { /* Mac (\r only) */
803 (void)ungetc(ch, fd); /* Push next character back to fd */
816 * Make a "unique" filename. It is important that if
817 * called again with the same "what" that the result
818 * will be identical. This allows us to use the file
819 * without saving its name, and re-generate the name
820 * so that it can be deleted.
822 void make_unique_filename(POOLMEM **name, int Id, char *what)
824 Mmsg(name, "%s/%s.%s.%d.tmp", working_directory, my_name, what, Id);
827 char *escape_filename(const char *file_path)
829 if (file_path == NULL || strpbrk(file_path, "\"\\") == NULL) {
833 char *escaped_path = (char *)bmalloc(2 * (strlen(file_path) + 1));
834 char *cur_char = escaped_path;
837 if (*file_path == '\\' || *file_path == '"') {
841 *cur_char++ = *file_path++;
850 * For the moment preventing suspensions is only
851 * implemented on Windows.
854 void prevent_os_suspensions()
857 void allow_os_suspensions()
862 #if HAVE_BACKTRACE && HAVE_GCC
864 #include <execinfo.h>
867 const size_t max_depth = 100;
869 void *stack_addrs[max_depth];
870 char **stack_strings;
872 stack_depth = backtrace(stack_addrs, max_depth);
873 stack_strings = backtrace_symbols(stack_addrs, stack_depth);
875 for (size_t i = 3; i < stack_depth; i++) {
876 size_t sz = 200; /* just a guess, template names will go much wider */
877 char *function = (char *)actuallymalloc(sz);
878 char *begin = 0, *end = 0;
879 /* find the parentheses and address offset surrounding the mangled name */
880 for (char *j = stack_strings[i]; *j; ++j) {
883 } else if (*j == '+') {
890 /* found our mangled name, now in [begin, end] */
893 char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
895 /* return value may be a realloc() of the input */
898 /* demangling failed, just pretend it's a C function with no args */
899 strncpy(function, begin, sz);
900 strncat(function, "()", sz);
901 function[sz-1] = '\0';
903 Pmsg2(000, " %s:%s\n", stack_strings[i], function);
906 /* didn't find the mangled name, just print the whole line */
907 Pmsg1(000, " %s\n", stack_strings[i]);
909 actuallyfree(function);
911 actuallyfree(stack_strings); /* malloc()ed by backtrace_symbols */
913 #else /* HAVE_BACKTRACE && HAVE_GCC */
914 void stack_trace() {}
915 #endif /* HAVE_BACKTRACE && HAVE_GCC */