17 #include <netlink-local.h> 18 #include <netlink/netlink.h> 19 #include <netlink/cache.h> 20 #include <netlink/utils.h> 23 static NL_RW_LOCK(cache_ops_lock);
30 struct nl_cache_ops *__nl_cache_ops_lookup(
const char *name)
34 for (ops = cache_ops; ops; ops = ops->co_next)
35 if (!strcmp(ops->co_name, name))
72 nl_read_lock(&cache_ops_lock);
73 ops = __nl_cache_ops_lookup(name);
74 nl_read_unlock(&cache_ops_lock);
92 nl_write_lock(&cache_ops_lock);
93 if ((ops = __nl_cache_ops_lookup(name)))
95 nl_write_unlock(&cache_ops_lock);
100 static struct nl_cache_ops *__cache_ops_associate(
int protocol,
int msgtype)
105 for (ops = cache_ops; ops; ops = ops->co_next) {
106 if (ops->co_protocol != protocol)
109 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++)
110 if (ops->co_msgtypes[i].
mt_id == msgtype)
133 nl_read_lock(&cache_ops_lock);
134 ops = __cache_ops_associate(protocol, msgtype);
135 nl_read_unlock(&cache_ops_lock);
157 nl_write_lock(&cache_ops_lock);
158 if ((ops = __cache_ops_associate(protocol, msgtype)))
160 nl_write_unlock(&cache_ops_lock);
182 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++)
183 if (ops->co_msgtypes[i].
mt_id == msgtype)
184 return &ops->co_msgtypes[i];
194 for (ops = cache_ops; ops; ops = ops->co_next)
195 if (ops->co_obj_ops == obj_ops)
211 nl_read_lock(&cache_ops_lock);
212 for (ops = cache_ops; ops; ops = ops->co_next)
214 nl_read_unlock(&cache_ops_lock);
229 return nl_error(EINVAL,
"No cache name specified");
231 if (!ops->co_obj_ops)
232 return nl_error(EINVAL,
"No obj cache ops specified");
234 nl_write_lock(&cache_ops_lock);
235 if (__nl_cache_ops_lookup(ops->co_name)) {
236 nl_write_unlock(&cache_ops_lock);
237 return nl_error(EEXIST,
"Cache operations already exist");
241 ops->co_next = cache_ops;
243 nl_write_unlock(&cache_ops_lock);
245 NL_DBG(1,
"Registered cache operations %s\n", ops->co_name);
265 nl_write_lock(&cache_ops_lock);
268 nl_write_unlock(&cache_ops_lock);
269 return nl_error(EBUSY,
"Cache operations busy");
272 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
277 nl_write_unlock(&cache_ops_lock);
278 return nl_error(ENOENT,
"No such cache operations");
281 NL_DBG(1,
"Unregistered cache operations %s\n", ops->co_name);
284 nl_write_unlock(&cache_ops_lock);
308 nl_write_lock(&cache_ops_lock);
310 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
315 ops->co_major_cache = cache;
318 nl_write_unlock(&cache_ops_lock);
333 nl_write_lock(&cache_ops_lock);
335 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
338 else if (ops->co_major_cache == cache) {
340 ops->co_major_cache = NULL;
343 nl_write_unlock(&cache_ops_lock);
361 if (!ops || !ops->co_major_cache) {
362 fprintf(stderr,
"Application BUG: Your application must " 363 "call nl_cache_mngt_provide() and\nprovide a valid " 364 "%s cache to be used for internal lookups.\nSee the " 365 " API documentation for more details.\n", name);
370 return ops->co_major_cache;
Message type to cache action association.
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.
void nl_cache_ops_get(struct nl_cache_ops *ops)
Increment reference counter.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
unsigned int co_refcnt
Reference counter.
struct nl_cache * nl_cache_mngt_require(const char *name)
Demand the use of a global cache.
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
void nl_cache_ops_foreach(void(*cb)(struct nl_cache_ops *, void *), void *arg)
Call a function for each registered cache operation.
int mt_id
Netlink message type.
struct nl_cache_ops * nl_cache_ops_associate(int protocol, int msgtype)
Associate protocol and message type to cache operations.
struct nl_msgtype * nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
Lookup message type cache association.
struct nl_cache_ops * nl_cache_ops_associate_safe(int protocol, int msgtype)
Associate protocol and message type to cache operations.