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