[multipathd] set the netlink uevent socket's rcv buf to 262,142
[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         int rcvbufsz = 128*1024;
53         int rcvsz = 0;
54         int rcvszsz = sizeof(rcvsz);
55         unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
56
57         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
58         snl.nl_family = AF_NETLINK;
59         snl.nl_pid = getpid();
60         snl.nl_groups = 0xffffffff;
61
62         sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
63         if (sock == -1) {
64                 condlog(0, "error getting socket, exit\n");
65                 return 1;
66         }
67
68         /*
69          * try to avoid dropping uevents, even so, this is not a guarantee,
70          * but it does help to change the netlink uevent socket's
71          * receive buffer threshold from the default value of 106,496 to
72          * the maximum value of 262,142.
73          */
74         retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
75                             sizeof(rcvbufsz));
76
77         if (retval < 0) {
78                 condlog(0, "error setting receive buffer size for socket, exit\n");
79                 exit(1);
80         }
81         retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
82
83         if (retval < 0) {
84                 condlog(0, "error setting receive buffer size for socket, exit\n");
85                 exit(1);
86         }
87         condlog(3, "receive buffer size for socket is %u.\n", rcvsz);
88
89         retval = bind(sock, (struct sockaddr *) &snl,
90                       sizeof(struct sockaddr_nl));
91         if (retval < 0) {
92                 condlog(0, "bind failed, exit\n");
93                 goto exit;
94         }
95
96         while (1) {
97                 static char buffer[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
98                 int i;
99                 char *pos;
100                 size_t bufpos;
101                 ssize_t buflen;
102                 struct uevent *uev;
103
104                 buflen = recv(sock, &buffer, sizeof(buffer), 0);
105                 if (buflen <  0) {
106                         condlog(0, "error receiving message\n");
107                         continue;
108                 }
109
110                 if ((size_t)buflen > sizeof(buffer)-1)
111                         buflen = sizeof(buffer)-1;
112
113                 buffer[buflen] = '\0';
114                 uev = alloc_uevent();
115
116                 if (!uev) {
117                         condlog(1, "lost uevent, oom");
118                         continue;
119                 }
120
121                 /* save start of payload */
122                 bufpos = strlen(buffer) + 1;
123
124                 /* action string */
125                 uev->action = buffer;
126                 pos = strchr(buffer, '@');
127                 if (!pos)
128                         continue;
129                 pos[0] = '\0';
130
131                 /* sysfs path */
132                 uev->devpath = &pos[1];
133
134                 /* hotplug events have the environment attached - reconstruct envp[] */
135                 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
136                         int keylen;
137                         char *key;
138
139                         key = &buffer[bufpos];
140                         keylen = strlen(key);
141                         uev->envp[i] = key;
142                         bufpos += keylen + 1;
143                 }
144                 uev->envp[i] = NULL;
145
146                 condlog(3, "uevent '%s' from '%s'\n", uev->action, uev->devpath);
147
148                 /* print payload environment */
149                 for (i = 0; uev->envp[i] != NULL; i++)
150                         condlog(3, "%s\n", uev->envp[i]);
151
152                 if (uev_trigger && uev_trigger(uev, trigger_data))
153                         condlog(0, "uevent trigger error");
154
155                 FREE(uev);
156         }
157
158 exit:
159         close(sock);
160         return 1;
161 }