6 * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
7 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <sys/types.h>
26 #include <sys/socket.h>
36 #include <linux/stddef.h>
39 #include "udev_version.h"
43 /* global variables */
47 unsigned char logname[LOGNAME_SIZE];
48 void log_message (int level, const char *format, ...)
52 va_start(args, format);
53 vsyslog(level, format, args);
58 static int start_daemon(void)
72 execl(UDEVD_BIN, "udevd", NULL);
73 dbg("exec of daemon failed");
76 dbg("fork of daemon failed");
83 dbg("fork of helper failed");
86 waitpid(pid, NULL, 0);
91 static void run_udev(const char *subsystem)
99 execl(UDEV_BIN, "udev", subsystem, NULL);
100 dbg("exec of child failed");
104 dbg("fork of child failed");
107 waitpid(pid, NULL, 0);
111 int main(int argc, char *argv[], char *envp[])
113 static struct udevsend_msg usend_msg;
117 struct sockaddr_un saddr;
119 const char *subsystem_argv;
120 int subsystem_env = 0;
123 int started_daemon = 0;
125 logging_init("udevsend");
126 dbg("version %s", UDEV_VERSION);
128 subsystem_argv = argv[1];
129 if (subsystem_argv == NULL) {
134 /* prevent loops in the scripts we execute */
135 if (getenv("MANAGED_EVENT") != NULL) {
136 dbg("seems that the event source is not the kernel, just exit");
140 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
142 dbg("error getting socket");
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_SOCK_PATH);
150 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
152 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
154 strcpy(usend_msg.magic, UDEV_MAGIC);
156 /* copy all keys to send buffer */
157 for (i = 0; envp[i]; i++) {
162 keylen = strlen(key);
163 if (bufpos + keylen >= HOTPLUG_BUFFER_SIZE-1) {
164 dbg("environment buffer too small, probably not called by the kernel");
168 /* older kernels do not have the SUBSYSTEM in the environment */
169 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
172 dbg("add '%s' to env[%i] buffer", key, i);
173 strcpy(&usend_msg.envbuf[bufpos], key);
174 bufpos += keylen + 1;
176 if (!subsystem_env) {
177 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", subsystem_argv) + 1;
178 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", subsystem_argv, i);
181 usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
182 dbg("usend_msg_len=%i", usend_msg_len);
184 /* If we can't send, try to start daemon and resend message */
185 loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
187 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
193 if (errno != ECONNREFUSED) {
194 dbg("error sending message (%s)", strerror(errno));
198 if (!started_daemon) {
199 dbg("try to start udevd daemon");
200 retval = start_daemon();
202 info("error starting daemon");
205 info("udevd daemon started");
208 dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
209 usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
214 info("unable to connect to event daemon, try to call udev directly");
215 run_udev(subsystem_argv);