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