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"
44 unsigned char logname[42];
45 void log_message (int level, const char *format, ...)
49 va_start(args, format);
50 vsyslog(level, format, args);
55 static inline char *get_action(void)
59 action = getenv("ACTION");
63 static inline char *get_devpath(void)
67 devpath = getenv("DEVPATH");
71 static inline char *get_seqnum(void)
75 seqnum = getenv("SEQNUM");
79 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
80 char *devpath, char *subsystem, int seqnum)
82 memset(msg, 0x00, sizeof(*msg));
83 strfieldcpy(msg->magic, UDEV_MAGIC);
85 strncpy(msg->action, action, 8);
86 strncpy(msg->devpath, devpath, 128);
87 strncpy(msg->subsystem, subsystem, 16);
88 return sizeof(struct hotplug_msg);
91 static int start_daemon(void)
106 execl(UDEVD_BIN, "udevd", NULL);
107 dbg("exec of daemon failed");
110 dbg("fork of daemon failed");
117 dbg("fork of helper failed");
125 int main(int argc, char* argv[])
127 struct hotplug_msg msg;
136 struct timespec tspec;
138 struct sockaddr_un saddr;
140 int started_daemon = 0;
143 init_logging("udevsend");
145 dbg("version %s", UDEV_VERSION);
148 if (subsystem == NULL) {
152 dbg("subsystem = '%s'", subsystem);
154 devpath = get_devpath();
155 if (devpath == NULL) {
159 dbg("DEVPATH = '%s'", devpath);
161 action = get_action();
162 if (action == NULL) {
166 dbg("ACTION = '%s'", action);
168 seqnum = get_seqnum();
173 dbg("SEQNUM = '%d'", seq);
175 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
177 dbg("error getting socket");
181 memset(&saddr, 0x00, sizeof(saddr));
182 saddr.sun_family = AF_LOCAL;
183 /* use abstract namespace for socket path */
184 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
185 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
187 size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
189 /* If we can't send, try to start daemon and resend message */
190 loop = UDEVSEND_CONNECT_RETRY;
192 retval = sendto(sock, &msg, size, 0, (struct sockaddr *)&saddr, addrlen);
198 if (errno != ECONNREFUSED) {
199 dbg("error sending message");
203 if (!started_daemon) {
204 dbg("connect failed, try starting daemon...");
205 retval = start_daemon();
207 dbg("error starting daemon");
211 dbg("daemon started");
214 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
216 tspec.tv_nsec = 100000000; /* 100 millisec */
217 nanosleep(&tspec, NULL);