chiark / gitweb /
[PATCH] add NAME{ignore_remove} attribute
[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         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
136         if (sock == -1) {
137                 dbg("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 udevsend_msg));
148
149         strcpy(usend_msg.magic, UDEV_MAGIC);
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                 if (bufpos + keylen >= HOTPLUG_BUFFER_SIZE-1) {
159                         dbg("environment buffer too small, probably not called by the kernel");
160                         continue;
161                 }
162
163                 /* older kernels do not have the SUBSYSTEM in the environment */
164                 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
165                         subsystem_env = 1;
166
167                 dbg("add '%s' to env[%i] buffer", key, i);
168                 strcpy(&usend_msg.envbuf[bufpos], key);
169                 bufpos += keylen + 1;
170         }
171         if (!subsystem_env) {
172                 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", subsystem_argv) + 1;
173                 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", subsystem_argv, i);
174         }
175
176         usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
177         dbg("usend_msg_len=%i", usend_msg_len);
178
179         /* If we can't send, try to start daemon and resend message */
180         loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
181         while (--loop) {
182                 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
183                 if (retval != -1) {
184                         retval = 0;
185                         goto exit;
186                 }
187
188                 if (errno != ECONNREFUSED) {
189                         dbg("error sending message (%s)", strerror(errno));
190                         goto fallback;
191                 }
192
193                 if (!started_daemon) {
194                         dbg("try to start udevd daemon");
195                         retval = start_daemon();
196                         if (retval) {
197                                 info("error starting daemon");
198                                 goto fallback;
199                         }
200                         info("udevd daemon started");
201                         started_daemon = 1;
202                 } else {
203                         dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
204                         usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
205                 }
206         }
207
208 fallback:
209         info("unable to connect to event daemon, try to call udev directly");
210         run_udev(subsystem_argv);
211
212 exit:
213         if (sock != -1)
214                 close(sock);
215
216         logging_close();
217
218         return retval;
219 }