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", 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 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="))) {
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");
88 info("send max_childs=%i", *intval);
89 } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
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");
102 info("send max_childs_running=%i", *intval);
103 } else if (!strncmp(arg, "env", strlen("env"))) {
106 fprintf(stderr, "missing key\n");
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");
124 fprintf(stderr, "unrecognized command '%s'\n", arg);
129 fprintf(stderr, "root privileges required\n");
133 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
135 err("error getting socket: %s", strerror(errno));
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;
145 retval = sendto(sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&saddr, addrlen);
147 err("error sending message: %s", strerror(errno));
150 dbg("sent message type=0x%02x, %u bytes sent", ctrl_msg.type, retval);