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>
40 #include "udev_version.h"
45 unsigned char logname[LOGNAME_SIZE];
46 void log_message (int level, const char *format, ...)
50 va_start(args, format);
51 vsyslog(level, format, args);
56 static void build_hotplugmsg(struct hotplug_msg *msg, char *action,
57 char *devpath, char *subsystem, int seqnum)
59 memset(msg, 0x00, sizeof(struct hotplug_msg));
60 strfieldcpy(msg->magic, UDEV_MAGIC);
62 strfieldcpy(msg->action, action);
63 strfieldcpy(msg->devpath, devpath);
64 strfieldcpy(msg->subsystem, subsystem);
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 static void run_udev(const char *subsystem)
109 execl(UDEV_BIN, "udev", subsystem, NULL);
110 dbg("exec of child failed");
114 dbg("fork of child failed");
121 int main(int argc, char* argv[])
123 struct hotplug_msg msg;
131 struct timespec tspec;
133 struct sockaddr_un saddr;
135 int started_daemon = 0;
137 init_logging("udevsend");
138 dbg("version %s", UDEV_VERSION);
140 subsystem = get_subsystem(argv[1]);
141 if (subsystem == NULL) {
145 dbg("subsystem = '%s'", subsystem);
147 devpath = get_devpath();
148 if (devpath == NULL) {
152 dbg("DEVPATH = '%s'", devpath);
154 action = get_action();
155 if (action == NULL) {
159 dbg("ACTION = '%s'", action);
161 seqnum = get_seqnum();
166 dbg("SEQNUM = '%d'", seq);
168 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
170 dbg("error getting socket");
174 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
175 saddr.sun_family = AF_LOCAL;
176 /* use abstract namespace for socket path */
177 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
178 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
180 build_hotplugmsg(&msg, action, devpath, subsystem, seq);
182 /* If we can't send, try to start daemon and resend message */
183 loop = UDEVSEND_CONNECT_RETRY;
185 retval = sendto(sock, &msg, sizeof(struct hotplug_msg), 0,
186 (struct sockaddr *)&saddr, addrlen);
192 if (errno != ECONNREFUSED) {
193 dbg("error sending message");
197 if (!started_daemon) {
198 info("starting udevd daemon");
199 retval = start_daemon();
201 info("error starting daemon");
204 dbg("daemon started");
207 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
209 tspec.tv_nsec = 100000000; /* 100 millisec */
210 nanosleep(&tspec, NULL);
215 info("unable to connect to event daemon, try to call udev directly");