]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
6 * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
7 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
37 #include "udev_version.h"
41 unsigned char logname
[42];
43 static inline char *get_action(void)
47 action
= getenv("ACTION");
51 static inline char *get_devpath(void)
55 devpath
= getenv("DEVPATH");
59 static inline char *get_seqnum(void)
63 seqnum
= getenv("SEQNUM");
67 static int build_hotplugmsg(struct hotplug_msg
*msg
, char *action
,
68 char *devpath
, char *subsystem
, int seqnum
)
70 memset(msg
, 0x00, sizeof(*msg
));
71 strfieldcpy(msg
->magic
, UDEV_MAGIC
);
73 strncpy(msg
->action
, action
, 8);
74 strncpy(msg
->devpath
, devpath
, 128);
75 strncpy(msg
->subsystem
, subsystem
, 16);
76 return sizeof(struct hotplug_msg
);
79 static int start_daemon(void)
94 execl(UDEVD_BIN
, "udevd", NULL
);
95 dbg("exec of daemon failed");
98 dbg("fork of daemon failed");
105 dbg("fork of helper failed");
113 int main(int argc
, char* argv
[])
115 struct hotplug_msg message
;
121 int retval
= -EINVAL
;
124 struct timespec tspec
;
126 struct sockaddr_un saddr
;
129 init_logging("udevsend");
133 if (subsystem
== NULL
) {
138 devpath
= get_devpath();
139 if (devpath
== NULL
) {
144 action
= get_action();
145 if (action
== NULL
) {
150 seqnum
= get_seqnum();
156 sock
= socket(AF_LOCAL
, SOCK_STREAM
, 0);
158 dbg("error getting socket");
162 memset(&saddr
, 0x00, sizeof(saddr
));
163 saddr
.sun_family
= AF_LOCAL
;
164 strcpy(saddr
.sun_path
, UDEVD_SOCK
);
166 /* try to connect, if it fails start daemon */
167 retval
= connect(sock
, (struct sockaddr
*) &saddr
, sizeof(saddr
));
171 dbg("connect failed, try starting daemon...");
172 retval
= start_daemon();
174 dbg("daemon started");
176 dbg("error starting daemon");
181 /* try to connect while daemon to starts */
183 tspec
.tv_nsec
= 100000000; /* 100 millisec */
184 loop
= UDEVSEND_CONNECT_RETRY
;
186 retval
= connect(sock
, (struct sockaddr
*) &saddr
, sizeof(saddr
));
190 dbg("retry to connect %d",
191 UDEVSEND_CONNECT_RETRY
- loop
);
192 nanosleep(&tspec
, NULL
);
194 dbg("error connecting to daemon, start daemon failed");
198 size
= build_hotplugmsg(&message
, action
, devpath
, subsystem
, seq
);
199 retval
= send(sock
, &message
, size
, 0);
201 dbg("error sending message");