2 * Miscellaneous Bacula memory and thread safe routines
3 * Generally, these are interfaces to system or standard
6 * Bacula utility functions are in util.c
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40 * Guarantee that the string is properly terminated */
41 char *bstrncpy(char *dest, const char *src, int maxlen)
43 strncpy(dest, src, maxlen-1);
48 char *bstrncat(char *dest, const char *src, int maxlen)
50 strncat(dest, src, maxlen-1);
57 void *bmalloc(size_t size)
63 Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), strerror(errno));
69 void *b_malloc(char *file, int line, size_t size)
74 buf = sm_malloc(file, line, size);
79 e_msg(file, line, M_ABORT, 0, _("Out of memory: ERR=%s\n"), strerror(errno));
85 void *brealloc (void *buf, size_t size)
87 buf = realloc(buf, size);
89 Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), strerror(errno));
95 void *bcalloc (size_t size1, size_t size2)
99 buf = calloc(size1, size2);
101 Emsg1(M_ABORT, 0, _("Out of memory: ERR=%s\n"), strerror(errno));
111 int bsnprintf(char *str, int32_t size, const char *fmt, ...)
113 #ifdef HAVE_VSNPRINTF
117 va_start(arg_ptr, fmt);
118 len = vsnprintf(str, size, fmt, arg_ptr);
129 buf = get_memory(BIG_BUF);
130 va_start(arg_ptr, fmt);
131 len = vsprintf(buf, fmt, arg_ptr);
133 if (len >= BIG_BUF) {
134 Emsg0(M_ABORT, 0, _("Buffer overflow.\n"));
136 memcpy(str, buf, size);
144 * Implement vsnprintf()
146 int bvsnprintf(char *str, int32_t size, const char *format, va_list ap)
148 #ifdef HAVE_VSNPRINTF
150 len = vsnprintf(str, size, format, ap);
158 buf = get_memory(BIG_BUF);
159 len = vsprintf(buf, format, ap);
160 if (len >= BIG_BUF) {
161 Emsg0(M_ABORT, 0, _("Buffer overflow.\n"));
163 memcpy(str, buf, size);
170 #ifndef HAVE_LOCALTIME_R
172 struct tm *localtime_r(const time_t *timep, struct tm *tm)
174 static pthread_mutex_t mutex;
175 static int first = 1;
179 pthread_mutex_init(&mutex, NULL);
183 ltm = localtime(timep);
185 memcpy(tm, ltm, sizeof(struct tm));
188 return ltm ? tm : NULL;
190 #endif /* HAVE_LOCALTIME_R */
192 #ifndef HAVE_READDIR_R
196 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
198 static pthread_mutex_t mutex;
199 static int first = 1;
204 pthread_mutex_init(&mutex, NULL);
209 ndir = readdir(dirp);
212 memcpy(entry, ndir, sizeof(struct dirent));
213 strcpy(entry->d_name, ndir->d_name);
223 #endif /* HAVE_READDIR_R */
225 #ifdef xxxxxxxxxx_STRERROR_R
226 int strerror_r(int errnum, char *buf, size_t bufsiz)
228 static pthread_mutex_t mutex;
229 static int first = 1;
234 pthread_mutex_init(&mutex, NULL);
239 msg = strerror(errnum);
241 msg = _("Bad errno");
244 bstrncpy(buf, msg, bufsiz);
248 #endif /* HAVE_STRERROR_R */
251 * These are mutex routines that do error checking
252 * for deadlock and such. Normally not turned on.
255 void _p(char *file, int line, pthread_mutex_t *m)
258 if ((errstat = pthread_mutex_trylock(m))) {
259 e_msg(file, line, M_ERROR, 0, _("Possible mutex deadlock.\n"));
260 /* We didn't get the lock, so do it definitely now */
261 if ((errstat=pthread_mutex_lock(m))) {
262 e_msg(file, line, M_ABORT, 0, _("Mutex lock failure. ERR=%s\n"),
265 e_msg(file, line, M_ERROR, 0, _("Possible mutex deadlock resolved.\n"));
271 void _v(char *file, int line, pthread_mutex_t *m)
275 if ((errstat=pthread_mutex_trylock(m)) == 0) {
276 e_msg(file, line, M_ERROR, 0, _("Mutex unlock not locked. ERR=%s\n"),
279 if ((errstat=pthread_mutex_unlock(m))) {
280 e_msg(file, line, M_ABORT, 0, _("Mutex unlock failure. ERR=%s\n"),
284 #endif /* DEBUG_MUTEX */
286 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
287 static int del_pid_file_ok = FALSE;
291 * Create a standard "Unix" pid file.
293 void create_pid_file(char *dir, char *progname, int port)
295 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
299 POOLMEM *fname = get_pool_memory(PM_FNAME);
302 Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
303 if (stat(mp_chr(fname), &statp) == 0) {
304 /* File exists, see what we have */
306 if ((pidfd = open(mp_chr(fname), O_RDONLY)) < 0 ||
307 read(pidfd, &pidbuf, sizeof(pidbuf)) < 0 ||
308 sscanf(pidbuf, "%d", &oldpid) != 1) {
309 Emsg2(M_ERROR_TERM, 0, _("Cannot open pid file. %s ERR=%s\n"), fname, strerror(errno));
311 /* See if other Bacula is still alive */
312 if (kill(oldpid, 0) != -1 || errno != ESRCH) {
313 Emsg3(M_ERROR_TERM, 0, _("%s is already running. pid=%d\nCheck file %s\n"),
314 progname, oldpid, fname);
316 /* He is not alive, so take over file ownership */
317 unlink(mp_chr(fname)); /* remove stale pid file */
319 /* Create new pid file */
320 if ((pidfd = open(mp_chr(fname), O_CREAT | O_TRUNC | O_WRONLY, 0644)) >= 0) {
321 len = sprintf(pidbuf, "%d\n", (int)getpid());
322 write(pidfd, pidbuf, len);
324 del_pid_file_ok = TRUE; /* we created it so we can delete it */
326 Emsg2(M_ERROR_TERM, 0, _("Could not open pid file. %s ERR=%s\n"), fname, strerror(errno));
328 free_pool_memory(fname);
333 * Delete the pid file if we created it
335 int delete_pid_file(char *dir, char *progname, int port)
337 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
338 POOLMEM *fname = get_pool_memory(PM_FNAME);
340 if (!del_pid_file_ok) {
341 free_pool_memory(fname);
344 del_pid_file_ok = FALSE;
345 Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
346 unlink(mp_chr(fname));
347 free_pool_memory(fname);
353 * Drop to privilege new userid and new gid if non-NULL
355 void drop(char *uid, char *gid)
362 if ((group = getgrnam(gid)) == NULL) {
363 Emsg1(M_ERROR_TERM, 0, _("Could not find specified group: %s\n"), gid);
365 if (setgid(group->gr_gid)) {
366 Emsg1(M_ERROR_TERM, 0, _("Could not set specified group: %s\n"), gid);
368 gr_list[0] = group->gr_gid;
369 if (setgroups(1, gr_list)) {
370 Emsg1(M_ERROR_TERM, 0, _("Could not set specified group: %s\n"), gid);
377 struct passwd *passw;
378 if ((passw = getpwnam(uid)) == NULL) {
379 Emsg1(M_ERROR_TERM, 0, _("Could not find specified userid: %s\n"), uid);
381 if (setuid(passw->pw_uid)) {
382 Emsg1(M_ERROR_TERM, 0, _("Could not set specified userid: %s\n"), uid);
389 static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
390 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
393 * This routine will sleep (sec, microsec). Note, however, that if a
394 * signal occurs, it will return early. It is up to the caller
395 * to recall this routine if he/she REALLY wants to sleep the
398 int bmicrosleep(time_t sec, long usec)
400 struct timespec timeout;
405 timeout.tv_sec = sec;
406 timeout.tv_nsec = usec * 1000;
408 #ifdef HAVE_NANOSLEEP
409 stat = nanosleep(&timeout, NULL);
410 if (!(stat < 0 && errno == ENOSYS)) {
413 /* If we reach here it is because nanosleep is not supported by the OS */
416 /* Do it the old way */
417 gettimeofday(&tv, &tz);
418 timeout.tv_nsec += tv.tv_usec * 1000;
419 timeout.tv_sec += tv.tv_sec;
420 while (timeout.tv_nsec >= 1000000000) {
421 timeout.tv_nsec -= 1000000000;
425 Dmsg1(200, "pthread_cond_timedwait sec=%d\n", timeout.tv_sec);
426 /* Note, this unlocks mutex during the sleep */
428 stat = pthread_cond_timedwait(&timer, &timer_mutex, &timeout);
429 Dmsg1(200, "pthread_cond_timedwait stat=%d\n", stat);
434 /* BSDI does not have this. This is a *poor* simulation */
437 strtoll(const char *ptr, char **endptr, int base)
439 return (long long int)strtod(ptr, endptr);
444 * Bacula's implementation of fgets(). The difference is that it handles
445 * being interrupted by a signal (e.g. a SIGCHLD).
448 char *bfgets(char *s, int size, FILE *fd)
453 for (int i=0; i < size-1; i++) {
457 } while (ch == -1 && (errno == EINTR || errno == EAGAIN));
475 * Make a "unique" filename. It is important that if
476 * called again with the same "what" that the result
477 * will be identical. This allows us to use the file
478 * without saving its name, and re-generate the name
479 * so that it can be deleted.
481 void make_unique_filename(POOLMEM **name, int Id, char *what)
483 Mmsg(name, "%s/%s.%s.%d.tmp", working_directory, my_name, what, Id);