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>
38 #include "udev_version.h"
42 unsigned char logname[42];
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;
131 init_logging("udevsend");
135 if (subsystem == NULL) {
140 devpath = get_devpath();
141 if (devpath == NULL) {
146 action = get_action();
147 if (action == NULL) {
152 seqnum = get_seqnum();
158 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
160 dbg("error getting socket");
164 memset(&saddr, 0x00, sizeof(saddr));
165 saddr.sun_family = AF_LOCAL;
166 /* use abstract namespace for socket path */
167 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
168 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
170 /* try to connect, if it fails start daemon */
171 retval = connect(sock, (struct sockaddr *) &saddr, addrlen);
175 dbg("connect failed, try starting daemon...");
176 retval = start_daemon();
178 dbg("daemon started");
180 dbg("error starting daemon");
185 /* try to connect while daemon to starts */
187 tspec.tv_nsec = 100000000; /* 100 millisec */
188 loop = UDEVSEND_CONNECT_RETRY;
190 retval = connect(sock, (struct sockaddr *) &saddr, sizeof(saddr));
194 dbg("retry to connect %d",
195 UDEVSEND_CONNECT_RETRY - loop);
196 nanosleep(&tspec, NULL);
198 dbg("error connecting to daemon, start daemon failed");
202 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
203 retval = send(sock, &message, size, 0);
205 dbg("error sending message");