]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevsend.c
[PATCH] add a "first" list to udevstart and make it contain the class/mem/ devices
[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
e5a2989e
KS
56static void build_hotplugmsg(struct hotplug_msg *msg, char *action,
57 char *devpath, char *subsystem, int seqnum)
7fafc032 58{
e5a2989e 59 memset(msg, 0x00, sizeof(struct hotplug_msg));
53921bfa 60 strfieldcpy(msg->magic, UDEV_MAGIC);
71c077fb 61 msg->seqnum = seqnum;
c472e3c8
KS
62 strfieldcpy(msg->action, action);
63 strfieldcpy(msg->devpath, devpath);
64 strfieldcpy(msg->subsystem, subsystem);
7fafc032
KS
65}
66
33db4b8d
KS
67static int start_daemon(void)
68{
69 pid_t pid;
70 pid_t child_pid;
71
72 pid = fork();
73 switch (pid) {
74 case 0:
75 /* helper child */
76 child_pid = fork();
77 switch (child_pid) {
78 case 0:
79 /* daemon */
a695feae 80 setsid();
2a25816f 81 chdir("/");
35b7d88c 82 execl(UDEVD_BIN, "udevd", NULL);
33db4b8d
KS
83 dbg("exec of daemon failed");
84 exit(1);
85 case -1:
86 dbg("fork of daemon failed");
87 return -1;
88 default:
89 exit(0);
90 }
91 break;
92 case -1:
93 dbg("fork of helper failed");
94 return -1;
95 default:
a695feae 96 wait(NULL);
33db4b8d
KS
97 }
98 return 0;
99}
100
28e169f0
KS
101static void run_udev(const char *subsystem)
102{
103 pid_t pid;
104
105 pid = fork();
106 switch (pid) {
107 case 0:
108 /* child */
109 execl(UDEV_BIN, "udev", subsystem, NULL);
110 dbg("exec of child failed");
111 exit(1);
112 break;
113 case -1:
114 dbg("fork of child failed");
115 break;
116 default:
117 wait(NULL);
118 }
119}
120
7fafc032
KS
121int main(int argc, char* argv[])
122{
0028653c 123 struct hotplug_msg msg;
7fafc032
KS
124 char *action;
125 char *devpath;
126 char *subsystem;
127 char *seqnum;
128 int seq;
2f6cbd19 129 int retval = 1;
33db4b8d
KS
130 int loop;
131 struct timespec tspec;
28e169f0 132 int sock = -1;
53921bfa 133 struct sockaddr_un saddr;
1dadabd7 134 socklen_t addrlen;
2f6cbd19 135 int started_daemon = 0;
7fafc032 136
95a6f4c8 137 init_logging("udevsend");
8bbf2751 138 dbg("version %s", UDEV_VERSION);
95a6f4c8 139
e964c2c0 140 subsystem = get_subsystem(argv[1]);
7fafc032
KS
141 if (subsystem == NULL) {
142 dbg("no subsystem");
143 goto exit;
144 }
8bbf2751 145 dbg("subsystem = '%s'", subsystem);
7fafc032
KS
146
147 devpath = get_devpath();
148 if (devpath == NULL) {
149 dbg("no devpath");
150 goto exit;
151 }
8bbf2751 152 dbg("DEVPATH = '%s'", devpath);
7fafc032
KS
153
154 action = get_action();
155 if (action == NULL) {
156 dbg("no action");
157 goto exit;
158 }
8bbf2751 159 dbg("ACTION = '%s'", action);
7fafc032
KS
160
161 seqnum = get_seqnum();
86590cd5 162 if (seqnum == NULL)
2f6cbd19 163 seq = -1;
86590cd5
KS
164 else
165 seq = atoi(seqnum);
8bbf2751 166 dbg("SEQNUM = '%d'", seq);
7b15897b 167
2f6cbd19 168 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa
KS
169 if (sock == -1) {
170 dbg("error getting socket");
28e169f0 171 goto fallback;
7fafc032
KS
172 }
173
e5a2989e 174 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
53921bfa 175 saddr.sun_family = AF_LOCAL;
872344c4
KS
176 /* use abstract namespace for socket path */
177 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 178 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 179
e5a2989e 180 build_hotplugmsg(&msg, action, devpath, subsystem, seq);
0028653c 181
2f6cbd19
KS
182 /* If we can't send, try to start daemon and resend message */
183 loop = UDEVSEND_CONNECT_RETRY;
184 while (loop--) {
e5a2989e
KS
185 retval = sendto(sock, &msg, sizeof(struct hotplug_msg), 0,
186 (struct sockaddr *)&saddr, addrlen);
2f6cbd19
KS
187 if (retval != -1) {
188 retval = 0;
28e169f0 189 goto exit;
2f6cbd19 190 }
28e169f0 191
2f6cbd19
KS
192 if (errno != ECONNREFUSED) {
193 dbg("error sending message");
28e169f0 194 goto fallback;
2f6cbd19 195 }
28e169f0 196
2f6cbd19 197 if (!started_daemon) {
28e169f0 198 info("starting udevd daemon");
2f6cbd19
KS
199 retval = start_daemon();
200 if (retval) {
28e169f0
KS
201 info("error starting daemon");
202 goto fallback;
2f6cbd19 203 }
53921bfa 204 dbg("daemon started");
2f6cbd19 205 started_daemon = 1;
53921bfa 206 } else {
2f6cbd19
KS
207 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
208 tspec.tv_sec = 0;
209 tspec.tv_nsec = 100000000; /* 100 millisec */
210 nanosleep(&tspec, NULL);
53921bfa 211 }
7fafc032 212 }
28e169f0
KS
213
214fallback:
215 info("unable to connect to event daemon, try to call udev directly");
216 run_udev(subsystem);
217
33db4b8d 218exit:
28e169f0
KS
219 if (sock != -1)
220 close(sock);
221
2f6cbd19 222 return retval;
7fafc032 223}