chiark / gitweb /
udevadmi: control = exit with rc=2 if there is some system error
[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                 rc = 2;
73                 goto exit;
74         }
75
76         while (1) {
77                 int option;
78                 int i;
79                 char *endp;
80
81                 option = getopt_long(argc, argv, "l:sSRp:m:M:h", options, NULL);
82                 if (option == -1)
83                         break;
84
85                 if (option > 255) {
86                         err(udev, "udevadm control expects commands without underscore, "
87                             "this will stop working in a future release\n");
88                 }
89
90                 switch (option) {
91                 case 'l':
92                 case 'l' + 256:
93                         i = util_log_priority(optarg);
94                         if (i < 0) {
95                                 fprintf(stderr, "invalid number '%s'\n", optarg);
96                                 goto exit;
97                         }
98                         if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg)) < 0)
99                                 rc = 2;
100                         else
101                                 rc = 0;
102                         break;
103                 case 's':
104                 case 's' + 256:
105                         if (udev_ctrl_send_stop_exec_queue(uctrl) < 0)
106                                 rc = 2;
107                         else
108                                 rc = 0;
109                         break;
110                 case 'S':
111                 case 'S' + 256:
112                         if (udev_ctrl_send_start_exec_queue(uctrl) < 0)
113                                 rc = 2;
114                         else
115                                 rc = 0;
116                         break;
117                 case 'R':
118                 case 'R' + 256:
119                         if (udev_ctrl_send_reload_rules(uctrl) < 0)
120                                 rc = 2;
121                         else
122                                 rc = 0;
123                         break;
124                 case 'p':
125                         if (strchr(optarg, '=') == NULL) {
126                                 fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg);
127                                 goto exit;
128                         }
129                         if (udev_ctrl_send_set_env(uctrl, optarg) < 0)
130                                 rc = 2;
131                         else
132                                 rc = 0;
133                         break;
134                 case 'm':
135                 case 'm' + 256:
136                         i = strtoul(optarg, &endp, 0);
137                         if (endp[0] != '\0' || i < 1) {
138                                 fprintf(stderr, "invalid number '%s'\n", optarg);
139                                 goto exit;
140                         }
141                         if (udev_ctrl_send_set_max_childs(uctrl, i) < 0)
142                                 rc = 2;
143                         else
144                                 rc = 0;
145                         break;
146                 case 'h':
147                         print_help();
148                         rc = 0;
149                         goto exit;
150                 default:
151                         goto exit;
152                 }
153         }
154
155         /* compat stuff which will be removed in a future release */
156         if (argv[optind] != NULL) {
157                 const char *arg = argv[optind];
158
159                 err(udev, "udevadm control commands requires the --<command> format, "
160                     "this will stop working in a future release\n");
161
162                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
163                         if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(&arg[strlen("log_priority=")])) < 0)
164                                 rc = 2;
165                         else
166                                 rc = 0;
167                         goto exit;
168                 } else if (!strcmp(arg, "stop_exec_queue")) {
169                         if (udev_ctrl_send_stop_exec_queue(uctrl) < 0)
170                                 rc = 2;
171                         else
172                                 rc = 0;
173                         goto exit;
174                 } else if (!strcmp(arg, "start_exec_queue")) {
175                         if (udev_ctrl_send_start_exec_queue(uctrl) < 0)
176                                 rc = 2;
177                         else
178                                 rc = 0;
179                         goto exit;
180                 } else if (!strcmp(arg, "reload_rules")) {
181                         if (udev_ctrl_send_reload_rules(uctrl) < 0)
182                                 rc = 2;
183                         else
184                                 rc = 0;
185                         goto exit;
186                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
187                         if (udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0)) < 0)
188                                 rc = 2;
189                         else
190                                 rc = 0;
191                         goto exit;
192                 } else if (!strncmp(arg, "env", strlen("env"))) {
193                         if (udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]) < 0)
194                                 rc = 2;
195                         else
196                                 rc = 0;
197                         goto exit;
198                 }
199         }
200
201         if (rc == 1)
202                 err(udev, "unrecognized command\n");
203 exit:
204         udev_ctrl_unref(uctrl);
205         return rc;
206 }