[patch 9/9] Implement better error reporting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a qemudLog() function which uses syslog() if we're in
daemon mode, doesn't output INFO/DEBUG messages unless
the verbose flag is set and doesn't output DEBUG messages
unless compiled with --enable-debug.

Also, make a first pass through fatal errors and add
error messages for them.

Signed-off-by: Mark McLoughlin <markmc@xxxxxxxxxx>

Index: libvirt/qemud/qemud.c
===================================================================
--- libvirt.orig/qemud/qemud.c
+++ libvirt/qemud/qemud.c
@@ -38,6 +38,8 @@
 #include <stdlib.h>
 #include <pwd.h>
 #include <stdio.h>
+#include <stdarg.h>
+#include <syslog.h>
 #include <string.h>
 #include <errno.h>
 #include <getopt.h>
@@ -52,6 +54,8 @@
 #include "conf.h"
 #include "iptables.h"
 
+static int godaemon = 0;
+static int verbose = 0;
 static int sigwrite = -1;
 
 static void sig_handler(int sig) {
@@ -73,8 +77,14 @@ static int qemudDispatchSignal(struct qe
     struct qemud_network *network;
     int ret;
 
-    if (read(server->sigread, &sigc, 1) != 1)
+    if (read(server->sigread, &sigc, 1) != 1) {
+        qemudLog(QEMUD_ERR, "Failed to read from signal pipe: %s",
+                 strerror(errno));
         return -1;
+    }
+
+    qemudLog(QEMUD_INFO, "Received signal %d; shuting down guests and "
+             "networks and purging config", sigc);
 
     /* shutdown active VMs */
     vm = server->activevms;
@@ -116,11 +126,13 @@ static int qemudDispatchSignal(struct qe
 
     switch (sigc) {
     case SIGHUP:
+        qemudLog(QEMUD_INFO, "Reloading configuration");
         ret = qemudScanConfigs(server);
         break;
 
     case SIGINT:
     case SIGTERM:
+        qemudLog(QEMUD_WARN, "Shutting down on signal %d", sigc);
         server->shutdown = 1;
         break;
 
@@ -133,27 +145,92 @@ static int qemudDispatchSignal(struct qe
 
 static int qemudSetCloseExec(int fd) {
     int flags;
-    if ((flags = fcntl(fd, F_GETFD)) < 0) {
-        return -1;
-    }
+    if ((flags = fcntl(fd, F_GETFD)) < 0)
+        goto error;
     flags |= FD_CLOEXEC;
-    if ((fcntl(fd, F_SETFD, flags)) < 0) {
-        return -1;
-    }
+    if ((fcntl(fd, F_SETFD, flags)) < 0)
+        goto error;
     return 0;
+ error:
+    qemudLog(QEMUD_ERR, "Failed to set close-on-exec file descriptor flag");
+    return -1;
 }
 
 
 static int qemudSetNonBlock(int fd) {
     int flags;
-    if ((flags = fcntl(fd, F_GETFL)) < 0) {
-        return -1;
-    }
+    if ((flags = fcntl(fd, F_GETFL)) < 0)
+        goto error;
     flags |= O_NONBLOCK;
-    if ((fcntl(fd, F_SETFL, flags)) < 0) {
-        return -1;
-    }
+    if ((fcntl(fd, F_SETFL, flags)) < 0)
+        goto error;
     return 0;
+ error:
+    qemudLog(QEMUD_ERR, "Failed to set non-blocking file descriptor flag");
+    return -1;
+}
+
+void qemudLog(int priority, const char *fmt, ...)
+{
+    va_list args;
+
+    va_start(args, fmt);
+
+    if (godaemon) {
+        int sysprio = -1;
+
+        switch(priority) {
+        case QEMUD_ERR:
+            sysprio = LOG_ERR;
+            break;
+        case QEMUD_WARN:
+            sysprio = LOG_WARNING;
+            break;
+        case QEMUD_INFO:
+            if (verbose)
+                sysprio = LOG_INFO;
+            break;
+#ifdef ENABLE_DEBUG
+        case QEMUD_DEBUG:
+            if (verbose)
+                sysprio = LOG_DEBUG;
+            break;
+#endif
+        default:
+            break;
+        }
+
+        if (sysprio != -1)
+            vsyslog(sysprio, fmt, args);
+    } else {
+        switch(priority) {
+        case QEMUD_ERR:
+        case QEMUD_WARN:
+            vfprintf(stderr, fmt, args);
+            fputc('\n', stderr);
+            break;
+
+        case QEMUD_INFO:
+            if (verbose) {
+                vprintf(fmt, args);
+                fputc('\n', stdout);
+            }
+            break;
+
+#ifdef ENABLE_DEBUG
+        case QEMUD_DEBUG:
+            if (verbose) {
+                vprintf(fmt, args);
+                fputc('\n', stdout);
+            }
+            break;
+#endif
+        default:
+            break;
+        }
+    }
+
+    va_end(args);
 }
 
 static int qemudGoDaemon(void) {
@@ -229,20 +306,24 @@ static int qemudListenUnix(struct qemud_
     struct sockaddr_un addr;
     mode_t oldmask;
 
-    if (!sock)
+    if (!sock) {
+        qemudLog(QEMUD_ERR, "Failed to allocate memory for struct qemud_socket");
         return -1;
+    }
 
     sock->readonly = readonly;
     sock->next = server->sockets;
     server->sockets = sock;
     server->nsockets++;
 
-    if ((sock->fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
+    if ((sock->fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
+        qemudLog(QEMUD_ERR, "Failed to create socket: %s",
+                 strerror(errno));
         return -1;
+    }
 
-    if (qemudSetCloseExec(sock->fd) < 0)
-        return -1;
-    if (qemudSetNonBlock(sock->fd) < 0)
+    if (qemudSetCloseExec(sock->fd) < 0 ||
+        qemudSetNonBlock(sock->fd) < 0)
         return -1;
 
     memset(&addr, 0, sizeof(addr));
@@ -256,12 +337,18 @@ static int qemudListenUnix(struct qemud_
         oldmask = umask(~(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
     else
         oldmask = umask(~(S_IRUSR | S_IWUSR));
-    if (bind(sock->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
+    if (bind(sock->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+        qemudLog(QEMUD_ERR, "Failed to bind socket to '%s': %s",
+                 path, strerror(errno));
         return -1;
+    }
     umask(oldmask);
 
-    if (listen(sock->fd, 30) < 0)
+    if (listen(sock->fd, 30) < 0) {
+        qemudLog(QEMUD_ERR, "Failed to listen for connections on '%s': %s",
+                 path, strerror(errno));
         return -1;
+    }
 
     return 0;
 }
@@ -270,17 +357,16 @@ static int qemudListen(struct qemud_serv
     char sockname[PATH_MAX];
 
     if (sys) {
-        if (snprintf(sockname, sizeof(sockname), "%s/run/libvirt/qemud-sock", LOCAL_STATE_DIR) >= (int)sizeof(sockname)) {
-            return -1;
-        }
+        if (snprintf(sockname, sizeof(sockname), "%s/run/libvirt/qemud-sock", LOCAL_STATE_DIR) >= (int)sizeof(sockname))
+            goto snprintf_error;
+
         unlink(sockname);
         if (qemudListenUnix(server, sockname, 0) < 0)
             return -1;
 
+        if (snprintf(sockname, sizeof(sockname), "%s/run/libvirt/qemud-sock-ro", LOCAL_STATE_DIR) >= (int)sizeof(sockname))
+            goto snprintf_error;
 
-        if (snprintf(sockname, sizeof(sockname), "%s/run/libvirt/qemud-sock-ro", LOCAL_STATE_DIR) >= (int)sizeof(sockname)) {
-            return -1;
-        }
         unlink(sockname);
         if (qemudListenUnix(server, sockname, 1) < 0)
             return -1;
@@ -289,29 +375,38 @@ static int qemudListen(struct qemud_serv
         int uid;
 
         if ((uid = geteuid()) < 0) {
+            qemudLog(QEMUD_ERR, "You must run the daemon as root to use system mode");
             return -1;
         }
 
-        if (!(pw = getpwuid(uid)))
-            return -1;
-
-        if (snprintf(sockname, sizeof(sockname), "@%s/.libvirt/qemud-sock", pw->pw_dir) >= (int)sizeof(sockname)) {
+        if (!(pw = getpwuid(uid))) {
+            qemudLog(QEMUD_ERR, "Failed to find user record for uid '%s': %s",
+                     uid, strerror(errno));
             return -1;
         }
 
+        if (snprintf(sockname, sizeof(sockname), "@%s/.libvirt/qemud-sock", pw->pw_dir) >= (int)sizeof(sockname))
+            goto snprintf_error;
+
         if (qemudListenUnix(server, sockname, 0) < 0)
             return -1;
     }
 
     return 0;
+
+ snprintf_error:
+    qemudLog(QEMUD_ERR, "Resulting path to long for buffer in qemudListen()");
+    return -1;
 }
 
 static struct qemud_server *qemudInitialize(int sys, int sigread) {
     struct qemud_server *server;
     char libvirtConf[PATH_MAX];
 
-    if (!(server = calloc(1, sizeof(struct qemud_server))))
+    if (!(server = calloc(1, sizeof(struct qemud_server)))) {
+        qemudLog(QEMUD_ERR, "Failed to allocate struct qemud_server");
         return NULL;
+    }
 
     /* XXX extract actual version */
     server->qemuVersion = (0*1000000)+(8*1000)+(0);
@@ -321,47 +416,54 @@ static struct qemud_server *qemudInitial
 
     if (sys) {
         if (snprintf(libvirtConf, sizeof(libvirtConf), "%s/libvirt", SYSCONF_DIR) >= (int)sizeof(libvirtConf)) {
-            goto cleanup;
+            goto snprintf_cleanup;
         }
         if (mkdir(libvirtConf, 0777) < 0) {
             if (errno != EEXIST) {
+                qemudLog(QEMUD_ERR, "Failed to create directory '%s': %s",
+                         libvirtConf, strerror(errno));
                 goto cleanup;
             }
         }
 
         if (snprintf(server->configDir, sizeof(server->configDir), "%s/libvirt/qemu", SYSCONF_DIR) >= (int)sizeof(server->configDir)) {
-            goto cleanup;
+            goto snprintf_cleanup;
         }
         if (snprintf(server->networkConfigDir, sizeof(server->networkConfigDir), "%s/libvirt/qemu/networks", SYSCONF_DIR) >= (int)sizeof(server->networkConfigDir)) {
-            goto cleanup;
+            goto snprintf_cleanup;
         }
     } else {
         struct passwd *pw;
         int uid;
 
         if ((uid = geteuid()) < 0) {
+            qemudLog(QEMUD_ERR, "You must run the daemon as root to use system mode");
             goto cleanup;
         }
         if (!(pw = getpwuid(uid))) {
+            qemudLog(QEMUD_ERR, "Failed to find user record for uid '%s': %s",
+                     uid, strerror(errno));
             goto cleanup;
         }
 
         if (snprintf(libvirtConf, sizeof(libvirtConf), "%s/.libvirt", pw->pw_dir) >= (int)sizeof(libvirtConf)) {
-            goto cleanup;
+            goto snprintf_cleanup;
         }
         if (mkdir(libvirtConf, 0777) < 0) {
             if (errno != EEXIST) {
+                qemudLog(QEMUD_ERR, "Failed to create directory '%s': %s",
+                         libvirtConf, strerror(errno));
                 goto cleanup;
             }
         }
 
 
         if (snprintf(server->configDir, sizeof(server->configDir), "%s/.libvirt/qemu", pw->pw_dir) >= (int)sizeof(server->configDir)) {
-            goto cleanup;
+            goto snprintf_cleanup;
         }
 
         if (snprintf(server->networkConfigDir, sizeof(server->networkConfigDir), "%s/.libvirt/qemu/networks", pw->pw_dir) >= (int)sizeof(server->networkConfigDir)) {
-            goto cleanup;
+            goto snprintf_cleanup;
         }
     }
 
@@ -376,6 +478,9 @@ static struct qemud_server *qemudInitial
 
     return server;
 
+ snprintf_cleanup:
+    qemudLog(QEMUD_ERR, "Resulting path to long for buffer in qemudInitialize()");
+
  cleanup:
     if (server) {
         struct qemud_socket *sock = server->sockets;
@@ -399,15 +504,12 @@ static int qemudDispatchServer(struct qe
     if ((fd = accept(sock->fd, (struct sockaddr *)&addr, &addrlen)) < 0) {
         if (errno == EAGAIN)
             return 0;
+        qemudLog(QEMUD_ERR, "Failed to accept connection: %s", strerror(errno));
         return -1;
     }
 
-    if (qemudSetCloseExec(fd) < 0) {
-        close(fd);
-        return -1;
-    }
-
-    if (qemudSetNonBlock(fd) < 0) {
+    if (qemudSetCloseExec(fd) < 0 ||
+        qemudSetNonBlock(fd) < 0) {
         close(fd);
         return -1;
     }
@@ -585,12 +687,12 @@ static int qemudClientRead(struct qemud_
                            char *buf, size_t want) {
     int ret;
     if ((ret = read(client->fd, buf, want)) <= 0) {
-        QEMUD_DEBUG("Plain read error %d\n", ret);
+        qemudDebug("Plain read error %d", ret);
         if (!ret || errno != EAGAIN)
             qemudDispatchClientFailure(server, client);
         return -1;
     }
-    QEMUD_DEBUG("Plain data read %d\n", ret);
+    qemudDebug("Plain data read %d", ret);
     return ret;
 }
 
@@ -615,17 +717,17 @@ static void qemudDispatchClientRead(stru
 
     /* If we've finished header, move onto body */
     if (client->incomingReceived == sizeof(struct qemud_packet_header)) {
-        QEMUD_DEBUG("Type %d, data %d\n",
+        qemudDebug("Type %d, data %d",
                     client->incoming.header.type,
                     client->incoming.header.dataSize);
         /* Client lied about dataSize */
         if (client->incoming.header.dataSize > sizeof(union qemud_packet_data)) {
-            QEMUD_DEBUG("Bogus data size %u\n", client->incoming.header.dataSize);
+            qemudDebug("Bogus data size %u", client->incoming.header.dataSize);
             qemudDispatchClientFailure(server, client);
             return;
         }
         if (client->incoming.header.dataSize) {
-            QEMUD_DEBUG("- Restarting recv to process body (%d bytes)\n",
+            qemudDebug("- Restarting recv to process body (%d bytes)",
                         client->incoming.header.dataSize);
             goto restart;
         }
@@ -635,7 +737,7 @@ static void qemudDispatchClientRead(stru
     if (ret == want) {
         if (qemudDispatchClientRequest(server, client) < 0)
             qemudDispatchClientFailure(server, client);
-        QEMUD_DEBUG("Dispatch\n");
+        qemudDebug("Dispatch");
     }
 }
 
@@ -645,12 +747,12 @@ static int qemudClientWrite(struct qemud
                            char *buf, size_t want) {
     int ret;
     if ((ret = write(client->fd, buf, want)) < 0) {
-        QEMUD_DEBUG("Plain write error %d\n", ret);
+        qemudDebug("Plain write error %d", ret);
         if (errno != EAGAIN)
             qemudDispatchClientFailure(server, client);
         return -1;
     }
-    QEMUD_DEBUG("Plain data write %d\n", ret);
+    qemudDebug("Plain data write %d", ret);
     return ret;
 }
 
@@ -664,7 +766,7 @@ static void qemudDispatchClientWrite(str
         return;
     }
     client->outgoingSent += ret;
-    QEMUD_DEBUG("Done %d %d\n", todo, ret);
+    qemudDebug("Done %d %d", todo, ret);
     if (todo == ret)
         client->tx = 0;
 }
@@ -726,12 +828,12 @@ static int qemudVMData(struct qemud_serv
                         close(monfd);
                         return -1;
                     }
-                    QEMUD_DEBUG("[%s]\n", line);
+                    qemudDebug("[%s]", line);
                 }
                 vm->monitor = monfd;
             }
         }
-        QEMUD_DEBUG("[%s]\n", buf);
+        qemudDebug("[%s]", buf);
     }
 }
 
@@ -746,6 +848,8 @@ int qemudShutdownVMDaemon(struct qemud_s
     struct qemud_vm *prev = NULL, *curr = server->activevms;
     struct qemud_vm_net_def *net;
 
+    qemudLog(QEMUD_INFO, "Shutting down VM '%s'", vm->def->name);
+
     /* Already cleaned-up */
     if (vm->pid < 0)
         return 0;
@@ -772,7 +876,7 @@ int qemudShutdownVMDaemon(struct qemud_s
     }
 
     if (!curr) {
-        QEMUD_DEBUG("Could not find VM to shutdown\n");
+        qemudDebug("Could not find VM to shutdown");
         return 0;
     }
 
@@ -797,7 +901,7 @@ int qemudShutdownVMDaemon(struct qemud_s
     if (waitpid(vm->pid, NULL, WNOHANG) != vm->pid) {
         kill(vm->pid, SIGKILL);
         if (waitpid(vm->pid, NULL, 0) != vm->pid) {
-            QEMUD_DEBUG("Got unexpected pid, damn\n");
+            qemudLog(QEMUD_WARN, "Got unexpected pid, damn");
         }
     }
 
@@ -1129,14 +1233,14 @@ int qemudStartNetworkDaemon(struct qemud
  err_delbr1:
     if (network->def->ipAddress[0] &&
         (err = brSetInterfaceUp(server->brctl, network->bridge, 0))) {
-        printf("Damn! Failed to bring down bridge '%s' : %s\n",
-               network->bridge, strerror(err));
+        qemudLog(QEMUD_WARN, "Failed to bring down bridge '%s' : %s",
+                 network->bridge, strerror(err));
     }
 
  err_delbr:
     if ((err = brDeleteBridge(server->brctl, network->bridge))) {
-        printf("Damn! Couldn't delete bridge '%s' : %s\n",
-               network->bridge, strerror(err));
+        qemudLog(QEMUD_WARN, "Failed to delete bridge '%s' : %s\n",
+                 network->bridge, strerror(err));
     }
 
     return -1;
@@ -1148,6 +1252,8 @@ int qemudShutdownNetworkDaemon(struct qe
     struct qemud_network *prev, *curr;
     int err;
 
+    qemudLog(QEMUD_INFO, "Shutting down network '%s'", network->def->name);
+
     if (!network->active)
         return 0;
 
@@ -1158,13 +1264,13 @@ int qemudShutdownNetworkDaemon(struct qe
 
     if (network->def->ipAddress[0] &&
         (err = brSetInterfaceUp(server->brctl, network->bridge, 0))) {
-        printf("Damn! Failed to bring down bridge '%s' : %s\n",
-               network->bridge, strerror(err));
+        qemudLog(QEMUD_WARN, "Failed to bring down bridge '%s' : %s\n",
+                 network->bridge, strerror(err));
     }
 
     if ((err = brDeleteBridge(server->brctl, network->bridge))) {
-        printf("Damn! Failed to delete bridge '%s' : %s\n",
-               network->bridge, strerror(err));
+        qemudLog(QEMUD_WARN, "Failed to delete bridge '%s' : %s\n",
+                 network->bridge, strerror(err));
     }
 
     /* Move it to inactive networks list */
@@ -1192,7 +1298,7 @@ int qemudShutdownNetworkDaemon(struct qe
         waitpid(network->dnsmasqPid, NULL, WNOHANG) != network->dnsmasqPid) {
         kill(network->dnsmasqPid, SIGKILL);
         if (waitpid(network->dnsmasqPid, NULL, 0) != network->dnsmasqPid)
-            printf("Got unexpected pid for dnsmasq, damn\n");
+            qemudLog(QEMUD_WARN, "Got unexpected pid for dnsmasq\n");
     }
 
     network->bridge[0] = '\0';
@@ -1226,6 +1332,7 @@ static int qemudDispatchPoll(struct qemu
 
     while (sock) {
         struct qemud_socket *next = sock->next;
+        /* FIXME: the daemon shouldn't exit on error here */
         if (fds[fd].revents)
             if (qemudDispatchServer(server, sock) < 0)
                 return -1;
@@ -1236,7 +1343,7 @@ static int qemudDispatchPoll(struct qemu
     while (client) {
         struct qemud_client *next = client->next;
         if (fds[fd].revents) {
-            QEMUD_DEBUG("Poll data normal\n");
+            qemudDebug("Poll data normal");
             if (fds[fd].revents == POLLOUT)
                 qemudDispatchClientWrite(server, client);
             else if (fds[fd].revents == POLLIN)
@@ -1281,7 +1388,7 @@ static int qemudDispatchPoll(struct qemu
         }
         vm = next;
         if (failed)
-            ret = -1;
+            ret = -1; /* FIXME: the daemon shouldn't exit on failure here */
     }
 
     /* Cleanup any VMs which shutdown & dont have an associated
@@ -1389,12 +1496,16 @@ static int qemudOneLoop(struct qemud_ser
         if (errno == EINTR) {
             goto retry;
         }
+        qemudLog(QEMUD_ERR, "Error polling on file descriptors: %s",
+                 strerror(errno));
         return -1;
     }
 
     /* Must have timed out */
-    if (ret == 0)
+    if (ret == 0) {
+        qemudLog(QEMUD_INFO, "Timed out while polling on file descriptors");
         return -1;
+    }
 
     if (qemudDispatchPoll(server, fds) < 0)
         return -1;
@@ -1427,8 +1538,6 @@ static void qemudCleanup(struct qemud_se
 
 #define MAX_LISTEN 5
 int main(int argc, char **argv) {
-    int godaemon = 0;
-    int verbose = 0;
     int sys = 0;
     int timeout = -1;
     struct qemud_server *server;
@@ -1484,10 +1593,16 @@ int main(int argc, char **argv) {
         }
     }
 
+    if (godaemon)
+        openlog("libvirt-qemud", 0, 0);
+
     if (pipe(sigpipe) < 0 ||
         qemudSetNonBlock(sigpipe[0]) < 0 ||
-        qemudSetNonBlock(sigpipe[1]) < 0)
+        qemudSetNonBlock(sigpipe[1]) < 0) {
+        qemudLog(QEMUD_ERR, "Failed to create pipe: %s",
+                 strerror(errno));
         return 1;
+    }
 
     sigwrite = sigpipe[1];
 
@@ -1505,8 +1620,11 @@ int main(int argc, char **argv) {
 
     if (godaemon) {
         int pid = qemudGoDaemon();
-        if (pid < 0)
+        if (pid < 0) {
+            qemudLog(QEMUD_ERR, "Failed to fork as daemon: %s",
+                     strerror(errno));
             return 1;
+        }
         if (pid > 0)
             return 0;
     }
@@ -1520,6 +1638,9 @@ int main(int argc, char **argv) {
 
     close(sigwrite);
 
+    if (godaemon)
+        closelog();
+
     return 0;
 }
 
Index: libvirt/qemud/dispatch.c
===================================================================
--- libvirt.orig/qemud/dispatch.c
+++ libvirt/qemud/dispatch.c
@@ -812,7 +812,7 @@ int qemudDispatch(struct qemud_server *s
                   struct qemud_packet *in, struct qemud_packet *out) {
     clientFunc *funcs;
     unsigned int type = in->header.type;
-    QEMUD_DEBUG("> Dispatching request %d readonly ? %d\n", type, client->readonly);
+    qemudDebug("> Dispatching request %d readonly ? %d", type, client->readonly);
 
     server->errorCode = 0;
     server->errorMessage[0] = '\0';
@@ -820,12 +820,12 @@ int qemudDispatch(struct qemud_server *s
     memset(out, 0, sizeof(struct qemud_packet));
 
     if (type >= QEMUD_PKT_MAX) {
-        QEMUD_DEBUG("Illegal request type\n");
+        qemudDebug("Illegal request type");
         return -1;
     }
 
     if (type == QEMUD_PKT_FAILURE) {
-        QEMUD_DEBUG("Illegal request type\n");
+        qemudDebug("Illegal request type");
         return -1;
     }
 
@@ -835,17 +835,17 @@ int qemudDispatch(struct qemud_server *s
         funcs = funcsTransmitRW;
 
     if (!funcs[type]) {
-        QEMUD_DEBUG("Illegal operation requested\n");
+        qemudDebug("Illegal operation requested");
         qemudReportError(server, VIR_ERR_OPERATION_DENIED, NULL);
         qemudDispatchFailure(server, client, out);
     } else {
         if ((funcs[type])(server, client, in, out) < 0) {
-            QEMUD_DEBUG("Dispatch failed\n");
+            qemudDebug("Dispatch failed");
             return -1;
         }
     }
 
-    QEMUD_DEBUG("< Returning reply %d (%d bytes)\n",
+    qemudDebug("< Returning reply %d (%d bytes)",
            out->header.type,
            out->header.dataSize);
 
Index: libvirt/qemud/internal.h
===================================================================
--- libvirt.orig/qemud/internal.h
+++ libvirt/qemud/internal.h
@@ -44,14 +44,17 @@
 #define ATTRIBUTE_UNUSED
 #endif
 
-#ifdef DEBUG
-#define QEMUD_DEBUG(args...) fprintf(stderr, args)
-#else
-#define QEMUD_DEBUG(args...) do {} while(0)
-#endif
-
 #define UUID_LEN 16
 
+typedef enum {
+    QEMUD_ERR,
+    QEMUD_WARN,
+    QEMUD_INFO,
+#ifdef ENABLE_DEBUG
+    QEMUD_DEBUG
+#endif
+} qemudLogPriority;
+
 /* Different types of QEMU acceleration possible */
 enum qemud_vm_virt_type {
     QEMUD_VIRT_QEMU,
@@ -307,6 +310,13 @@ int qemudStartNetworkDaemon(struct qemud
 int qemudShutdownNetworkDaemon(struct qemud_server *server,
                                struct qemud_network *network);
 
+void qemudLog(int priority, const char *fmt, ...);
+
+#ifdef ENABLE_DEBUG
+#define qemudDebug(...) qemudLog(QEMUD_DEBUG, __VA_ARGS__)
+#else
+#define qemudDebug(fmt, ...) do { } while(0);
+#endif
 
 #endif
 
Index: libvirt/configure.in
===================================================================
--- libvirt.orig/configure.in
+++ libvirt/configure.in
@@ -78,6 +78,14 @@ dnl
 	CFLAGS="-g -O -W -Wformat -Wunused -Wimplicit -Wreturn-type -Wswitch -Wcomment -Wtrigraphs -Wformat -Wchar-subscripts -Wuninitialized -Wparentheses -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wredundant-decls -Wall"
     fi
 
+dnl --enable-debug=(yes|no)
+AC_ARG_ENABLE(debug,
+              AC_HELP_STRING([--enable-debug=no/yes],
+                             [enable debugging output]))
+if test x"$enable_debug" = x"yes"; then
+   AC_DEFINE(ENABLE_DEBUG, [], [whether debugging is enabled])
+fi
+
 dnl
 dnl allow the creation of iptables rules in chains with a
 dnl specific prefix rather than in the standard toplevel chains
Index: libvirt/qemud/conf.c
===================================================================
--- libvirt.orig/qemud/conf.c
+++ libvirt/qemud/conf.c
@@ -1644,6 +1644,8 @@ int qemudScanConfigDir(struct qemud_serv
     if (!(dir = opendir(configDir))) {
         if (errno == ENOENT)
             return 0;
+        qemudLog(QEMUD_ERR, "Failed to open dir '%s': %s",
+                 configDir, strerror(errno));
         return -1;
     }
 
Index: libvirt/qemud/driver.c
===================================================================
--- libvirt.orig/qemud/driver.c
+++ libvirt/qemud/driver.c
@@ -90,7 +90,7 @@ int qemudMonitorCommand(struct qemud_ser
             size += got;
         }
         if (buf)
-            QEMUD_DEBUG("Mon [%s]\n", buf);
+            qemudDebug("Mon [%s]", buf);
         /* Look for QEMU prompt to indicate completion */
         if (buf && ((tmp = strstr(buf, "\n(qemu)")) != NULL)) {
             tmp[0] = '\0';
@@ -203,7 +203,7 @@ static int qemudGetProcessInfo(unsigned 
     }
 
     if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &usertime, &systime) != 2) {
-        QEMUD_DEBUG("not enough arg\n");
+        qemudDebug("not enough arg");
         return -1;
     }
 
@@ -214,7 +214,7 @@ static int qemudGetProcessInfo(unsigned 
      */
     *cpuTime = 1000 * 1000 * 1000 * (usertime + systime) / sysconf(_SC_CLK_TCK);
 
-    QEMUD_DEBUG("Got %lu %lu %lld\n", usertime, systime, *cpuTime);
+    qemudDebug("Got %lu %lu %lld", usertime, systime, *cpuTime);
 
     fclose(pidinfo);
 
@@ -322,7 +322,7 @@ int qemudDomainSuspend(struct qemud_serv
         qemudReportError(server, VIR_ERR_OPERATION_FAILED, "suspend operation failed");
         return -1;
     }
-    printf("Reply %s\n", info);
+    qemudDebug("Reply %s", info);
     free(info);
     return 0;
 }
@@ -343,7 +343,7 @@ int qemudDomainResume(struct qemud_serve
         qemudReportError(server, VIR_ERR_OPERATION_FAILED, "resume operation failed");
         return -1;
     }
-    printf("Reply %s\n", info);
+    qemudDebug("Reply %s", info);
     free(info);
     return -1;
 }

-- 


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]