]> xenbits.xensource.com Git - people/dstodden/blktap.git/commitdiff
CA-31481: Add syslog facility option to blktapctrl and tapdisk.
authorDaniel Stodden <daniel.stodden@citrix.com>
Mon, 26 Oct 2009 12:48:23 +0000 (05:48 -0700)
committerDaniel Stodden <daniel.stodden@citrix.com>
Mon, 26 Oct 2009 12:48:23 +0000 (05:48 -0700)
daemon/tapdisk-channel.c
daemon/tapdisk-daemon.c
daemon/tapdisk-dispatch.h
drivers/tapdisk-diff.c
drivers/tapdisk-stream.c
drivers/tapdisk-utils.c
drivers/tapdisk-utils.h
drivers/tapdisk.c
drivers/tapdisk2.c

index edf94ac6bd704f22f972fe7d3061cbca911ef08f..12963cd796570abc067ad714b87d563e7ddf8734 100644 (file)
@@ -1228,7 +1228,8 @@ tapdisk_channel_start_process(tapdisk_channel_t *channel,
                              char *write_dev, char *read_dev)
 {
        pid_t child;
-       char *argv[] = { "tapdisk", write_dev, read_dev, NULL };
+       char opt_facility[32];
+       char *argv[] = { "tapdisk", opt_facility, write_dev, read_dev, NULL };
        const char *tapdisk;
        int i;
 
@@ -1244,6 +1245,9 @@ tapdisk_channel_start_process(tapdisk_channel_t *channel,
                    i != STDERR_FILENO)
                        close(i);
 
+       snprintf(opt_facility, sizeof(opt_facility),
+                "-l%d", tapdisk_daemon_log_facility);
+
        tapdisk = getenv("TAPDISK");
        if (!tapdisk)
                tapdisk = argv[0];
index c35d26fbbcf182088ba436ec4c99443ab082f7e7..a323d190b8185801dbc2986a845ede467d3d7c20 100644 (file)
@@ -34,6 +34,9 @@
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <signal.h>
+#include <getopt.h>
+#define SYSLOG_NAMES
+#include <syslog.h>
 
 #include <xs.h>
 #include "disktypes.h"
@@ -55,6 +58,7 @@ typedef struct tapdisk_daemon {
 } tapdisk_daemon_t;
 
 static tapdisk_daemon_t tapdisk_daemon;
+int tapdisk_daemon_log_facility;
 
 #define tapdisk_daemon_for_each_channel(c, tmp) \
        list_for_each_entry_safe(c, tmp, &tapdisk_daemon.channels, list)
@@ -67,6 +71,7 @@ tapdisk_daemon_print_drivers(void)
        int i, size;
 
        DPRINTF("blktap-daemon: v1.0.2\n");
+       DPRINTF("Syslog facility %d\n", tapdisk_daemon_log_facility);
 
        size = sizeof(dtypes) / sizeof(disk_info_t *);
        for (i = 0; i < size; i++)
@@ -680,11 +685,68 @@ tapdisk_daemon_close_channel(tapdisk_channel_t *channel)
        close(channel->write_fd);
 }
 
+static void
+tapdisk_daemon_openlog(const char *ident, const char *facility_name)
+{
+       static char buf[128];
+       int facility;
+
+       facility = LOG_DAEMON;
+
+       if (facility_name) {
+               char *endptr;
+
+               facility = strtol(facility_name, &endptr, 0);
+               if (*endptr != 0) {
+                       CODE *c;
+
+                       facility = LOG_DAEMON;
+                       for (c = facilitynames; c->c_name != NULL; ++c)
+                               if (!strcmp(c->c_name, facility_name))
+                                       facility = c->c_val;
+               }
+       }
+
+       snprintf(buf, sizeof(buf), "%s[%d]", ident, getpid());
+       openlog(buf, LOG_CONS | LOG_ODELAY, facility);
+       tapdisk_daemon_log_facility = facility;
+}
+
+static const char *program;
+
+static void
+usage(FILE *stream)
+{
+       fprintf(stream, "Usage: %s [-h] [-l <syslog_facility>]\n", program);
+}
+
 int
 main(int argc, char *argv[])
 {
        int err;
-       char buf[128];
+       const char *facility;
+
+       program  = basename(argv[0]);
+       facility = "daemon";
+
+       do {
+               char c;
+
+               c = getopt(argc, argv, "hl:");
+               if (c < 0)
+                       break;
+
+               switch (c) {
+               case 'h':
+                       usage(stdout);
+                       return 0;
+               case 'l':
+                       facility = optarg;
+                       break;
+               default:
+                       goto usage;
+               }
+       } while (1);
 
        daemon(0, 0);
 
@@ -701,8 +763,7 @@ main(int argc, char *argv[])
        }
 #endif
 
-       snprintf(buf, sizeof(buf), "BLKTAP-DAEMON[%d]", getpid());
-       openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
+       tapdisk_daemon_openlog("BLKTAP-DAEMON", facility);
 
        err = tapdisk_daemon_write_pidfile(getpid());
        if (err)
@@ -729,6 +790,10 @@ main(int argc, char *argv[])
 
 out:
        closelog();
-
        return err ? 1 : 0;
+
+usage:
+       usage(stderr);
+       return EINVAL;
+
 }
index 7c2664e6bfae96051536fc46e2e6003248626fc4..01d4d7248f46d6931998f2b5c19e217c07b14193 100644 (file)
@@ -111,6 +111,8 @@ struct tapdisk_channel {
        struct xs_handle         *xsh;
 };
 
+extern int tapdisk_daemon_log_facility;
+
 typedef struct tapdisk_channel tapdisk_channel_t;
 
 int strsep_len(const char *str, char c, unsigned int len);
index c97a6607c13353abc1d23ba4c9c08410ce018918..9593376b9bd400a241f9d470495b498944b94532 100644 (file)
@@ -748,7 +748,7 @@ main(int argc, char *argv[])
        if (err)
                return err;
 
-       tapdisk_start_logging("tapdisk-diff");
+       tapdisk_start_logging("tapdisk-diff", "daemon");
 
        err = tapdisk_server_initialize(NULL, NULL);
        if (err)
index c704daf8999b3b174a5bf69df527c2c31665228f..96abc9a22595b2de5a40dfac264a68c3bb87a6ac 100644 (file)
@@ -581,7 +581,7 @@ main(int argc, char *argv[])
                return err;
        }
 
-       tapdisk_start_logging("tapdisk-stream");
+       tapdisk_start_logging("tapdisk-stream", "daemon");
 
        err = tapdisk_stream_open(&stream, path, type, count, skip);
        if (err)
index 7d49104e71abe50f1c001fc287ca8e1d019d1ff0..40cc3335ff6d1f976ea630b97093e532aa812289 100644 (file)
@@ -25,6 +25,8 @@
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+
+#include <stdlib.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
@@ -35,6 +37,9 @@
 #include <sys/ioctl.h>
 #include <sys/resource.h>
 
+#define SYSLOG_NAMES
+#include <syslog.h>
+
 #include "tapdisk.h"
 #include "disktypes.h"
 #include "blktaplib.h"
 #include "tapdisk-utils.h"
 
 void
-tapdisk_start_logging(const char *name)
+tapdisk_start_logging(const char *ident, const char *facility_name)
 {
        static char buf[128];
+       int facility;
+
+       facility = LOG_DAEMON;
+
+       if (facility_name) {
+               char *endptr;
+
+               facility = strtol(facility_name, &endptr, 0);
+               if (*endptr != 0) {
+                       CODE *c;
+
+                       facility = LOG_DAEMON;
+                       for (c = facilitynames; c->c_name != NULL; ++c)
+                               if (!strcmp(c->c_name, facility_name))
+                                       facility = c->c_val;
+               }
+       }
 
-       snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
-       openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
+       snprintf(buf, sizeof(buf), "%s[%d]", ident, getpid());
+       openlog(buf, LOG_CONS | LOG_ODELAY, facility);
        open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
 }
 
index 216c9023777a5c70c728d9351c0293bdfd8543b6..48b57d17b065805531f363c4979017f257623ed8 100644 (file)
@@ -32,7 +32,7 @@
 
 #define MAX_NAME_LEN                 1000
 
-void tapdisk_start_logging(const char *);
+void tapdisk_start_logging(const char *, const char *);
 void tapdisk_stop_logging(void);
 int tapdisk_set_resource_limits(void);
 int tapdisk_namedup(char **, const char *);
index 4fbc52b0e52c7ef2d28a6cad7192fbb181a8d630..032799a9183f082a415e77f25345b9da8f0ac456 100644 (file)
@@ -52,17 +52,23 @@ main(int argc, char *argv[])
                { "detach",             0, 0, 'D' }
        };
        int err, detach = 0;
-       
-       program = basename(argv[0]);
+       const char *facility;
+       const char *ipc_read, *ipc_write;
+
+       program  = basename(argv[0]);
+       facility = "daemon";
 
        do {
                int c;
 
-               c = getopt_long(argc, argv, "hD", longopts, NULL);
+               c = getopt_long(argc, argv, "hDl:", longopts, NULL);
                if (c == -1)
                        break;
 
                switch (c) {
+               case 'l':
+                       facility = optarg;
+                       break;
                case 'h':
                        usage(stdout);
                        return 0;
@@ -76,7 +82,13 @@ main(int argc, char *argv[])
        
        if (argc - optind < 2)
                goto usage;
-       
+
+       ipc_read  = argv[optind++];
+       ipc_write = argv[optind++];
+
+       if (argc - optind)
+               goto usage;
+
        if (detach) {
                /* NB. This is expected to rarely, if ever, be
                   used. Blktapctrl is already detached, and breaking
@@ -88,9 +100,9 @@ main(int argc, char *argv[])
                }
        }
 
-       tapdisk_start_logging("TAPDISK");
+       tapdisk_start_logging("TAPDISK", facility);
 
-       err = tapdisk_server_initialize(argv[1], argv[2]);
+       err = tapdisk_server_initialize(ipc_read, ipc_write);
        if (err) {
                EPRINTF("failed to initialize tapdisk server: %d\n", err);
                goto out;
index 45b27ecc194b060e866b0ee93196361cf9581976..59ec9203453b52abb107f479b7ae5517dd751c85 100644 (file)
@@ -321,7 +321,6 @@ tapdisk2_create_device(const char *params)
        int err, type;
 
        chdir("/");
-       tapdisk_start_logging("tapdisk2");
 
        err = tapdisk2_set_child_fds();
        if (err)
@@ -401,11 +400,16 @@ main(int argc, char *argv[])
 {
        int c;
        char *params;
+       const char *facility;
 
-       params = NULL;
+       params   = NULL;
+       facility = "daemon";
 
-       while ((c = getopt(argc, argv, "n:h")) != -1) {
+       while ((c = getopt(argc, argv, "n:hl:")) != -1) {
                switch (c) {
+               case 'l':
+                       facility = optarg;
+                       break;
                case 'n':
                        params = optarg;
                        break;
@@ -429,6 +433,7 @@ main(int argc, char *argv[])
                printf("fork failed: %d\n", errno);
                return errno;
        case 0:
+               tapdisk_start_logging("tapdisk2", facility);
                return tapdisk2_create_device(params);
        default:
                return tapdisk2_wait_for_device();