]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/genl.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/socket.h>
29 #include <linux/genetlink.h>
30 #include <linux/rtnetlink.h>
35 static int genetlink_resolve_family(const char *family
)
37 struct nl_handler handler
;
39 struct genlmsg
*request
, *reply
;
40 struct genlmsghdr
*genlmsghdr
;
44 request
= genlmsg_alloc(GENLMSG_GOOD_SIZE
);
48 reply
= genlmsg_alloc(GENLMSG_GOOD_SIZE
);
50 genlmsg_free(request
);
54 request
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(GENL_HDRLEN
);
55 request
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
56 request
->nlmsghdr
.nlmsg_type
= GENL_ID_CTRL
;
58 genlmsghdr
= NLMSG_DATA(&request
->nlmsghdr
);
59 genlmsghdr
->cmd
= CTRL_CMD_GETFAMILY
;
61 ret
= netlink_open(&handler
, NETLINK_GENERIC
);
65 ret
= nla_put_string((struct nlmsg
*)&request
->nlmsghdr
,
66 CTRL_ATTR_FAMILY_NAME
, family
);
70 ret
= netlink_transaction(&handler
, (struct nlmsg
*)&request
->nlmsghdr
,
71 (struct nlmsg
*)&reply
->nlmsghdr
);
75 genlmsghdr
= NLMSG_DATA(&reply
->nlmsghdr
);
76 len
= reply
->nlmsghdr
.nlmsg_len
;
79 if (reply
->nlmsghdr
.nlmsg_type
!= GENL_ID_CTRL
)
82 if (genlmsghdr
->cmd
!= CTRL_CMD_NEWFAMILY
)
86 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
90 attr
= (struct nlattr
*)GENLMSG_DATA(reply
);
91 attr
= (struct nlattr
*)((char *)attr
+ NLA_ALIGN(attr
->nla_len
));
94 if (attr
->nla_type
!= CTRL_ATTR_FAMILY_ID
)
97 ret
= *(__u16
*) NLA_DATA(attr
);
99 netlink_close(&handler
);
101 genlmsg_free(request
);
106 extern int genetlink_open(struct genl_handler
*handler
, const char *family
)
109 handler
->family
= genetlink_resolve_family(family
);
110 if (handler
->family
< 0)
111 return handler
->family
;
113 ret
= netlink_open(&handler
->nlh
, NETLINK_GENERIC
);
118 extern int genetlink_close(struct genl_handler
*handler
)
120 return netlink_close(&handler
->nlh
);
123 extern int genetlink_rcv(struct genl_handler
*handler
, struct genlmsg
*genlmsg
)
125 return netlink_rcv(&handler
->nlh
, (struct nlmsg
*)&genlmsg
->nlmsghdr
);
128 extern int genetlink_send(struct genl_handler
*handler
, struct genlmsg
*genlmsg
)
131 return netlink_send(&handler
->nlh
, (struct nlmsg
*)&genlmsg
->nlmsghdr
);
134 extern int genetlink_transaction(struct genl_handler
*handler
,
135 struct genlmsg
*request
, struct genlmsg
*answer
)
137 return netlink_transaction(&handler
->nlh
, (struct nlmsg
*)&request
->nlmsghdr
,
138 (struct nlmsg
*)&answer
->nlmsghdr
);
141 extern struct genlmsg
*genlmsg_alloc(size_t size
)
143 size_t len
= NLMSG_LENGTH(GENL_HDRLEN
) + size
;
144 return (struct genlmsg
*)nlmsg_alloc(len
);
147 extern void genlmsg_free(struct genlmsg
*genlmsg
)