chiark / gitweb /
rename udev_libc_wrapper -> udev_sysdeps
[elogind.git] / udevcontrol.c
1 /*
2  * udevcontrol.c
3  *
4  * Copyright (C) 2005 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 static void usage(void)
54 {
55         printf("Usage: udevcontrol COMMAND\n"
56                 "  log_priority=<level>   set the udev log level for the daemon\n"
57                 "  stop_exec_queue        keep udevd from executing events, queue only\n"
58                 "  start_exec_queue       execute events, flush queue\n"
59                 "  reload_rules           reloads the rules files\n"
60                 "  max_childs=<N>         maximum number of childs\n"
61                 "  max_childs_running=<N> maximum number of childs running at the same time\n"
62                 "  --help                 print this help text\n\n");
63 }
64
65 int main(int argc, char *argv[], char *envp[])
66 {
67         static struct udevd_ctrl_msg ctrl_msg;
68         struct sockaddr_un saddr;
69         socklen_t addrlen;
70         const char *env;
71         const char *val;
72         int *intval;
73         int i;
74         int retval = 1;
75
76         env = getenv("UDEV_LOG");
77         if (env)
78                 udev_log = log_priority(env);
79
80         logging_init("udevcontrol");
81         dbg("version %s", UDEV_VERSION);
82
83         if (argc < 2) {
84                 fprintf(stderr, "missing command\n\n");
85                 usage();
86                 goto exit;
87         }
88
89         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
90         strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
91
92         for (i = 1 ; i < argc; i++) {
93                 char *arg = argv[i];
94
95                 if (!strcmp(arg, "stop_exec_queue"))
96                         ctrl_msg.type = UDEVD_CTRL_STOP_EXEC_QUEUE;
97                 else if (!strcmp(arg, "start_exec_queue"))
98                         ctrl_msg.type = UDEVD_CTRL_START_EXEC_QUEUE;
99                 else if (!strcmp(arg, "reload_rules"))
100                         ctrl_msg.type = UDEVD_CTRL_RELOAD_RULES;
101                 else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
102                         intval = (int *) ctrl_msg.buf;
103                         val = &arg[strlen("log_priority=")];
104                         ctrl_msg.type = UDEVD_CTRL_SET_LOG_LEVEL;
105                         *intval = log_priority(val);
106                         info("send log_priority=%i", *intval);
107                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
108                         char *endp;
109                         int count;
110
111                         intval = (int *) ctrl_msg.buf;
112                         val = &arg[strlen("max_childs=")];
113                         ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS;
114                         count = strtoul(val, &endp, 0);
115                         if (endp[0] != '\0' || count < 1) {
116                                 fprintf(stderr, "invalid number\n");
117                                 goto exit;
118                         }
119                         *intval = count;
120                         info("send max_childs=%i", *intval);
121                 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
122                         char *endp;
123                         int count;
124
125                         intval = (int *) ctrl_msg.buf;
126                         val = &arg[strlen("max_childs_running=")];
127                         ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS_RUNNING;
128                         count = strtoul(val, &endp, 0);
129                         if (endp[0] != '\0' || count < 1) {
130                                 fprintf(stderr, "invalid number\n");
131                                 goto exit;
132                         }
133                         *intval = count;
134                         info("send max_childs_running=%i", *intval);
135                 } else if (strcmp(arg, "help") == 0  || strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0) {
136                         usage();
137                         goto exit;
138                 } else {
139                         fprintf(stderr, "unknown option\n\n");
140                         usage();
141                         goto exit;
142                 }
143         }
144
145         if (getuid() != 0) {
146                 fprintf(stderr, "need to be root, exit\n\n");
147                 goto exit;
148         }
149
150         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
151         if (sock == -1) {
152                 err("error getting socket: %s", strerror(errno));
153                 goto exit;
154         }
155
156         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
157         saddr.sun_family = AF_LOCAL;
158         /* use abstract namespace for socket path */
159         strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
160         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
161
162         retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
163         if (retval == -1) {
164                 err("error sending message: %s", strerror(errno));
165                 retval = 1;
166         } else {
167                 dbg("sent message type=0x%02x, %u bytes sent", ctrl_msg.type, retval);
168                 retval = 0;
169         }
170
171         close(sock);
172 exit:
173         logging_close();
174         return retval;
175 }