chiark / gitweb /
0447804c95fb5a697dda079da7918f7fb3b15719
[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                 "  --property=<KEY>=<value> set a global property for all events\n"
38                 "  --children-max=<N>       maximum number of children\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         static const struct option options[] = {
48                 { "log-priority", required_argument, NULL, 'l' },
49                 { "stop-exec-queue", no_argument, NULL, 's' },
50                 { "start-exec-queue", no_argument, NULL, 'S' },
51                 { "reload-rules", no_argument, NULL, 'R' },
52                 { "property", required_argument, NULL, 'p' },
53                 { "env", required_argument, NULL, 'p' },
54                 { "children-max", required_argument, NULL, 'm' },
55                 { "help", no_argument, NULL, 'h' },
56                 {}
57         };
58
59         if (getuid() != 0) {
60                 fprintf(stderr, "root privileges required\n");
61                 return 1;
62         }
63
64         uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
65         if (uctrl == NULL)
66                 return 2;
67
68         for (;;) {
69                 int option;
70                 int i;
71                 char *endp;
72
73                 option = getopt_long(argc, argv, "l:sSRp:m:h", options, NULL);
74                 if (option == -1)
75                         break;
76
77                 switch (option) {
78                 case 'l':
79                         i = util_log_priority(optarg);
80                         if (i < 0) {
81                                 fprintf(stderr, "invalid number '%s'\n", optarg);
82                                 goto exit;
83                         }
84                         if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg)) < 0)
85                                 rc = 2;
86                         else
87                                 rc = 0;
88                         break;
89                 case 's':
90                         if (udev_ctrl_send_stop_exec_queue(uctrl) < 0)
91                                 rc = 2;
92                         else
93                                 rc = 0;
94                         break;
95                 case 'S':
96                         if (udev_ctrl_send_start_exec_queue(uctrl) < 0)
97                                 rc = 2;
98                         else
99                                 rc = 0;
100                         break;
101                 case 'R':
102                         if (udev_ctrl_send_reload_rules(uctrl) < 0)
103                                 rc = 2;
104                         else
105                                 rc = 0;
106                         break;
107                 case 'p':
108                         if (strchr(optarg, '=') == NULL) {
109                                 fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg);
110                                 goto exit;
111                         }
112                         if (udev_ctrl_send_set_env(uctrl, optarg) < 0)
113                                 rc = 2;
114                         else
115                                 rc = 0;
116                         break;
117                 case 'm':
118                         i = strtoul(optarg, &endp, 0);
119                         if (endp[0] != '\0' || i < 1) {
120                                 fprintf(stderr, "invalid number '%s'\n", optarg);
121                                 goto exit;
122                         }
123                         if (udev_ctrl_send_set_children_max(uctrl, i) < 0)
124                                 rc = 2;
125                         else
126                                 rc = 0;
127                         break;
128                 case 'h':
129                         print_help();
130                         rc = 0;
131                         break;
132                 }
133         }
134
135         if (rc == 1)
136                 err(udev, "unrecognized command\n");
137 exit:
138         udev_ctrl_unref(uctrl);
139         return rc;
140 }