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 int start_daemon(void)
71 execl(UDEVD_BIN, "udevd", NULL);
72 dbg("exec of daemon failed");
75 dbg("fork of daemon failed");
82 dbg("fork of helper failed");
90 static void run_udev(const char *subsystem)
98 execl(UDEV_BIN, "udev", subsystem, NULL);
99 dbg("exec of child failed");
103 dbg("fork of child failed");
110 int main(int argc, char* argv[])
112 struct hotplug_msg msg;
117 unsigned long long seq;
120 struct timespec tspec;
122 struct sockaddr_un saddr;
124 int started_daemon = 0;
126 init_logging("udevsend");
127 dbg("version %s", UDEV_VERSION);
129 subsystem = get_subsystem(argv[1]);
130 if (subsystem == NULL) {
134 dbg("subsystem = '%s'", subsystem);
136 devpath = get_devpath();
137 if (devpath == NULL) {
141 dbg("DEVPATH = '%s'", devpath);
143 action = get_action();
144 if (action == NULL) {
148 dbg("ACTION = '%s'", action);
150 seqnum = get_seqnum();
154 seq = strtoull(seqnum, NULL, 10);
155 dbg("SEQNUM = '%llu'", seq);
157 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
159 dbg("error getting socket");
163 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
164 saddr.sun_family = AF_LOCAL;
165 /* use abstract namespace for socket path */
166 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
167 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
169 memset(&msg, 0x00, sizeof(struct hotplug_msg));
170 strcpy(msg.magic, UDEV_MAGIC);
172 strfieldcpy(msg.action, action);
173 strfieldcpy(msg.devpath, devpath);
174 strfieldcpy(msg.subsystem, subsystem);
176 /* If we can't send, try to start daemon and resend message */
177 loop = UDEVSEND_CONNECT_RETRY;
179 retval = sendto(sock, &msg, sizeof(struct hotplug_msg), 0,
180 (struct sockaddr *)&saddr, addrlen);
186 if (errno != ECONNREFUSED) {
187 dbg("error sending message");
191 if (!started_daemon) {
192 info("starting udevd daemon");
193 retval = start_daemon();
195 info("error starting daemon");
198 dbg("daemon started");
201 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
203 tspec.tv_nsec = 100000000; /* 100 millisec */
204 nanosleep(&tspec, NULL);
209 info("unable to connect to event daemon, try to call udev directly");