]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevsend.c
[PATCH] make udev-test.pl test for root permissions before running
[thirdparty/systemd.git] / udevsend.c
CommitLineData
7fafc032
KS
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>
0be0c18d
GKH
26#include <sys/socket.h>
27#include <sys/wait.h>
28#include <sys/un.h>
e5a2989e 29#include <time.h>
7fafc032
KS
30#include <errno.h>
31#include <stdio.h>
32#include <stdlib.h>
1dadabd7 33#include <stddef.h>
7fafc032 34#include <string.h>
33db4b8d 35#include <unistd.h>
2f6cbd19 36#include <linux/stddef.h>
7fafc032
KS
37
38#include "udev.h"
c81b35c0 39#include "udev_lib.h"
35b7d88c 40#include "udev_version.h"
7fafc032
KS
41#include "udevd.h"
42#include "logging.h"
43
d026a35d 44#ifdef LOG
d00bd172 45unsigned char logname[LOGNAME_SIZE];
d026a35d 46void log_message (int level, const char *format, ...)
51a8bb2f 47{
d026a35d
GKH
48 va_list args;
49
50 va_start(args, format);
51 vsyslog(level, format, args);
52 va_end(args);
51a8bb2f 53}
d026a35d 54#endif
51a8bb2f 55
33db4b8d
KS
56static int start_daemon(void)
57{
58 pid_t pid;
59 pid_t child_pid;
60
61 pid = fork();
62 switch (pid) {
63 case 0:
64 /* helper child */
65 child_pid = fork();
66 switch (child_pid) {
67 case 0:
68 /* daemon */
a695feae 69 setsid();
2a25816f 70 chdir("/");
35b7d88c 71 execl(UDEVD_BIN, "udevd", NULL);
33db4b8d
KS
72 dbg("exec of daemon failed");
73 exit(1);
74 case -1:
75 dbg("fork of daemon failed");
76 return -1;
77 default:
78 exit(0);
79 }
80 break;
81 case -1:
82 dbg("fork of helper failed");
83 return -1;
84 default:
e920fed3 85 waitpid(pid, NULL, 0);
33db4b8d
KS
86 }
87 return 0;
88}
89
28e169f0
KS
90static void run_udev(const char *subsystem)
91{
92 pid_t pid;
93
94 pid = fork();
95 switch (pid) {
96 case 0:
97 /* child */
5cab7caa 98 execl(UDEV_BIN, UDEV_BIN, subsystem, NULL);
28e169f0 99 dbg("exec of child failed");
5cab7caa 100 _exit(1);
28e169f0
KS
101 break;
102 case -1:
103 dbg("fork of child failed");
104 break;
105 default:
e920fed3 106 waitpid(pid, NULL, 0);
28e169f0
KS
107 }
108}
109
7fafc032
KS
110int main(int argc, char* argv[])
111{
0028653c 112 struct hotplug_msg msg;
7fafc032
KS
113 char *action;
114 char *devpath;
115 char *subsystem;
116 char *seqnum;
cdc60e8a 117 unsigned long long seq;
2f6cbd19 118 int retval = 1;
33db4b8d 119 int loop;
28e169f0 120 int sock = -1;
53921bfa 121 struct sockaddr_un saddr;
1dadabd7 122 socklen_t addrlen;
2f6cbd19 123 int started_daemon = 0;
7fafc032 124
7257cb18 125 logging_init("udevsend");
8bbf2751 126 dbg("version %s", UDEV_VERSION);
95a6f4c8 127
e964c2c0 128 subsystem = get_subsystem(argv[1]);
7fafc032
KS
129 if (subsystem == NULL) {
130 dbg("no subsystem");
131 goto exit;
132 }
8bbf2751 133 dbg("subsystem = '%s'", subsystem);
7fafc032
KS
134
135 devpath = get_devpath();
136 if (devpath == NULL) {
137 dbg("no devpath");
138 goto exit;
139 }
8bbf2751 140 dbg("DEVPATH = '%s'", devpath);
7fafc032
KS
141
142 action = get_action();
143 if (action == NULL) {
144 dbg("no action");
145 goto exit;
146 }
8bbf2751 147 dbg("ACTION = '%s'", action);
7fafc032
KS
148
149 seqnum = get_seqnum();
86590cd5 150 if (seqnum == NULL)
cdc60e8a 151 seq = 0;
86590cd5 152 else
cdc60e8a
KS
153 seq = strtoull(seqnum, NULL, 10);
154 dbg("SEQNUM = '%llu'", seq);
7b15897b 155
2f6cbd19 156 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa
KS
157 if (sock == -1) {
158 dbg("error getting socket");
28e169f0 159 goto fallback;
7fafc032
KS
160 }
161
6e3e3c34
HH
162 set_cloexec_flag(sock, 1);
163
e5a2989e 164 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
53921bfa 165 saddr.sun_family = AF_LOCAL;
872344c4
KS
166 /* use abstract namespace for socket path */
167 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 168 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 169
cdc60e8a
KS
170 memset(&msg, 0x00, sizeof(struct hotplug_msg));
171 strcpy(msg.magic, UDEV_MAGIC);
172 msg.seqnum = seq;
173 strfieldcpy(msg.action, action);
174 strfieldcpy(msg.devpath, devpath);
175 strfieldcpy(msg.subsystem, subsystem);
0028653c 176
2f6cbd19 177 /* If we can't send, try to start daemon and resend message */
5cab7caa
KS
178 loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
179 while (--loop) {
e5a2989e
KS
180 retval = sendto(sock, &msg, sizeof(struct hotplug_msg), 0,
181 (struct sockaddr *)&saddr, addrlen);
2f6cbd19
KS
182 if (retval != -1) {
183 retval = 0;
28e169f0 184 goto exit;
2f6cbd19 185 }
28e169f0 186
2f6cbd19
KS
187 if (errno != ECONNREFUSED) {
188 dbg("error sending message");
28e169f0 189 goto fallback;
2f6cbd19 190 }
28e169f0 191
2f6cbd19 192 if (!started_daemon) {
28e169f0 193 info("starting udevd daemon");
2f6cbd19
KS
194 retval = start_daemon();
195 if (retval) {
28e169f0
KS
196 info("error starting daemon");
197 goto fallback;
2f6cbd19 198 }
53921bfa 199 dbg("daemon started");
2f6cbd19 200 started_daemon = 1;
53921bfa 201 } else {
5cab7caa
KS
202 dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
203 usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
53921bfa 204 }
7fafc032 205 }
28e169f0
KS
206
207fallback:
208 info("unable to connect to event daemon, try to call udev directly");
209 run_udev(subsystem);
210
33db4b8d 211exit:
28e169f0
KS
212 if (sock != -1)
213 close(sock);
214
7257cb18
KS
215 logging_close();
216
2f6cbd19 217 return retval;
7fafc032 218}