4 * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <linux/stddef.h>
36 #include "udev_version.h"
40 /* global variables */
44 void log_message (int priority, const char *format, ...)
48 if (priority > udev_log_priority)
51 va_start(args, format);
52 vsyslog(priority, format, args);
57 int main(int argc, char *argv[], char *envp[])
59 static struct udevd_msg usend_msg;
62 struct sockaddr_un saddr;
66 const char *subsystem = NULL;
68 logging_init("udevsend");
72 dbg("version %s", UDEV_VERSION);
74 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
76 err("error getting socket: %s", strerror(errno));
81 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
82 saddr.sun_family = AF_LOCAL;
83 /* use abstract namespace for socket path */
84 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
85 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
87 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
88 strcpy(usend_msg.magic, UDEV_MAGIC);
89 usend_msg.type = UDEVD_UEVENT_UDEVSEND;
91 /* copy all keys to send buffer */
92 for (i = 0; envp[i]; i++) {
99 /* ignore events which are already sent on the netlink socket */
100 if (strncmp(key, "SEQNUM=", 7) == 0) {
101 dbg("ignoring event with SEQNUM set");
106 /* prevent loops in the scripts we execute */
107 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
108 err("event loop, already passed through the daemon, exit");
113 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
114 err("environment buffer too small, probably not called by the kernel");
118 /* remember the SUBSYSTEM */
119 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
120 subsystem = &key[10];
122 dbg("add '%s' to env[%i] buffer", key, i);
123 strcpy(&usend_msg.envbuf[bufpos], key);
124 bufpos += keylen + 1;
127 usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
128 dbg("usend_msg_len=%i", usend_msg_len);
130 if (sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen) < 0) {
132 err("error sending message: %s", strerror(errno));