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>
35 #include <sys/socket.h>
39 #include "udev_version.h"
44 static inline char *get_action(void)
48 action = getenv("ACTION");
52 static inline char *get_devpath(void)
56 devpath = getenv("DEVPATH");
60 static inline char *get_seqnum(void)
64 seqnum = getenv("SEQNUM");
68 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
69 char *devpath, char *subsystem, int seqnum)
71 memset(msg, 0x00, sizeof(*msg));
72 strfieldcpy(msg->magic, UDEV_MAGIC);
74 strncpy(msg->action, action, 8);
75 strncpy(msg->devpath, devpath, 128);
76 strncpy(msg->subsystem, subsystem, 16);
77 return sizeof(struct hotplug_msg);
80 static int start_daemon(void)
95 execl(UDEVD_BIN, "udevd", NULL);
96 dbg("exec of daemon failed");
99 dbg("fork of daemon failed");
106 dbg("fork of helper failed");
114 int main(int argc, char* argv[])
116 struct hotplug_msg message;
122 int retval = -EINVAL;
125 struct timespec tspec;
127 struct sockaddr_un saddr;
130 if (subsystem == NULL) {
135 devpath = get_devpath();
136 if (devpath == NULL) {
141 action = get_action();
142 if (action == NULL) {
147 seqnum = get_seqnum();
148 if (seqnum == NULL) {
154 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
156 dbg("error getting socket");
160 memset(&saddr, 0x00, sizeof(saddr));
161 saddr.sun_family = AF_LOCAL;
162 strcpy(saddr.sun_path, UDEVD_SOCKET);
164 /* try to connect, if it fails start daemon */
165 retval = connect(sock, &saddr, sizeof(saddr));
169 dbg("connect failed, try starting daemon...");
170 retval = start_daemon();
172 dbg("daemon started");
174 dbg("error starting daemon");
179 /* try to connect while daemon to starts */
181 tspec.tv_nsec = 100000000; /* 100 millisec */
182 loop = UDEVSEND_CONNECT_RETRY;
184 retval = connect(sock, &saddr, sizeof(saddr));
188 dbg("retry to connect %d",
189 UDEVSEND_CONNECT_RETRY - loop);
190 nanosleep(&tspec, NULL);
192 dbg("error connecting to daemon, start daemon failed");
196 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
197 retval = send(sock, &message, size, 0);
199 dbg("error sending message");