chiark / gitweb /
00a909fc3171ed1c1a0af14b84bef65aa20ed1d9
[elogind.git] / src / udev / udevadm-control.c
1 /*
2  * Copyright (C) 2005-2011 Kay Sievers <kay@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                 "  -e,--exit                 instruct the daemon to cleanup and exit\n"
34                 "  -l,--log-priority=LEVEL   set the udev log level for the daemon\n"
35                 "  -s,--stop-exec-queue      do not execute events, queue only\n"
36                 "  -S,--start-exec-queue     execute events, flush queue\n"
37                 "  -R,--reload               reload rules and databases\n"
38                 "  -p,--property=KEY=VALUE   set a global property for all events\n"
39                 "  -m,--children-max=N       maximum number of children\n"
40                 "     --timeout=SECONDS      maximum time to block for a reply\n"
41                 "  -h,--help                 print this help text\n\n");
42 }
43
44 static int adm_control(struct udev *udev, int argc, char *argv[])
45 {
46         struct udev_ctrl *uctrl = NULL;
47         int timeout = 60;
48         int rc = 1, c;
49
50         static const struct option options[] = {
51                 { "exit",             no_argument,       NULL, 'e' },
52                 { "log-priority",     required_argument, NULL, 'l' },
53                 { "stop-exec-queue",  no_argument,       NULL, 's' },
54                 { "start-exec-queue", no_argument,       NULL, 'S' },
55                 { "reload",           no_argument,       NULL, 'R' },
56                 { "reload-rules",     no_argument,       NULL, 'R' }, /* alias for -R */
57                 { "property",         required_argument, NULL, 'p' },
58                 { "env",              required_argument, NULL, 'p' }, /* alias for -p */
59                 { "children-max",     required_argument, NULL, 'm' },
60                 { "timeout",          required_argument, NULL, 't' },
61                 { "help",             no_argument,       NULL, 'h' },
62                 {}
63         };
64
65         if (getuid() != 0) {
66                 fprintf(stderr, "root privileges required\n");
67                 return 1;
68         }
69
70         uctrl = udev_ctrl_new(udev);
71         if (uctrl == NULL)
72                 return 2;
73
74         while ((c = getopt_long(argc, argv, "el:sSRp:m:h", options, NULL)) >= 0)
75                 switch (c) {
76                 case 'e':
77                         if (udev_ctrl_send_exit(uctrl, timeout) < 0)
78                                 rc = 2;
79                         else
80                                 rc = 0;
81                         break;
82                 case 'l': {
83                         int i;
84
85                         i = util_log_priority(optarg);
86                         if (i < 0) {
87                                 fprintf(stderr, "invalid number '%s'\n", optarg);
88                                 goto out;
89                         }
90                         if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0)
91                                 rc = 2;
92                         else
93                                 rc = 0;
94                         break;
95                 }
96                 case 's':
97                         if (udev_ctrl_send_stop_exec_queue(uctrl, timeout) < 0)
98                                 rc = 2;
99                         else
100                                 rc = 0;
101                         break;
102                 case 'S':
103                         if (udev_ctrl_send_start_exec_queue(uctrl, timeout) < 0)
104                                 rc = 2;
105                         else
106                                 rc = 0;
107                         break;
108                 case 'R':
109                         if (udev_ctrl_send_reload(uctrl, timeout) < 0)
110                                 rc = 2;
111                         else
112                                 rc = 0;
113                         break;
114                 case 'p':
115                         if (strchr(optarg, '=') == NULL) {
116                                 fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg);
117                                 goto out;
118                         }
119                         if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0)
120                                 rc = 2;
121                         else
122                                 rc = 0;
123                         break;
124                 case 'm': {
125                         char *endp;
126                         int i;
127
128                         i = strtoul(optarg, &endp, 0);
129                         if (endp[0] != '\0' || i < 1) {
130                                 fprintf(stderr, "invalid number '%s'\n", optarg);
131                                 goto out;
132                         }
133                         if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0)
134                                 rc = 2;
135                         else
136                                 rc = 0;
137                         break;
138                 }
139                 case 't': {
140                         int seconds;
141
142                         seconds = atoi(optarg);
143                         if (seconds >= 0)
144                                 timeout = seconds;
145                         else
146                                 fprintf(stderr, "invalid timeout value\n");
147                         break;
148                 }
149                 case 'h':
150                         print_help();
151                         rc = 0;
152                         break;
153                 }
154
155         if (optind < argc)
156                 fprintf(stderr, "Extraneous argument: %s\n", argv[optind]);
157         else if (optind == 1)
158                 fprintf(stderr, "Option missing\n");
159 out:
160         udev_ctrl_unref(uctrl);
161         return rc;
162 }
163
164 const struct udevadm_cmd udevadm_control = {
165         .name = "control",
166         .cmd = adm_control,
167         .help = "control the udev daemon",
168 };