1*86d7f5d3SJohn Marino /* $NetBSD: local.c,v 1.1.1.1 2009/12/02 00:27:10 haad Exp $ */
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
5*86d7f5d3SJohn Marino *
6*86d7f5d3SJohn Marino * This copyrighted material is made available to anyone wishing to use,
7*86d7f5d3SJohn Marino * modify, copy, or redistribute it subject to the terms and conditions
8*86d7f5d3SJohn Marino * of the GNU Lesser General Public License v.2.1.
9*86d7f5d3SJohn Marino *
10*86d7f5d3SJohn Marino * You should have received a copy of the GNU Lesser General Public License
11*86d7f5d3SJohn Marino * along with this program; if not, write to the Free Software Foundation,
12*86d7f5d3SJohn Marino * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13*86d7f5d3SJohn Marino */
14*86d7f5d3SJohn Marino #include <unistd.h>
15*86d7f5d3SJohn Marino #include <errno.h>
16*86d7f5d3SJohn Marino #include <string.h>
17*86d7f5d3SJohn Marino #include <stdint.h>
18*86d7f5d3SJohn Marino #include <sys/types.h>
19*86d7f5d3SJohn Marino #include <sys/socket.h>
20*86d7f5d3SJohn Marino #include <sys/poll.h>
21*86d7f5d3SJohn Marino #include <linux/connector.h>
22*86d7f5d3SJohn Marino #include <linux/netlink.h>
23*86d7f5d3SJohn Marino
24*86d7f5d3SJohn Marino #include "dm-log-userspace.h"
25*86d7f5d3SJohn Marino #include "functions.h"
26*86d7f5d3SJohn Marino #include "cluster.h"
27*86d7f5d3SJohn Marino #include "common.h"
28*86d7f5d3SJohn Marino #include "logging.h"
29*86d7f5d3SJohn Marino #include "link_mon.h"
30*86d7f5d3SJohn Marino #include "local.h"
31*86d7f5d3SJohn Marino
32*86d7f5d3SJohn Marino #ifndef CN_IDX_DM
33*86d7f5d3SJohn Marino #warning Kernel should be at least 2.6.31
34*86d7f5d3SJohn Marino #define CN_IDX_DM 0x7 /* Device Mapper */
35*86d7f5d3SJohn Marino #define CN_VAL_DM_USERSPACE_LOG 0x1
36*86d7f5d3SJohn Marino #endif
37*86d7f5d3SJohn Marino
38*86d7f5d3SJohn Marino static int cn_fd; /* Connector (netlink) socket fd */
39*86d7f5d3SJohn Marino static char recv_buf[2048];
40*86d7f5d3SJohn Marino static char send_buf[2048];
41*86d7f5d3SJohn Marino
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino /* FIXME: merge this function with kernel_send_helper */
kernel_ack(uint32_t seq,int error)44*86d7f5d3SJohn Marino static int kernel_ack(uint32_t seq, int error)
45*86d7f5d3SJohn Marino {
46*86d7f5d3SJohn Marino int r;
47*86d7f5d3SJohn Marino struct nlmsghdr *nlh = (struct nlmsghdr *)send_buf;
48*86d7f5d3SJohn Marino struct cn_msg *msg = NLMSG_DATA(nlh);
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino if (error < 0) {
51*86d7f5d3SJohn Marino LOG_ERROR("Programmer error: error codes must be positive");
52*86d7f5d3SJohn Marino return -EINVAL;
53*86d7f5d3SJohn Marino }
54*86d7f5d3SJohn Marino
55*86d7f5d3SJohn Marino memset(send_buf, 0, sizeof(send_buf));
56*86d7f5d3SJohn Marino
57*86d7f5d3SJohn Marino nlh->nlmsg_seq = 0;
58*86d7f5d3SJohn Marino nlh->nlmsg_pid = getpid();
59*86d7f5d3SJohn Marino nlh->nlmsg_type = NLMSG_DONE;
60*86d7f5d3SJohn Marino nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct cn_msg));
61*86d7f5d3SJohn Marino nlh->nlmsg_flags = 0;
62*86d7f5d3SJohn Marino
63*86d7f5d3SJohn Marino msg->len = 0;
64*86d7f5d3SJohn Marino msg->id.idx = CN_IDX_DM;
65*86d7f5d3SJohn Marino msg->id.val = CN_VAL_DM_USERSPACE_LOG;
66*86d7f5d3SJohn Marino msg->seq = seq;
67*86d7f5d3SJohn Marino msg->ack = error;
68*86d7f5d3SJohn Marino
69*86d7f5d3SJohn Marino r = send(cn_fd, nlh, NLMSG_LENGTH(sizeof(struct cn_msg)), 0);
70*86d7f5d3SJohn Marino /* FIXME: do better error processing */
71*86d7f5d3SJohn Marino if (r <= 0)
72*86d7f5d3SJohn Marino return -EBADE;
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino return 0;
75*86d7f5d3SJohn Marino }
76*86d7f5d3SJohn Marino
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino /*
79*86d7f5d3SJohn Marino * kernel_recv
80*86d7f5d3SJohn Marino * @rq: the newly allocated request from kernel
81*86d7f5d3SJohn Marino *
82*86d7f5d3SJohn Marino * Read requests from the kernel and allocate space for the new request.
83*86d7f5d3SJohn Marino * If there is no request from the kernel, *rq is NULL.
84*86d7f5d3SJohn Marino *
85*86d7f5d3SJohn Marino * This function is not thread safe due to returned stack pointer. In fact,
86*86d7f5d3SJohn Marino * the returned pointer must not be in-use when this function is called again.
87*86d7f5d3SJohn Marino *
88*86d7f5d3SJohn Marino * Returns: 0 on success, -EXXX on error
89*86d7f5d3SJohn Marino */
kernel_recv(struct clog_request ** rq)90*86d7f5d3SJohn Marino static int kernel_recv(struct clog_request **rq)
91*86d7f5d3SJohn Marino {
92*86d7f5d3SJohn Marino int r = 0;
93*86d7f5d3SJohn Marino int len;
94*86d7f5d3SJohn Marino struct cn_msg *msg;
95*86d7f5d3SJohn Marino struct dm_ulog_request *u_rq;
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino *rq = NULL;
98*86d7f5d3SJohn Marino memset(recv_buf, 0, sizeof(recv_buf));
99*86d7f5d3SJohn Marino
100*86d7f5d3SJohn Marino len = recv(cn_fd, recv_buf, sizeof(recv_buf), 0);
101*86d7f5d3SJohn Marino if (len < 0) {
102*86d7f5d3SJohn Marino LOG_ERROR("Failed to recv message from kernel");
103*86d7f5d3SJohn Marino r = -errno;
104*86d7f5d3SJohn Marino goto fail;
105*86d7f5d3SJohn Marino }
106*86d7f5d3SJohn Marino
107*86d7f5d3SJohn Marino switch (((struct nlmsghdr *)recv_buf)->nlmsg_type) {
108*86d7f5d3SJohn Marino case NLMSG_ERROR:
109*86d7f5d3SJohn Marino LOG_ERROR("Unable to recv message from kernel: NLMSG_ERROR");
110*86d7f5d3SJohn Marino r = -EBADE;
111*86d7f5d3SJohn Marino goto fail;
112*86d7f5d3SJohn Marino case NLMSG_DONE:
113*86d7f5d3SJohn Marino msg = (struct cn_msg *)NLMSG_DATA((struct nlmsghdr *)recv_buf);
114*86d7f5d3SJohn Marino len -= sizeof(struct nlmsghdr);
115*86d7f5d3SJohn Marino
116*86d7f5d3SJohn Marino if (len < sizeof(struct cn_msg)) {
117*86d7f5d3SJohn Marino LOG_ERROR("Incomplete request from kernel received");
118*86d7f5d3SJohn Marino r = -EBADE;
119*86d7f5d3SJohn Marino goto fail;
120*86d7f5d3SJohn Marino }
121*86d7f5d3SJohn Marino
122*86d7f5d3SJohn Marino if (msg->len > DM_ULOG_REQUEST_SIZE) {
123*86d7f5d3SJohn Marino LOG_ERROR("Not enough space to receive kernel request (%d/%d)",
124*86d7f5d3SJohn Marino msg->len, DM_ULOG_REQUEST_SIZE);
125*86d7f5d3SJohn Marino r = -EBADE;
126*86d7f5d3SJohn Marino goto fail;
127*86d7f5d3SJohn Marino }
128*86d7f5d3SJohn Marino
129*86d7f5d3SJohn Marino if (!msg->len)
130*86d7f5d3SJohn Marino LOG_ERROR("Zero length message received");
131*86d7f5d3SJohn Marino
132*86d7f5d3SJohn Marino len -= sizeof(struct cn_msg);
133*86d7f5d3SJohn Marino
134*86d7f5d3SJohn Marino if (len < msg->len)
135*86d7f5d3SJohn Marino LOG_ERROR("len = %d, msg->len = %d", len, msg->len);
136*86d7f5d3SJohn Marino
137*86d7f5d3SJohn Marino msg->data[msg->len] = '\0'; /* Cleaner way to ensure this? */
138*86d7f5d3SJohn Marino u_rq = (struct dm_ulog_request *)msg->data;
139*86d7f5d3SJohn Marino
140*86d7f5d3SJohn Marino if (!u_rq->request_type) {
141*86d7f5d3SJohn Marino LOG_DBG("Bad transmission, requesting resend [%u]",
142*86d7f5d3SJohn Marino msg->seq);
143*86d7f5d3SJohn Marino r = -EAGAIN;
144*86d7f5d3SJohn Marino
145*86d7f5d3SJohn Marino if (kernel_ack(msg->seq, EAGAIN)) {
146*86d7f5d3SJohn Marino LOG_ERROR("Failed to NACK kernel transmission [%u]",
147*86d7f5d3SJohn Marino msg->seq);
148*86d7f5d3SJohn Marino r = -EBADE;
149*86d7f5d3SJohn Marino }
150*86d7f5d3SJohn Marino }
151*86d7f5d3SJohn Marino
152*86d7f5d3SJohn Marino /*
153*86d7f5d3SJohn Marino * Now we've got sizeof(struct cn_msg) + sizeof(struct nlmsghdr)
154*86d7f5d3SJohn Marino * worth of space that precede the request structure from the
155*86d7f5d3SJohn Marino * kernel. Since that space isn't going to be used again, we
156*86d7f5d3SJohn Marino * can take it for our purposes; rather than allocating a whole
157*86d7f5d3SJohn Marino * new structure and doing a memcpy.
158*86d7f5d3SJohn Marino *
159*86d7f5d3SJohn Marino * We should really make sure 'clog_request' doesn't grow
160*86d7f5d3SJohn Marino * beyond what is available to us, but we need only check it
161*86d7f5d3SJohn Marino * once... perhaps at compile time?
162*86d7f5d3SJohn Marino */
163*86d7f5d3SJohn Marino // *rq = container_of(u_rq, struct clog_request, u_rq);
164*86d7f5d3SJohn Marino *rq = (void *)u_rq -
165*86d7f5d3SJohn Marino (sizeof(struct clog_request) -
166*86d7f5d3SJohn Marino sizeof(struct dm_ulog_request));
167*86d7f5d3SJohn Marino
168*86d7f5d3SJohn Marino /* Clear the wrapper container fields */
169*86d7f5d3SJohn Marino memset(*rq, 0, (void *)u_rq - (void *)(*rq));
170*86d7f5d3SJohn Marino break;
171*86d7f5d3SJohn Marino default:
172*86d7f5d3SJohn Marino LOG_ERROR("Unknown nlmsg_type");
173*86d7f5d3SJohn Marino r = -EBADE;
174*86d7f5d3SJohn Marino }
175*86d7f5d3SJohn Marino
176*86d7f5d3SJohn Marino fail:
177*86d7f5d3SJohn Marino if (r)
178*86d7f5d3SJohn Marino *rq = NULL;
179*86d7f5d3SJohn Marino
180*86d7f5d3SJohn Marino return (r == -EAGAIN) ? 0 : r;
181*86d7f5d3SJohn Marino }
182*86d7f5d3SJohn Marino
kernel_send_helper(void * data,int out_size)183*86d7f5d3SJohn Marino static int kernel_send_helper(void *data, int out_size)
184*86d7f5d3SJohn Marino {
185*86d7f5d3SJohn Marino int r;
186*86d7f5d3SJohn Marino struct nlmsghdr *nlh;
187*86d7f5d3SJohn Marino struct cn_msg *msg;
188*86d7f5d3SJohn Marino
189*86d7f5d3SJohn Marino memset(send_buf, 0, sizeof(send_buf));
190*86d7f5d3SJohn Marino
191*86d7f5d3SJohn Marino nlh = (struct nlmsghdr *)send_buf;
192*86d7f5d3SJohn Marino nlh->nlmsg_seq = 0; /* FIXME: Is this used? */
193*86d7f5d3SJohn Marino nlh->nlmsg_pid = getpid();
194*86d7f5d3SJohn Marino nlh->nlmsg_type = NLMSG_DONE;
195*86d7f5d3SJohn Marino nlh->nlmsg_len = NLMSG_LENGTH(out_size + sizeof(struct cn_msg));
196*86d7f5d3SJohn Marino nlh->nlmsg_flags = 0;
197*86d7f5d3SJohn Marino
198*86d7f5d3SJohn Marino msg = NLMSG_DATA(nlh);
199*86d7f5d3SJohn Marino memcpy(msg->data, data, out_size);
200*86d7f5d3SJohn Marino msg->len = out_size;
201*86d7f5d3SJohn Marino msg->id.idx = CN_IDX_DM;
202*86d7f5d3SJohn Marino msg->id.val = CN_VAL_DM_USERSPACE_LOG;
203*86d7f5d3SJohn Marino msg->seq = 0;
204*86d7f5d3SJohn Marino
205*86d7f5d3SJohn Marino r = send(cn_fd, nlh, NLMSG_LENGTH(out_size + sizeof(struct cn_msg)), 0);
206*86d7f5d3SJohn Marino /* FIXME: do better error processing */
207*86d7f5d3SJohn Marino if (r <= 0)
208*86d7f5d3SJohn Marino return -EBADE;
209*86d7f5d3SJohn Marino
210*86d7f5d3SJohn Marino return 0;
211*86d7f5d3SJohn Marino }
212*86d7f5d3SJohn Marino
213*86d7f5d3SJohn Marino /*
214*86d7f5d3SJohn Marino * do_local_work
215*86d7f5d3SJohn Marino *
216*86d7f5d3SJohn Marino * Any processing errors are placed in the 'rq'
217*86d7f5d3SJohn Marino * structure to be reported back to the kernel.
218*86d7f5d3SJohn Marino * It may be pointless for this function to
219*86d7f5d3SJohn Marino * return an int.
220*86d7f5d3SJohn Marino *
221*86d7f5d3SJohn Marino * Returns: 0 on success, -EXXX on failure
222*86d7f5d3SJohn Marino */
do_local_work(void * data)223*86d7f5d3SJohn Marino static int do_local_work(void *data)
224*86d7f5d3SJohn Marino {
225*86d7f5d3SJohn Marino int r;
226*86d7f5d3SJohn Marino struct clog_request *rq;
227*86d7f5d3SJohn Marino struct dm_ulog_request *u_rq = NULL;
228*86d7f5d3SJohn Marino
229*86d7f5d3SJohn Marino r = kernel_recv(&rq);
230*86d7f5d3SJohn Marino if (r)
231*86d7f5d3SJohn Marino return r;
232*86d7f5d3SJohn Marino
233*86d7f5d3SJohn Marino if (!rq)
234*86d7f5d3SJohn Marino return 0;
235*86d7f5d3SJohn Marino
236*86d7f5d3SJohn Marino u_rq = &rq->u_rq;
237*86d7f5d3SJohn Marino LOG_DBG("[%s] Request from kernel received: [%s/%u]",
238*86d7f5d3SJohn Marino SHORT_UUID(u_rq->uuid), RQ_TYPE(u_rq->request_type),
239*86d7f5d3SJohn Marino u_rq->seq);
240*86d7f5d3SJohn Marino switch (u_rq->request_type) {
241*86d7f5d3SJohn Marino case DM_ULOG_CTR:
242*86d7f5d3SJohn Marino case DM_ULOG_DTR:
243*86d7f5d3SJohn Marino case DM_ULOG_GET_REGION_SIZE:
244*86d7f5d3SJohn Marino case DM_ULOG_IN_SYNC:
245*86d7f5d3SJohn Marino case DM_ULOG_GET_SYNC_COUNT:
246*86d7f5d3SJohn Marino case DM_ULOG_STATUS_INFO:
247*86d7f5d3SJohn Marino case DM_ULOG_STATUS_TABLE:
248*86d7f5d3SJohn Marino case DM_ULOG_PRESUSPEND:
249*86d7f5d3SJohn Marino /* We do not specify ourselves as server here */
250*86d7f5d3SJohn Marino r = do_request(rq, 0);
251*86d7f5d3SJohn Marino if (r)
252*86d7f5d3SJohn Marino LOG_DBG("Returning failed request to kernel [%s]",
253*86d7f5d3SJohn Marino RQ_TYPE(u_rq->request_type));
254*86d7f5d3SJohn Marino r = kernel_send(u_rq);
255*86d7f5d3SJohn Marino if (r)
256*86d7f5d3SJohn Marino LOG_ERROR("Failed to respond to kernel [%s]",
257*86d7f5d3SJohn Marino RQ_TYPE(u_rq->request_type));
258*86d7f5d3SJohn Marino
259*86d7f5d3SJohn Marino break;
260*86d7f5d3SJohn Marino case DM_ULOG_RESUME:
261*86d7f5d3SJohn Marino /*
262*86d7f5d3SJohn Marino * Resume is a special case that requires a local
263*86d7f5d3SJohn Marino * component to join the CPG, and a cluster component
264*86d7f5d3SJohn Marino * to handle the request.
265*86d7f5d3SJohn Marino */
266*86d7f5d3SJohn Marino r = local_resume(u_rq);
267*86d7f5d3SJohn Marino if (r) {
268*86d7f5d3SJohn Marino LOG_DBG("Returning failed request to kernel [%s]",
269*86d7f5d3SJohn Marino RQ_TYPE(u_rq->request_type));
270*86d7f5d3SJohn Marino r = kernel_send(u_rq);
271*86d7f5d3SJohn Marino if (r)
272*86d7f5d3SJohn Marino LOG_ERROR("Failed to respond to kernel [%s]",
273*86d7f5d3SJohn Marino RQ_TYPE(u_rq->request_type));
274*86d7f5d3SJohn Marino break;
275*86d7f5d3SJohn Marino }
276*86d7f5d3SJohn Marino /* ELSE, fall through */
277*86d7f5d3SJohn Marino case DM_ULOG_IS_CLEAN:
278*86d7f5d3SJohn Marino case DM_ULOG_FLUSH:
279*86d7f5d3SJohn Marino case DM_ULOG_MARK_REGION:
280*86d7f5d3SJohn Marino case DM_ULOG_GET_RESYNC_WORK:
281*86d7f5d3SJohn Marino case DM_ULOG_SET_REGION_SYNC:
282*86d7f5d3SJohn Marino case DM_ULOG_IS_REMOTE_RECOVERING:
283*86d7f5d3SJohn Marino case DM_ULOG_POSTSUSPEND:
284*86d7f5d3SJohn Marino r = cluster_send(rq);
285*86d7f5d3SJohn Marino if (r) {
286*86d7f5d3SJohn Marino u_rq->data_size = 0;
287*86d7f5d3SJohn Marino u_rq->error = r;
288*86d7f5d3SJohn Marino kernel_send(u_rq);
289*86d7f5d3SJohn Marino }
290*86d7f5d3SJohn Marino
291*86d7f5d3SJohn Marino break;
292*86d7f5d3SJohn Marino case DM_ULOG_CLEAR_REGION:
293*86d7f5d3SJohn Marino r = kernel_ack(u_rq->seq, 0);
294*86d7f5d3SJohn Marino
295*86d7f5d3SJohn Marino r = cluster_send(rq);
296*86d7f5d3SJohn Marino if (r) {
297*86d7f5d3SJohn Marino /*
298*86d7f5d3SJohn Marino * FIXME: store error for delivery on flush
299*86d7f5d3SJohn Marino * This would allow us to optimize MARK_REGION
300*86d7f5d3SJohn Marino * too.
301*86d7f5d3SJohn Marino */
302*86d7f5d3SJohn Marino }
303*86d7f5d3SJohn Marino
304*86d7f5d3SJohn Marino break;
305*86d7f5d3SJohn Marino default:
306*86d7f5d3SJohn Marino LOG_ERROR("Invalid log request received (%u), ignoring.",
307*86d7f5d3SJohn Marino u_rq->request_type);
308*86d7f5d3SJohn Marino
309*86d7f5d3SJohn Marino return 0;
310*86d7f5d3SJohn Marino }
311*86d7f5d3SJohn Marino
312*86d7f5d3SJohn Marino if (r && !u_rq->error)
313*86d7f5d3SJohn Marino u_rq->error = r;
314*86d7f5d3SJohn Marino
315*86d7f5d3SJohn Marino return r;
316*86d7f5d3SJohn Marino }
317*86d7f5d3SJohn Marino
318*86d7f5d3SJohn Marino /*
319*86d7f5d3SJohn Marino * kernel_send
320*86d7f5d3SJohn Marino * @u_rq: result to pass back to kernel
321*86d7f5d3SJohn Marino *
322*86d7f5d3SJohn Marino * This function returns the u_rq structure
323*86d7f5d3SJohn Marino * (containing the results) to the kernel.
324*86d7f5d3SJohn Marino * It then frees the structure.
325*86d7f5d3SJohn Marino *
326*86d7f5d3SJohn Marino * WARNING: should the structure be freed if
327*86d7f5d3SJohn Marino * there is an error? I vote 'yes'. If the
328*86d7f5d3SJohn Marino * kernel doesn't get the response, it should
329*86d7f5d3SJohn Marino * resend the request.
330*86d7f5d3SJohn Marino *
331*86d7f5d3SJohn Marino * Returns: 0 on success, -EXXX on failure
332*86d7f5d3SJohn Marino */
kernel_send(struct dm_ulog_request * u_rq)333*86d7f5d3SJohn Marino int kernel_send(struct dm_ulog_request *u_rq)
334*86d7f5d3SJohn Marino {
335*86d7f5d3SJohn Marino int r;
336*86d7f5d3SJohn Marino int size;
337*86d7f5d3SJohn Marino
338*86d7f5d3SJohn Marino if (!u_rq)
339*86d7f5d3SJohn Marino return -EINVAL;
340*86d7f5d3SJohn Marino
341*86d7f5d3SJohn Marino size = sizeof(struct dm_ulog_request) + u_rq->data_size;
342*86d7f5d3SJohn Marino
343*86d7f5d3SJohn Marino if (!u_rq->data_size && !u_rq->error) {
344*86d7f5d3SJohn Marino /* An ACK is all that is needed */
345*86d7f5d3SJohn Marino
346*86d7f5d3SJohn Marino /* FIXME: add ACK code */
347*86d7f5d3SJohn Marino } else if (size > DM_ULOG_REQUEST_SIZE) {
348*86d7f5d3SJohn Marino /*
349*86d7f5d3SJohn Marino * If we gotten here, we've already overrun
350*86d7f5d3SJohn Marino * our allotted space somewhere.
351*86d7f5d3SJohn Marino *
352*86d7f5d3SJohn Marino * We must do something, because the kernel
353*86d7f5d3SJohn Marino * is waiting for a response.
354*86d7f5d3SJohn Marino */
355*86d7f5d3SJohn Marino LOG_ERROR("Not enough space to respond to server");
356*86d7f5d3SJohn Marino u_rq->error = -ENOSPC;
357*86d7f5d3SJohn Marino size = sizeof(struct dm_ulog_request);
358*86d7f5d3SJohn Marino }
359*86d7f5d3SJohn Marino
360*86d7f5d3SJohn Marino r = kernel_send_helper(u_rq, size);
361*86d7f5d3SJohn Marino if (r)
362*86d7f5d3SJohn Marino LOG_ERROR("Failed to send msg to kernel.");
363*86d7f5d3SJohn Marino
364*86d7f5d3SJohn Marino return r;
365*86d7f5d3SJohn Marino }
366*86d7f5d3SJohn Marino
367*86d7f5d3SJohn Marino /*
368*86d7f5d3SJohn Marino * init_local
369*86d7f5d3SJohn Marino *
370*86d7f5d3SJohn Marino * Initialize kernel communication socket (netlink)
371*86d7f5d3SJohn Marino *
372*86d7f5d3SJohn Marino * Returns: 0 on success, values from common.h on failure
373*86d7f5d3SJohn Marino */
init_local(void)374*86d7f5d3SJohn Marino int init_local(void)
375*86d7f5d3SJohn Marino {
376*86d7f5d3SJohn Marino int r = 0;
377*86d7f5d3SJohn Marino int opt;
378*86d7f5d3SJohn Marino struct sockaddr_nl addr;
379*86d7f5d3SJohn Marino
380*86d7f5d3SJohn Marino cn_fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
381*86d7f5d3SJohn Marino if (cn_fd < 0)
382*86d7f5d3SJohn Marino return EXIT_KERNEL_SOCKET;
383*86d7f5d3SJohn Marino
384*86d7f5d3SJohn Marino /* memset to fix valgrind complaint */
385*86d7f5d3SJohn Marino memset(&addr, 0, sizeof(struct sockaddr_nl));
386*86d7f5d3SJohn Marino
387*86d7f5d3SJohn Marino addr.nl_family = AF_NETLINK;
388*86d7f5d3SJohn Marino addr.nl_groups = CN_IDX_DM;
389*86d7f5d3SJohn Marino addr.nl_pid = 0;
390*86d7f5d3SJohn Marino
391*86d7f5d3SJohn Marino r = bind(cn_fd, (struct sockaddr *) &addr, sizeof(addr));
392*86d7f5d3SJohn Marino if (r < 0) {
393*86d7f5d3SJohn Marino close(cn_fd);
394*86d7f5d3SJohn Marino return EXIT_KERNEL_BIND;
395*86d7f5d3SJohn Marino }
396*86d7f5d3SJohn Marino
397*86d7f5d3SJohn Marino opt = addr.nl_groups;
398*86d7f5d3SJohn Marino r = setsockopt(cn_fd, 270, NETLINK_ADD_MEMBERSHIP, &opt, sizeof(opt));
399*86d7f5d3SJohn Marino if (r) {
400*86d7f5d3SJohn Marino close(cn_fd);
401*86d7f5d3SJohn Marino return EXIT_KERNEL_SETSOCKOPT;
402*86d7f5d3SJohn Marino }
403*86d7f5d3SJohn Marino
404*86d7f5d3SJohn Marino /*
405*86d7f5d3SJohn Marino r = fcntl(cn_fd, F_SETFL, FNDELAY);
406*86d7f5d3SJohn Marino */
407*86d7f5d3SJohn Marino
408*86d7f5d3SJohn Marino links_register(cn_fd, "local", do_local_work, NULL);
409*86d7f5d3SJohn Marino
410*86d7f5d3SJohn Marino return 0;
411*86d7f5d3SJohn Marino }
412*86d7f5d3SJohn Marino
413*86d7f5d3SJohn Marino /*
414*86d7f5d3SJohn Marino * cleanup_local
415*86d7f5d3SJohn Marino *
416*86d7f5d3SJohn Marino * Clean up before exiting
417*86d7f5d3SJohn Marino */
cleanup_local(void)418*86d7f5d3SJohn Marino void cleanup_local(void)
419*86d7f5d3SJohn Marino {
420*86d7f5d3SJohn Marino links_unregister(cn_fd);
421*86d7f5d3SJohn Marino close(cn_fd);
422*86d7f5d3SJohn Marino }
423