Initial git import.
[platform/upstream/multipath-tools.git] / libmultipath / uevent.c
1 /*
2  * uevent.c - trigger upon netlink uevents from the kernel
3  *
4  *      Only kernels from version 2.6.10* on provide the uevent netlink socket.
5  *      Until the libc-kernel-headers are updated, you need to compile with:
6  *
7  *        gcc -I /lib/modules/`uname -r`/build/include -o uevent_listen uevent_listen.c
8  *
9  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
10  *
11  *      This program is free software; you can redistribute it and/or modify it
12  *      under the terms of the GNU General Public License as published by the
13  *      Free Software Foundation version 2 of the License.
14  *
15  *      This program is distributed in the hope that it will be useful, but
16  *      WITHOUT ANY WARRANTY; without even the implied warranty of
17  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *      General Public License for more details.
19  *
20  *      You should have received a copy of the GNU General Public License along
21  *      with this program; if not, write to the Free Software Foundation, Inc.,
22  *      675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <sys/socket.h>
33 #include <sys/user.h>
34 #include <asm/types.h>
35 #include <linux/netlink.h>
36
37 #include "memory.h"
38 #include "debug.h"
39 #include "uevent.h"
40
41 struct uevent * alloc_uevent (void)
42 {
43         return (struct uevent *)MALLOC(sizeof(struct uevent));
44 }
45
46 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
47                   void * trigger_data)
48 {
49         int sock;
50         struct sockaddr_nl snl;
51         int retval;
52
53         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
54         snl.nl_family = AF_NETLINK;
55         snl.nl_pid = getpid();
56         snl.nl_groups = 0xffffffff;
57
58         sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
59         if (sock == -1) {
60                 condlog(0, "error getting socket, exit\n");
61                 exit(1);
62         }
63
64         retval = bind(sock, (struct sockaddr *) &snl,
65                       sizeof(struct sockaddr_nl));
66         if (retval < 0) {
67                 condlog(0, "bind failed, exit\n");
68                 goto exit;
69         }
70
71         while (1) {
72                 static char buffer[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
73                 int i;
74                 char *pos;
75                 size_t bufpos;
76                 ssize_t buflen;
77                 struct uevent *uev;
78
79                 buflen = recv(sock, &buffer, sizeof(buffer), 0);
80                 if (buflen <  0) {
81                         condlog(0, "error receiving message\n");
82                         continue;
83                 }
84
85                 if ((size_t)buflen > sizeof(buffer)-1)
86                         buflen = sizeof(buffer)-1;
87
88                 buffer[buflen] = '\0';
89                 uev = alloc_uevent();
90
91                 if (!uev) {
92                         condlog(1, "lost uevent, oom");
93                         continue;
94                 }
95
96                 /* save start of payload */
97                 bufpos = strlen(buffer) + 1;
98
99                 /* action string */
100                 uev->action = buffer;
101                 pos = strchr(buffer, '@');
102                 if (!pos)
103                         continue;
104                 pos[0] = '\0';
105
106                 /* sysfs path */
107                 uev->devpath = &pos[1];
108
109                 /* hotplug events have the environment attached - reconstruct envp[] */
110                 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
111                         int keylen;
112                         char *key;
113
114                         key = &buffer[bufpos];
115                         keylen = strlen(key);
116                         uev->envp[i] = key;
117                         bufpos += keylen + 1;
118                 }
119                 uev->envp[i] = NULL;
120
121                 condlog(3, "uevent '%s' from '%s'\n", uev->action, uev->devpath);
122
123                 /* print payload environment */
124                 for (i = 0; uev->envp[i] != NULL; i++)
125                         condlog(3, "%s\n", uev->envp[i]);
126
127                 if (uev_trigger && uev_trigger(uev, trigger_data))
128                         condlog(0, "uevent trigger error");
129                         
130         }
131
132 exit:
133         close(sock);
134         return 1;
135 }