diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/attr.c | 6 | ||||
-rw-r--r-- | src/callback.c | 9 | ||||
-rw-r--r-- | src/libmnl.map | 5 | ||||
-rw-r--r-- | src/nlmsg.c | 6 | ||||
-rw-r--r-- | src/socket.c | 73 |
5 files changed, 83 insertions, 16 deletions
@@ -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. @@ -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 @@ -200,6 +199,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), }; /** @@ -427,7 +427,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) @@ -568,7 +567,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/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, diff --git a/src/libmnl.map b/src/libmnl.map index dbc332e..e5920e5 100644 --- a/src/libmnl.map +++ b/src/libmnl.map @@ -72,3 +72,8 @@ local: *; LIBMNL_1.1 { mnl_attr_parse_payload; } LIBMNL_1.0; + +LIBMNL_1.2 { + mnl_socket_open2; + mnl_socket_fdopen; +} LIBMNL_1.1; diff --git a/src/nlmsg.c b/src/nlmsg.c index 000829e..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. @@ -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 @@ -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) { diff --git a/src/socket.c b/src/socket.c index 6d54563..d63ab87 100644 --- a/src/socket.c +++ b/src/socket.c @@ -103,30 +103,87 @@ unsigned int mnl_socket_get_portid(const struct mnl_socket *nl) } EXPORT_SYMBOL(mnl_socket_get_portid); +static struct mnl_socket *__mnl_socket_open(int bus, int flags) +{ + struct mnl_socket *nl; + + nl = calloc(1, sizeof(struct mnl_socket)); + if (nl == NULL) + return NULL; + + nl->fd = socket(AF_NETLINK, SOCK_RAW | flags, bus); + if (nl->fd == -1) { + free(nl); + return NULL; + } + + return nl; +} + /** * 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) { + 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. + * + * 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); - nl = calloc(sizeof(struct mnl_socket), 1); - if (nl == NULL) + ret = getsockname(fd, (struct sockaddr *) &addr, &addr_len); + if (ret == -1) return NULL; - nl->fd = socket(AF_NETLINK, SOCK_RAW, bus); - if (nl->fd == -1) { - free(nl); + 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_open); +EXPORT_SYMBOL(mnl_socket_fdopen); /** * mnl_socket_bind - bind netlink socket |