4 * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <linux/stddef.h>
36 #include "udev_version.h"
40 /* global variables */
44 void log_message (int priority, const char *format, ...)
48 if (priority > udev_log_priority)
51 va_start(args, format);
52 vsyslog(priority, 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 err("exec of daemon failed");
77 err("fork of daemon failed");
84 err("fork of helper failed");
87 waitpid(pid, NULL, 0);
92 int main(int argc, char *argv[], char *envp[])
94 static struct udevd_msg usend_msg;
98 struct sockaddr_un saddr;
102 int started_daemon = 0;
103 const char *subsystem = NULL;
105 logging_init("udevsend");
109 dbg("version %s", UDEV_VERSION);
111 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
113 err("error getting socket");
117 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
118 saddr.sun_family = AF_LOCAL;
119 /* use abstract namespace for socket path */
120 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
121 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
123 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
124 strcpy(usend_msg.magic, UDEV_MAGIC);
125 usend_msg.type = UDEVD_UEVENT_UDEVSEND;
127 /* copy all keys to send buffer */
128 for (i = 0; envp[i]; i++) {
133 keylen = strlen(key);
135 /* prevent loops in the scripts we execute */
136 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
137 dbg("seems that the event source is not the kernel, just exit");
141 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
142 err("environment buffer too small, probably not called by the kernel");
146 /* remember the SUBSYSTEM */
147 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
148 subsystem = &key[10];
150 dbg("add '%s' to env[%i] buffer", key, i);
151 strcpy(&usend_msg.envbuf[bufpos], key);
152 bufpos += keylen + 1;
154 /* older kernels passed the SUBSYSTEM only as the first argument */
155 if (!subsystem && argc == 2) {
156 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
157 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
160 usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
161 dbg("usend_msg_len=%i", usend_msg_len);
163 /* If we can't send, try to start daemon and resend message */
164 loop = UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND;
166 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
172 if (errno != ECONNREFUSED) {
173 err("error sending message (%s)", strerror(errno));
177 if (!started_daemon) {
178 info("try to start udevd daemon");
179 retval = start_daemon();
181 dbg("error starting daemon");
184 dbg("udevd daemon started");
187 dbg("retry to connect %d", UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND - loop);
188 usleep(1000 * 1000 / UDEVSEND_WAIT_LOOP_PER_SECOND);