]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevsend.c
[PATCH] udev - next round of udev event order daemon
[thirdparty/systemd.git] / udevsend.c
CommitLineData
7fafc032
KS
1/*
2 * udevsend.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
7 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
8 *
9 *
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.
13 *
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.
18 *
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.
22 *
23 */
24
25#include <sys/types.h>
26#include <sys/ipc.h>
27#include <sys/msg.h>
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
33db4b8d
KS
32#include <unistd.h>
33#include <time.h>
34#include <wait.h>
7fafc032
KS
35
36#include "udev.h"
37#include "udevd.h"
38#include "logging.h"
39
40static inline char *get_action(void)
41{
42 char *action;
43
44 action = getenv("ACTION");
45 return action;
46}
47
48static inline char *get_devpath(void)
49{
50 char *devpath;
51
52 devpath = getenv("DEVPATH");
53 return devpath;
54}
55
56static inline char *get_seqnum(void)
57{
58 char *seqnum;
59
60 seqnum = getenv("SEQNUM");
61 return seqnum;
62}
63
64static int build_hotplugmsg(struct hotplug_msg **ppmsg, char *action,
65 char *devpath, char *subsystem, int seqnum)
66{
67 struct hotplug_msg *pmsg;
68
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);
75 *ppmsg = pmsg;
76 return sizeof(struct hotplug_msg);
77}
78
79static void free_hotplugmsg(struct hotplug_msg *pmsg)
80{
81 free(pmsg);
82}
83
33db4b8d
KS
84static int start_daemon(void)
85{
86 pid_t pid;
87 pid_t child_pid;
88
89 pid = fork();
90 switch (pid) {
91 case 0:
92 /* helper child */
93 child_pid = fork();
94 switch (child_pid) {
95 case 0:
96 /* daemon */
97 execl(DEFAULT_UDEVD_EXEC, NULL);
98 dbg("exec of daemon failed");
99 exit(1);
100 case -1:
101 dbg("fork of daemon failed");
102 return -1;
103 default:
104 exit(0);
105 }
106 break;
107 case -1:
108 dbg("fork of helper failed");
109 return -1;
110 default:
111 wait(0);
112 }
113 return 0;
114}
115
116
7fafc032
KS
117int main(int argc, char* argv[])
118{
119 int msgid;
120 key_t key;
121 struct msqid_ds msg_queue;
122 struct msgbuf *pmsg;
123 char *action;
124 char *devpath;
125 char *subsystem;
126 char *seqnum;
127 int seq;
128 int retval = -EINVAL;
129 int size;
33db4b8d
KS
130 int loop;
131 struct timespec tspec;
7fafc032
KS
132
133 subsystem = argv[1];
134 if (subsystem == NULL) {
135 dbg("no subsystem");
136 goto exit;
137 }
138
139 devpath = get_devpath();
140 if (devpath == NULL) {
141 dbg("no devpath");
142 goto exit;
143 }
144
145 action = get_action();
146 if (action == NULL) {
147 dbg("no action");
148 goto exit;
149 }
150
151 seqnum = get_seqnum();
152 if (seqnum == NULL) {
153 dbg("no seqnum");
154 goto exit;
155 }
7fafc032 156 seq = atoi(seqnum);
7b15897b 157
90c210eb 158 /* create ipc message queue or get id of our existing one */
33db4b8d 159 key = ftok(DEFAULT_UDEVD_EXEC, IPC_KEY_ID);
7fafc032
KS
160 size = build_hotplugmsg( (struct hotplug_msg**) &pmsg, action, devpath, subsystem, seq);
161 msgid = msgget(key, IPC_CREAT);
7b15897b 162 if (msgid == -1) {
90c210eb 163 dbg("error open ipc queue");
7fafc032
KS
164 goto exit;
165 }
166
90c210eb 167 /* send ipc message to the daemon */
7fafc032
KS
168 retval = msgsnd(msgid, pmsg, size, 0);
169 free_hotplugmsg( (struct hotplug_msg*) pmsg);
7b15897b 170 if (retval == -1) {
90c210eb 171 dbg("error sending ipc message");
7fafc032
KS
172 goto exit;
173 }
7fafc032 174
33db4b8d
KS
175 /* get state of ipc queue */
176 tspec.tv_sec = 0;
177 tspec.tv_nsec = 10000000; /* 10 millisec */
178 loop = 20;
179 while (loop--) {
180 retval = msgctl(msgid, IPC_STAT, &msg_queue);
181 if (retval == -1) {
182 dbg("error getting info on ipc queue");
183 goto exit;
184 }
185 if (msg_queue.msg_qnum == 0)
186 goto exit;
187 nanosleep(&tspec, NULL);
188 }
7fafc032 189
33db4b8d
KS
190 info("message is still in the ipc queue, starting daemon...");
191 retval = start_daemon();
192
193exit:
7fafc032
KS
194 return retval;
195}