]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
[PATCH] udevd race conditions and performance, assorted cleanups - take 2
[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/socket.h>
27 #include <sys/wait.h>
28 #include <sys/un.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <linux/stddef.h>
37
38 #include "udev.h"
39 #include "udev_lib.h"
40 #include "udev_version.h"
41 #include "udevd.h"
42 #include "logging.h"
43
44 #ifdef LOG
45 unsigned char logname[LOGNAME_SIZE];
46 void log_message (int level, const char *format, ...)
47 {
48 va_list args;
49
50 va_start(args, format);
51 vsyslog(level, format, args);
52 va_end(args);
53 }
54 #endif
55
56 static void build_hotplugmsg(struct hotplug_msg *msg, char *action,
57 char *devpath, char *subsystem, int seqnum)
58 {
59 memset(msg, 0x00, sizeof(struct hotplug_msg));
60 strfieldcpy(msg->magic, UDEV_MAGIC);
61 msg->seqnum = seqnum;
62 strfieldcpy(msg->action, action);
63 strfieldcpy(msg->devpath, devpath);
64 strfieldcpy(msg->subsystem, subsystem);
65 }
66
67 static int start_daemon(void)
68 {
69 pid_t pid;
70 pid_t child_pid;
71
72 pid = fork();
73 switch (pid) {
74 case 0:
75 /* helper child */
76 child_pid = fork();
77 switch (child_pid) {
78 case 0:
79 /* daemon */
80 setsid();
81 chdir("/");
82 execl(UDEVD_BIN, "udevd", NULL);
83 dbg("exec of daemon failed");
84 exit(1);
85 case -1:
86 dbg("fork of daemon failed");
87 return -1;
88 default:
89 exit(0);
90 }
91 break;
92 case -1:
93 dbg("fork of helper failed");
94 return -1;
95 default:
96 wait(NULL);
97 }
98 return 0;
99 }
100
101 int main(int argc, char* argv[])
102 {
103 struct hotplug_msg msg;
104 char *action;
105 char *devpath;
106 char *subsystem;
107 char *seqnum;
108 int seq;
109 int retval = 1;
110 int loop;
111 struct timespec tspec;
112 int sock;
113 struct sockaddr_un saddr;
114 socklen_t addrlen;
115 int started_daemon = 0;
116
117 #ifdef DEBUG
118 init_logging("udevsend");
119 #endif
120 dbg("version %s", UDEV_VERSION);
121
122 subsystem = get_subsystem(argv[1]);
123 if (subsystem == NULL) {
124 dbg("no subsystem");
125 goto exit;
126 }
127 dbg("subsystem = '%s'", subsystem);
128
129 devpath = get_devpath();
130 if (devpath == NULL) {
131 dbg("no devpath");
132 goto exit;
133 }
134 dbg("DEVPATH = '%s'", devpath);
135
136 action = get_action();
137 if (action == NULL) {
138 dbg("no action");
139 goto exit;
140 }
141 dbg("ACTION = '%s'", action);
142
143 seqnum = get_seqnum();
144 if (seqnum == NULL)
145 seq = -1;
146 else
147 seq = atoi(seqnum);
148 dbg("SEQNUM = '%d'", seq);
149
150 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
151 if (sock == -1) {
152 dbg("error getting socket");
153 goto exit;
154 }
155
156 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
157 saddr.sun_family = AF_LOCAL;
158 /* use abstract namespace for socket path */
159 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
160 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
161
162 build_hotplugmsg(&msg, action, devpath, subsystem, seq);
163
164 /* If we can't send, try to start daemon and resend message */
165 loop = UDEVSEND_CONNECT_RETRY;
166 while (loop--) {
167 retval = sendto(sock, &msg, sizeof(struct hotplug_msg), 0,
168 (struct sockaddr *)&saddr, addrlen);
169 if (retval != -1) {
170 retval = 0;
171 goto close_and_exit;
172 }
173
174 if (errno != ECONNREFUSED) {
175 dbg("error sending message");
176 goto close_and_exit;
177 }
178
179 if (!started_daemon) {
180 dbg("connect failed, try starting daemon...");
181 retval = start_daemon();
182 if (retval) {
183 dbg("error starting daemon");
184 goto exit;
185 }
186
187 dbg("daemon started");
188 started_daemon = 1;
189 } else {
190 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
191 tspec.tv_sec = 0;
192 tspec.tv_nsec = 100000000; /* 100 millisec */
193 nanosleep(&tspec, NULL);
194 }
195 }
196
197 close_and_exit:
198 close(sock);
199 exit:
200 return retval;
201 }