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 int build_hotplugmsg(struct hotplug_msg *msg, char *action,
56 char *devpath, char *subsystem, int seqnum)
58 memset(msg, 0x00, sizeof(*msg));
59 strfieldcpy(msg->magic, UDEV_MAGIC);
61 strfieldcpy(msg->action, action);
62 strfieldcpy(msg->devpath, devpath);
63 strfieldcpy(msg->subsystem, subsystem);
64 return sizeof(struct hotplug_msg);
67 static int start_daemon(void)
82 execl(UDEVD_BIN, "udevd", NULL);
83 dbg("exec of daemon failed");
86 dbg("fork of daemon failed");
93 dbg("fork of helper failed");
101 int main(int argc, char* argv[])
103 struct hotplug_msg msg;
112 struct timespec tspec;
114 struct sockaddr_un saddr;
116 int started_daemon = 0;
119 init_logging("udevsend");
121 dbg("version %s", UDEV_VERSION);
123 subsystem = get_subsystem(argv[1]);
124 if (subsystem == NULL) {
128 dbg("subsystem = '%s'", subsystem);
130 devpath = get_devpath();
131 if (devpath == NULL) {
135 dbg("DEVPATH = '%s'", devpath);
137 action = get_action();
138 if (action == NULL) {
142 dbg("ACTION = '%s'", action);
144 seqnum = get_seqnum();
149 dbg("SEQNUM = '%d'", seq);
151 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
153 dbg("error getting socket");
157 memset(&saddr, 0x00, sizeof(saddr));
158 saddr.sun_family = AF_LOCAL;
159 /* use abstract namespace for socket path */
160 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
161 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
163 size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
165 /* If we can't send, try to start daemon and resend message */
166 loop = UDEVSEND_CONNECT_RETRY;
168 retval = sendto(sock, &msg, size, 0, (struct sockaddr *)&saddr, addrlen);
174 if (errno != ECONNREFUSED) {
175 dbg("error sending message");
179 if (!started_daemon) {
180 dbg("connect failed, try starting daemon...");
181 retval = start_daemon();
183 dbg("error starting daemon");
187 dbg("daemon started");
190 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
192 tspec.tv_nsec = 100000000; /* 100 millisec */
193 nanosleep(&tspec, NULL);