]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-control.c
udev: use usec_t for timeout in udev_ctrl_send_*()
[thirdparty/systemd.git] / src / udev / udevadm-control.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
3b47c739 2/*
55e9959b
KS
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
3b47c739 7 *
55e9959b
KS
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
3b47c739
KS
12 */
13
3b47c739 14#include <errno.h>
cf0fbc49
TA
15#include <getopt.h>
16#include <stddef.h>
3b47c739
KS
17#include <stdio.h>
18#include <stdlib.h>
3b47c739
KS
19#include <string.h>
20#include <unistd.h>
3b47c739 21
bb291b72 22#include "parse-util.h"
dccca82b 23#include "process-util.h"
46f0fbd8 24#include "syslog-util.h"
1f1a5e8b 25#include "time-util.h"
3d05193e 26#include "udevadm.h"
7d68eb1b
YW
27#include "udev-ctrl.h"
28#include "util.h"
c494b739 29#include "virt.h"
d59f11e1 30
bb291b72 31static int help(void) {
5639df9a 32 printf("%s control OPTION\n\n"
5ac0162c
LP
33 "Control the udev daemon.\n\n"
34 " -h --help Show this help\n"
5639df9a 35 " -V --version Show package version\n"
5ac0162c
LP
36 " -e --exit Instruct the daemon to cleanup and exit\n"
37 " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
38 " -s --stop-exec-queue Do not execute events, queue only\n"
39 " -S --start-exec-queue Execute events, flush queue\n"
40 " -R --reload Reload rules and databases\n"
41 " -p --property=KEY=VALUE Set a global property for all events\n"
42 " -m --children-max=N Maximum number of children\n"
a04a9027 43 " -t --timeout=SECONDS Maximum time to block for a reply\n"
5ac0162c 44 , program_invocation_short_name);
bb291b72
YW
45
46 return 0;
d59f11e1 47}
3b47c739 48
3d05193e 49int control_main(int argc, char *argv[], void *userdata) {
8e766630 50 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
3797776e 51 usec_t timeout = 60 * USEC_PER_SEC;
bb291b72 52 int c, r;
2b725651 53
912541b0 54 static const struct option options[] = {
7643ac9a
ZJS
55 { "exit", no_argument, NULL, 'e' },
56 { "log-priority", required_argument, NULL, 'l' },
57 { "stop-exec-queue", no_argument, NULL, 's' },
58 { "start-exec-queue", no_argument, NULL, 'S' },
59 { "reload", no_argument, NULL, 'R' },
60 { "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */
61 { "property", required_argument, NULL, 'p' },
62 { "env", required_argument, NULL, 'p' }, /* alias for -p */
63 { "children-max", required_argument, NULL, 'm' },
64 { "timeout", required_argument, NULL, 't' },
5639df9a 65 { "version", no_argument, NULL, 'V' },
7643ac9a 66 { "help", no_argument, NULL, 'h' },
912541b0
KS
67 {}
68 };
3b47c739 69
bb291b72
YW
70 r = must_be_root();
71 if (r < 0)
72 return r;
73
c494b739
YW
74 if (running_in_chroot() > 0) {
75 log_info("Running in chroot, ignoring request.");
76 return 0;
77 }
78
bb291b72 79 if (argc <= 1)
cf7f5013
YW
80 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
81 "This command expects one or more options.");
2b725651 82
2024ed61 83 uctrl = udev_ctrl_new();
bb291b72 84 if (!uctrl)
cf7f5013 85 return log_oom();
2b725651 86
5639df9a 87 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
7643ac9a 88 switch (c) {
912541b0 89 case 'e':
bb291b72
YW
90 r = udev_ctrl_send_exit(uctrl, timeout);
91 if (r < 0)
92 return r;
912541b0 93 break;
46f0fbd8
YW
94 case 'l':
95 r = log_level_from_string(optarg);
96 if (r < 0)
97 return log_error_errno(r, "Failed to parse log priority '%s': %m", optarg);
bb291b72 98
46f0fbd8 99 r = udev_ctrl_send_set_log_level(uctrl, r, timeout);
bb291b72
YW
100 if (r < 0)
101 return r;
912541b0 102 break;
912541b0 103 case 's':
bb291b72
YW
104 r = udev_ctrl_send_stop_exec_queue(uctrl, timeout);
105 if (r < 0)
106 return r;
912541b0
KS
107 break;
108 case 'S':
bb291b72
YW
109 r = udev_ctrl_send_start_exec_queue(uctrl, timeout);
110 if (r < 0)
111 return r;
912541b0
KS
112 break;
113 case 'R':
bb291b72
YW
114 r = udev_ctrl_send_reload(uctrl, timeout);
115 if (r < 0)
116 return r;
912541b0
KS
117 break;
118 case 'p':
47c8fcbe
YW
119 if (!strchr(optarg, '='))
120 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", optarg);
121
bb291b72
YW
122 r = udev_ctrl_send_set_env(uctrl, optarg, timeout);
123 if (r < 0)
124 return r;
912541b0
KS
125 break;
126 case 'm': {
bb291b72 127 unsigned i;
ff2c503d 128
bb291b72
YW
129 r = safe_atou(optarg, &i);
130 if (r < 0)
131 return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
132
133 r = udev_ctrl_send_set_children_max(uctrl, i, timeout);
134 if (r < 0)
135 return r;
912541b0
KS
136 break;
137 }
3797776e
YW
138 case 't':
139 r = parse_sec(optarg, &timeout);
1f1a5e8b 140 if (r < 0)
3797776e 141 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
912541b0 142 break;
5639df9a 143 case 'V':
51b006e1 144 return print_version();
912541b0 145 case 'h':
bb291b72
YW
146 return help();
147 case '?':
148 return -EINVAL;
149 default:
150 assert_not_reached("Unknown option.");
912541b0 151 }
5cc4112e 152
cf7f5013
YW
153 if (optind < argc)
154 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
155 "Extraneous argument: %s", argv[optind]);
bb291b72
YW
156
157 return 0;
3b47c739 158}