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.
26 #include <sys/types.h>
27 #include <sys/socket.h>
35 static int udev_log = 0;
37 int udevcontrol(int argc, char *argv[], char *envp[])
39 static struct udevd_ctrl_msg ctrl_msg;
40 struct sockaddr_un saddr;
48 env = getenv("UDEV_LOG");
50 udev_log = log_priority(env);
52 logging_init("udevcontrol");
53 dbg("version %s\n", UDEV_VERSION);
56 fprintf(stderr, "missing command\n\n");
59 memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
60 strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
63 /* allow instructions passed as options */
64 if (strncmp(arg, "--", 2) == 0)
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="))) {
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");
92 info("send max_childs=%i\n", *intval);
93 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
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");
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=")];
113 fprintf(stderr, "missing key\n");
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");
131 fprintf(stderr, "unrecognized command '%s'\n", arg);
136 fprintf(stderr, "root privileges required\n");
140 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
142 err("error getting socket: %s\n", strerror(errno));
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]);
152 retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
154 err("error sending message: %s\n", strerror(errno));
157 dbg("sent message type=0x%02x, %u bytes sent\n", ctrl_msg.type, retval);