35 #include <netlink-private/netlink.h> 36 #include <netlink/netlink.h> 37 #include <netlink/cache.h> 38 #include <netlink/utils.h> 41 #define NASSOC_INIT 16 42 #define NASSOC_EXPAND 8 45 static int include_cb(
struct nl_object *obj,
struct nl_parser_param *p)
47 struct nl_cache_assoc *ca = p->pp_arg;
48 struct nl_cache_ops *ops = ca->ca_cache->c_ops;
50 NL_DBG(2,
"Including object %p into cache %p\n", obj, ca->ca_cache);
56 if (ops->co_event_filter)
57 if (ops->co_event_filter(ca->ca_cache, obj) !=
NL_OK)
60 if (ops->co_include_event)
61 return ops->co_include_event(ca->ca_cache, obj, ca->ca_change,
64 return nl_cache_include(ca->ca_cache, obj, ca->ca_change, ca->ca_change_data);
67 static int event_input(
struct nl_msg *msg,
void *arg)
69 struct nl_cache_mngr *mngr = arg;
70 int protocol = nlmsg_get_proto(msg);
72 struct nl_cache_ops *ops;
74 struct nl_parser_param p = {
78 NL_DBG(2,
"Cache manager %p, handling new message %p as event\n",
85 if (mngr->cm_protocol != protocol)
88 for (i = 0; i < mngr->cm_nassocs; i++) {
89 if (mngr->cm_assocs[i].ca_cache) {
90 ops = mngr->cm_assocs[i].ca_cache->c_ops;
91 for (n = 0; ops->co_msgtypes[n].mt_id >= 0; n++)
92 if (ops->co_msgtypes[n].mt_id == type)
100 NL_DBG(2,
"Associated message %p to cache %p\n",
101 msg, mngr->cm_assocs[i].ca_cache);
102 p.pp_arg = &mngr->cm_assocs[i];
104 return nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
136 struct nl_cache_mngr **result)
138 struct nl_cache_mngr *mngr;
139 int err = -NLE_NOMEM;
142 if (flags & NL_ALLOCATED_SOCK)
145 mngr = calloc(1,
sizeof(*mngr));
153 flags |= NL_ALLOCATED_SOCK;
157 mngr->cm_nassocs = NASSOC_INIT;
158 mngr->cm_protocol = protocol;
159 mngr->cm_flags = flags;
160 mngr->cm_assocs = calloc(mngr->cm_nassocs,
161 sizeof(
struct nl_cache_assoc));
162 if (!mngr->cm_assocs)
168 if ((err =
nl_connect(mngr->cm_sock, protocol)) < 0)
176 if (!mngr->cm_sync_sock) {
180 if ((err =
nl_connect(mngr->cm_sync_sock, protocol)) < 0)
181 goto errout_free_sync_sock;
183 NL_DBG(1,
"Allocated cache manager %p, protocol %d, %d caches\n",
184 mngr, protocol, mngr->cm_nassocs);
189 errout_free_sync_sock:
222 change_func_t cb,
void *data)
224 struct nl_cache_ops *ops;
225 struct nl_af_group *grp;
232 if (ops->co_protocol != mngr->cm_protocol)
233 return -NLE_PROTO_MISMATCH;
235 if (ops->co_groups == NULL)
236 return -NLE_OPNOTSUPP;
238 for (i = 0; i < mngr->cm_nassocs; i++)
239 if (mngr->cm_assocs[i].ca_cache &&
240 mngr->cm_assocs[i].ca_cache->c_ops == ops)
244 for (i = 0; i < mngr->cm_nassocs; i++)
245 if (!mngr->cm_assocs[i].ca_cache)
248 if (i >= mngr->cm_nassocs) {
249 mngr->cm_nassocs += NASSOC_EXPAND;
250 mngr->cm_assocs = realloc(mngr->cm_assocs,
252 sizeof(
struct nl_cache_assoc));
253 if (mngr->cm_assocs == NULL)
256 memset(mngr->cm_assocs + (mngr->cm_nassocs - NASSOC_EXPAND), 0,
257 NASSOC_EXPAND *
sizeof(
struct nl_cache_assoc));
259 NL_DBG(1,
"Increased capacity of cache manager %p " \
260 "to %d\n", mngr, mngr->cm_nassocs);
264 for (grp = ops->co_groups; grp->ag_group; grp++) {
265 err = nl_socket_add_membership(mngr->cm_sock, grp->ag_group);
272 goto errout_drop_membership;
274 mngr->cm_assocs[i].ca_cache = cache;
275 mngr->cm_assocs[i].ca_change = cb;
276 mngr->cm_assocs[i].ca_change_data = data;
278 if (mngr->cm_flags & NL_AUTO_PROVIDE)
281 NL_DBG(1,
"Added cache %p <%s> to cache manager %p\n",
282 cache, nl_cache_name(cache), mngr);
286 errout_drop_membership:
287 for (grp = ops->co_groups; grp->ag_group; grp++)
288 nl_socket_drop_membership(mngr->cm_sock, grp->ag_group);
322 change_func_t cb,
void *data,
struct nl_cache **result)
324 struct nl_cache_ops *ops;
325 struct nl_cache *cache;
339 goto errout_free_cache;
386 struct pollfd fds = {
391 NL_DBG(3,
"Cache manager %p, poll() fd %d\n", mngr, fds.fd);
392 ret = poll(&fds, 1, timeout);
393 NL_DBG(3,
"Cache manager %p, poll() returned %d\n", mngr, ret);
397 NL_DBG(4,
"nl_cache_mngr_poll(%p): poll() failed with %d (%s)\n",
398 mngr, errno, strerror_r(errno, buf,
sizeof(buf)));
399 return -nl_syserr2nlerr(errno);
429 NL_DBG(2,
"Cache manager %p, reading new data from fd %d\n",
439 NL_DBG(2,
"Cache manager %p, recvmsgs read %d messages\n",
445 if (err < 0 && err != -NLE_AGAIN)
465 nl_dump_line(p,
"cache-manager <%p>\n", mngr);
466 nl_dump_line(p,
" .protocol = %s\n",
467 nl_nlfamily2str(mngr->cm_protocol, buf,
sizeof(buf)));
468 nl_dump_line(p,
" .flags = %#x\n", mngr->cm_flags);
469 nl_dump_line(p,
" .nassocs = %u\n", mngr->cm_nassocs);
470 nl_dump_line(p,
" .sock = <%p>\n", mngr->cm_sock);
472 for (i = 0; i < mngr->cm_nassocs; i++) {
473 struct nl_cache_assoc *assoc = &mngr->cm_assocs[i];
475 if (assoc->ca_cache) {
476 nl_dump_line(p,
" .cache[%d] = <%p> {\n", i, assoc->ca_cache);
477 nl_dump_line(p,
" .name = %s\n", assoc->ca_cache->c_ops->co_name);
478 nl_dump_line(p,
" .change_func = <%p>\n", assoc->ca_change);
479 nl_dump_line(p,
" .change_data = <%p>\n", assoc->ca_change_data);
481 nl_dump_line(p,
" .objects = {\n");
487 nl_dump_line(p,
" }\n");
488 nl_dump_line(p,
" }\n");
509 if (mngr->cm_sync_sock) {
514 if (mngr->cm_flags & NL_ALLOCATED_SOCK)
517 for (i = 0; i < mngr->cm_nassocs; i++) {
518 if (mngr->cm_assocs[i].ca_cache) {
524 free(mngr->cm_assocs);
526 NL_DBG(1,
"Cache manager %p freed\n", mngr);
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags, struct nl_cache_mngr **result)
Allocate new cache manager.
int nl_cache_mngr_get_fd(struct nl_cache_mngr *mngr)
Get socket file descriptor.
Customized handler specified by the user.
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
void nl_cache_mngr_info(struct nl_cache_mngr *mngr, struct nl_dump_params *p)
Print information about cache manager.
int nl_cache_mngr_poll(struct nl_cache_mngr *mngr, int timeout)
Check for event notifications.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
Dump message in human readable format to file descriptor.
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
int nl_socket_set_nonblocking(const struct nl_sock *sk)
Set file descriptor of socket to non-blocking state.
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
int nl_cache_nitems(struct nl_cache *cache)
Return the number of items in the cache.
int nl_cache_mngr_add_cache(struct nl_cache_mngr *mngr, struct nl_cache *cache, change_func_t cb, void *data)
Add cache to cache manager.
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Proceed with wathever would come next.
int dp_prefix
Specifies the number of whitespaces to be put in front of every new line (indentation).
int nl_cache_mngr_data_ready(struct nl_cache_mngr *mngr)
Receive available event notifications.
void nl_close(struct nl_sock *sk)
Close Netlink socket.
void nl_cache_mngr_free(struct nl_cache_mngr *mngr)
Free cache manager and all caches.
int nl_cache_mngr_add(struct nl_cache_mngr *mngr, const char *name, change_func_t cb, void *data, struct nl_cache **result)
Add cache to cache manager.
int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket and report parsed messages.
int nl_debug
Global variable indicating the desired level of debugging output.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.