]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
[PATCH] udevd - allow to bypass sequence number
[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 <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <time.h>
35
36 #include "udev.h"
37 #include "udev_version.h"
38 #include "udevd.h"
39 #include "logging.h"
40
41 unsigned char logname[42];
42
43 static inline char *get_action(void)
44 {
45 char *action;
46
47 action = getenv("ACTION");
48 return action;
49 }
50
51 static inline char *get_devpath(void)
52 {
53 char *devpath;
54
55 devpath = getenv("DEVPATH");
56 return devpath;
57 }
58
59 static inline char *get_seqnum(void)
60 {
61 char *seqnum;
62
63 seqnum = getenv("SEQNUM");
64 return seqnum;
65 }
66
67 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
68 char *devpath, char *subsystem, int seqnum)
69 {
70 memset(msg, 0x00, sizeof(*msg));
71 strfieldcpy(msg->magic, UDEV_MAGIC);
72 msg->seqnum = seqnum;
73 strncpy(msg->action, action, 8);
74 strncpy(msg->devpath, devpath, 128);
75 strncpy(msg->subsystem, subsystem, 16);
76 return sizeof(struct hotplug_msg);
77 }
78
79 static int start_daemon(void)
80 {
81 pid_t pid;
82 pid_t child_pid;
83
84 pid = fork();
85 switch (pid) {
86 case 0:
87 /* helper child */
88 child_pid = fork();
89 switch (child_pid) {
90 case 0:
91 /* daemon */
92 setsid();
93 chdir("/");
94 execl(UDEVD_BIN, "udevd", NULL);
95 dbg("exec of daemon failed");
96 exit(1);
97 case -1:
98 dbg("fork of daemon failed");
99 return -1;
100 default:
101 exit(0);
102 }
103 break;
104 case -1:
105 dbg("fork of helper failed");
106 return -1;
107 default:
108 wait(NULL);
109 }
110 return 0;
111 }
112
113 int main(int argc, char* argv[])
114 {
115 struct hotplug_msg message;
116 char *action;
117 char *devpath;
118 char *subsystem;
119 char *seqnum;
120 int seq;
121 int retval = -EINVAL;
122 int size;
123 int loop;
124 struct timespec tspec;
125 int sock;
126 struct sockaddr_un saddr;
127
128 #ifdef DEBUG
129 init_logging("udevsend");
130 #endif
131
132 subsystem = argv[1];
133 if (subsystem == NULL) {
134 dbg("no subsystem");
135 goto exit;
136 }
137
138 devpath = get_devpath();
139 if (devpath == NULL) {
140 dbg("no devpath");
141 goto exit;
142 }
143
144 action = get_action();
145 if (action == NULL) {
146 dbg("no action");
147 goto exit;
148 }
149
150 seqnum = get_seqnum();
151 if (seqnum == NULL)
152 seq = 0;
153 else
154 seq = atoi(seqnum);
155
156 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
157 if (sock == -1) {
158 dbg("error getting socket");
159 goto exit;
160 }
161
162 memset(&saddr, 0x00, sizeof(saddr));
163 saddr.sun_family = AF_LOCAL;
164 strcpy(saddr.sun_path, UDEVD_SOCK);
165
166 /* try to connect, if it fails start daemon */
167 retval = connect(sock, (struct sockaddr *) &saddr, sizeof(saddr));
168 if (retval != -1) {
169 goto send;
170 } else {
171 dbg("connect failed, try starting daemon...");
172 retval = start_daemon();
173 if (retval == 0) {
174 dbg("daemon started");
175 } else {
176 dbg("error starting daemon");
177 goto exit;
178 }
179 }
180
181 /* try to connect while daemon to starts */
182 tspec.tv_sec = 0;
183 tspec.tv_nsec = 100000000; /* 100 millisec */
184 loop = UDEVSEND_CONNECT_RETRY;
185 while (loop--) {
186 retval = connect(sock, (struct sockaddr *) &saddr, sizeof(saddr));
187 if (retval != -1)
188 goto send;
189 else
190 dbg("retry to connect %d",
191 UDEVSEND_CONNECT_RETRY - loop);
192 nanosleep(&tspec, NULL);
193 }
194 dbg("error connecting to daemon, start daemon failed");
195 goto exit;
196
197 send:
198 size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
199 retval = send(sock, &message, size, 0);
200 if (retval == -1) {
201 dbg("error sending message");
202 close (sock);
203 goto exit;
204 }
205 close (sock);
206 return 0;
207
208 exit:
209 return 1;
210 }