chiark / gitweb /
42818d9db74e4fcac941605dbb93ecc5f2b1ebd6
[elogind.git] / udev / udevadm-control.c
1 /*
2  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
8  *
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.
13  */
14
15 #include <time.h>
16 #include <errno.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/wait.h>
26 #include <sys/un.h>
27
28 #include "udev.h"
29
30 static void print_help(void)
31 {
32         printf("Usage: udevadm control COMMAND\n"
33                 "  --log-priority=<level>   set the udev log level for the daemon\n"
34                 "  --stop-exec-queue        keep udevd from executing events, queue only\n"
35                 "  --start-exec-queue       execute events, flush queue\n"
36                 "  --reload-rules           reloads the rules files\n"
37                 "  --property=<KEY>=<value> set a global property for all events\n"
38                 "  --max-childs=<N>         maximum number of childs\n"
39                 "  --help                   print this help text\n\n");
40 }
41
42 int udevadm_control(struct udev *udev, int argc, char *argv[])
43 {
44         struct udev_ctrl *uctrl = NULL;
45         int rc = 1;
46
47         /* compat values with '_' will be removed in a future release */
48         static const struct option options[] = {
49                 { "log-priority", required_argument, NULL, 'l' },
50                 { "log_priority", required_argument, NULL, 'l' + 256 },
51                 { "stop-exec-queue", no_argument, NULL, 's' },
52                 { "stop_exec_queue", no_argument, NULL, 's' + 256 },
53                 { "start-exec-queue", no_argument, NULL, 'S' },
54                 { "start_exec_queue", no_argument, NULL, 'S' + 256},
55                 { "reload-rules", no_argument, NULL, 'R' },
56                 { "reload_rules", no_argument, NULL, 'R' + 256},
57                 { "property", required_argument, NULL, 'p' },
58                 { "env", required_argument, NULL, 'p' },
59                 { "max-childs", required_argument, NULL, 'm' },
60                 { "max_childs", required_argument, NULL, 'm' + 256},
61                 { "help", no_argument, NULL, 'h' },
62                 {}
63         };
64
65         if (getuid() != 0) {
66                 fprintf(stderr, "root privileges required\n");
67                 goto exit;
68         }
69
70         uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
71         if (uctrl == NULL)
72                 goto exit;
73
74         while (1) {
75                 int option;
76                 int i;
77                 char *endp;
78
79                 option = getopt_long(argc, argv, "l:sSRp:m:M:h", options, NULL);
80                 if (option == -1)
81                         break;
82
83                 if (option > 255) {
84                         err(udev, "udevadm control expects commands without underscore, "
85                             "this will stop working in a future release\n");
86                 }
87
88                 switch (option) {
89                 case 'l':
90                 case 'l' + 256:
91                         i = util_log_priority(optarg);
92                         if (i < 0) {
93                                 fprintf(stderr, "invalid number '%s'\n", optarg);
94                                 goto exit;
95                         }
96                         udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg));
97                         rc = 0;
98                         break;
99                 case 's':
100                 case 's' + 256:
101                         udev_ctrl_send_stop_exec_queue(uctrl);
102                         rc = 0;
103                         break;
104                 case 'S':
105                 case 'S' + 256:
106                         udev_ctrl_send_start_exec_queue(uctrl);
107                         rc = 0;
108                         break;
109                 case 'R':
110                 case 'R' + 256:
111                         udev_ctrl_send_reload_rules(uctrl);
112                         rc = 0;
113                         break;
114                 case 'p':
115                         if (strchr(optarg, '=') == NULL) {
116                                 fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg);
117                                 goto exit;
118                         }
119                         udev_ctrl_send_set_env(uctrl, optarg);
120                         rc = 0;
121                         break;
122                 case 'm':
123                 case 'm' + 256:
124                         i = strtoul(optarg, &endp, 0);
125                         if (endp[0] != '\0' || i < 1) {
126                                 fprintf(stderr, "invalid number '%s'\n", optarg);
127                                 goto exit;
128                         }
129                         udev_ctrl_send_set_max_childs(uctrl, i);
130                         rc = 0;
131                         break;
132                 case 'h':
133                         print_help();
134                         rc = 0;
135                         goto exit;
136                 default:
137                         goto exit;
138                 }
139         }
140
141         /* compat stuff which will be removed in a future release */
142         if (argv[optind] != NULL) {
143                 const char *arg = argv[optind];
144
145                 err(udev, "udevadm control commands requires the --<command> format, "
146                     "this will stop working in a future release\n");
147
148                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
149                         udev_ctrl_send_set_log_level(uctrl, util_log_priority(&arg[strlen("log_priority=")]));
150                         rc = 0;
151                         goto exit;
152                 } else if (!strcmp(arg, "stop_exec_queue")) {
153                         udev_ctrl_send_stop_exec_queue(uctrl);
154                         rc = 0;
155                         goto exit;
156                 } else if (!strcmp(arg, "start_exec_queue")) {
157                         udev_ctrl_send_start_exec_queue(uctrl);
158                         rc = 0;
159                         goto exit;
160                 } else if (!strcmp(arg, "reload_rules")) {
161                         udev_ctrl_send_reload_rules(uctrl);
162                         rc = 0;
163                         goto exit;
164                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
165                         udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
166                         rc = 0;
167                         goto exit;
168                 } else if (!strncmp(arg, "env", strlen("env"))) {
169                         udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
170                         rc = 0;
171                         goto exit;
172                 }
173         }
174
175         if (rc != 0) {
176                 err(udev, "unrecognized command\n");
177         }
178 exit:
179         udev_ctrl_unref(uctrl);
180         return rc;
181 }