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