]>
Commit | Line | Data |
---|---|---|
3b47c739 | 1 | /* |
1298001e | 2 | * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org> |
3b47c739 | 3 | * |
55e9959b KS |
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. | |
3b47c739 | 8 | * |
55e9959b KS |
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. | |
3b47c739 KS |
13 | */ |
14 | ||
3b47c739 | 15 | #include <errno.h> |
cf0fbc49 TA |
16 | #include <getopt.h> |
17 | #include <stddef.h> | |
3b47c739 KS |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
3b47c739 KS |
20 | #include <string.h> |
21 | #include <unistd.h> | |
3b47c739 | 22 | |
44433ebd | 23 | #include "udev-util.h" |
cf0fbc49 | 24 | #include "udev.h" |
d59f11e1 | 25 | |
9ec6e95b | 26 | static void print_help(void) { |
5ac0162c LP |
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); | |
d59f11e1 | 40 | } |
3b47c739 | 41 | |
9ec6e95b | 42 | static int adm_control(struct udev *udev, int argc, char *argv[]) { |
44433ebd | 43 | _cleanup_udev_ctrl_unref_ struct udev_ctrl *uctrl = NULL; |
912541b0 | 44 | int timeout = 60; |
7643ac9a | 45 | int rc = 1, c; |
2b725651 | 46 | |
912541b0 | 47 | static const struct option options[] = { |
7643ac9a ZJS |
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' }, | |
912541b0 KS |
59 | {} |
60 | }; | |
3b47c739 | 61 | |
912541b0 KS |
62 | if (getuid() != 0) { |
63 | fprintf(stderr, "root privileges required\n"); | |
64 | return 1; | |
65 | } | |
2b725651 | 66 | |
912541b0 KS |
67 | uctrl = udev_ctrl_new(udev); |
68 | if (uctrl == NULL) | |
69 | return 2; | |
2b725651 | 70 | |
7643ac9a ZJS |
71 | while ((c = getopt_long(argc, argv, "el:sSRp:m:h", options, NULL)) >= 0) |
72 | switch (c) { | |
912541b0 KS |
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; | |
ff2c503d | 81 | |
912541b0 KS |
82 | i = util_log_priority(optarg); |
83 | if (i < 0) { | |
84 | fprintf(stderr, "invalid number '%s'\n", optarg); | |
44433ebd | 85 | return rc; |
912541b0 KS |
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); | |
44433ebd | 114 | return rc; |
912541b0 KS |
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; | |
ff2c503d | 124 | |
912541b0 KS |
125 | i = strtoul(optarg, &endp, 0); |
126 | if (endp[0] != '\0' || i < 1) { | |
127 | fprintf(stderr, "invalid number '%s'\n", optarg); | |
44433ebd | 128 | return rc; |
912541b0 KS |
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; | |
ff2c503d | 138 | |
912541b0 KS |
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 | } | |
5cc4112e | 151 | |
7643ac9a ZJS |
152 | if (optind < argc) |
153 | fprintf(stderr, "Extraneous argument: %s\n", argv[optind]); | |
912541b0 | 154 | else if (optind == 1) |
7643ac9a | 155 | fprintf(stderr, "Option missing\n"); |
912541b0 | 156 | return rc; |
3b47c739 | 157 | } |
1985c76e KS |
158 | |
159 | const struct udevadm_cmd udevadm_control = { | |
912541b0 KS |
160 | .name = "control", |
161 | .cmd = adm_control, | |
5ac0162c | 162 | .help = "Control the udev daemon", |
1985c76e | 163 | }; |