chiark / gitweb /
autogen.sh: add debug
[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 static int udev_log = 0;
38
39 int udevadm_control(int argc, char *argv[])
40 {
41         struct udev_ctrl *uctrl;
42         const char *env;
43         int rc = 1;
44
45         /* compat values with '_' will be removed in a future release */
46         static const struct option options[] = {
47                 { "log-priority", 1, NULL, 'l' },
48                 { "log_priority", 1, NULL, 'l' + 256 },
49                 { "stop-exec-queue", 0, NULL, 's' },
50                 { "stop_exec_queue", 0, NULL, 's' + 256 },
51                 { "start-exec-queue", 0, NULL, 'S' },
52                 { "start_exec_queue", 0, NULL, 'S' + 256},
53                 { "reload-rules", 0, NULL, 'R' },
54                 { "reload_rules", 0, NULL, 'R' + 256},
55                 { "env", 1, NULL, 'e' },
56                 { "max-childs", 1, NULL, 'm' },
57                 { "max_childs", 1, NULL, 'm' + 256},
58                 { "max-childs-running", 1, NULL, 'M' },
59                 { "max_childs_running", 1, NULL, 'M' + 256},
60                 { "help", 0, NULL, 'h' },
61                 {}
62         };
63
64         env = getenv("UDEV_LOG");
65         if (env)
66                 udev_log = log_priority(env);
67
68         logging_init("udevcontrol");
69         dbg("version %s\n", VERSION);
70
71         if (getuid() != 0) {
72                 fprintf(stderr, "root privileges required\n");
73                 goto exit;
74         }
75
76         uctrl = udev_ctrl_new_from_socket(UDEVD_CTRL_SOCK_PATH);
77         if (uctrl == NULL)
78                 goto exit;
79
80         while (1) {
81                 int option;
82                 int i;
83                 char *endp;
84
85                 option = getopt_long(argc, argv, "l:sSRe:m:M:h", options, NULL);
86                 if (option == -1)
87                         break;
88
89                 if (option > 255) {
90                         info("udevadm control expects commands without underscore, "
91                             "this will stop working in a future release\n");
92                         fprintf(stderr, "udevadm control expects commands without underscore, "
93                                 "this will stop working in a future release\n");
94                 }
95
96                 switch (option) {
97                 case 'l':
98                 case 'l' + 256:
99                         i = log_priority(optarg);
100                         if (i < 0) {
101                                 fprintf(stderr, "invalid number '%s'\n", optarg);
102                                 goto exit;
103                         }
104                         udev_ctrl_set_log_level(uctrl, log_priority(optarg));
105                         break;
106                 case 's':
107                 case 's' + 256:
108                         udev_ctrl_stop_exec_queue(uctrl);
109                         break;
110                 case 'S':
111                 case 'S' + 256:
112                         udev_ctrl_start_exec_queue(uctrl);
113                         break;
114                 case 'R':
115                 case 'R' + 256:
116                         udev_ctrl_reload_rules(uctrl);
117                         break;
118                 case 'e':
119                         if (strchr(optarg, '=') == NULL) {
120                                 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
121                                 goto exit;
122                         }
123                         udev_ctrl_set_env(uctrl, optarg);
124                         break;
125                 case 'm':
126                 case 'm' + 256:
127                         i = strtoul(optarg, &endp, 0);
128                         if (endp[0] != '\0' || i < 1) {
129                                 fprintf(stderr, "invalid number '%s'\n", optarg);
130                                 goto exit;
131                         }
132                         udev_ctrl_set_max_childs(uctrl, i);
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                         udev_ctrl_set_max_childs_running(uctrl, i);
142                         break;
143                         break;
144                 case 'h':
145                         printf("Usage: udevadm control COMMAND\n"
146                                 "  --log-priority=<level>   set the udev log level for the daemon\n"
147                                 "  --stop-exec-queue        keep udevd from executing events, queue only\n"
148                                 "  --start-exec-queue       execute events, flush queue\n"
149                                 "  --reload-rules           reloads the rules files\n"
150                                 "  --env=<KEY>=<value>      set a global environment variable\n"
151                                 "  --max-childs=<N>         maximum number of childs\n"
152                                 "  --max-childs-running=<N> maximum number of childs running at the same time\n"
153                                 "  --help                   print this help text\n\n");
154                         goto exit;
155                 default:
156                         goto exit;
157                 }
158         }
159
160         /* compat stuff which will be removed in a future release */
161         if (argv[optind] != NULL) {
162                 const char *arg = argv[optind];
163
164                 fprintf(stderr, "udevadm control commands requires the --<command> format, "
165                         "this will stop working in a future release\n");
166                 err("udevadm control commands requires the --<command> format, "
167                     "this will stop working in a future release\n");
168
169                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
170                         udev_ctrl_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
171                 } else if (!strcmp(arg, "stop_exec_queue")) {
172                         udev_ctrl_stop_exec_queue(uctrl);
173                 } else if (!strcmp(arg, "start_exec_queue")) {
174                         udev_ctrl_start_exec_queue(uctrl);
175                 } else if (!strcmp(arg, "reload_rules")) {
176                         udev_ctrl_reload_rules(uctrl);
177                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
178                         udev_ctrl_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
179                 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
180                         udev_ctrl_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
181                 } else if (!strncmp(arg, "env", strlen("env"))) {
182                         udev_ctrl_set_env(uctrl, &arg[strlen("env=")]);
183                 } else {
184                         fprintf(stderr, "unrecognized command '%s'\n", arg);
185                         err("unrecognized command '%s'\n", arg);
186                 }
187         }
188 exit:
189         udev_ctrl_unref(uctrl);
190         logging_close();
191         return rc;
192 }