2 * lib/cache_mngt.c Cache Management
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cache_mngt Cache Management
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/cache.h>
21 #include <netlink/utils.h>
24 * @name Access Functions
29 * Return the cache type of the cache operations
30 * @arg ops cache operations
32 char *nl_cache_ops_get_name(struct nl_cache_ops *ops)
40 * @name Message Type Association
44 static struct nl_cache_ops *cache_ops;
47 * Associate a message type to a set of cache operations
48 * @arg protocol netlink protocol
49 * @arg message_type netlink message type
51 * Associates the specified netlink message type with
52 * a registered set of cache operations.
54 * @return The cache operations or NULL if no association
57 struct nl_cache_ops *nl_cache_mngt_associate(int protocol, int message_type)
60 struct nl_cache_ops *ops;
62 for (ops = cache_ops; ops; ops = ops->co_next)
63 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
64 if (ops->co_msgtypes[i].mt_id == message_type &&
65 ops->co_protocol == protocol)
72 * Convert message type to character string.
73 * @arg ops Cache operations.
74 * @arg msgtype Message type.
75 * @arg buf Destination buffer.
76 * @arg len Size of destination buffer.
78 * Converts a message type to a character string and stores it in the
81 * @return The destination buffer or the message type encoded in
82 * hexidecimal form if no match was found.
84 char *nl_cache_mngt_type2name(struct nl_cache_ops *ops, int msgtype,
85 char *buf, size_t len)
89 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
90 if (ops->co_msgtypes[i].mt_id == msgtype) {
91 snprintf(buf, len, "%s::%s",
93 ops->co_msgtypes[i].mt_name);
98 snprintf(buf, len, "%s->0x%x()", ops->co_name, msgtype);
105 * @name Cache Type Management
110 * Lookup the set cache operations of a certain cache type
111 * @arg name name of the cache type
113 * @return The cache operations or NULL if no operations
114 * have been registered under the specified name.
116 struct nl_cache_ops *nl_cache_mngt_lookup(const char *name)
118 struct nl_cache_ops *ops;
120 for (ops = cache_ops; ops; ops = ops->co_next)
121 if (!strcmp(ops->co_name, name))
128 * Register a set of cache operations
129 * @arg ops cache operations
131 * Called by users of caches to announce the avaibility of
132 * a certain cache type.
134 * @return 0 on success or a negative error code.
136 int nl_cache_mngt_register(struct nl_cache_ops *ops)
139 return nl_error(EINVAL, "No cache name specified");
141 if (nl_cache_mngt_lookup(ops->co_name))
142 return nl_error(EEXIST, "Cache operations already exist");
144 ops->co_next = cache_ops;
147 NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
153 * Unregister a set of cache operations
154 * @arg ops cache operations
156 * Called by users of caches to announce a set of
157 * cache operations is no longer available. The
158 * specified cache operations must have been registered
159 * previously using nl_cache_mngt_register()
161 * @return 0 on success or a negative error code
163 int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
165 struct nl_cache_ops *t, **tp;
167 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
172 return nl_error(ENOENT, "No such cache operations");
174 NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
183 * @name Global Cache Provisioning/Requiring
188 * Provide a cache for global use
189 * @arg cache cache to provide
191 * Offers the specified cache to be used by other modules.
192 * Only one cache per type may be shared at a time,
193 * a previsouly provided caches will be overwritten.
195 void nl_cache_mngt_provide(struct nl_cache *cache)
197 struct nl_cache_ops *ops;
199 ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
203 ops->co_major_cache = cache;
207 * Unprovide a cache for global use
208 * @arg cache cache to unprovide
210 * Cancels the offer to use a cache globally. The
211 * cache will no longer be returned via lookups but
212 * may still be in use.
214 void nl_cache_mngt_unprovide(struct nl_cache *cache)
216 struct nl_cache_ops *ops;
218 ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
221 else if (ops->co_major_cache == cache)
222 ops->co_major_cache = NULL;
226 * Demand the use of a global cache
227 * @arg name name of the required cache type
229 * Trys to find a cache of the specified type for global
232 * @return A cache provided by another subsystem of the
233 * specified type marked to be available.
235 struct nl_cache *nl_cache_mngt_require(const char *name)
237 struct nl_cache_ops *ops;
239 ops = nl_cache_mngt_lookup(name);
240 if (!ops || !ops->co_major_cache) {
241 fprintf(stderr, "Application BUG: Your application must "
242 "call nl_cache_mngt_provide() and\nprovide a valid "
243 "%s cache to be used for internal lookups.\nSee the "
244 " API documentation for more details.\n", name);
249 return ops->co_major_cache;