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