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