chiark / gitweb /
[PATCH] udevd: serialization of the event sequence of a chain of devices
[elogind.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_version.h"
40 #include "udevd.h"
41 #include "logging.h"
42
43 /* global variables */
44 static int sock = -1;
45
46 #ifdef LOG
47 unsigned char logname[LOGNAME_SIZE];
48 void log_message (int level, const char *format, ...)
49 {
50         va_list args;
51
52         va_start(args, format);
53         vsyslog(level, format, args);
54         va_end(args);
55 }
56 #endif
57
58 static int start_daemon(void)
59 {
60         pid_t pid;
61         pid_t child_pid;
62         char *const argv[] = { "udevd", NULL };
63         char *const envp[] = { NULL };
64
65         pid = fork();
66         switch (pid) {
67         case 0:
68                 /* helper child */
69                 child_pid = fork();
70                 switch (child_pid) {
71                 case 0:
72                         /* daemon with empty environment */
73                         close(sock);
74                         execve(UDEVD_BIN, argv, envp);
75                         dbg("exec of daemon failed");
76                         _exit(1);
77                 case -1:
78                         dbg("fork of daemon failed");
79                         return -1;
80                 default:
81                         exit(0);
82                 }
83                 break;
84         case -1:
85                 dbg("fork of helper failed");
86                 return -1;
87         default:
88                 waitpid(pid, NULL, 0);
89         }
90         return 0;
91 }
92
93 static void run_udev(const char *subsystem)
94 {
95         char *const argv[] = { "udev", (char *)subsystem, NULL };
96         pid_t pid;
97
98         pid = fork();
99         switch (pid) {
100         case 0:
101                 /* child */
102                 execv(UDEV_BIN, argv);
103                 dbg("exec of child failed");
104                 _exit(1);
105                 break;
106         case -1:
107                 dbg("fork of child failed");
108                 break;
109         default:
110                 waitpid(pid, NULL, 0);
111         }
112 }
113
114 int main(int argc, char *argv[], char *envp[])
115 {
116         static struct udevsend_msg usend_msg;
117         int usend_msg_len;
118         int i;
119         int loop;
120         struct sockaddr_un saddr;
121         socklen_t addrlen;
122         int bufpos = 0;
123         int retval = 1;
124         int started_daemon = 0;
125         const char *subsystem = NULL;
126
127         logging_init("udevsend");
128         dbg("version %s", UDEV_VERSION);
129
130         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
131         if (sock == -1) {
132                 dbg("error getting socket");
133                 goto fallback;
134         }
135
136         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
137         saddr.sun_family = AF_LOCAL;
138         /* use abstract namespace for socket path */
139         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
140         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
141
142         memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
143         strcpy(usend_msg.magic, UDEV_MAGIC);
144
145         /* copy all keys to send buffer */
146         for (i = 0; envp[i]; i++) {
147                 const char *key;
148                 int keylen;
149
150                 key = envp[i];
151                 keylen = strlen(key);
152                 if (bufpos + keylen >= HOTPLUG_BUFFER_SIZE-1) {
153                         dbg("environment buffer too small, probably not called by the kernel");
154                         continue;
155                 }
156
157                 /* prevent loops in the scripts we execute */
158                 if (strncmp(key, "MANAGED_EVENT=", 14) == 0) {
159                         dbg("seems that the event source is not the kernel, just exit");
160                         goto exit;
161                 }
162
163                 /* remember the SUBSYSTEM */
164                 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
165                         subsystem = &key[10];
166
167                 dbg("add '%s' to env[%i] buffer", key, i);
168                 strcpy(&usend_msg.envbuf[bufpos], key);
169                 bufpos += keylen + 1;
170         }
171         /* older kernels passed the SUBSYSTEM only as the first argument */
172         if (!subsystem && argc == 2) {
173                 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
174                 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
175         }
176
177         usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
178         dbg("usend_msg_len=%i", usend_msg_len);
179
180         /* If we can't send, try to start daemon and resend message */
181         loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
182         while (--loop) {
183                 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
184                 if (retval != -1) {
185                         retval = 0;
186                         goto exit;
187                 }
188
189                 if (errno != ECONNREFUSED) {
190                         dbg("error sending message (%s)", strerror(errno));
191                         goto fallback;
192                 }
193
194                 if (!started_daemon) {
195                         dbg("try to start udevd daemon");
196                         retval = start_daemon();
197                         if (retval) {
198                                 info("error starting daemon");
199                                 goto fallback;
200                         }
201                         info("udevd daemon started");
202                         started_daemon = 1;
203                 } else {
204                         dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
205                         usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
206                 }
207         }
208
209 fallback:
210         info("unable to connect to event daemon, try to call udev directly");
211         run_udev(subsystem);
212
213 exit:
214         if (sock != -1)
215                 close(sock);
216
217         logging_close();
218
219         return retval;
220 }