]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-control.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / udev / udevadm-control.c
CommitLineData
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
KS
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>
2b725651 22#include <getopt.h>
9571122e
KS
23#include <sys/types.h>
24#include <sys/socket.h>
25#include <sys/wait.h>
26#include <sys/un.h>
3b47c739
KS
27
28#include "udev.h"
44433ebd 29#include "udev-util.h"
d59f11e1 30
9ec6e95b 31static void print_help(void) {
912541b0 32 printf("Usage: udevadm control COMMAND\n"
7643ac9a
ZJS
33 " -e,--exit instruct the daemon to cleanup and exit\n"
34 " -l,--log-priority=LEVEL set the udev log level for the daemon\n"
35 " -s,--stop-exec-queue do not execute events, queue only\n"
36 " -S,--start-exec-queue execute events, flush queue\n"
37 " -R,--reload reload rules and databases\n"
38 " -p,--property=KEY=VALUE set a global property for all events\n"
39 " -m,--children-max=N maximum number of children\n"
40 " --timeout=SECONDS maximum time to block for a reply\n"
41 " -h,--help print this help text\n\n");
d59f11e1 42}
3b47c739 43
9ec6e95b 44static int adm_control(struct udev *udev, int argc, char *argv[]) {
44433ebd 45 _cleanup_udev_ctrl_unref_ struct udev_ctrl *uctrl = NULL;
912541b0 46 int timeout = 60;
7643ac9a 47 int rc = 1, c;
2b725651 48
912541b0 49 static const struct option options[] = {
7643ac9a
ZJS
50 { "exit", no_argument, NULL, 'e' },
51 { "log-priority", required_argument, NULL, 'l' },
52 { "stop-exec-queue", no_argument, NULL, 's' },
53 { "start-exec-queue", no_argument, NULL, 'S' },
54 { "reload", no_argument, NULL, 'R' },
55 { "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */
56 { "property", required_argument, NULL, 'p' },
57 { "env", required_argument, NULL, 'p' }, /* alias for -p */
58 { "children-max", required_argument, NULL, 'm' },
59 { "timeout", required_argument, NULL, 't' },
60 { "help", no_argument, NULL, 'h' },
912541b0
KS
61 {}
62 };
3b47c739 63
912541b0
KS
64 if (getuid() != 0) {
65 fprintf(stderr, "root privileges required\n");
66 return 1;
67 }
2b725651 68
912541b0
KS
69 uctrl = udev_ctrl_new(udev);
70 if (uctrl == NULL)
71 return 2;
2b725651 72
7643ac9a
ZJS
73 while ((c = getopt_long(argc, argv, "el:sSRp:m:h", options, NULL)) >= 0)
74 switch (c) {
912541b0
KS
75 case 'e':
76 if (udev_ctrl_send_exit(uctrl, timeout) < 0)
77 rc = 2;
78 else
79 rc = 0;
80 break;
81 case 'l': {
82 int i;
ff2c503d 83
912541b0
KS
84 i = util_log_priority(optarg);
85 if (i < 0) {
86 fprintf(stderr, "invalid number '%s'\n", optarg);
44433ebd 87 return rc;
912541b0
KS
88 }
89 if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0)
90 rc = 2;
91 else
92 rc = 0;
93 break;
94 }
95 case 's':
96 if (udev_ctrl_send_stop_exec_queue(uctrl, timeout) < 0)
97 rc = 2;
98 else
99 rc = 0;
100 break;
101 case 'S':
102 if (udev_ctrl_send_start_exec_queue(uctrl, timeout) < 0)
103 rc = 2;
104 else
105 rc = 0;
106 break;
107 case 'R':
108 if (udev_ctrl_send_reload(uctrl, timeout) < 0)
109 rc = 2;
110 else
111 rc = 0;
112 break;
113 case 'p':
114 if (strchr(optarg, '=') == NULL) {
115 fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg);
44433ebd 116 return rc;
912541b0
KS
117 }
118 if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0)
119 rc = 2;
120 else
121 rc = 0;
122 break;
123 case 'm': {
124 char *endp;
125 int i;
ff2c503d 126
912541b0
KS
127 i = strtoul(optarg, &endp, 0);
128 if (endp[0] != '\0' || i < 1) {
129 fprintf(stderr, "invalid number '%s'\n", optarg);
44433ebd 130 return rc;
912541b0
KS
131 }
132 if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0)
133 rc = 2;
134 else
135 rc = 0;
136 break;
137 }
138 case 't': {
139 int seconds;
ff2c503d 140
912541b0
KS
141 seconds = atoi(optarg);
142 if (seconds >= 0)
143 timeout = seconds;
144 else
145 fprintf(stderr, "invalid timeout value\n");
146 break;
147 }
148 case 'h':
149 print_help();
150 rc = 0;
151 break;
152 }
5cc4112e 153
7643ac9a
ZJS
154 if (optind < argc)
155 fprintf(stderr, "Extraneous argument: %s\n", argv[optind]);
912541b0 156 else if (optind == 1)
7643ac9a 157 fprintf(stderr, "Option missing\n");
912541b0 158 return rc;
3b47c739 159}
1985c76e
KS
160
161const struct udevadm_cmd udevadm_control = {
912541b0
KS
162 .name = "control",
163 .cmd = adm_control,
164 .help = "control the udev daemon",
1985c76e 165};