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];
50 static inline char *get_action(void)
54 action = getenv("ACTION");
58 static inline char *get_devpath(void)
62 devpath = getenv("DEVPATH");
66 static inline char *get_seqnum(void)
70 seqnum = getenv("SEQNUM");
74 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
75 char *devpath, char *subsystem, int seqnum)
77 memset(msg, 0x00, sizeof(*msg));
78 strfieldcpy(msg->magic, UDEV_MAGIC);
80 strncpy(msg->action, action, 8);
81 strncpy(msg->devpath, devpath, 128);
82 strncpy(msg->subsystem, subsystem, 16);
83 return sizeof(struct hotplug_msg);
86 static int start_daemon(void)
101 execl(UDEVD_BIN, "udevd", NULL);
102 dbg("exec of daemon failed");
105 dbg("fork of daemon failed");
112 dbg("fork of helper failed");
120 int main(int argc, char* argv[])
122 struct hotplug_msg msg;
131 struct timespec tspec;
133 struct sockaddr_un saddr;
135 int started_daemon = 0;
138 init_logging("udevsend");
142 if (subsystem == NULL) {
147 devpath = get_devpath();
148 if (devpath == NULL) {
153 action = get_action();
154 if (action == NULL) {
159 seqnum = get_seqnum();
165 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
167 dbg("error getting socket");
171 memset(&saddr, 0x00, sizeof(saddr));
172 saddr.sun_family = AF_LOCAL;
173 /* use abstract namespace for socket path */
174 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
175 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
177 size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
179 /* If we can't send, try to start daemon and resend message */
180 loop = UDEVSEND_CONNECT_RETRY;
182 retval = sendto(sock, &msg, size, 0, (struct sockaddr *)&saddr, addrlen);
188 if (errno != ECONNREFUSED) {
189 dbg("error sending message");
193 if (!started_daemon) {
194 dbg("connect failed, try starting daemon...");
195 retval = start_daemon();
197 dbg("error starting daemon");
201 dbg("daemon started");
204 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
206 tspec.tv_nsec = 100000000; /* 100 millisec */
207 nanosleep(&tspec, NULL);