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 void log_message (int level, const char *format, ...)
51 va_start(args, format);
52 vsyslog(level, format, args);
57 static int start_daemon(void)
61 char *const argv[] = { "udevd", NULL };
62 char *const envp[] = { NULL };
71 /* daemon with empty environment */
73 execve(UDEVD_BIN, argv, envp);
74 dbg("exec of daemon failed");
77 dbg("fork of daemon failed");
84 dbg("fork of helper failed");
87 waitpid(pid, NULL, 0);
92 static void run_udev(const char *subsystem)
94 char *const argv[] = { "udev", (char *)subsystem, NULL };
101 execv(UDEV_BIN, argv);
102 dbg("exec of child failed");
106 dbg("fork of child failed");
109 waitpid(pid, NULL, 0);
113 int main(int argc, char *argv[], char *envp[])
115 static struct udevsend_msg usend_msg;
119 struct sockaddr_un saddr;
123 int started_daemon = 0;
124 const char *subsystem = NULL;
126 logging_init("udevsend");
127 dbg("version %s", UDEV_VERSION);
129 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
131 dbg("error getting socket");
135 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
136 saddr.sun_family = AF_LOCAL;
137 /* use abstract namespace for socket path */
138 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
139 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
141 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
142 strcpy(usend_msg.magic, UDEV_MAGIC);
144 /* copy all keys to send buffer */
145 for (i = 0; envp[i]; i++) {
150 keylen = strlen(key);
152 /* prevent loops in the scripts we execute */
153 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
154 dbg("seems that the event source is not the kernel, just exit");
158 if (bufpos + keylen >= HOTPLUG_BUFFER_SIZE-1) {
159 dbg("environment buffer too small, probably not called by the kernel");
163 /* remember the SUBSYSTEM */
164 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
165 subsystem = &key[10];
167 dbg("add '%s' to env[%i] buffer", key, i);
168 strcpy(&usend_msg.envbuf[bufpos], key);
169 bufpos += keylen + 1;
171 /* older kernels passed the SUBSYSTEM only as the first argument */
172 if (!subsystem && argc == 2) {
173 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
174 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
177 usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
178 dbg("usend_msg_len=%i", usend_msg_len);
180 /* If we can't send, try to start daemon and resend message */
181 loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
183 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
189 if (errno != ECONNREFUSED) {
190 dbg("error sending message (%s)", strerror(errno));
194 if (!started_daemon) {
195 dbg("try to start udevd daemon");
196 retval = start_daemon();
198 dbg("error starting daemon");
201 dbg("udevd daemon started");
204 dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
205 usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
210 info("unable to connect to event daemon, try to call udev directly");