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>
37 #include "udev_version.h"
41 static inline char *get_action(void)
45 action = getenv("ACTION");
49 static inline char *get_devpath(void)
53 devpath = getenv("DEVPATH");
57 static inline char *get_seqnum(void)
61 seqnum = getenv("SEQNUM");
65 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
66 char *devpath, char *subsystem, int seqnum)
68 memset(msg, 0x00, sizeof(*msg));
69 msg->mtype = HOTPLUGMSGTYPE;
71 strncpy(msg->action, action, 8);
72 strncpy(msg->devpath, devpath, 128);
73 strncpy(msg->subsystem, subsystem, 16);
74 return sizeof(struct hotplug_msg);
77 static int start_daemon(void)
92 execl(UDEVD_BIN, "udevd", NULL);
93 dbg("exec of daemon failed");
96 dbg("fork of daemon failed");
103 dbg("fork of helper failed");
112 int main(int argc, char* argv[])
116 struct msqid_ds msg_queue;
117 struct hotplug_msg message;
123 int retval = -EINVAL;
126 struct timespec tspec;
129 if (subsystem == NULL) {
134 devpath = get_devpath();
135 if (devpath == NULL) {
140 action = get_action();
141 if (action == NULL) {
146 seqnum = get_seqnum();
147 if (seqnum == NULL) {
153 /* create ipc message queue or get id of our existing one */
154 key = ftok(UDEVD_BIN, IPC_KEY_ID);
155 dbg("using ipc queue 0x%0x", key);
156 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
157 msgid = msgget(key, IPC_CREAT);
159 dbg("error open ipc queue");
163 /* send ipc message to the daemon */
164 retval = msgsnd(msgid, &message, size, 0);
166 dbg("error sending ipc message");
170 /* get state of ipc queue */
172 tspec.tv_nsec = 10000000; /* 10 millisec */
173 loop = UDEVSEND_RETRY_COUNT;
175 retval = msgctl(msgid, IPC_STAT, &msg_queue);
177 dbg("error getting info on ipc queue");
180 if (msg_queue.msg_qnum == 0)
182 nanosleep(&tspec, NULL);
185 info("message is still in the ipc queue, starting daemon...");
186 retval = start_daemon();