]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevsend.c
[PATCH] 022 release
[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
d026a35d 43#ifdef LOG
d00bd172 44unsigned char logname[LOGNAME_SIZE];
d026a35d 45void log_message (int level, const char *format, ...)
51a8bb2f 46{
d026a35d
GKH
47 va_list args;
48
49 va_start(args, format);
50 vsyslog(level, format, args);
51 va_end(args);
51a8bb2f 52}
d026a35d 53#endif
51a8bb2f 54
71c077fb 55static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
7fafc032
KS
56 char *devpath, char *subsystem, int seqnum)
57{
35b7d88c 58 memset(msg, 0x00, sizeof(*msg));
53921bfa 59 strfieldcpy(msg->magic, UDEV_MAGIC);
71c077fb 60 msg->seqnum = seqnum;
c472e3c8
KS
61 strfieldcpy(msg->action, action);
62 strfieldcpy(msg->devpath, devpath);
63 strfieldcpy(msg->subsystem, subsystem);
7fafc032
KS
64 return sizeof(struct hotplug_msg);
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
7fafc032
KS
101int main(int argc, char* argv[])
102{
0028653c 103 struct hotplug_msg msg;
7fafc032
KS
104 char *action;
105 char *devpath;
106 char *subsystem;
107 char *seqnum;
108 int seq;
2f6cbd19 109 int retval = 1;
7fafc032 110 int size;
33db4b8d
KS
111 int loop;
112 struct timespec tspec;
53921bfa
KS
113 int sock;
114 struct sockaddr_un saddr;
1dadabd7 115 socklen_t addrlen;
2f6cbd19 116 int started_daemon = 0;
7fafc032 117
e047ca9b 118#ifdef DEBUG
95a6f4c8 119 init_logging("udevsend");
e047ca9b 120#endif
8bbf2751 121 dbg("version %s", UDEV_VERSION);
95a6f4c8 122
e964c2c0 123 subsystem = get_subsystem(argv[1]);
7fafc032
KS
124 if (subsystem == NULL) {
125 dbg("no subsystem");
126 goto exit;
127 }
8bbf2751 128 dbg("subsystem = '%s'", subsystem);
7fafc032
KS
129
130 devpath = get_devpath();
131 if (devpath == NULL) {
132 dbg("no devpath");
133 goto exit;
134 }
8bbf2751 135 dbg("DEVPATH = '%s'", devpath);
7fafc032
KS
136
137 action = get_action();
138 if (action == NULL) {
139 dbg("no action");
140 goto exit;
141 }
8bbf2751 142 dbg("ACTION = '%s'", action);
7fafc032
KS
143
144 seqnum = get_seqnum();
86590cd5 145 if (seqnum == NULL)
2f6cbd19 146 seq = -1;
86590cd5
KS
147 else
148 seq = atoi(seqnum);
8bbf2751 149 dbg("SEQNUM = '%d'", seq);
7b15897b 150
2f6cbd19 151 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa
KS
152 if (sock == -1) {
153 dbg("error getting socket");
7fafc032
KS
154 goto exit;
155 }
156
53921bfa
KS
157 memset(&saddr, 0x00, sizeof(saddr));
158 saddr.sun_family = AF_LOCAL;
872344c4
KS
159 /* use abstract namespace for socket path */
160 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 161 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 162
0028653c
KS
163 size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
164
2f6cbd19
KS
165 /* If we can't send, try to start daemon and resend message */
166 loop = UDEVSEND_CONNECT_RETRY;
167 while (loop--) {
7b1cbec9 168 retval = sendto(sock, &msg, size, 0, (struct sockaddr *)&saddr, addrlen);
2f6cbd19
KS
169 if (retval != -1) {
170 retval = 0;
171 goto close_and_exit;
172 }
173
174 if (errno != ECONNREFUSED) {
175 dbg("error sending message");
176 goto close_and_exit;
177 }
178
179 if (!started_daemon) {
180 dbg("connect failed, try starting daemon...");
181 retval = start_daemon();
182 if (retval) {
183 dbg("error starting daemon");
184 goto exit;
185 }
186
53921bfa 187 dbg("daemon started");
2f6cbd19 188 started_daemon = 1;
53921bfa 189 } else {
2f6cbd19
KS
190 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
191 tspec.tv_sec = 0;
192 tspec.tv_nsec = 100000000; /* 100 millisec */
193 nanosleep(&tspec, NULL);
53921bfa 194 }
7fafc032 195 }
2f6cbd19
KS
196
197close_and_exit:
198 close(sock);
33db4b8d 199exit:
2f6cbd19 200 return retval;
7fafc032 201}