]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevsend.c
[PATCH] fix up 'make release' to use bk to build the export tree.
[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>
7fafc032
KS
29#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
1dadabd7 32#include <stddef.h>
7fafc032 33#include <string.h>
33db4b8d
KS
34#include <unistd.h>
35#include <time.h>
2f6cbd19 36#include <linux/stddef.h>
7fafc032
KS
37
38#include "udev.h"
35b7d88c 39#include "udev_version.h"
7fafc032
KS
40#include "udevd.h"
41#include "logging.h"
42
95a6f4c8 43unsigned char logname[42];
53921bfa 44
51a8bb2f
GKH
45int log_ok(void)
46{
47 return 1;
48}
49
7fafc032
KS
50static inline char *get_action(void)
51{
52 char *action;
53
54 action = getenv("ACTION");
55 return action;
56}
57
58static inline char *get_devpath(void)
59{
60 char *devpath;
61
62 devpath = getenv("DEVPATH");
63 return devpath;
64}
65
66static inline char *get_seqnum(void)
67{
68 char *seqnum;
69
70 seqnum = getenv("SEQNUM");
71 return seqnum;
72}
73
71c077fb 74static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
7fafc032
KS
75 char *devpath, char *subsystem, int seqnum)
76{
35b7d88c 77 memset(msg, 0x00, sizeof(*msg));
53921bfa 78 strfieldcpy(msg->magic, UDEV_MAGIC);
71c077fb
GKH
79 msg->seqnum = seqnum;
80 strncpy(msg->action, action, 8);
81 strncpy(msg->devpath, devpath, 128);
82 strncpy(msg->subsystem, subsystem, 16);
7fafc032
KS
83 return sizeof(struct hotplug_msg);
84}
85
33db4b8d
KS
86static int start_daemon(void)
87{
88 pid_t pid;
89 pid_t child_pid;
90
91 pid = fork();
92 switch (pid) {
93 case 0:
94 /* helper child */
95 child_pid = fork();
96 switch (child_pid) {
97 case 0:
98 /* daemon */
a695feae 99 setsid();
2a25816f 100 chdir("/");
35b7d88c 101 execl(UDEVD_BIN, "udevd", NULL);
33db4b8d
KS
102 dbg("exec of daemon failed");
103 exit(1);
104 case -1:
105 dbg("fork of daemon failed");
106 return -1;
107 default:
108 exit(0);
109 }
110 break;
111 case -1:
112 dbg("fork of helper failed");
113 return -1;
114 default:
a695feae 115 wait(NULL);
33db4b8d
KS
116 }
117 return 0;
118}
119
7fafc032
KS
120int main(int argc, char* argv[])
121{
0028653c 122 struct hotplug_msg msg;
7fafc032
KS
123 char *action;
124 char *devpath;
125 char *subsystem;
126 char *seqnum;
127 int seq;
2f6cbd19 128 int retval = 1;
7fafc032 129 int size;
33db4b8d
KS
130 int loop;
131 struct timespec tspec;
53921bfa
KS
132 int sock;
133 struct sockaddr_un saddr;
1dadabd7 134 socklen_t addrlen;
2f6cbd19 135 int started_daemon = 0;
0028653c
KS
136 struct iovec iov;
137 struct msghdr smsg;
138 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
139 struct cmsghdr *cmsg;
140 struct ucred *cred;
141
142
7fafc032 143
e047ca9b 144#ifdef DEBUG
95a6f4c8 145 init_logging("udevsend");
e047ca9b 146#endif
95a6f4c8 147
7fafc032
KS
148 subsystem = argv[1];
149 if (subsystem == NULL) {
150 dbg("no subsystem");
151 goto exit;
152 }
153
154 devpath = get_devpath();
155 if (devpath == NULL) {
156 dbg("no devpath");
157 goto exit;
158 }
159
160 action = get_action();
161 if (action == NULL) {
162 dbg("no action");
163 goto exit;
164 }
165
166 seqnum = get_seqnum();
86590cd5 167 if (seqnum == NULL)
2f6cbd19 168 seq = -1;
86590cd5
KS
169 else
170 seq = atoi(seqnum);
7b15897b 171
2f6cbd19 172 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa
KS
173 if (sock == -1) {
174 dbg("error getting socket");
7fafc032
KS
175 goto exit;
176 }
177
53921bfa
KS
178 memset(&saddr, 0x00, sizeof(saddr));
179 saddr.sun_family = AF_LOCAL;
872344c4
KS
180 /* use abstract namespace for socket path */
181 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 182 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 183
0028653c
KS
184 size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
185
186 /* prepare message with credentials to authenticate ourself */
187 iov.iov_base = &msg;
188 iov.iov_len = size;
189
190 smsg.msg_name = &saddr;
191 smsg.msg_namelen = addrlen;
192 smsg.msg_iov = &iov;
193 smsg.msg_iovlen = 1;
194 smsg.msg_control = cred_msg;
195 smsg.msg_controllen = CMSG_LEN(sizeof(struct ucred));;
196 smsg.msg_flags = 0;
197
198 cmsg = CMSG_FIRSTHDR(&smsg);
199 cmsg->cmsg_level = SOL_SOCKET;
200 cmsg->cmsg_type = SCM_CREDENTIALS;
201 cmsg->cmsg_len = sizeof(cred_msg);
202 cred = (struct ucred *) CMSG_DATA(cmsg);
203 cred->uid = getuid();
204 cred->gid = getgid();
205 cred->pid = getpid();
206 cred->pid = getpid();
207
2f6cbd19
KS
208 /* If we can't send, try to start daemon and resend message */
209 loop = UDEVSEND_CONNECT_RETRY;
210 while (loop--) {
0028653c 211 retval = sendmsg(sock, &smsg, 0);
2f6cbd19
KS
212 if (retval != -1) {
213 retval = 0;
214 goto close_and_exit;
215 }
216
217 if (errno != ECONNREFUSED) {
218 dbg("error sending message");
219 goto close_and_exit;
220 }
221
222 if (!started_daemon) {
223 dbg("connect failed, try starting daemon...");
224 retval = start_daemon();
225 if (retval) {
226 dbg("error starting daemon");
227 goto exit;
228 }
229
53921bfa 230 dbg("daemon started");
2f6cbd19 231 started_daemon = 1;
53921bfa 232 } else {
2f6cbd19
KS
233 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
234 tspec.tv_sec = 0;
235 tspec.tv_nsec = 100000000; /* 100 millisec */
236 nanosleep(&tspec, NULL);
53921bfa 237 }
7fafc032 238 }
2f6cbd19
KS
239
240close_and_exit:
241 close(sock);
33db4b8d 242exit:
2f6cbd19 243 return retval;
7fafc032 244}