2 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <sys/types.h>
30 #include <sys/socket.h>
37 static int udev_log = 0;
40 extern struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path);
41 extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
42 extern int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority);
43 extern int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl);
44 extern int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl);
45 extern int udev_ctrl_reload_rules(struct udev_ctrl *uctrl);
46 extern int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key);
47 extern int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count);
48 extern int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count);
52 struct sockaddr_un saddr;
56 struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
58 struct udev_ctrl *uctrl;
60 uctrl = malloc(sizeof(struct udev_ctrl));
63 memset(uctrl, 0x00, sizeof(struct udev_ctrl));
65 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
66 if (uctrl->sock < 0) {
67 err("error getting socket: %s\n", strerror(errno));
72 uctrl->saddr.sun_family = AF_LOCAL;
73 strcpy(uctrl->saddr.sun_path, socket_path);
74 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
75 /* translate leading '@' to abstract namespace */
76 if (uctrl->saddr.sun_path[0] == '@')
77 uctrl->saddr.sun_path[0] = '\0';
81 void udev_ctrl_unref(struct udev_ctrl *uctrl)
88 static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
90 struct udevd_ctrl_msg ctrl_msg;
93 memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
94 strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
98 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
100 ctrl_msg.intval = intval;
102 err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
104 err("error sending message: %s\n", strerror(errno));
109 int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
111 ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
115 int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
117 ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
121 int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
123 ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
127 int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
129 ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
133 int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
135 ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
139 int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
141 ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
145 int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
147 ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
151 int udevcontrol(int argc, char *argv[])
153 struct udev_ctrl *uctrl;
157 /* compat values with '_' will be removed in a future release */
158 static const struct option options[] = {
159 { "log-priority", 1, NULL, 'l' },
160 { "log_priority", 1, NULL, 'l' + 256 },
161 { "stop-exec-queue", 0, NULL, 's' },
162 { "stop_exec_queue", 0, NULL, 's' + 256 },
163 { "start-exec-queue", 0, NULL, 'S' },
164 { "start_exec_queue", 0, NULL, 'S' + 256},
165 { "reload-rules", 0, NULL, 'R' },
166 { "reload_rules", 0, NULL, 'R' + 256},
167 { "env", 1, NULL, 'e' },
168 { "max-childs", 1, NULL, 'm' },
169 { "max_childs", 1, NULL, 'm' + 256},
170 { "max-childs-running", 1, NULL, 'M' },
171 { "max_childs_running", 1, NULL, 'M' + 256},
172 { "help", 0, NULL, 'h' },
176 env = getenv("UDEV_LOG");
178 udev_log = log_priority(env);
180 logging_init("udevcontrol");
181 dbg("version %s\n", VERSION);
184 fprintf(stderr, "root privileges required\n");
188 uctrl = udev_ctrl_new_from_socket(UDEVD_CTRL_SOCK_PATH);
197 option = getopt_long(argc, argv, "l:sSRe:m:M:h", options, NULL);
202 info("udevadm control expects commands without underscore, "
203 "this will stop working in a future release\n");
204 fprintf(stderr, "udevadm control expects commands without underscore, "
205 "this will stop working in a future release\n");
211 i = log_priority(optarg);
213 fprintf(stderr, "invalid number '%s'\n", optarg);
216 udev_ctrl_set_log_level(uctrl, log_priority(optarg));
220 udev_ctrl_stop_exec_queue(uctrl);
224 udev_ctrl_start_exec_queue(uctrl);
228 udev_ctrl_reload_rules(uctrl);
231 if (strchr(optarg, '=') == NULL) {
232 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
235 udev_ctrl_set_env(uctrl, optarg);
239 i = strtoul(optarg, &endp, 0);
240 if (endp[0] != '\0' || i < 1) {
241 fprintf(stderr, "invalid number '%s'\n", optarg);
244 udev_ctrl_set_max_childs(uctrl, i);
248 i = strtoul(optarg, &endp, 0);
249 if (endp[0] != '\0' || i < 1) {
250 fprintf(stderr, "invalid number '%s'\n", optarg);
253 udev_ctrl_set_max_childs_running(uctrl, i);
257 printf("Usage: udevadm control COMMAND\n"
258 " --log-priority=<level> set the udev log level for the daemon\n"
259 " --stop-exec-queue keep udevd from executing events, queue only\n"
260 " --start-exec-queue execute events, flush queue\n"
261 " --reload-rules reloads the rules files\n"
262 " --env=<KEY>=<value> set a global environment variable\n"
263 " --max-childs=<N> maximum number of childs\n"
264 " --max-childs-running=<N> maximum number of childs running at the same time\n"
265 " --help print this help text\n\n");
272 /* compat stuff which will be removed in a future release */
273 if (argv[optind] != NULL) {
274 const char *arg = argv[optind];
276 fprintf(stderr, "udevadm control commands requires the --<command> format, "
277 "this will stop working in a future release\n");
278 err("udevadm control commands requires the --<command> format, "
279 "this will stop working in a future release\n");
281 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
282 udev_ctrl_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
283 } else if (!strcmp(arg, "stop_exec_queue")) {
284 udev_ctrl_stop_exec_queue(uctrl);
285 } else if (!strcmp(arg, "start_exec_queue")) {
286 udev_ctrl_start_exec_queue(uctrl);
287 } else if (!strcmp(arg, "reload_rules")) {
288 udev_ctrl_reload_rules(uctrl);
289 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
290 udev_ctrl_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
291 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
292 udev_ctrl_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
293 } else if (!strncmp(arg, "env", strlen("env"))) {
294 udev_ctrl_set_env(uctrl, &arg[strlen("env=")]);
296 fprintf(stderr, "unrecognized command '%s'\n", arg);
297 err("unrecognized command '%s'\n", arg);
301 udev_ctrl_unref(uctrl);