2 * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
25 #include <sys/types.h>
26 #include <sys/socket.h>
32 static void print_help(void)
34 printf("Usage: udevadm control COMMAND\n"
35 " --log-priority=<level> set the udev log level for the daemon\n"
36 " --stop-exec-queue keep udevd from executing events, queue only\n"
37 " --start-exec-queue execute events, flush queue\n"
38 " --reload-rules reloads the rules files\n"
39 " --env=<KEY>=<value> set a global environment variable\n"
40 " --max-childs=<N> maximum number of childs\n"
41 " --help print this help text\n\n");
44 int udevadm_control(struct udev *udev, int argc, char *argv[])
46 struct udev_ctrl *uctrl = NULL;
49 /* compat values with '_' will be removed in a future release */
50 static const struct option options[] = {
51 { "log-priority", 1, NULL, 'l' },
52 { "log_priority", 1, NULL, 'l' + 256 },
53 { "stop-exec-queue", 0, NULL, 's' },
54 { "stop_exec_queue", 0, NULL, 's' + 256 },
55 { "start-exec-queue", 0, NULL, 'S' },
56 { "start_exec_queue", 0, NULL, 'S' + 256},
57 { "reload-rules", 0, NULL, 'R' },
58 { "reload_rules", 0, NULL, 'R' + 256},
59 { "env", 1, NULL, 'e' },
60 { "max-childs", 1, NULL, 'm' },
61 { "max_childs", 1, NULL, 'm' + 256},
62 { "help", 0, NULL, 'h' },
67 fprintf(stderr, "root privileges required\n");
71 uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
80 option = getopt_long(argc, argv, "l:sSRe:m:M:h", options, NULL);
85 fprintf(stderr, "udevadm control expects commands without underscore, "
86 "this will stop working in a future release\n");
87 err(udev, "udevadm control expects commands without underscore, "
88 "this will stop working in a future release\n");
94 i = log_priority(optarg);
96 fprintf(stderr, "invalid number '%s'\n", optarg);
99 udev_ctrl_send_set_log_level(uctrl, log_priority(optarg));
104 udev_ctrl_send_stop_exec_queue(uctrl);
109 udev_ctrl_send_start_exec_queue(uctrl);
114 udev_ctrl_send_reload_rules(uctrl);
118 if (strchr(optarg, '=') == NULL) {
119 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
122 udev_ctrl_send_set_env(uctrl, optarg);
127 i = strtoul(optarg, &endp, 0);
128 if (endp[0] != '\0' || i < 1) {
129 fprintf(stderr, "invalid number '%s'\n", optarg);
132 udev_ctrl_send_set_max_childs(uctrl, i);
144 /* compat stuff which will be removed in a future release */
145 if (argv[optind] != NULL) {
146 const char *arg = argv[optind];
148 fprintf(stderr, "udevadm control commands requires the --<command> format, "
149 "this will stop working in a future release\n");
150 err(udev, "udevadm control commands requires the --<command> format, "
151 "this will stop working in a future release\n");
153 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
154 udev_ctrl_send_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
157 } else if (!strcmp(arg, "stop_exec_queue")) {
158 udev_ctrl_send_stop_exec_queue(uctrl);
161 } else if (!strcmp(arg, "start_exec_queue")) {
162 udev_ctrl_send_start_exec_queue(uctrl);
165 } else if (!strcmp(arg, "reload_rules")) {
166 udev_ctrl_send_reload_rules(uctrl);
169 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
170 udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
173 } else if (!strncmp(arg, "env", strlen("env"))) {
174 udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
181 fprintf(stderr, "unrecognized command\n");
182 err(udev, "unrecognized command\n");
185 udev_ctrl_unref(uctrl);