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