This commit was generated by cvs2svn to compensate for changes in r786,
[libnl.git] / lib / cache_mngt.c
1 /*
2  * lib/cache_mngt.c     Cache Management
3  *
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
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 /**
13  * @ingroup utils
14  * @defgroup cache_mngt Cache Management
15  * @{
16  */
17
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/cache.h>
21 #include <netlink/utils.h>
22
23 /**
24  * @name Access Functions
25  * @{
26  */
27
28 /**
29  * Return the cache type of the cache operations
30  * @arg ops             cache operations
31  */
32 char *nl_cache_ops_get_name(struct nl_cache_ops *ops)
33 {
34         return ops->co_name;
35 }
36
37 /** @} */
38
39 /**
40  * @name Message Type Association
41  * @{
42  */
43
44 static struct nl_cache_ops *cache_ops;
45
46 /**
47  * Associate a message type to a set of cache operations
48  * @arg protocol                netlink protocol
49  * @arg message_type            netlink message type
50  *
51  * Associates the specified netlink message type with
52  * a registered set of cache operations.
53  *
54  * @return The cache operations or NULL if no association
55  *         could be made.
56  */
57 struct nl_cache_ops *nl_cache_mngt_associate(int protocol, int message_type)
58 {
59         int i;
60         struct nl_cache_ops *ops;
61
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)
66                                 return ops;
67
68         return NULL;
69 }
70
71 /**
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.
77  *
78  * Converts a message type to a character string and stores it in the
79  * provided buffer.
80  *
81  * @return The destination buffer or the message type encoded in
82  *         hexidecimal form if no match was found.
83  */
84 char *nl_cache_mngt_type2name(struct nl_cache_ops *ops, int msgtype,
85                               char *buf, size_t len)
86 {
87         int i;
88
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",
92                                  ops->co_name,
93                                  ops->co_msgtypes[i].mt_name);
94                         return buf;
95                 }
96         }
97
98         snprintf(buf, len, "%s->0x%x()", ops->co_name, msgtype);
99         return buf;
100 }
101
102 /** @} */
103
104 /**
105  * @name Cache Type Management
106  * @{
107  */
108
109 /**
110  * Lookup the set cache operations of a certain cache type
111  * @arg name            name of the cache type
112  *
113  * @return The cache operations or NULL if no operations
114  *         have been registered under the specified name.
115  */
116 struct nl_cache_ops *nl_cache_mngt_lookup(const char *name)
117 {
118         struct nl_cache_ops *ops;
119
120         for (ops = cache_ops; ops; ops = ops->co_next)
121                 if (!strcmp(ops->co_name, name))
122                         return ops;
123
124         return NULL;
125 }
126
127 /**
128  * Register a set of cache operations
129  * @arg ops             cache operations
130  *
131  * Called by users of caches to announce the avaibility of
132  * a certain cache type.
133  *
134  * @return 0 on success or a negative error code.
135  */
136 int nl_cache_mngt_register(struct nl_cache_ops *ops)
137 {
138         if (!ops->co_name)
139                 return nl_error(EINVAL, "No cache name specified");
140
141         if (nl_cache_mngt_lookup(ops->co_name))
142                 return nl_error(EEXIST, "Cache operations already exist");
143             
144         ops->co_next = cache_ops;
145         cache_ops = ops;
146
147         NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
148
149         return 0;
150 }
151
152 /**
153  * Unregister a set of cache operations
154  * @arg ops             cache operations
155  *
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()
160  *
161  * @return 0 on success or a negative error code
162  */
163 int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
164 {
165         struct nl_cache_ops *t, **tp;
166
167         for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
168                 if (t == ops)
169                         break;
170
171         if (!t)
172                 return nl_error(ENOENT, "No such cache operations");
173
174         NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
175
176         *tp = t->co_next;
177         return 0;
178 }
179
180 /** @} */
181
182 /**
183  * @name Global Cache Provisioning/Requiring
184  * @{
185  */
186
187 /**
188  * Provide a cache for global use
189  * @arg cache           cache to provide
190  *
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.
194  */
195 void nl_cache_mngt_provide(struct nl_cache *cache)
196 {
197         struct nl_cache_ops *ops;
198
199         ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
200         if (!ops)
201                 BUG();
202         else
203                 ops->co_major_cache = cache;
204 }
205
206 /**
207  * Unprovide a cache for global use
208  * @arg cache           cache to unprovide
209  *
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.
213  */
214 void nl_cache_mngt_unprovide(struct nl_cache *cache)
215 {
216         struct nl_cache_ops *ops;
217
218         ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
219         if (!ops)
220                 BUG();
221         else if (ops->co_major_cache == cache)
222                 ops->co_major_cache = NULL;
223 }
224
225 /**
226  * Demand the use of a global cache
227  * @arg name            name of the required cache type
228  *
229  * Trys to find a cache of the specified type for global
230  * use.
231  *
232  * @return A cache provided by another subsystem of the
233  *         specified type marked to be available.
234  */
235 struct nl_cache *nl_cache_mngt_require(const char *name)
236 {
237         struct nl_cache_ops *ops;
238
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);
245
246                 return NULL;
247         }
248         
249         return ops->co_major_cache;
250 }
251
252 /** @} */
253
254 /** @} */