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