chiark / gitweb /
check for ignore_device in loop looks redundant
[elogind.git] / udevsend.c
1 /*
2  * udevsend.c
3  *
4  * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
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.
10  * 
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.
15  * 
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/un.h>
33 #include <linux/stddef.h>
34
35 #include "udev.h"
36 #include "udevd.h"
37
38 /* global variables */
39 static int sock = -1;
40
41 #ifdef USE_LOG
42 void log_message (int priority, const char *format, ...)
43 {
44         va_list args;
45
46         if (priority > udev_log_priority)
47                 return;
48
49         va_start(args, format);
50         vsyslog(priority, format, args);
51         va_end(args);
52 }
53 #endif
54
55 int main(int argc, char *argv[], char *envp[])
56 {
57         static struct udevd_msg usend_msg;
58         int usend_msg_len;
59         int i;
60         struct sockaddr_un saddr;
61         socklen_t addrlen;
62         int bufpos = 0;
63         int retval = 0;
64         const char *subsystem = NULL;
65
66         logging_init("udevsend");
67 #ifdef USE_LOG
68         udev_config_init();
69 #endif
70         dbg("version %s", UDEV_VERSION);
71
72         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
73         if (sock < 0) {
74                 err("error getting socket: %s", strerror(errno));
75                 retval = 1;
76                 goto exit;
77         }
78
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;
84
85         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
86         strcpy(usend_msg.magic, UDEV_MAGIC);
87         usend_msg.type = UDEVD_UEVENT_UDEVSEND;
88
89         /* copy all keys to send buffer */
90         for (i = 0; envp[i]; i++) {
91                 const char *key;
92                 int keylen;
93
94                 key = envp[i];
95                 keylen = strlen(key);
96
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");
100                         retval = 0;
101                         goto exit;
102                 }
103
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");
107                         retval = 2;
108                         goto exit;
109                 }
110
111                 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
112                         err("environment buffer too small, probably not called by the kernel");
113                         continue;
114                 }
115
116                 /* remember the SUBSYSTEM */
117                 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
118                         subsystem = &key[10];
119
120                 dbg("add '%s' to env[%i] buffer", key, i);
121                 strcpy(&usend_msg.envbuf[bufpos], key);
122                 bufpos += keylen + 1;
123         }
124
125         usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
126         dbg("usend_msg_len=%i", usend_msg_len);
127
128         if (sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen) < 0) {
129                 retval = 3;
130                 err("error sending message: %s", strerror(errno));
131         }
132
133 exit:
134         if (sock != -1)
135                 close(sock);
136
137         logging_close();
138         return retval;
139 }