* 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
*/
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
}
/*
}
#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
/*
* 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)
{
#if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
int pidfd, len;
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));
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);
#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)
{
#if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
POOLMEM *fname = get_pool_memory(PM_FNAME);
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
*/
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;
}
}
*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;
}