]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev/udevcontrol.c
delete all Makefiles and move udev source to udev/
[thirdparty/systemd.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 <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\n", 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 /* allow instructions passed as options */
64 if (strncmp(arg, "--", 2) == 0)
65 arg += 2;
66
67 if (!strcmp(arg, "stop_exec_queue"))
68 ctrl_msg.type = UDEVD_CTRL_STOP_EXEC_QUEUE;
69 else if (!strcmp(arg, "start_exec_queue"))
70 ctrl_msg.type = UDEVD_CTRL_START_EXEC_QUEUE;
71 else if (!strcmp(arg, "reload_rules"))
72 ctrl_msg.type = UDEVD_CTRL_RELOAD_RULES;
73 else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
74 intval = (int *) ctrl_msg.buf;
75 val = &arg[strlen("log_priority=")];
76 ctrl_msg.type = UDEVD_CTRL_SET_LOG_LEVEL;
77 *intval = log_priority(val);
78 info("send log_priority=%i\n", *intval);
79 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
80 char *endp;
81 int count;
82
83 intval = (int *) ctrl_msg.buf;
84 val = &arg[strlen("max_childs=")];
85 ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS;
86 count = strtoul(val, &endp, 0);
87 if (endp[0] != '\0' || count < 1) {
88 fprintf(stderr, "invalid number\n");
89 goto exit;
90 }
91 *intval = count;
92 info("send max_childs=%i\n", *intval);
93 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
94 char *endp;
95 int count;
96
97 intval = (int *) ctrl_msg.buf;
98 val = &arg[strlen("max_childs_running=")];
99 ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS_RUNNING;
100 count = strtoul(val, &endp, 0);
101 if (endp[0] != '\0' || count < 1) {
102 fprintf(stderr, "invalid number\n");
103 goto exit;
104 }
105 *intval = count;
106 info("send max_childs_running=%i\n", *intval);
107 } else if (!strncmp(arg, "env", strlen("env"))) {
108 if (!strncmp(arg, "env=", strlen("env=")))
109 val = &arg[strlen("env=")];
110 else
111 val = argv[2];
112 if (val == NULL) {
113 fprintf(stderr, "missing key\n");
114 goto exit;
115 }
116 ctrl_msg.type = UDEVD_CTRL_ENV;
117 strlcpy(ctrl_msg.buf, val, sizeof(ctrl_msg.buf));
118 info("send env '%s'\n", val);
119 } else if (strcmp(arg, "help") == 0 || strcmp(arg, "-h") == 0) {
120 printf("Usage: udevadm control COMMAND\n"
121 " --log_priority=<level> set the udev log level for the daemon\n"
122 " --stop_exec_queue keep udevd from executing events, queue only\n"
123 " --start_exec_queue execute events, flush queue\n"
124 " --reload_rules reloads the rules files\n"
125 " --env=<KEY>=<value> set a global environment variable\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 if (getuid() != 0) {
136 fprintf(stderr, "root privileges required\n");
137 goto exit;
138 }
139
140 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
141 if (sock == -1) {
142 err("error getting socket: %s\n", strerror(errno));
143 goto exit;
144 }
145
146 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
147 saddr.sun_family = AF_LOCAL;
148 /* use abstract namespace for socket path */
149 strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
150 addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
151
152 retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
153 if (retval == -1) {
154 err("error sending message: %s\n", strerror(errno));
155 retval = 1;
156 } else {
157 dbg("sent message type=0x%02x, %u bytes sent\n", ctrl_msg.type, retval);
158 retval = 0;
159 }
160
161 close(sock);
162 exit:
163 logging_close();
164 return retval;
165 }