chiark / gitweb /
udevadm: log message if udevadm link is used
[elogind.git] / udev / udevadm-control.c
1 /*
2  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  *      This program is free software; you can redistribute it and/or modify it
5  *      under the terms of the GNU General Public License as published by the
6  *      Free Software Foundation version 2 of the License.
7  * 
8  *      This program is distributed in the hope that it will be useful, but
9  *      WITHOUT ANY WARRANTY; without even the implied warranty of
10  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *      General Public License for more details.
12  * 
13  *      You should have received a copy of the GNU General Public License along
14  *      with this program; if not, write to the Free Software Foundation, Inc.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  *
17  */
18
19 #include "config.h"
20
21 #include <time.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/un.h>
33
34 #include "udev.h"
35
36 static void print_help(void)
37 {
38         printf("Usage: udevadm control COMMAND\n"
39                 "  --log-priority=<level>   set the udev log level for the daemon\n"
40                 "  --stop-exec-queue        keep udevd from executing events, queue only\n"
41                 "  --start-exec-queue       execute events, flush queue\n"
42                 "  --reload-rules           reloads the rules files\n"
43                 "  --env=<KEY>=<value>      set a global environment variable\n"
44                 "  --max-childs=<N>         maximum number of childs\n"
45                 "  --max-childs-running=<N> maximum number of childs running at the same time\n"
46                 "  --help                   print this help text\n\n");
47 }
48
49 int udevadm_control(struct udev *udev, int argc, char *argv[])
50 {
51         struct udev_ctrl *uctrl = NULL;
52         int rc = 1;
53
54         /* compat values with '_' will be removed in a future release */
55         static const struct option options[] = {
56                 { "log-priority", 1, NULL, 'l' },
57                 { "log_priority", 1, NULL, 'l' + 256 },
58                 { "stop-exec-queue", 0, NULL, 's' },
59                 { "stop_exec_queue", 0, NULL, 's' + 256 },
60                 { "start-exec-queue", 0, NULL, 'S' },
61                 { "start_exec_queue", 0, NULL, 'S' + 256},
62                 { "reload-rules", 0, NULL, 'R' },
63                 { "reload_rules", 0, NULL, 'R' + 256},
64                 { "env", 1, NULL, 'e' },
65                 { "max-childs", 1, NULL, 'm' },
66                 { "max_childs", 1, NULL, 'm' + 256},
67                 { "max-childs-running", 1, NULL, 'M' },
68                 { "max_childs_running", 1, NULL, 'M' + 256},
69                 { "help", 0, NULL, 'h' },
70                 {}
71         };
72
73         if (getuid() != 0) {
74                 fprintf(stderr, "root privileges required\n");
75                 goto exit;
76         }
77
78         uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
79         if (uctrl == NULL)
80                 goto exit;
81
82         while (1) {
83                 int option;
84                 int i;
85                 char *endp;
86
87                 option = getopt_long(argc, argv, "l:sSRe:m:M:h", options, NULL);
88                 if (option == -1)
89                         break;
90
91                 if (option > 255) {
92                         fprintf(stderr, "udevadm control expects commands without underscore, "
93                                 "this will stop working in a future release\n");
94                         err(udev, "udevadm control expects commands without underscore, "
95                             "this will stop working in a future release\n");
96                 }
97
98                 switch (option) {
99                 case 'l':
100                 case 'l' + 256:
101                         i = log_priority(optarg);
102                         if (i < 0) {
103                                 fprintf(stderr, "invalid number '%s'\n", optarg);
104                                 goto exit;
105                         }
106                         udev_ctrl_send_set_log_level(uctrl, log_priority(optarg));
107                         rc = 0;
108                         break;
109                 case 's':
110                 case 's' + 256:
111                         udev_ctrl_send_stop_exec_queue(uctrl);
112                         rc = 0;
113                         break;
114                 case 'S':
115                 case 'S' + 256:
116                         udev_ctrl_send_start_exec_queue(uctrl);
117                         rc = 0;
118                         break;
119                 case 'R':
120                 case 'R' + 256:
121                         udev_ctrl_send_reload_rules(uctrl);
122                         rc = 0;
123                         break;
124                 case 'e':
125                         if (strchr(optarg, '=') == NULL) {
126                                 fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
127                                 goto exit;
128                         }
129                         udev_ctrl_send_set_env(uctrl, optarg);
130                         rc = 0;
131                         break;
132                 case 'm':
133                 case 'm' + 256:
134                         i = strtoul(optarg, &endp, 0);
135                         if (endp[0] != '\0' || i < 1) {
136                                 fprintf(stderr, "invalid number '%s'\n", optarg);
137                                 goto exit;
138                         }
139                         udev_ctrl_send_set_max_childs(uctrl, i);
140                         rc = 0;
141                         break;
142                 case 'M':
143                 case 'M' + 256:
144                         i = strtoul(optarg, &endp, 0);
145                         if (endp[0] != '\0' || i < 1) {
146                                 fprintf(stderr, "invalid number '%s'\n", optarg);
147                                 goto exit;
148                         }
149                         udev_ctrl_send_set_max_childs_running(uctrl, i);
150                         rc = 0;
151                         break;
152                 case 'h':
153                         print_help();
154                         rc = 0;
155                         goto exit;
156                 default:
157                         goto exit;
158                 }
159         }
160
161         /* compat stuff which will be removed in a future release */
162         if (argv[optind] != NULL) {
163                 const char *arg = argv[optind];
164
165                 fprintf(stderr, "udevadm control commands requires the --<command> format, "
166                         "this will stop working in a future release\n");
167                 err(udev, "udevadm control commands requires the --<command> format, "
168                     "this will stop working in a future release\n");
169
170                 if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
171                         udev_ctrl_send_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
172                         rc = 0;
173                         goto exit;
174                 } else if (!strcmp(arg, "stop_exec_queue")) {
175                         udev_ctrl_send_stop_exec_queue(uctrl);
176                         rc = 0;
177                         goto exit;
178                 } else if (!strcmp(arg, "start_exec_queue")) {
179                         udev_ctrl_send_start_exec_queue(uctrl);
180                         rc = 0;
181                         goto exit;
182                 } else if (!strcmp(arg, "reload_rules")) {
183                         udev_ctrl_send_reload_rules(uctrl);
184                         rc = 0;
185                         goto exit;
186                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
187                         udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
188                         rc = 0;
189                         goto exit;
190                 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
191                         udev_ctrl_send_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
192                         rc = 0;
193                         goto exit;
194                 } else if (!strncmp(arg, "env", strlen("env"))) {
195                         udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
196                         rc = 0;
197                         goto exit;
198                 }
199         }
200
201         if (rc != 0) {
202                 fprintf(stderr, "unrecognized command\n");
203                 err(udev, "unrecognized command\n");
204         }
205 exit:
206         udev_ctrl_unref(uctrl);
207         return rc;
208 }