chiark / gitweb /
udevadm: rename source files
[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 struct udev_ctrl;
40 extern struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path);
41 extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
42 extern int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority);
43 extern int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl);
44 extern int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl);
45 extern int udev_ctrl_reload_rules(struct udev_ctrl *uctrl);
46 extern int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key);
47 extern int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count);
48 extern int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count);
49
50 struct udev_ctrl {
51         int sock;
52         struct sockaddr_un saddr;
53         socklen_t addrlen;
54 };
55
56 struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
57 {
58         struct udev_ctrl *uctrl;
59
60         uctrl = malloc(sizeof(struct udev_ctrl));
61         if (uctrl == NULL)
62                 return NULL;
63         memset(uctrl, 0x00, sizeof(struct udev_ctrl));
64
65         uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
66         if (uctrl->sock < 0) {
67                 err("error getting socket: %s\n", strerror(errno));
68                 free(uctrl);
69                 return NULL;
70         }
71
72         uctrl->saddr.sun_family = AF_LOCAL;
73         strcpy(uctrl->saddr.sun_path, socket_path);
74         uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
75         /* translate leading '@' to abstract namespace */
76         if (uctrl->saddr.sun_path[0] == '@')
77                 uctrl->saddr.sun_path[0] = '\0';
78         return uctrl;
79 }
80
81 void udev_ctrl_unref(struct udev_ctrl *uctrl)
82 {
83         if (uctrl == NULL)
84                 return;
85         close(uctrl->sock);
86 }
87
88 static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
89 {
90         struct udevd_ctrl_msg ctrl_msg;
91         int err;
92
93         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
94         strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
95         ctrl_msg.type = type;
96
97         if (buf != NULL)
98                 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
99         else
100                 ctrl_msg.intval = intval;
101
102         err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
103         if (err == -1) {
104                 err("error sending message: %s\n", strerror(errno));
105         }
106         return err;
107 }
108
109 int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
110 {
111         ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
112         return 0;
113 }
114
115 int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
116 {
117         ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
118         return 0;
119 }
120
121 int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
122 {
123         ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
124         return 0;
125 }
126
127 int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
128 {
129         ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
130         return 0;
131 }
132
133 int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
134 {
135         ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
136         return 0;
137 }
138
139 int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
140 {
141         ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
142         return 0;
143 }
144
145 int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
146 {
147         ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
148         return 0;
149 }
150
151 int udevcontrol(int argc, char *argv[])
152 {
153         struct udev_ctrl *uctrl;
154         const char *env;
155         int rc = 1;
156
157         /* compat values with '_' will be removed in a future release */
158         static const struct option options[] = {
159                 { "log-priority", 1, NULL, 'l' },
160                 { "log_priority", 1, NULL, 'l' + 256 },
161                 { "stop-exec-queue", 0, NULL, 's' },
162                 { "stop_exec_queue", 0, NULL, 's' + 256 },
163                 { "start-exec-queue", 0, NULL, 'S' },
164                 { "start_exec_queue", 0, NULL, 'S' + 256},
165                 { "reload-rules", 0, NULL, 'R' },
166                 { "reload_rules", 0, NULL, 'R' + 256},
167                 { "env", 1, NULL, 'e' },
168                 { "max-childs", 1, NULL, 'm' },
169                 { "max_childs", 1, NULL, 'm' + 256},
170                 { "max-childs-running", 1, NULL, 'M' },
171                 { "max_childs_running", 1, NULL, 'M' + 256},
172                 { "help", 0, NULL, 'h' },
173                 {}
174         };
175
176         env = getenv("UDEV_LOG");
177         if (env)
178                 udev_log = log_priority(env);
179
180         logging_init("udevcontrol");
181         dbg("version %s\n", VERSION);
182
183         if (getuid() != 0) {
184                 fprintf(stderr, "root privileges required\n");
185                 goto exit;
186         }
187
188         uctrl = udev_ctrl_new_from_socket(UDEVD_CTRL_SOCK_PATH);
189         if (uctrl == NULL)
190                 goto exit;
191
192         while (1) {
193                 int option;
194                 int i;
195                 char *endp;
196
197                 option = getopt_long(argc, argv, "l:sSRe:m:M:h", options, NULL);
198                 if (option == -1)
199                         break;
200
201                 if (option > 255) {
202                         info("udevadm control expects commands without underscore, "
203                             "this will stop working in a future release\n");
204                         fprintf(stderr, "udevadm control expects commands without underscore, "
205                                 "this will stop working in a future release\n");
206                 }
207
208                 switch (option) {
209                 case 'l':
210                 case 'l' + 256:
211                         i = log_priority(optarg);
212                         if (i < 0) {
213                                 fprintf(stderr, "invalid number '%s'\n", optarg);
214                                 goto exit;
215                         }
216                         udev_ctrl_set_log_level(uctrl, log_priority(optarg));
217                         break;
218                 case 's':
219                 case 's' + 256:
220                         udev_ctrl_stop_exec_queue(uctrl);
221                         break;
222                 case 'S':
223                 case 'S' + 256:
224                         udev_ctrl_start_exec_queue(uctrl);
225                         break;
226                 case 'R':
227                 case 'R' + 256:
228                         udev_ctrl_reload_rules(uctrl);
229                         break;
230                 case 'e':
231                         if (strchr(optarg, '=') == NULL) {
232                                 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
233                                 goto exit;
234                         }
235                         udev_ctrl_set_env(uctrl, optarg);
236                         break;
237                 case 'm':
238                 case 'm' + 256:
239                         i = strtoul(optarg, &endp, 0);
240                         if (endp[0] != '\0' || i < 1) {
241                                 fprintf(stderr, "invalid number '%s'\n", optarg);
242                                 goto exit;
243                         }
244                         udev_ctrl_set_max_childs(uctrl, i);
245                         break;
246                 case 'M':
247                 case 'M' + 256:
248                         i = strtoul(optarg, &endp, 0);
249                         if (endp[0] != '\0' || i < 1) {
250                                 fprintf(stderr, "invalid number '%s'\n", optarg);
251                                 goto exit;
252                         }
253                         udev_ctrl_set_max_childs_running(uctrl, i);
254                         break;
255                         break;
256                 case 'h':
257                         printf("Usage: udevadm control COMMAND\n"
258                                 "  --log-priority=<level>   set the udev log level for the daemon\n"
259                                 "  --stop-exec-queue        keep udevd from executing events, queue only\n"
260                                 "  --start-exec-queue       execute events, flush queue\n"
261                                 "  --reload-rules           reloads the rules files\n"
262                                 "  --env=<KEY>=<value>      set a global environment variable\n"
263                                 "  --max-childs=<N>         maximum number of childs\n"
264                                 "  --max-childs-running=<N> maximum number of childs running at the same time\n"
265                                 "  --help                   print this help text\n\n");
266                         goto exit;
267                 default:
268                         goto exit;
269                 }
270         }
271
272         /* compat stuff which will be removed in a future release */
273         if (argv[optind] != NULL) {
274                 const char *arg = argv[optind];
275
276                 fprintf(stderr, "udevadm control commands requires the --<command> format, "
277                         "this will stop working in a future release\n");
278                 err("udevadm control commands requires the --<command> format, "
279                     "this will stop working in a future release\n");
280
281                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
282                         udev_ctrl_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
283                 } else if (!strcmp(arg, "stop_exec_queue")) {
284                         udev_ctrl_stop_exec_queue(uctrl);
285                 } else if (!strcmp(arg, "start_exec_queue")) {
286                         udev_ctrl_start_exec_queue(uctrl);
287                 } else if (!strcmp(arg, "reload_rules")) {
288                         udev_ctrl_reload_rules(uctrl);
289                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
290                         udev_ctrl_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
291                 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
292                         udev_ctrl_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
293                 } else if (!strncmp(arg, "env", strlen("env"))) {
294                         udev_ctrl_set_env(uctrl, &arg[strlen("env=")]);
295                 } else {
296                         fprintf(stderr, "unrecognized command '%s'\n", arg);
297                         err("unrecognized command '%s'\n", arg);
298                 }
299         }
300 exit:
301         udev_ctrl_unref(uctrl);
302         logging_close();
303         return rc;
304 }