chiark / gitweb /
release 137
[elogind.git] / udev / udevadm-control.c
1 /*
2  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@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 <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>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/wait.h>
26 #include <sys/un.h>
27
28 #include "udev.h"
29
30 static void print_help(void)
31 {
32         printf("Usage: udevadm control COMMAND\n"
33                 "  --log-priority=<level>   set the udev log level for the daemon\n"
34                 "  --stop-exec-queue        keep udevd from executing events, queue only\n"
35                 "  --start-exec-queue       execute events, flush queue\n"
36                 "  --reload-rules           reloads the rules files\n"
37                 "  --env=<KEY>=<value>      set a global environment variable\n"
38                 "  --max-childs=<N>         maximum number of childs\n"
39                 "  --help                   print this help text\n\n");
40 }
41
42 int udevadm_control(struct udev *udev, int argc, char *argv[])
43 {
44         struct udev_ctrl *uctrl = NULL;
45         int rc = 1;
46
47         /* compat values with '_' will be removed in a future release */
48         static const struct option options[] = {
49                 { "log-priority", required_argument, NULL, 'l' },
50                 { "log_priority", required_argument, NULL, 'l' + 256 },
51                 { "stop-exec-queue", no_argument, NULL, 's' },
52                 { "stop_exec_queue", no_argument, NULL, 's' + 256 },
53                 { "start-exec-queue", no_argument, NULL, 'S' },
54                 { "start_exec_queue", no_argument, NULL, 'S' + 256},
55                 { "reload-rules", no_argument, NULL, 'R' },
56                 { "reload_rules", no_argument, NULL, 'R' + 256},
57                 { "env", required_argument, NULL, 'e' },
58                 { "max-childs", required_argument, NULL, 'm' },
59                 { "max_childs", required_argument, NULL, 'm' + 256},
60                 { "help", no_argument, NULL, 'h' },
61                 {}
62         };
63
64         if (getuid() != 0) {
65                 fprintf(stderr, "root privileges required\n");
66                 goto exit;
67         }
68
69         uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
70         if (uctrl == NULL)
71                 goto exit;
72
73         while (1) {
74                 int option;
75                 int i;
76                 char *endp;
77
78                 option = getopt_long(argc, argv, "l:sSRe:m:M:h", options, NULL);
79                 if (option == -1)
80                         break;
81
82                 if (option > 255) {
83                         fprintf(stderr, "udevadm control expects commands without underscore, "
84                                 "this will stop working in a future release\n");
85                         err(udev, "udevadm control expects commands without underscore, "
86                             "this will stop working in a future release\n");
87                 }
88
89                 switch (option) {
90                 case 'l':
91                 case 'l' + 256:
92                         i = util_log_priority(optarg);
93                         if (i < 0) {
94                                 fprintf(stderr, "invalid number '%s'\n", optarg);
95                                 goto exit;
96                         }
97                         udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg));
98                         rc = 0;
99                         break;
100                 case 's':
101                 case 's' + 256:
102                         udev_ctrl_send_stop_exec_queue(uctrl);
103                         rc = 0;
104                         break;
105                 case 'S':
106                 case 'S' + 256:
107                         udev_ctrl_send_start_exec_queue(uctrl);
108                         rc = 0;
109                         break;
110                 case 'R':
111                 case 'R' + 256:
112                         udev_ctrl_send_reload_rules(uctrl);
113                         rc = 0;
114                         break;
115                 case 'e':
116                         if (strchr(optarg, '=') == NULL) {
117                                 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
118                                 goto exit;
119                         }
120                         udev_ctrl_send_set_env(uctrl, optarg);
121                         rc = 0;
122                         break;
123                 case 'm':
124                 case 'm' + 256:
125                         i = strtoul(optarg, &endp, 0);
126                         if (endp[0] != '\0' || i < 1) {
127                                 fprintf(stderr, "invalid number '%s'\n", optarg);
128                                 goto exit;
129                         }
130                         udev_ctrl_send_set_max_childs(uctrl, i);
131                         rc = 0;
132                         break;
133                 case 'h':
134                         print_help();
135                         rc = 0;
136                         goto exit;
137                 default:
138                         goto exit;
139                 }
140         }
141
142         /* compat stuff which will be removed in a future release */
143         if (argv[optind] != NULL) {
144                 const char *arg = argv[optind];
145
146                 fprintf(stderr, "udevadm control commands requires the --<command> format, "
147                         "this will stop working in a future release\n");
148                 err(udev, "udevadm control commands requires the --<command> format, "
149                     "this will stop working in a future release\n");
150
151                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
152                         udev_ctrl_send_set_log_level(uctrl, util_log_priority(&arg[strlen("log_priority=")]));
153                         rc = 0;
154                         goto exit;
155                 } else if (!strcmp(arg, "stop_exec_queue")) {
156                         udev_ctrl_send_stop_exec_queue(uctrl);
157                         rc = 0;
158                         goto exit;
159                 } else if (!strcmp(arg, "start_exec_queue")) {
160                         udev_ctrl_send_start_exec_queue(uctrl);
161                         rc = 0;
162                         goto exit;
163                 } else if (!strcmp(arg, "reload_rules")) {
164                         udev_ctrl_send_reload_rules(uctrl);
165                         rc = 0;
166                         goto exit;
167                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
168                         udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
169                         rc = 0;
170                         goto exit;
171                 } else if (!strncmp(arg, "env", strlen("env"))) {
172                         udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
173                         rc = 0;
174                         goto exit;
175                 }
176         }
177
178         if (rc != 0) {
179                 fprintf(stderr, "unrecognized command\n");
180                 err(udev, "unrecognized command\n");
181         }
182 exit:
183         udev_ctrl_unref(uctrl);
184         return rc;
185 }