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