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>
37 #include "udev_version.h"
41 unsigned char logname[42];
43 static inline char *get_action(void)
47 action = getenv("ACTION");
51 static inline char *get_devpath(void)
55 devpath = getenv("DEVPATH");
59 static inline char *get_seqnum(void)
63 seqnum = getenv("SEQNUM");
67 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
68 char *devpath, char *subsystem, int seqnum)
70 memset(msg, 0x00, sizeof(*msg));
71 strfieldcpy(msg->magic, UDEV_MAGIC);
73 strncpy(msg->action, action, 8);
74 strncpy(msg->devpath, devpath, 128);
75 strncpy(msg->subsystem, subsystem, 16);
76 return sizeof(struct hotplug_msg);
79 static int start_daemon(void)
94 execl(UDEVD_BIN, "udevd", NULL);
95 dbg("exec of daemon failed");
98 dbg("fork of daemon failed");
105 dbg("fork of helper failed");
113 int main(int argc, char* argv[])
115 struct hotplug_msg message;
121 int retval = -EINVAL;
124 struct timespec tspec;
126 struct sockaddr_un saddr;
129 init_logging("udevsend");
133 if (subsystem == NULL) {
138 devpath = get_devpath();
139 if (devpath == NULL) {
144 action = get_action();
145 if (action == NULL) {
150 seqnum = get_seqnum();
151 if (seqnum == NULL) {
157 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
159 dbg("error getting socket");
163 memset(&saddr, 0x00, sizeof(saddr));
164 saddr.sun_family = AF_LOCAL;
165 strcpy(saddr.sun_path, UDEVD_SOCK);
167 /* try to connect, if it fails start daemon */
168 retval = connect(sock, (struct sockaddr *) &saddr, sizeof(saddr));
172 dbg("connect failed, try starting daemon...");
173 retval = start_daemon();
175 dbg("daemon started");
177 dbg("error starting daemon");
182 /* try to connect while daemon to starts */
184 tspec.tv_nsec = 100000000; /* 100 millisec */
185 loop = UDEVSEND_CONNECT_RETRY;
187 retval = connect(sock, (struct sockaddr *) &saddr, sizeof(saddr));
191 dbg("retry to connect %d",
192 UDEVSEND_CONNECT_RETRY - loop);
193 nanosleep(&tspec, NULL);
195 dbg("error connecting to daemon, start daemon failed");
199 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
200 retval = send(sock, &message, size, 0);
202 dbg("error sending message");