]> sourceware.org Git - dm.git/commitdiff
working dm_get_registered_device(). dmevent.c update to use it.
authorHeinz Mauelshagen <heinzm@redhat.com>
Tue, 3 May 2005 16:15:20 +0000 (16:15 +0000)
committerHeinz Mauelshagen <heinzm@redhat.com>
Tue, 3 May 2005 16:15:20 +0000 (16:15 +0000)
dmeventd/dmevent.c
lib/event/dmeventd.c
lib/event/libdm-event.c

index 57c3b14c40092ab73086b0e7c588c05a5a993325..7a9dfb2ae2144c830b3cf0a6c27acfba92034ed9 100644 (file)
@@ -27,7 +27,7 @@
 #include <unistd.h>
 
 static enum event_type events = ALL_ERRORS; /* All until we can distinguish. */
-static char *default_dso_name = "noop";  /* default DSO is noop */
+static char default_dso_name[] = "noop";  /* default DSO is noop */
 static int default_reg = 1;             /* default action is register */
 
 /* Display help. */
@@ -58,7 +58,9 @@ static int parse_argv(int argc, char **argv,
        while ((c = getopt(argc, argv, options)) != -1) {
                switch (c) {
                case 'd':
-                       *dso_name = optarg;
+                       if (!(*dso_name = strdup(optarg)))
+                               exit(EXIT_FAILURE);
+
                        break;
                case 'h':
                        print_usage(argv[0]);
@@ -80,14 +82,16 @@ static int parse_argv(int argc, char **argv,
                }
        }
 
-       if (!*list) {
-               if (optind >= argc) {
+       if (!*dso_name && !(*dso_name = strdup(default_dso_name)))
+               exit(EXIT_FAILURE);
+
+       if (optind >= argc) {
+               if (!*list) {
                        fprintf(stderr, "You need to specify a device.\n");
                        return 0;
                }
-
-               *device = argv[optind];
-       }
+       } else if (!(*device = strdup(argv[optind])))
+                       exit(EXIT_FAILURE);
 
        return 1;
 }
@@ -95,43 +99,58 @@ static int parse_argv(int argc, char **argv,
 int main(int argc, char **argv)
 {
        int list = 0, next = 0, ret, reg = default_reg;
-       char *device = NULL, *dso_name = default_dso_name, *dso_name_arg = NULL;
+       char *device = NULL, *device_arg, *dso_name = NULL, *dso_name_arg;
 
-       if (!parse_argv(argc, argv, &dso_name_arg, &device, &reg, &list))
+       if (!parse_argv(argc, argv, &dso_name, &device, &reg, &list))
                exit(EXIT_FAILURE);
 
+       device_arg = device;
+       dso_name_arg = dso_name;
+
        if (list) {
-               dso_name = dso_name_arg;
-                       
                do {
-                       /* FIXME: dso_name and/or device name given. */
                        if (!(ret= dm_get_registered_device(&dso_name,
                                                            &device,
                                                            &events, next))) {
                                printf("%s %s 0x%x\n",
                                       dso_name, device, events);
 
+                               if (device_arg)
+                                       break;
+
                                next = 1;
                        }
                } while (!ret);
 
-               exit(EXIT_SUCCESS);
-       }
+               if (dso_name)
+                       free(dso_name);
 
-       if (dso_name_arg)
-               dso_name = dso_name_arg;
+               if (device)
+                       free(device);
+
+               ret = (ret && device_arg) ? EXIT_FAILURE : EXIT_SUCCESS;
+               goto out;
+       }
 
        if ((ret = reg ? dm_register_for_event(dso_name, device, events) :
                         dm_unregister_for_event(dso_name, device, events))) {
                fprintf(stderr, "Failed to %sregister %s: %s\n",
                        reg ? "": "un", device, strerror(-ret));
-
-               exit(EXIT_FAILURE);
+               ret = EXIT_FAILURE;
+       } else {
+               printf("%s %sregistered successfully.\n",
+                      device, reg ? "" : "un");
+               ret = EXIT_SUCCESS;
        }
 
-       printf("%s %sregistered successfully.\n", device, reg ? "" : "un");
+   out:
+       if (device_arg)
+               free(device_arg);
+
+       if (dso_name_arg)
+               free(dso_name_arg);
 
-       exit(EXIT_SUCCESS);
+       exit(ret);
 }
 
 /*
index 7e6bc9f72462b7cdb33623c8182457fb6f10db2c..b6e51f97a354c2637850032fb0ecaff3bbe07207 100644 (file)
@@ -351,6 +351,7 @@ static int event_wait(struct thread_status *thread)
        if (!(dmt = dm_task_create(DM_DEVICE_WAITEVENT)))
                return 0;
 
+/* FIXME: check for event_nr increased. */
        if ((ret = dm_task_set_name(dmt, basename(thread->device_path))) &&
            (ret = dm_task_set_event_nr(dmt, 0)) &&
            (ret = dm_task_run(dmt))) {
index cdf390495df3f41d210ea7d218a3208a188ac6d6..5ac3351e672df704fdac9294dc5962cae27fda9d 100644 (file)
@@ -315,45 +315,52 @@ static int do_event(int cmd, struct daemon_message *msg,
 }
 
 /* External library interface. */
-int dm_register_for_event(char *dso_name, char *device, enum event_type events)
+int dm_register_for_event(char *dso_name, char *device_path,
+                         enum event_type events)
 {
        struct daemon_message msg;
 
-       if (!device_exists(device))
+       if (!device_exists(device_path))
                return -ENODEV;
 
-       return do_event(CMD_REGISTER_FOR_EVENT, &msg, dso_name, device, events);
+       return do_event(CMD_REGISTER_FOR_EVENT, &msg,
+                       dso_name, device_path, events);
 }
 
-int dm_unregister_for_event(char *dso_name, char *device,
+int dm_unregister_for_event(char *dso_name, char *device_path,
                           enum event_type events)
 {
        struct daemon_message msg;
 
-       if (!device_exists(device))
+       if (!device_exists(device_path))
                return -ENODEV;
 
-       return do_event(CMD_UNREGISTER_FOR_EVENT, &msg, dso_name,
-                       device, events);
+       return do_event(CMD_UNREGISTER_FOR_EVENT, &msg,
+                       dso_name, device_path, events);
 }
 
-int dm_get_registered_device(char **dso_name, char **device,
+int dm_get_registered_device(char **dso_name, char **device_path,
                             enum event_type *events, int next)
 {
        int ret;
-       char *dso_name_sav = *dso_name, *device_sav = *device;
+       char *dso_name_sav = NULL, *device_path_sav = NULL;
        struct daemon_message msg;
 
+       if (next) {
+               dso_name_sav = *dso_name;
+               device_path_sav = *device_path;
+       }
+
        if (!(ret = do_event(next ? CMD_GET_NEXT_REGISTERED_DEVICE :
                                    CMD_GET_REGISTERED_DEVICE,
-                            &msg, *dso_name, *device, *events)))
-               ret = parse_message(&msg, dso_name, device, events);
+                            &msg, *dso_name, *device_path, *events)))
+               ret = parse_message(&msg, dso_name, device_path, events);
 
        if (dso_name_sav)
                free(dso_name_sav);
 
-       if (device_sav)
-               free(device_sav);
+       if (device_path_sav)
+               free(device_path_sav);
 
        return ret;
 }
This page took 0.033509 seconds and 5 git commands to generate.