chiark / gitweb /
431befe39cc210961da8dcde66365ede21059666
[elogind.git] / udevcontrol.c
1 /*
2  * udevcontrol.c
3  *
4  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
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 #ifdef USE_LOG
40 void log_message (int priority, const char *format, ...)
41 {
42         va_list args;
43
44         if (priority > udev_log)
45                 return;
46
47         va_start(args, format);
48         vsyslog(priority, format, args);
49         va_end(args);
50 }
51 #endif
52
53 int main(int argc, char *argv[], char *envp[])
54 {
55         static struct udevd_ctrl_msg ctrl_msg;
56         struct sockaddr_un saddr;
57         socklen_t addrlen;
58         const char *env;
59         const char *val;
60         int *intval;
61         int i;
62         int retval = 1;
63
64         env = getenv("UDEV_LOG");
65         if (env)
66                 udev_log = log_priority(env);
67
68         logging_init("udevcontrol");
69         dbg("version %s", UDEV_VERSION);
70
71         if (argc < 2) {
72                 fprintf(stderr, "missing command\n\n");
73                 goto exit;
74         }
75
76         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
77         strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
78
79         for (i = 1 ; i < argc; i++) {
80                 char *arg = argv[i];
81
82                 if (!strcmp(arg, "stop_exec_queue"))
83                         ctrl_msg.type = UDEVD_CTRL_STOP_EXEC_QUEUE;
84                 else if (!strcmp(arg, "start_exec_queue"))
85                         ctrl_msg.type = UDEVD_CTRL_START_EXEC_QUEUE;
86                 else if (!strcmp(arg, "reload_rules"))
87                         ctrl_msg.type = UDEVD_CTRL_RELOAD_RULES;
88                 else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
89                         intval = (int *) ctrl_msg.buf;
90                         val = &arg[strlen("log_priority=")];
91                         ctrl_msg.type = UDEVD_CTRL_SET_LOG_LEVEL;
92                         *intval = log_priority(val);
93                         info("send log_priority=%i", *intval);
94                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
95                         char *endp;
96                         int count;
97
98                         intval = (int *) ctrl_msg.buf;
99                         val = &arg[strlen("max_childs=")];
100                         ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS;
101                         count = strtoul(val, &endp, 0);
102                         if (endp[0] != '\0' || count < 1) {
103                                 fprintf(stderr, "invalid number\n");
104                                 goto exit;
105                         }
106                         *intval = count;
107                         info("send max_childs=%i", *intval);
108                 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
109                         char *endp;
110                         int count;
111
112                         intval = (int *) ctrl_msg.buf;
113                         val = &arg[strlen("max_childs_running=")];
114                         ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS_RUNNING;
115                         count = strtoul(val, &endp, 0);
116                         if (endp[0] != '\0' || count < 1) {
117                                 fprintf(stderr, "invalid number\n");
118                                 goto exit;
119                         }
120                         *intval = count;
121                         info("send max_childs_running=%i", *intval);
122                 } else if (strcmp(arg, "help") == 0  || strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0) {
123                         printf("Usage: udevcontrol COMMAND\n"
124                                 "  log_priority=<level>   set the udev log level for the daemon\n"
125                                 "  stop_exec_queue        keep udevd from executing events, queue only\n"
126                                 "  start_exec_queue       execute events, flush queue\n"
127                                 "  reload_rules           reloads the rules files\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
138         if (getuid() != 0) {
139                 fprintf(stderr, "root privileges required\n");
140                 goto exit;
141         }
142
143         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
144         if (sock == -1) {
145                 err("error getting socket: %s", strerror(errno));
146                 goto exit;
147         }
148
149         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
150         saddr.sun_family = AF_LOCAL;
151         /* use abstract namespace for socket path */
152         strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
153         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
154
155         retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
156         if (retval == -1) {
157                 err("error sending message: %s", strerror(errno));
158                 retval = 1;
159         } else {
160                 dbg("sent message type=0x%02x, %u bytes sent", ctrl_msg.type, retval);
161                 retval = 0;
162         }
163
164         close(sock);
165 exit:
166         logging_close();
167         return retval;
168 }