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;