chiark / gitweb /
add .gitignore
[elogind.git] / udevsend.c
1 /*
2  * udevsend.c
3  *
4  * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/un.h>
33 #include <linux/stddef.h>
34
35 #include "udev.h"
36 #include "udev_version.h"
37 #include "udevd.h"
38 #include "logging.h"
39
40 /* global variables */
41 static int sock = -1;
42
43 #ifdef USE_LOG
44 void log_message (int priority, const char *format, ...)
45 {
46         va_list args;
47
48         if (priority > udev_log_priority)
49                 return;
50
51         va_start(args, format);
52         vsyslog(priority, format, args);
53         va_end(args);
54 }
55 #endif
56
57 static int start_daemon(void)
58 {
59         pid_t pid;
60         pid_t child_pid;
61         char *const argv[] = { "udevd", NULL };
62         char *const envp[] = { NULL };
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 with empty environment */
72                         close(sock);
73                         execve(UDEVD_BIN, argv, envp);
74                         err("exec of daemon failed");
75                         _exit(1);
76                 case -1:
77                         err("fork of daemon failed");
78                         return -1;
79                 default:
80                         exit(0);
81                 }
82                 break;
83         case -1:
84                 err("fork of helper failed");
85                 return -1;
86         default:
87                 waitpid(pid, NULL, 0);
88         }
89         return 0;
90 }
91
92 int main(int argc, char *argv[], char *envp[])
93 {
94         static struct udevd_msg usend_msg;
95         int usend_msg_len;
96         int i;
97         int loop;
98         struct sockaddr_un saddr;
99         socklen_t addrlen;
100         int bufpos = 0;
101         int retval = 1;
102         int started_daemon = 0;
103         const char *subsystem = NULL;
104
105         logging_init("udevsend");
106 #ifdef USE_LOG
107         udev_init_config();
108 #endif
109         dbg("version %s", UDEV_VERSION);
110
111         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
112         if (sock == -1) {
113                 err("error getting socket");
114                 goto exit;
115         }
116
117         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
118         saddr.sun_family = AF_LOCAL;
119         /* use abstract namespace for socket path */
120         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
121         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
122
123         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
124         strcpy(usend_msg.magic, UDEV_MAGIC);
125         usend_msg.type = UDEVD_UEVENT_UDEVSEND;
126
127         /* copy all keys to send buffer */
128         for (i = 0; envp[i]; i++) {
129                 const char *key;
130                 int keylen;
131
132                 key = envp[i];
133                 keylen = strlen(key);
134
135                 /* prevent loops in the scripts we execute */
136                 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
137                         dbg("seems that the event source is not the kernel, just exit");
138                         goto exit;
139                 }
140
141                 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
142                         err("environment buffer too small, probably not called by the kernel");
143                         continue;
144                 }
145
146                 /* remember the SUBSYSTEM */
147                 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
148                         subsystem = &key[10];
149
150                 dbg("add '%s' to env[%i] buffer", key, i);
151                 strcpy(&usend_msg.envbuf[bufpos], key);
152                 bufpos += keylen + 1;
153         }
154         /* older kernels passed the SUBSYSTEM only as the first argument */
155         if (!subsystem && argc == 2) {
156                 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
157                 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
158         }
159
160         usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
161         dbg("usend_msg_len=%i", usend_msg_len);
162
163         /* If we can't send, try to start daemon and resend message */
164         loop = UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND;
165         while (--loop) {
166                 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
167                 if (retval != -1) {
168                         retval = 0;
169                         goto exit;
170                 }
171
172                 if (errno != ECONNREFUSED) {
173                         err("error sending message (%s)", strerror(errno));
174                         goto exit;
175                 }
176
177                 if (!started_daemon) {
178                         info("try to start udevd daemon");
179                         retval = start_daemon();
180                         if (retval) {
181                                 dbg("error starting daemon");
182                                 goto exit;
183                         }
184                         dbg("udevd daemon started");
185                         started_daemon = 1;
186                 } else {
187                         dbg("retry to connect %d", UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND - loop);
188                         usleep(1000 * 1000 / UDEVSEND_WAIT_LOOP_PER_SECOND);
189                 }
190         }
191
192 exit:
193         if (sock != -1)
194                 close(sock);
195
196         logging_close();
197
198         return retval;
199 }