2 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
28 #include <sys/types.h>
29 #include <sys/socket.h>
37 static int udev_log = 0;
39 int udevcontrol(int argc, char *argv[], char *envp[])
41 static struct udevd_ctrl_msg ctrl_msg;
42 struct sockaddr_un saddr;
50 env = getenv("UDEV_LOG");
52 udev_log = log_priority(env);
54 logging_init("udevcontrol");
55 dbg("version %s\n", VERSION);
58 fprintf(stderr, "missing command\n\n");
61 memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
62 strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
65 /* allow instructions passed as options */
66 if (strncmp(arg, "--", 2) == 0)
69 if (!strcmp(arg, "stop_exec_queue"))
70 ctrl_msg.type = UDEVD_CTRL_STOP_EXEC_QUEUE;
71 else if (!strcmp(arg, "start_exec_queue"))
72 ctrl_msg.type = UDEVD_CTRL_START_EXEC_QUEUE;
73 else if (!strcmp(arg, "reload_rules"))
74 ctrl_msg.type = UDEVD_CTRL_RELOAD_RULES;
75 else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
76 intval = (int *) ctrl_msg.buf;
77 val = &arg[strlen("log_priority=")];
78 ctrl_msg.type = UDEVD_CTRL_SET_LOG_LEVEL;
79 *intval = log_priority(val);
80 info("send log_priority=%i\n", *intval);
81 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
85 intval = (int *) ctrl_msg.buf;
86 val = &arg[strlen("max_childs=")];
87 ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS;
88 count = strtoul(val, &endp, 0);
89 if (endp[0] != '\0' || count < 1) {
90 fprintf(stderr, "invalid number\n");
94 info("send max_childs=%i\n", *intval);
95 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
99 intval = (int *) ctrl_msg.buf;
100 val = &arg[strlen("max_childs_running=")];
101 ctrl_msg.type = UDEVD_CTRL_SET_MAX_CHILDS_RUNNING;
102 count = strtoul(val, &endp, 0);
103 if (endp[0] != '\0' || count < 1) {
104 fprintf(stderr, "invalid number\n");
108 info("send max_childs_running=%i\n", *intval);
109 } else if (!strncmp(arg, "env", strlen("env"))) {
110 if (!strncmp(arg, "env=", strlen("env=")))
111 val = &arg[strlen("env=")];
115 fprintf(stderr, "missing key\n");
118 ctrl_msg.type = UDEVD_CTRL_ENV;
119 strlcpy(ctrl_msg.buf, val, sizeof(ctrl_msg.buf));
120 info("send env '%s'\n", val);
121 } else if (strcmp(arg, "help") == 0 || strcmp(arg, "-h") == 0) {
122 printf("Usage: udevadm control COMMAND\n"
123 " --log_priority=<level> set the udev log level for the daemon\n"
124 " --stop_exec_queue keep udevd from executing events, queue only\n"
125 " --start_exec_queue execute events, flush queue\n"
126 " --reload_rules reloads the rules files\n"
127 " --env=<KEY>=<value> set a global environment variable\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");
133 fprintf(stderr, "unrecognized command '%s'\n", arg);
138 fprintf(stderr, "root privileges required\n");
142 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
144 err("error getting socket: %s\n", strerror(errno));
148 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
149 saddr.sun_family = AF_LOCAL;
150 /* use abstract namespace for socket path */
151 strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
152 addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
154 retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
156 err("error sending message: %s\n", strerror(errno));
159 dbg("sent message type=0x%02x, %u bytes sent\n", ctrl_msg.type, retval);