diff --git a/ChangeLog b/ChangeLog index b021522163..761f806295 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Fri Feb 23 12:46:44 IST 2007 Mark McLoughlin + + * qemud/qemud.c: add --pid-file and default to writing + out a PID file to /var/run/libvirt_qemud.pid in daemon + mode. + + * configure.in: add --with-pid-file arg + Fri Feb 23 12:45:08 IST 2007 Mark McLoughlin * qemud/Makefile.am: install libvirt_qemud in /usr/sbin diff --git a/configure.in b/configure.in index 20f9b914ad..618bbe73c3 100644 --- a/configure.in +++ b/configure.in @@ -86,6 +86,21 @@ if test x"$enable_debug" = x"yes"; then AC_DEFINE(ENABLE_DEBUG, [], [whether debugging is enabled]) fi +dnl +dnl PID file +dnl +AC_MSG_CHECKING([where to write libvirt_qemud PID file]) +AC_ARG_WITH(pid-file, AC_HELP_STRING([--with-qemud-pid-file=[pidfile|none]], [PID file for libvirt_qemud])) +if test "x$with_qemud_pid_file" == "x" ; then + QEMUD_PID_FILE="$localstatedir/run/libvirt_qemud.pid" +elif test "x$with_qemud_pid_file" == "xnone" ; then + QEMUD_PID_FILE="" +else + QEMUD_PID_FILE="$with_qemud_pid_file" +fi +AC_DEFINE_UNQUOTED(QEMUD_PID_FILE, "$QEMUD_PID_FILE", [PID file path for qemud]) +AC_MSG_RESULT($QEMUD_PID_FILE) + dnl dnl allow the creation of iptables rules in chains with a dnl specific prefix rather than in the standard toplevel chains diff --git a/qemud/qemud.c b/qemud/qemud.c index a66a0dd6c6..bcfc613b1d 100644 --- a/qemud/qemud.c +++ b/qemud/qemud.c @@ -301,6 +301,42 @@ static int qemudGoDaemon(void) { } } +static int qemudWritePidFile(const char *pidFile) { + int fd; + FILE *fh; + + if (pidFile[0] == '\0') + return 0; + + if ((fd = open(pidFile, O_WRONLY|O_CREAT|O_EXCL, 0644)) < 0) { + qemudLog(QEMUD_ERR, "Failed to open pid file '%s' : %s", + pidFile, strerror(errno)); + return -1; + } + + if (!(fh = fdopen(fd, "w"))) { + qemudLog(QEMUD_ERR, "Failed to fdopen pid file '%s' : %s", + pidFile, strerror(errno)); + close(fd); + return -1; + } + + if (fprintf(fh, "%lu\n", (unsigned long)getpid()) < 0) { + qemudLog(QEMUD_ERR, "Failed to write to pid file '%s' : %s", + pidFile, strerror(errno)); + close(fd); + return -1; + } + + if (fclose(fh) == EOF) { + qemudLog(QEMUD_ERR, "Failed to close pid file '%s' : %s", + pidFile, strerror(errno)); + return -1; + } + + return 0; +} + static int qemudListenUnix(struct qemud_server *server, const char *path, int readonly) { struct qemud_socket *sock = calloc(1, sizeof(struct qemud_socket)); @@ -1483,12 +1519,15 @@ int main(int argc, char **argv) { struct qemud_server *server; struct sigaction sig_action; int sigpipe[2]; + char *pid_file = NULL; + int ret = 1; struct option opts[] = { { "verbose", no_argument, &verbose, 1}, { "daemon", no_argument, &godaemon, 1}, { "system", no_argument, &sys, 1}, { "timeout", required_argument, 0, 't'}, + { "pid-file", required_argument, 0, 'p'}, {0, 0, 0, 0} }; @@ -1497,7 +1536,7 @@ int main(int argc, char **argv) { int c; char *tmp; - c = getopt_long(argc, argv, "vsdt:", opts, &optidx); + c = getopt_long(argc, argv, "vsdt:p:", opts, &optidx); if (c == -1) { break; @@ -1524,6 +1563,11 @@ int main(int argc, char **argv) { if (timeout <= 0) timeout = -1; break; + + case 'p': + pid_file = strdup(optarg); + break; + case '?': return 2; break; @@ -1541,7 +1585,7 @@ int main(int argc, char **argv) { qemudSetNonBlock(sigpipe[1]) < 0) { qemudLog(QEMUD_ERR, "Failed to create pipe: %s", strerror(errno)); - return 1; + goto error1; } sigwrite = sigpipe[1]; @@ -1564,14 +1608,19 @@ int main(int argc, char **argv) { if (pid < 0) { qemudLog(QEMUD_ERR, "Failed to fork as daemon: %s", strerror(errno)); - return 1; + goto error1; } if (pid > 0) - return 0; + goto out; + + if (qemudWritePidFile(pid_file ? pid_file : QEMUD_PID_FILE) < 0) + goto error1; } - if (!(server = qemudInitialize(sys, sigpipe[0]))) - return 2; + if (!(server = qemudInitialize(sys, sigpipe[0]))) { + ret = 2; + goto error2; + } qemudRunLoop(server, timeout); @@ -1582,7 +1631,18 @@ int main(int argc, char **argv) { if (godaemon) closelog(); - return 0; + out: + ret = 0; + + error2: + if (godaemon) + unlink(pid_file ? pid_file : QEMUD_PID_FILE); + + error1: + if (pid_file) + free(pid_file); + + return ret; } /*