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