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