]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
[PATCH] udevd - cleanup and better timeout handling
[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 #include <sys/socket.h>
36 #include <sys/un.h>
37
38 #include "udev.h"
39 #include "udev_version.h"
40 #include "udevd.h"
41 #include "logging.h"
42
43
44 static inline char *get_action(void)
45 {
46 char *action;
47
48 action = getenv("ACTION");
49 return action;
50 }
51
52 static inline char *get_devpath(void)
53 {
54 char *devpath;
55
56 devpath = getenv("DEVPATH");
57 return devpath;
58 }
59
60 static inline char *get_seqnum(void)
61 {
62 char *seqnum;
63
64 seqnum = getenv("SEQNUM");
65 return seqnum;
66 }
67
68 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
69 char *devpath, char *subsystem, int seqnum)
70 {
71 memset(msg, 0x00, sizeof(*msg));
72 strfieldcpy(msg->magic, UDEV_MAGIC);
73 msg->seqnum = seqnum;
74 strncpy(msg->action, action, 8);
75 strncpy(msg->devpath, devpath, 128);
76 strncpy(msg->subsystem, subsystem, 16);
77 return sizeof(struct hotplug_msg);
78 }
79
80 static int start_daemon(void)
81 {
82 pid_t pid;
83 pid_t child_pid;
84
85 pid = fork();
86 switch (pid) {
87 case 0:
88 /* helper child */
89 child_pid = fork();
90 switch (child_pid) {
91 case 0:
92 /* daemon */
93 setsid();
94 chdir("/");
95 execl(UDEVD_BIN, "udevd", NULL);
96 dbg("exec of daemon failed");
97 exit(1);
98 case -1:
99 dbg("fork of daemon failed");
100 return -1;
101 default:
102 exit(0);
103 }
104 break;
105 case -1:
106 dbg("fork of helper failed");
107 return -1;
108 default:
109 wait(NULL);
110 }
111 return 0;
112 }
113
114 int main(int argc, char* argv[])
115 {
116 struct hotplug_msg message;
117 char *action;
118 char *devpath;
119 char *subsystem;
120 char *seqnum;
121 int seq;
122 int retval = -EINVAL;
123 int size;
124 int loop;
125 struct timespec tspec;
126 int sock;
127 struct sockaddr_un saddr;
128
129 subsystem = argv[1];
130 if (subsystem == NULL) {
131 dbg("no subsystem");
132 goto exit;
133 }
134
135 devpath = get_devpath();
136 if (devpath == NULL) {
137 dbg("no devpath");
138 goto exit;
139 }
140
141 action = get_action();
142 if (action == NULL) {
143 dbg("no action");
144 goto exit;
145 }
146
147 seqnum = get_seqnum();
148 if (seqnum == NULL) {
149 dbg("no seqnum");
150 goto exit;
151 }
152 seq = atoi(seqnum);
153
154 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
155 if (sock == -1) {
156 dbg("error getting socket");
157 goto exit;
158 }
159
160 memset(&saddr, 0x00, sizeof(saddr));
161 saddr.sun_family = AF_LOCAL;
162 strcpy(saddr.sun_path, UDEVD_SOCKET);
163
164 /* try to connect, if it fails start daemon */
165 retval = connect(sock, &saddr, sizeof(saddr));
166 if (retval != -1) {
167 goto send;
168 } else {
169 dbg("connect failed, try starting daemon...");
170 retval = start_daemon();
171 if (retval == 0) {
172 dbg("daemon started");
173 } else {
174 dbg("error starting daemon");
175 goto exit;
176 }
177 }
178
179 /* try to connect while daemon to starts */
180 tspec.tv_sec = 0;
181 tspec.tv_nsec = 100000000; /* 100 millisec */
182 loop = UDEVSEND_CONNECT_RETRY;
183 while (loop--) {
184 retval = connect(sock, &saddr, sizeof(saddr));
185 if (retval != -1)
186 goto send;
187 else
188 dbg("retry to connect %d",
189 UDEVSEND_CONNECT_RETRY - loop);
190 nanosleep(&tspec, NULL);
191 }
192 dbg("error connecting to daemon, start daemon failed");
193 goto exit;
194
195 send:
196 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
197 retval = send(sock, &message, size, 0);
198 if (retval == -1) {
199 dbg("error sending message");
200 close (sock);
201 goto exit;
202 }
203 close (sock);
204 return 0;
205
206 exit:
207 return 1;
208 }