Skip to content

Commit

Permalink
Allow to allocate cache manager with custom callback on refill socket
Browse files Browse the repository at this point in the history
Cache managers use two sockets: one for cache refill operation,
and another one for notifications.

In order to simulate NETLINK events by reading data from files,
we need to be able to overwrite both sockets.

This new function allows exactly that.
  • Loading branch information
ievenbach committed Apr 24, 2024
1 parent 96ddcd9 commit 08241df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/netlink/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ struct nl_cache_mngr;
extern int nl_cache_mngr_alloc(struct nl_sock *,
int, int,
struct nl_cache_mngr **);
extern int nl_cache_mngr_alloc_cb(struct nl_sock *,
int, int,
struct nl_cache_mngr **,
struct nl_cb *sync_cb);
extern int nl_cache_mngr_add(struct nl_cache_mngr *,
const char *,
change_func_t,
Expand Down
23 changes: 22 additions & 1 deletion lib/cache_mngr.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ static int event_input(struct nl_msg *msg, void *arg)
*/
int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags,
struct nl_cache_mngr **result)
{
return nl_cache_mngr_alloc_cb(sk, protocol, flags, result, NULL);
}

/**
* Allocate new cache manager, with custom callback on refill socket
* @arg sk Netlink socket or NULL to auto allocate
* @arg protocol Netlink protocol this manager is used for
* @arg flags Flags (\c NL_AUTO_PROVIDE)
* @arg result Result pointer
* @arg sync_cb Callback for cache refill socket
*
* Same as \f nl_cache_mngr_alloc, but sets custom callback for refill socket
*/
int nl_cache_mngr_alloc_cb(struct nl_sock *sk, int protocol, int flags,
struct nl_cache_mngr **result, struct nl_cb *sync_cb)
{
struct nl_cache_mngr *mngr;
int err = -NLE_NOMEM;
Expand Down Expand Up @@ -186,7 +202,12 @@ int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags,
goto errout;

/* Create and allocate socket for sync cache fills */
mngr->cm_sync_sock = nl_socket_alloc();
if(sync_cb) {
mngr->cm_sync_sock = nl_socket_alloc_cb(sync_cb);
} else {
mngr->cm_sync_sock = nl_socket_alloc();
}

if (!mngr->cm_sync_sock) {
err = -NLE_NOMEM;
goto errout;
Expand Down
1 change: 1 addition & 0 deletions libnl-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ global:
nl_cache_mngr_add;
nl_cache_mngr_add_cache;
nl_cache_mngr_alloc;
nl_cache_mngr_alloc_cb;
nl_cache_mngr_data_ready;
nl_cache_mngr_free;
nl_cache_mngr_get_fd;
Expand Down

0 comments on commit 08241df

Please sign in to comment.