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