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