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