]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
bdc8293df7a2d495d63071469b605240942fdd82
[thirdparty/systemd.git] / udevsend.c
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>
32 #include <unistd.h>
33 #include <time.h>
34 #include <wait.h>
35
36 #include "udev.h"
37 #include "udevd.h"
38 #include "logging.h"
39
40 static inline char *get_action(void)
41 {
42 char *action;
43
44 action = getenv("ACTION");
45 return action;
46 }
47
48 static inline char *get_devpath(void)
49 {
50 char *devpath;
51
52 devpath = getenv("DEVPATH");
53 return devpath;
54 }
55
56 static inline char *get_seqnum(void)
57 {
58 char *seqnum;
59
60 seqnum = getenv("SEQNUM");
61 return seqnum;
62 }
63
64 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
65 char *devpath, char *subsystem, int seqnum)
66 {
67 msg->mtype = HOTPLUGMSGTYPE;
68 msg->seqnum = seqnum;
69 strncpy(msg->action, action, 8);
70 strncpy(msg->devpath, devpath, 128);
71 strncpy(msg->subsystem, subsystem, 16);
72 return sizeof(struct hotplug_msg);
73 }
74
75 static int start_daemon(void)
76 {
77 pid_t pid;
78 pid_t child_pid;
79
80 pid = fork();
81 switch (pid) {
82 case 0:
83 /* helper child */
84 child_pid = fork();
85 switch (child_pid) {
86 case 0:
87 /* daemon */
88 execl(DEFAULT_UDEVD_EXEC, NULL);
89 dbg("exec of daemon failed");
90 exit(1);
91 case -1:
92 dbg("fork of daemon failed");
93 return -1;
94 default:
95 exit(0);
96 }
97 break;
98 case -1:
99 dbg("fork of helper failed");
100 return -1;
101 default:
102 wait(0);
103 }
104 return 0;
105 }
106
107
108 int main(int argc, char* argv[])
109 {
110 int msgid;
111 key_t key;
112 struct msqid_ds msg_queue;
113 struct hotplug_msg message;
114 char *action;
115 char *devpath;
116 char *subsystem;
117 char *seqnum;
118 int seq;
119 int retval = -EINVAL;
120 int size;
121 int loop;
122 struct timespec tspec;
123
124 subsystem = argv[1];
125 if (subsystem == NULL) {
126 dbg("no subsystem");
127 goto exit;
128 }
129
130 devpath = get_devpath();
131 if (devpath == NULL) {
132 dbg("no devpath");
133 goto exit;
134 }
135
136 action = get_action();
137 if (action == NULL) {
138 dbg("no action");
139 goto exit;
140 }
141
142 seqnum = get_seqnum();
143 if (seqnum == NULL) {
144 dbg("no seqnum");
145 goto exit;
146 }
147 seq = atoi(seqnum);
148
149 /* create ipc message queue or get id of our existing one */
150 key = ftok(DEFAULT_UDEVD_EXEC, IPC_KEY_ID);
151 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
152 msgid = msgget(key, IPC_CREAT);
153 if (msgid == -1) {
154 dbg("error open ipc queue");
155 goto exit;
156 }
157
158 /* send ipc message to the daemon */
159 retval = msgsnd(msgid, &message, size, 0);
160 if (retval == -1) {
161 dbg("error sending ipc message");
162 goto exit;
163 }
164
165 /* get state of ipc queue */
166 tspec.tv_sec = 0;
167 tspec.tv_nsec = 10000000; /* 10 millisec */
168 loop = 20;
169 while (loop--) {
170 retval = msgctl(msgid, IPC_STAT, &msg_queue);
171 if (retval == -1) {
172 dbg("error getting info on ipc queue");
173 goto exit;
174 }
175 if (msg_queue.msg_qnum == 0)
176 goto exit;
177 nanosleep(&tspec, NULL);
178 }
179
180 info("message is still in the ipc queue, starting daemon...");
181 retval = start_daemon();
182
183 exit:
184 return retval;
185 }