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 udevsend_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 udevsend_msg
));
148 strcpy(usend_msg
.magic
, UDEV_MAGIC
);
150 /* copy all keys to send buffer */
151 for (i
= 0; envp
[i
]; i
++) {
156 keylen
= strlen(key
);
158 /* prevent loops in the scripts we execute */
159 if (strncmp(key
, "UDEVD_EVENT=", 12) == 0) {
160 dbg("seems that the event source is not the kernel, just exit");
164 if (bufpos
+ keylen
>= HOTPLUG_BUFFER_SIZE
-1) {
165 err("environment buffer too small, probably not called by the kernel");
169 /* remember the SUBSYSTEM */
170 if (strncmp(key
, "SUBSYSTEM=", 10) == 0)
171 subsystem
= &key
[10];
173 dbg("add '%s' to env[%i] buffer", key
, i
);
174 strcpy(&usend_msg
.envbuf
[bufpos
], key
);
175 bufpos
+= keylen
+ 1;
177 /* older kernels passed the SUBSYSTEM only as the first argument */
178 if (!subsystem
&& argc
== 2) {
179 bufpos
+= sprintf(&usend_msg
.envbuf
[bufpos
], "SUBSYSTEM=%s", argv
[1]) + 1;
180 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv
[1], i
);
183 usend_msg_len
= offsetof(struct udevsend_msg
, envbuf
) + bufpos
;
184 dbg("usend_msg_len=%i", usend_msg_len
);
186 /* If we can't send, try to start daemon and resend message */
187 loop
= SEND_WAIT_MAX_SECONDS
* SEND_WAIT_LOOP_PER_SECOND
;
189 retval
= sendto(sock
, &usend_msg
, usend_msg_len
, 0, (struct sockaddr
*)&saddr
, addrlen
);
195 if (errno
!= ECONNREFUSED
) {
196 err("error sending message (%s)", strerror(errno
));
200 if (!started_daemon
) {
201 info("try to start udevd daemon");
202 retval
= start_daemon();
204 dbg("error starting daemon");
207 dbg("udevd daemon started");
210 dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS
* SEND_WAIT_LOOP_PER_SECOND
- loop
);
211 usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND
);
216 err("unable to connect to event daemon, try to call udev directly");