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 priority, const char *format, ...)
51 if (priority > udev_log_priority)
54 va_start(args, format);
55 vsyslog(priority, format, args);
60 static int start_daemon(void)
64 char *const argv[] = { "udevd", NULL };
65 char *const envp[] = { NULL };
74 /* daemon with empty environment */
76 execve(UDEVD_BIN, argv, envp);
77 err("exec of daemon failed");
80 err("fork of daemon failed");
87 err("fork of helper failed");
90 waitpid(pid, NULL, 0);
95 static void run_udev(const char *subsystem)
97 char *const argv[] = { "udev", (char *)subsystem, NULL };
104 execv(UDEV_BIN, argv);
105 err("exec of udev child failed");
109 err("fork of udev child failed");
112 waitpid(pid, NULL, 0);
116 int main(int argc, char *argv[], char *envp[])
118 static struct udevd_msg usend_msg;
122 struct sockaddr_un saddr;
126 int started_daemon = 0;
127 const char *subsystem = NULL;
129 logging_init("udevsend");
133 dbg("version %s", UDEV_VERSION);
135 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
137 err("error getting socket");
141 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
142 saddr.sun_family = AF_LOCAL;
143 /* use abstract namespace for socket path */
144 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
145 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
147 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
148 strcpy(usend_msg.magic, UDEV_MAGIC);
149 usend_msg.type = UDEVD_UEVENT;
151 /* copy all keys to send buffer */
152 for (i = 0; envp[i]; i++) {
157 keylen = strlen(key);
159 /* prevent loops in the scripts we execute */
160 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
161 dbg("seems that the event source is not the kernel, just exit");
165 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
166 err("environment buffer too small, probably not called by the kernel");
170 /* remember the SUBSYSTEM */
171 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
172 subsystem = &key[10];
174 dbg("add '%s' to env[%i] buffer", key, i);
175 strcpy(&usend_msg.envbuf[bufpos], key);
176 bufpos += keylen + 1;
178 /* older kernels passed the SUBSYSTEM only as the first argument */
179 if (!subsystem && argc == 2) {
180 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
181 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
184 usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
185 dbg("usend_msg_len=%i", usend_msg_len);
187 /* If we can't send, try to start daemon and resend message */
188 loop = UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND;
190 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
196 if (errno != ECONNREFUSED) {
197 err("error sending message (%s)", strerror(errno));
201 if (!started_daemon) {
202 info("try to start udevd daemon");
203 retval = start_daemon();
205 dbg("error starting daemon");
208 dbg("udevd daemon started");
211 dbg("retry to connect %d", UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND - loop);
212 usleep(1000 * 1000 / UDEVSEND_WAIT_LOOP_PER_SECOND);
217 err("unable to connect to event daemon, try to call udev directly");