chiark / gitweb /
udevd: print error if worker dies unexpectedly
[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                         fprintf(stderr, "udevadm control expects commands without underscore, "
85                                 "this will stop working in a future release\n");
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                         udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg));
99                         rc = 0;
100                         break;
101                 case 's':
102                 case 's' + 256:
103                         udev_ctrl_send_stop_exec_queue(uctrl);
104                         rc = 0;
105                         break;
106                 case 'S':
107                 case 'S' + 256:
108                         udev_ctrl_send_start_exec_queue(uctrl);
109                         rc = 0;
110                         break;
111                 case 'R':
112                 case 'R' + 256:
113                         udev_ctrl_send_reload_rules(uctrl);
114                         rc = 0;
115                         break;
116                 case 'p':
117                         if (strchr(optarg, '=') == NULL) {
118                                 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
119                                 goto exit;
120                         }
121                         udev_ctrl_send_set_env(uctrl, optarg);
122                         rc = 0;
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_send_set_max_childs(uctrl, i);
132                         rc = 0;
133                         break;
134                 case 'h':
135                         print_help();
136                         rc = 0;
137                         goto exit;
138                 default:
139                         goto exit;
140                 }
141         }
142
143         /* compat stuff which will be removed in a future release */
144         if (argv[optind] != NULL) {
145                 const char *arg = argv[optind];
146
147                 fprintf(stderr, "udevadm control commands requires the --<command> format, "
148                         "this will stop working in a future release\n");
149                 err(udev, "udevadm control commands requires the --<command> format, "
150                     "this will stop working in a future release\n");
151
152                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
153                         udev_ctrl_send_set_log_level(uctrl, util_log_priority(&arg[strlen("log_priority=")]));
154                         rc = 0;
155                         goto exit;
156                 } else if (!strcmp(arg, "stop_exec_queue")) {
157                         udev_ctrl_send_stop_exec_queue(uctrl);
158                         rc = 0;
159                         goto exit;
160                 } else if (!strcmp(arg, "start_exec_queue")) {
161                         udev_ctrl_send_start_exec_queue(uctrl);
162                         rc = 0;
163                         goto exit;
164                 } else if (!strcmp(arg, "reload_rules")) {
165                         udev_ctrl_send_reload_rules(uctrl);
166                         rc = 0;
167                         goto exit;
168                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
169                         udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
170                         rc = 0;
171                         goto exit;
172                 } else if (!strncmp(arg, "env", strlen("env"))) {
173                         udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
174                         rc = 0;
175                         goto exit;
176                 }
177         }
178
179         if (rc != 0) {
180                 fprintf(stderr, "unrecognized command\n");
181                 err(udev, "unrecognized command\n");
182         }
183 exit:
184         udev_ctrl_unref(uctrl);
185         return rc;
186 }