chiark / gitweb /
libudev: add monitor documentation
[elogind.git] / udev / udevcontrol.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 <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/wait.h>
31 #include <sys/un.h>
32
33 #include "udev.h"
34 #include "udevd.h"
35
36 static int sock = -1;
37 static int udev_log = 0;
38
39 int udevcontrol(int argc, char *argv[], char *envp[])
40 {
41         static struct udevd_ctrl_msg ctrl_msg;
42         struct sockaddr_un saddr;
43         socklen_t addrlen;
44         const char *env;
45         const char *arg;
46         const char *val;
47         int *intval;
48         int retval = 1;
49
50         env = getenv("UDEV_LOG");
51         if (env)
52                 udev_log = log_priority(env);
53
54         logging_init("udevcontrol");
55         dbg("version %s\n", VERSION);
56
57         if (argc < 2) {
58                 fprintf(stderr, "missing command\n\n");
59                 goto exit;
60         }
61         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
62         strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
63         arg = argv[1];
64
65         /* allow instructions passed as options */
66         if (strncmp(arg, "--", 2) == 0)
67                 arg += 2;
68
69         if (!strcmp(arg, "stop_exec_queue"))
70                 ctrl_msg.type = UDEVD_CTRL_STOP_EXEC_QUEUE;
71         else if (!strcmp(arg, "start_exec_queue"))
72                 ctrl_msg.type = UDEVD_CTRL_START_EXEC_QUEUE;
73         else if (!strcmp(arg, "reload_rules"))
74                 ctrl_msg.type = UDEVD_CTRL_RELOAD_RULES;
75         else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
76                 intval = (int *) ctrl_msg.buf;
77                 val = &arg[strlen("log_priority=")];
78                 ctrl_msg.type = UDEVD_CTRL_SET_LOG_LEVEL;
79                 *intval = log_priority(val);
80                 info("send log_priority=%i\n", *intval);
81         } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
82                 char *endp;
83                 int count;
84
85                 intval = (int *) ctrl_msg.buf;
86                 val = &arg[strlen("max_childs=")];
87                 ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS;
88                 count = strtoul(val, &endp, 0);
89                 if (endp[0] != '\0' || count < 1) {
90                         fprintf(stderr, "invalid number\n");
91                         goto exit;
92                 }
93                 *intval = count;
94                 info("send max_childs=%i\n", *intval);
95         } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
96                 char *endp;
97                 int count;
98
99                 intval = (int *) ctrl_msg.buf;
100                 val = &arg[strlen("max_childs_running=")];
101                 ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS_RUNNING;
102                 count = strtoul(val, &endp, 0);
103                 if (endp[0] != '\0' || count < 1) {
104                         fprintf(stderr, "invalid number\n");
105                         goto exit;
106                 }
107                 *intval = count;
108                 info("send max_childs_running=%i\n", *intval);
109         } else if (!strncmp(arg, "env", strlen("env"))) {
110                 if (!strncmp(arg, "env=", strlen("env=")))
111                         val = &arg[strlen("env=")];
112                 else
113                         val = argv[2];
114                 if (val == NULL) {
115                         fprintf(stderr, "missing key\n");
116                         goto exit;
117                 }
118                 ctrl_msg.type = UDEVD_CTRL_ENV;
119                 strlcpy(ctrl_msg.buf, val, sizeof(ctrl_msg.buf));
120                 info("send env '%s'\n", val);
121         } else if (strcmp(arg, "help") == 0  || strcmp(arg, "-h") == 0) {
122                 printf("Usage: udevadm control COMMAND\n"
123                         "  --log_priority=<level>   set the udev log level for the daemon\n"
124                         "  --stop_exec_queue        keep udevd from executing events, queue only\n"
125                         "  --start_exec_queue       execute events, flush queue\n"
126                         "  --reload_rules           reloads the rules files\n"
127                         "  --env=<KEY>=<value>      set a global environment variable\n"
128                         "  --max_childs=<N>         maximum number of childs\n"
129                         "  --max_childs_running=<N> maximum number of childs running at the same time\n"
130                         "  --help                   print this help text\n\n");
131                 goto exit;
132         } else {
133                 fprintf(stderr, "unrecognized command '%s'\n", arg);
134                 goto exit;
135         }
136
137         if (getuid() != 0) {
138                 fprintf(stderr, "root privileges required\n");
139                 goto exit;
140         }
141
142         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
143         if (sock == -1) {
144                 err("error getting socket: %s\n", strerror(errno));
145                 goto exit;
146         }
147
148         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
149         saddr.sun_family = AF_LOCAL;
150         /* use abstract namespace for socket path */
151         strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
152         addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
153
154         retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
155         if (retval == -1) {
156                 err("error sending message: %s\n", strerror(errno));
157                 retval = 1;
158         } else {
159                 dbg("sent message type=0x%02x, %u bytes sent\n", ctrl_msg.type, retval);
160                 retval = 0;
161         }
162
163         close(sock);
164 exit:
165         logging_close();
166         return retval;
167 }