]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-control.c
tree-wide: drop redundant _cleanup_ macros (#8810)
[thirdparty/systemd.git] / src / udev / udevadm-control.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
3b47c739 2/*
1298001e 3 * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org>
3b47c739 4 *
55e9959b
KS
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
3b47c739 9 *
55e9959b
KS
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
3b47c739
KS
14 */
15
3b47c739 16#include <errno.h>
cf0fbc49
TA
17#include <getopt.h>
18#include <stddef.h>
3b47c739
KS
19#include <stdio.h>
20#include <stdlib.h>
3b47c739
KS
21#include <string.h>
22#include <unistd.h>
3b47c739 23
dccca82b 24#include "process-util.h"
1f1a5e8b 25#include "time-util.h"
44433ebd 26#include "udev-util.h"
cf0fbc49 27#include "udev.h"
5639df9a 28#include "udevadm-util.h"
d59f11e1 29
9ec6e95b 30static void print_help(void) {
5639df9a 31 printf("%s control OPTION\n\n"
5ac0162c
LP
32 "Control the udev daemon.\n\n"
33 " -h --help Show this help\n"
5639df9a 34 " -V --version Show package version\n"
5ac0162c
LP
35 " -e --exit Instruct the daemon to cleanup and exit\n"
36 " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
37 " -s --stop-exec-queue Do not execute events, queue only\n"
38 " -S --start-exec-queue Execute events, flush queue\n"
39 " -R --reload Reload rules and databases\n"
40 " -p --property=KEY=VALUE Set a global property for all events\n"
41 " -m --children-max=N Maximum number of children\n"
a04a9027 42 " -t --timeout=SECONDS Maximum time to block for a reply\n"
5ac0162c 43 , program_invocation_short_name);
d59f11e1 44}
3b47c739 45
9ec6e95b 46static int adm_control(struct udev *udev, int argc, char *argv[]) {
8e766630 47 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
912541b0 48 int timeout = 60;
7643ac9a 49 int rc = 1, c;
2b725651 50
912541b0 51 static const struct option options[] = {
7643ac9a
ZJS
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' },
5639df9a 62 { "version", no_argument, NULL, 'V' },
7643ac9a 63 { "help", no_argument, NULL, 'h' },
912541b0
KS
64 {}
65 };
3b47c739 66
fba868fa 67 if (must_be_root() < 0)
912541b0 68 return 1;
2b725651 69
912541b0
KS
70 uctrl = udev_ctrl_new(udev);
71 if (uctrl == NULL)
72 return 2;
2b725651 73
5639df9a 74 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
7643ac9a 75 switch (c) {
912541b0
KS
76 case 'e':
77 if (udev_ctrl_send_exit(uctrl, timeout) < 0)
78 rc = 2;
79 else
80 rc = 0;
81 break;
82 case 'l': {
83 int i;
ff2c503d 84
912541b0
KS
85 i = util_log_priority(optarg);
86 if (i < 0) {
1f1a5e8b 87 log_error("invalid number '%s'", optarg);
44433ebd 88 return rc;
912541b0
KS
89 }
90 if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0)
91 rc = 2;
92 else
93 rc = 0;
94 break;
95 }
96 case 's':
97 if (udev_ctrl_send_stop_exec_queue(uctrl, timeout) < 0)
98 rc = 2;
99 else
100 rc = 0;
101 break;
102 case 'S':
103 if (udev_ctrl_send_start_exec_queue(uctrl, timeout) < 0)
104 rc = 2;
105 else
106 rc = 0;
107 break;
108 case 'R':
109 if (udev_ctrl_send_reload(uctrl, timeout) < 0)
110 rc = 2;
111 else
112 rc = 0;
113 break;
114 case 'p':
115 if (strchr(optarg, '=') == NULL) {
1f1a5e8b 116 log_error("expect <KEY>=<value> instead of '%s'", optarg);
44433ebd 117 return rc;
912541b0
KS
118 }
119 if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0)
120 rc = 2;
121 else
122 rc = 0;
123 break;
124 case 'm': {
125 char *endp;
126 int i;
ff2c503d 127
912541b0
KS
128 i = strtoul(optarg, &endp, 0);
129 if (endp[0] != '\0' || i < 1) {
1f1a5e8b 130 log_error("invalid number '%s'", optarg);
44433ebd 131 return rc;
912541b0
KS
132 }
133 if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0)
134 rc = 2;
135 else
136 rc = 0;
137 break;
138 }
139 case 't': {
be6b0c21 140 int r, seconds;
1f1a5e8b 141 usec_t s;
ff2c503d 142
1f1a5e8b
SS
143 r = parse_sec(optarg, &s);
144 if (r < 0)
145 return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg);
146
be6b0c21 147 if (DIV_ROUND_UP(s, USEC_PER_SEC) > INT_MAX)
1f1a5e8b
SS
148 log_error("Timeout value is out of range.");
149 else {
be6b0c21 150 seconds = s != USEC_INFINITY ? (int) DIV_ROUND_UP(s, USEC_PER_SEC) : INT_MAX;
912541b0 151 timeout = seconds;
1f1a5e8b
SS
152 rc = 0;
153 }
912541b0
KS
154 break;
155 }
5639df9a
YW
156 case 'V':
157 print_version();
158 rc = 0;
159 break;
912541b0
KS
160 case 'h':
161 print_help();
162 rc = 0;
163 break;
164 }
5cc4112e 165
7643ac9a 166 if (optind < argc)
1f1a5e8b 167 log_error("Extraneous argument: %s", argv[optind]);
912541b0 168 else if (optind == 1)
1f1a5e8b 169 log_error("Option missing");
912541b0 170 return rc;
3b47c739 171}
1985c76e
KS
172
173const struct udevadm_cmd udevadm_control = {
912541b0
KS
174 .name = "control",
175 .cmd = adm_control,
5ac0162c 176 .help = "Control the udev daemon",
1985c76e 177};