]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
[PATCH] selinux: fix handling during creation of symlinks
[thirdparty/systemd.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 udevsend_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 udevsend_msg));
148 strcpy(usend_msg.magic, UDEV_MAGIC);
149
150 /* copy all keys to send buffer */
151 for (i = 0; envp[i]; i++) {
152 const char *key;
153 int keylen;
154
155 key = envp[i];
156 keylen = strlen(key);
157
158 /* prevent loops in the scripts we execute */
159 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
160 dbg("seems that the event source is not the kernel, just exit");
161 goto exit;
162 }
163
164 if (bufpos + keylen >= HOTPLUG_BUFFER_SIZE-1) {
165 err("environment buffer too small, probably not called by the kernel");
166 continue;
167 }
168
169 /* remember the SUBSYSTEM */
170 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
171 subsystem = &key[10];
172
173 dbg("add '%s' to env[%i] buffer", key, i);
174 strcpy(&usend_msg.envbuf[bufpos], key);
175 bufpos += keylen + 1;
176 }
177 /* older kernels passed the SUBSYSTEM only as the first argument */
178 if (!subsystem && argc == 2) {
179 bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
180 dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
181 }
182
183 usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
184 dbg("usend_msg_len=%i", usend_msg_len);
185
186 /* If we can't send, try to start daemon and resend message */
187 loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
188 while (--loop) {
189 retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
190 if (retval != -1) {
191 retval = 0;
192 goto exit;
193 }
194
195 if (errno != ECONNREFUSED) {
196 err("error sending message (%s)", strerror(errno));
197 goto fallback;
198 }
199
200 if (!started_daemon) {
201 info("try to start udevd daemon");
202 retval = start_daemon();
203 if (retval) {
204 dbg("error starting daemon");
205 goto fallback;
206 }
207 dbg("udevd daemon started");
208 started_daemon = 1;
209 } else {
210 dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
211 usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
212 }
213 }
214
215 fallback:
216 err("unable to connect to event daemon, try to call udev directly");
217 run_udev(subsystem);
218
219 exit:
220 if (sock != -1)
221 close(sock);
222
223 logging_close();
224
225 return retval;
226 }