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 unsigned char logname[42];
45 static inline char *get_action(void)
49 action = getenv("ACTION");
53 static inline char *get_devpath(void)
57 devpath = getenv("DEVPATH");
61 static inline char *get_seqnum(void)
65 seqnum = getenv("SEQNUM");
69 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
70 char *devpath, char *subsystem, int seqnum)
72 memset(msg, 0x00, sizeof(*msg));
73 strfieldcpy(msg->magic, UDEV_MAGIC);
75 strncpy(msg->action, action, 8);
76 strncpy(msg->devpath, devpath, 128);
77 strncpy(msg->subsystem, subsystem, 16);
78 return sizeof(struct hotplug_msg);
81 static int start_daemon(void)
96 execl(UDEVD_BIN, "udevd", NULL);
97 dbg("exec of daemon failed");
100 dbg("fork of daemon failed");
107 dbg("fork of helper failed");
115 int main(int argc, char* argv[])
117 struct hotplug_msg message;
126 struct timespec tspec;
128 struct sockaddr_un saddr;
130 int started_daemon = 0;
133 init_logging("udevsend");
137 if (subsystem == NULL) {
142 devpath = get_devpath();
143 if (devpath == NULL) {
148 action = get_action();
149 if (action == NULL) {
154 seqnum = get_seqnum();
160 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
162 dbg("error getting socket");
166 memset(&saddr, 0x00, sizeof(saddr));
167 saddr.sun_family = AF_LOCAL;
168 /* use abstract namespace for socket path */
169 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
170 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
172 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
174 /* If we can't send, try to start daemon and resend message */
175 loop = UDEVSEND_CONNECT_RETRY;
177 retval = sendto(sock, &message, size, 0, (struct sockaddr*)&saddr, addrlen);
183 if (errno != ECONNREFUSED) {
184 dbg("error sending message");
188 if (!started_daemon) {
189 dbg("connect failed, try starting daemon...");
190 retval = start_daemon();
192 dbg("error starting daemon");
196 dbg("daemon started");
199 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
201 tspec.tv_nsec = 100000000; /* 100 millisec */
202 nanosleep(&tspec, NULL);