From 7aa4b6cc0b994a4af80a34d3f9872447412815e1 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Thu, 14 Mar 2013 12:02:35 +0100 Subject: doxygen: fix a variable name. Variable name in doxygen description was not correct. Signed-off-by: Eric Leblond --- src/nlmsg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nlmsg.c b/src/nlmsg.c index 000829e..fdb7af8 100644 --- a/src/nlmsg.c +++ b/src/nlmsg.c @@ -222,7 +222,7 @@ EXPORT_SYMBOL(mnl_nlmsg_seq_ok); /** * mnl_nlmsg_portid_ok - perform portID origin check * \param nlh current netlink message that we are handling - * \param seq netlink portid that we want to check + * \param portid netlink portid that we want to check * * This functions returns true if the origin is fulfilled, otherwise * false is returned. We skip the tracking for netlink message whose portID -- cgit v1.2.3 From 260759dfdebe774aba6866bf49c2928b2242bb7e Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Tue, 26 Mar 2013 22:15:35 +0100 Subject: callback: fix missing handling of NLM_F_DUMP_INTR Propagate the error to the user-space application, that should retry. [ I have mangled this patch to document EINTR in mnl_cb_run. --pablo ] Signed-off-by: Stephen Hemminger Acked-by: Nicolas Dichtel Signed-off-by: Pablo Neira Ayuso --- src/callback.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/callback.c b/src/callback.c index 6337acc..f023401 100644 --- a/src/callback.c +++ b/src/callback.c @@ -65,6 +65,12 @@ __mnl_cb_run(const void *buf, size_t numbytes, unsigned int seq, return -1; } + /* dump was interrupted */ + if (nlh->nlmsg_flags & NLM_F_DUMP_INTR) { + errno = EINTR; + return -1; + } + /* netlink data message handling */ if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) { if (cb_data){ @@ -117,7 +123,8 @@ out: * This function propagates the callback return value. On error, it returns * -1 and errno is explicitly set. If the portID is not the expected, errno * is set to ESRCH. If the sequence number is not the expected, errno is set - * to EPROTO. + * to EPROTO. If the dump was interrupted, errno is set to EINTR and you should + * request a new fresh dump again. */ int mnl_cb_run2(const void *buf, size_t numbytes, unsigned int seq, -- cgit v1.2.3 From 97d1ff851f00fb7caec35f51d43b1ea53e80d9f9 Mon Sep 17 00:00:00 2001 From: Shawn Landden Date: Wed, 4 Dec 2013 11:06:28 -0800 Subject: doc: mnl_socket_open returns NULL on error Signed-off-by: Florian Westphal --- src/socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/socket.c b/src/socket.c index 6d54563..676a08a 100644 --- a/src/socket.c +++ b/src/socket.c @@ -107,7 +107,7 @@ EXPORT_SYMBOL(mnl_socket_get_portid); * mnl_socket_open - open a netlink socket * \param bus the netlink socket bus ID (see NETLINK_* constants) * - * On error, it returns -1 and errno is appropriately set. Otherwise, it + * On error, it returns NULL and errno is appropriately set. Otherwise, it * returns a valid pointer to the mnl_socket structure. */ struct mnl_socket *mnl_socket_open(int bus) -- cgit v1.2.3 From 8b0c82d8bd01347874dcc9a493129082424a3649 Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Sat, 7 Dec 2013 20:27:46 +0900 Subject: attr: validate MNL_TYPE_MSEC same as MNL_TYPE_U64 Signed-off-by: Ken-ichirou MATSUZAWA Signed-off-by: Florian Westphal --- src/attr.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/attr.c b/src/attr.c index 1136c50..c3c57e6 100644 --- a/src/attr.c +++ b/src/attr.c @@ -200,6 +200,7 @@ static const size_t mnl_attr_data_type_len[MNL_TYPE_MAX] = { [MNL_TYPE_U16] = sizeof(uint16_t), [MNL_TYPE_U32] = sizeof(uint32_t), [MNL_TYPE_U64] = sizeof(uint64_t), + [MNL_TYPE_MSECS] = sizeof(uint64_t), }; /** -- cgit v1.2.3 From 2c458b2eb479cbc83c83de79dcd14bec6acc90bd Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 22 Sep 2014 13:11:23 +0200 Subject: socket: calloc expects struct size as second parameter The parameters where accidentally swapped. Reported-by: Chris Rapier Signed-off-by: Pablo Neira Ayuso --- src/socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/socket.c b/src/socket.c index 676a08a..0c3cd72 100644 --- a/src/socket.c +++ b/src/socket.c @@ -114,7 +114,7 @@ struct mnl_socket *mnl_socket_open(int bus) { struct mnl_socket *nl; - nl = calloc(sizeof(struct mnl_socket), 1); + nl = calloc(1, sizeof(struct mnl_socket)); if (nl == NULL) return NULL; -- cgit v1.2.3 From e374664f48724e5b13a848bad5c5353349f0ae38 Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Thu, 25 Sep 2014 09:33:27 +0900 Subject: socket: creating a struct mnl_socket from a pre-existing socket This patch defines a new function mnl_socket_fdopen() which creates a struct mnl_socket object from a pre-existing socket like obtained from other process and different domain/type from the same prodess. Signed-off-by: Ken-ichirou MATSUZAWA Signed-off-by: Pablo Neira Ayuso --- include/libmnl/libmnl.h | 1 + src/libmnl.map | 4 ++++ src/socket.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) (limited to 'src') diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h index 223709c..0de6678 100644 --- a/include/libmnl/libmnl.h +++ b/include/libmnl/libmnl.h @@ -22,6 +22,7 @@ extern "C" { struct mnl_socket; extern struct mnl_socket *mnl_socket_open(int type); +extern struct mnl_socket *mnl_socket_fdopen(int fd); extern int mnl_socket_bind(struct mnl_socket *nl, unsigned int groups, pid_t pid); extern int mnl_socket_close(struct mnl_socket *nl); extern int mnl_socket_get_fd(const struct mnl_socket *nl); diff --git a/src/libmnl.map b/src/libmnl.map index dbc332e..3c147a7 100644 --- a/src/libmnl.map +++ b/src/libmnl.map @@ -72,3 +72,7 @@ local: *; LIBMNL_1.1 { mnl_attr_parse_payload; } LIBMNL_1.0; + +LIBMNL_1.2 { + mnl_socket_fdopen; +} LIBMNL_1.1; diff --git a/src/socket.c b/src/socket.c index 0c3cd72..86657d4 100644 --- a/src/socket.c +++ b/src/socket.c @@ -128,6 +128,40 @@ struct mnl_socket *mnl_socket_open(int bus) } EXPORT_SYMBOL(mnl_socket_open); +/** + * mnl_socket_fdopen - associates a mnl_socket object with pre-existing socket. + * \param fd pre-existing socket descriptor. + * + * On error, it returns NULL and errno is appropriately set. Otherwise, it + * returns a valid pointer to the mnl_socket structure. It also sets the portID + * if the socket fd is already bound and it is AF_NETLINK. + * + * Note that mnl_socket_get_portid() returns 0 if this function is used with + * non-netlink socket. + */ +struct mnl_socket *mnl_socket_fdopen(int fd) +{ + int ret; + struct mnl_socket *nl; + struct sockaddr_nl addr; + socklen_t addr_len = sizeof(struct sockaddr_nl); + + ret = getsockname(fd, (struct sockaddr *) &addr, &addr_len); + if (ret == -1) + return NULL; + + nl = calloc(1, sizeof(struct mnl_socket)); + if (nl == NULL) + return NULL; + + nl->fd = fd; + if (addr.nl_family == AF_NETLINK) + nl->addr = addr; + + return nl; +} +EXPORT_SYMBOL(mnl_socket_fdopen); + /** * mnl_socket_bind - bind netlink socket * \param nl netlink socket obtained via mnl_socket_open() -- cgit v1.2.3 From 72aec11703c7fda93af77cb6356f9692f18f9e9b Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Fri, 24 Oct 2014 14:39:28 +0900 Subject: doc: minor fix mnl_attr_ok(): fix return value type mnl_attr_put_u8(): remove unused param - len mnl_attr_put_u8_check(): remove unused param - len mnl_nlmsg_ok(): fix return value type mnl_nlmsg_batch_stop(): not return batch size, but release it Signed-off-by: Ken-ichirou MATSUZAWA Signed-off-by: Florian Westphal --- src/attr.c | 4 +--- src/nlmsg.c | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/attr.c b/src/attr.c index c3c57e6..ca2cd25 100644 --- a/src/attr.c +++ b/src/attr.c @@ -89,7 +89,7 @@ EXPORT_SYMBOL(mnl_attr_get_payload); * truncated. * * This function does not set errno in case of error since it is intended - * for iterations. Thus, it returns 1 on success and 0 on error. + * for iterations. Thus, it returns true on success and false on error. * * The len parameter may be negative in the case of malformed messages during * attribute iteration, that is why we use a signed integer. @@ -428,7 +428,6 @@ EXPORT_SYMBOL(mnl_attr_put); * mnl_attr_put_u8 - add 8-bit unsigned integer attribute to netlink message * \param nlh pointer to the netlink message * \param type netlink attribute type - * \param len netlink attribute payload size * \param data 8-bit unsigned integer data that is stored by the new attribute * * This function updates the length field of the Netlink message (nlmsg_len) @@ -569,7 +568,6 @@ EXPORT_SYMBOL(mnl_attr_put_check); * \param nlh pointer to the netlink message * \param buflen size of buffer which stores the message * \param type netlink attribute type - * \param len netlink attribute payload size * \param data 8-bit unsigned integer data that is stored by the new attribute * * This function first checks that the data can be added to the message diff --git a/src/nlmsg.c b/src/nlmsg.c index fdb7af8..fd2f698 100644 --- a/src/nlmsg.c +++ b/src/nlmsg.c @@ -150,7 +150,7 @@ EXPORT_SYMBOL(mnl_nlmsg_get_payload_offset); * truncated. * * This function does not set errno in case of error since it is intended - * for iterations. Thus, it returns 1 on success and 0 on error. + * for iterations. Thus, it returns true on success and false on error. * * The len parameter may become negative in malformed messages during message * iteration, that is why we use a signed integer. @@ -462,7 +462,7 @@ EXPORT_SYMBOL(mnl_nlmsg_batch_start); * mnl_nlmsg_batch_stop - release a batch * \param b pointer to batch * - * This function returns the amount of data that is part of this batch. + * This function releases the batch allocated by mnl_nlmsg_batch_start(). */ void mnl_nlmsg_batch_stop(struct mnl_nlmsg_batch *b) { -- cgit v1.2.3 From c9f19b98cd8e108617e825e071091df14f78c53a Mon Sep 17 00:00:00 2001 From: Hisao Tanabe Date: Sat, 10 Jan 2015 18:02:07 +0900 Subject: attr: minor doc fix in mnl_attr_next() mnl_attr_next(): remove unused param - len Signed-off-by: Hisao Tanabe Signed-off-by: Pablo Neira Ayuso --- src/attr.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/attr.c b/src/attr.c index ca2cd25..c551d0b 100644 --- a/src/attr.c +++ b/src/attr.c @@ -105,7 +105,6 @@ EXPORT_SYMBOL(mnl_attr_ok); /** * mnl_attr_next - get the next attribute in the payload of a netlink message * \param attr pointer to the current attribute - * \param len length of the remaining bytes in the buffer (passed by reference). * * This function returns a pointer to the next attribute after the one passed * as parameter. You have to use mnl_attr_ok() to ensure that the next -- cgit v1.2.3 From 1891e0e2cefced50e7bfdacd50942cefe5bf73ba Mon Sep 17 00:00:00 2001 From: Guillaume Nault Date: Fri, 2 Oct 2015 22:12:33 +0200 Subject: socket: introduce mnl_socket_open2() Define mnl_socket_open2() so that user can pass a set of SOCK_* flags at socket creation time. Signed-off-by: Guillaume Nault Signed-off-by: Pablo Neira Ayuso --- include/libmnl/libmnl.h | 3 ++- src/libmnl.map | 1 + src/socket.c | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h index 3a589bc..5adb13c 100644 --- a/include/libmnl/libmnl.h +++ b/include/libmnl/libmnl.h @@ -21,7 +21,8 @@ extern "C" { struct mnl_socket; -extern struct mnl_socket *mnl_socket_open(int type); +extern struct mnl_socket *mnl_socket_open(int bus); +extern struct mnl_socket *mnl_socket_open2(int bus, int flags); extern struct mnl_socket *mnl_socket_fdopen(int fd); extern int mnl_socket_bind(struct mnl_socket *nl, unsigned int groups, pid_t pid); extern int mnl_socket_close(struct mnl_socket *nl); diff --git a/src/libmnl.map b/src/libmnl.map index 3c147a7..e5920e5 100644 --- a/src/libmnl.map +++ b/src/libmnl.map @@ -74,5 +74,6 @@ LIBMNL_1.1 { } LIBMNL_1.0; LIBMNL_1.2 { + mnl_socket_open2; mnl_socket_fdopen; } LIBMNL_1.1; diff --git a/src/socket.c b/src/socket.c index 86657d4..d63ab87 100644 --- a/src/socket.c +++ b/src/socket.c @@ -103,14 +103,7 @@ unsigned int mnl_socket_get_portid(const struct mnl_socket *nl) } EXPORT_SYMBOL(mnl_socket_get_portid); -/** - * mnl_socket_open - open a netlink socket - * \param bus the netlink socket bus ID (see NETLINK_* constants) - * - * On error, it returns NULL and errno is appropriately set. Otherwise, it - * returns a valid pointer to the mnl_socket structure. - */ -struct mnl_socket *mnl_socket_open(int bus) +static struct mnl_socket *__mnl_socket_open(int bus, int flags) { struct mnl_socket *nl; @@ -118,7 +111,7 @@ struct mnl_socket *mnl_socket_open(int bus) if (nl == NULL) return NULL; - nl->fd = socket(AF_NETLINK, SOCK_RAW, bus); + nl->fd = socket(AF_NETLINK, SOCK_RAW | flags, bus); if (nl->fd == -1) { free(nl); return NULL; @@ -126,8 +119,38 @@ struct mnl_socket *mnl_socket_open(int bus) return nl; } + +/** + * mnl_socket_open - open a netlink socket + * \param bus the netlink socket bus ID (see NETLINK_* constants) + * + * On error, it returns NULL and errno is appropriately set. Otherwise, it + * returns a valid pointer to the mnl_socket structure. + */ +struct mnl_socket *mnl_socket_open(int bus) +{ + return __mnl_socket_open(bus, 0); +} EXPORT_SYMBOL(mnl_socket_open); +/** + * mnl_socket_open2 - open a netlink socket with appropriate flags + * \param bus the netlink socket bus ID (see NETLINK_* constants) + * \param flags the netlink socket flags (see SOCK_* constants in socket(2)) + * + * This is similar to mnl_socket_open(), but allows to set flags like + * SOCK_CLOEXEC at socket creation time (useful for multi-threaded programs + * performing exec calls). + * + * On error, it returns NULL and errno is appropriately set. Otherwise, it + * returns a valid pointer to the mnl_socket structure. + */ +struct mnl_socket *mnl_socket_open2(int bus, int flags) +{ + return __mnl_socket_open(bus, flags); +} +EXPORT_SYMBOL(mnl_socket_open2); + /** * mnl_socket_fdopen - associates a mnl_socket object with pre-existing socket. * \param fd pre-existing socket descriptor. -- cgit v1.2.3