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>
38 /* global variables */
42 void log_message (int priority, const char *format, ...)
46 if (priority > udev_log_priority)
49 va_start(args, format);
50 vsyslog(priority, format, args);
55 int main(int argc, char *argv[], char *envp[])
57 static struct udevd_msg usend_msg;
60 struct sockaddr_un saddr;
64 const char *subsystem = NULL;
66 logging_init("udevsend");
70 dbg("version %s", UDEV_VERSION);
72 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
74 err("error getting socket: %s", strerror(errno));
79 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
80 saddr.sun_family = AF_LOCAL;
81 /* use abstract namespace for socket path */
82 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
83 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
85 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
86 strcpy(usend_msg.magic, UDEV_MAGIC);
87 usend_msg.type = UDEVD_UEVENT_UDEVSEND;
89 /* copy all keys to send buffer */
90 for (i = 0; envp[i]; i++) {
97 /* ignore events which are already sent on the netlink socket */
98 if (strncmp(key, "SEQNUM=", 7) == 0) {
99 dbg("ignoring event with SEQNUM set");
104 /* prevent loops in the scripts we execute */
105 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
106 err("event loop, already passed through the daemon, exit");
111 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
112 err("environment buffer too small, probably not called by the kernel");
116 /* remember the SUBSYSTEM */
117 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
118 subsystem = &key[10];
120 dbg("add '%s' to env[%i] buffer", key, i);
121 strcpy(&usend_msg.envbuf[bufpos], key);
122 bufpos += keylen + 1;
125 usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
126 dbg("usend_msg_len=%i", usend_msg_len);
128 if (sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen) < 0) {
130 err("error sending message: %s", strerror(errno));