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>
40 static inline char *get_action(void)
44 action = getenv("ACTION");
48 static inline char *get_devpath(void)
52 devpath = getenv("DEVPATH");
56 static inline char *get_seqnum(void)
60 seqnum = getenv("SEQNUM");
64 static int build_hotplugmsg(struct hotplug_msg **ppmsg, char *action,
65 char *devpath, char *subsystem, int seqnum)
67 struct hotplug_msg *pmsg;
69 pmsg = malloc(sizeof(struct hotplug_msg));
70 pmsg->mtype = HOTPLUGMSGTYPE;
71 pmsg->seqnum = seqnum;
72 strncpy(pmsg->action, action, 8);
73 strncpy(pmsg->devpath, devpath, 128);
74 strncpy(pmsg->subsystem, subsystem, 16);
76 return sizeof(struct hotplug_msg);
79 static void free_hotplugmsg(struct hotplug_msg *pmsg)
84 static int start_daemon(void)
97 execl(DEFAULT_UDEVD_EXEC, NULL);
98 dbg("exec of daemon failed");
101 dbg("fork of daemon failed");
108 dbg("fork of helper failed");
117 int main(int argc, char* argv[])
121 struct msqid_ds msg_queue;
128 int retval = -EINVAL;
131 struct timespec tspec;
134 if (subsystem == NULL) {
139 devpath = get_devpath();
140 if (devpath == NULL) {
145 action = get_action();
146 if (action == NULL) {
151 seqnum = get_seqnum();
152 if (seqnum == NULL) {
158 /* create ipc message queue or get id of our existing one */
159 key = ftok(DEFAULT_UDEVD_EXEC, IPC_KEY_ID);
160 size = build_hotplugmsg( (struct hotplug_msg**) &pmsg, action, devpath, subsystem, seq);
161 msgid = msgget(key, IPC_CREAT);
163 dbg("error open ipc queue");
167 /* send ipc message to the daemon */
168 retval = msgsnd(msgid, pmsg, size, 0);
169 free_hotplugmsg( (struct hotplug_msg*) pmsg);
171 dbg("error sending ipc message");
175 /* get state of ipc queue */
177 tspec.tv_nsec = 10000000; /* 10 millisec */
180 retval = msgctl(msgid, IPC_STAT, &msg_queue);
182 dbg("error getting info on ipc queue");
185 if (msg_queue.msg_qnum == 0)
187 nanosleep(&tspec, NULL);
190 info("message is still in the ipc queue, starting daemon...");
191 retval = start_daemon();