chiark / gitweb /
add a bunch of private device properties to udev_device
[elogind.git] / udev / udevadm-control.c
index de4b658d5b3315ceda83eab1893dadf2046f229e..8e319be9460855fe0c28f92086b97ea04dbd7e6b 100644 (file)
 #include <sys/un.h>
 
 #include "udev.h"
-#include "udevd.h"
 
-static int udev_log = 0;
+static void print_help(void)
+{
+       printf("Usage: udevadm control COMMAND\n"
+               "  --log-priority=<level>   set the udev log level for the daemon\n"
+               "  --stop-exec-queue        keep udevd from executing events, queue only\n"
+               "  --start-exec-queue       execute events, flush queue\n"
+               "  --reload-rules           reloads the rules files\n"
+               "  --env=<KEY>=<value>      set a global environment variable\n"
+               "  --max-childs=<N>         maximum number of childs\n"
+               "  --max-childs-running=<N> maximum number of childs running at the same time\n"
+               "  --help                   print this help text\n\n");
+}
 
-int udevadm_control(int argc, char *argv[])
+int udevadm_control(struct udev *udev, int argc, char *argv[])
 {
-       struct udev_ctrl *uctrl;
-       const char *env;
+       struct udev_ctrl *uctrl = NULL;
        int rc = 1;
 
        /* compat values with '_' will be removed in a future release */
@@ -61,19 +70,12 @@ int udevadm_control(int argc, char *argv[])
                {}
        };
 
-       env = getenv("UDEV_LOG");
-       if (env)
-               udev_log = log_priority(env);
-
-       logging_init("udevcontrol");
-       dbg("version %s\n", VERSION);
-
        if (getuid() != 0) {
                fprintf(stderr, "root privileges required\n");
                goto exit;
        }
 
-       uctrl = udev_ctrl_new_from_socket(UDEVD_CTRL_SOCK_PATH);
+       uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
        if (uctrl == NULL)
                goto exit;
 
@@ -87,7 +89,7 @@ int udevadm_control(int argc, char *argv[])
                        break;
 
                if (option > 255) {
-                       info("udevadm control expects commands without underscore, "
+                       info(udev, "udevadm control expects commands without underscore, "
                            "this will stop working in a future release\n");
                        fprintf(stderr, "udevadm control expects commands without underscore, "
                                "this will stop working in a future release\n");
@@ -101,26 +103,31 @@ int udevadm_control(int argc, char *argv[])
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_log_level(uctrl, log_priority(optarg));
+                       udev_ctrl_send_set_log_level(uctrl, log_priority(optarg));
+                       rc = 0;
                        break;
                case 's':
                case 's' + 256:
-                       udev_ctrl_stop_exec_queue(uctrl);
+                       udev_ctrl_send_stop_exec_queue(uctrl);
+                       rc = 0;
                        break;
                case 'S':
                case 'S' + 256:
-                       udev_ctrl_start_exec_queue(uctrl);
+                       udev_ctrl_send_start_exec_queue(uctrl);
+                       rc = 0;
                        break;
                case 'R':
                case 'R' + 256:
-                       udev_ctrl_reload_rules(uctrl);
+                       udev_ctrl_send_reload_rules(uctrl);
+                       rc = 0;
                        break;
                case 'e':
                        if (strchr(optarg, '=') == NULL) {
                                fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_env(uctrl, optarg);
+                       udev_ctrl_send_set_env(uctrl, optarg);
+                       rc = 0;
                        break;
                case 'm':
                case 'm' + 256:
@@ -129,7 +136,8 @@ int udevadm_control(int argc, char *argv[])
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_max_childs(uctrl, i);
+                       udev_ctrl_send_set_max_childs(uctrl, i);
+                       rc = 0;
                        break;
                case 'M':
                case 'M' + 256:
@@ -138,19 +146,12 @@ int udevadm_control(int argc, char *argv[])
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_max_childs_running(uctrl, i);
-                       break;
+                       udev_ctrl_send_set_max_childs_running(uctrl, i);
+                       rc = 0;
                        break;
                case 'h':
-                       printf("Usage: udevadm control COMMAND\n"
-                               "  --log-priority=<level>   set the udev log level for the daemon\n"
-                               "  --stop-exec-queue        keep udevd from executing events, queue only\n"
-                               "  --start-exec-queue       execute events, flush queue\n"
-                               "  --reload-rules           reloads the rules files\n"
-                               "  --env=<KEY>=<value>      set a global environment variable\n"
-                               "  --max-childs=<N>         maximum number of childs\n"
-                               "  --max-childs-running=<N> maximum number of childs running at the same time\n"
-                               "  --help                   print this help text\n\n");
+                       print_help();
+                       rc = 0;
                        goto exit;
                default:
                        goto exit;
@@ -163,30 +164,45 @@ int udevadm_control(int argc, char *argv[])
 
                fprintf(stderr, "udevadm control commands requires the --<command> format, "
                        "this will stop working in a future release\n");
-               err("udevadm control commands requires the --<command> format, "
+               err(udev, "udevadm control commands requires the --<command> format, "
                    "this will stop working in a future release\n");
 
                if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
-                       udev_ctrl_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
+                       udev_ctrl_send_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
+                       rc = 0;
+                       goto exit;
                } else if (!strcmp(arg, "stop_exec_queue")) {
-                       udev_ctrl_stop_exec_queue(uctrl);
+                       udev_ctrl_send_stop_exec_queue(uctrl);
+                       rc = 0;
+                       goto exit;
                } else if (!strcmp(arg, "start_exec_queue")) {
-                       udev_ctrl_start_exec_queue(uctrl);
+                       udev_ctrl_send_start_exec_queue(uctrl);
+                       rc = 0;
+                       goto exit;
                } else if (!strcmp(arg, "reload_rules")) {
-                       udev_ctrl_reload_rules(uctrl);
+                       udev_ctrl_send_reload_rules(uctrl);
+                       rc = 0;
+                       goto exit;
                } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
-                       udev_ctrl_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
+                       udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
+                       rc = 0;
+                       goto exit;
                } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
-                       udev_ctrl_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
+                       udev_ctrl_send_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
+                       rc = 0;
+                       goto exit;
                } else if (!strncmp(arg, "env", strlen("env"))) {
-                       udev_ctrl_set_env(uctrl, &arg[strlen("env=")]);
-               } else {
-                       fprintf(stderr, "unrecognized command '%s'\n", arg);
-                       err("unrecognized command '%s'\n", arg);
+                       udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
+                       rc = 0;
+                       goto exit;
                }
        }
+
+       if (rc != 0) {
+               fprintf(stderr, "unrecognized command\n");
+               err(udev, "unrecognized command\n");
+       }
 exit:
        udev_ctrl_unref(uctrl);
-       logging_close();
        return rc;
 }