cache_mngt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup core
  13. * @defgroup cache_mngt Caching
  14. * @{
  15. */
  16. #include <netlink-local.h>
  17. #include <netlink/netlink.h>
  18. #include <netlink/cache.h>
  19. #include <netlink/utils.h>
  20. static struct nl_cache_ops *cache_ops;
  21. /**
  22. * @name Cache Operations Sets
  23. * @{
  24. */
  25. /**
  26. * Lookup the set cache operations of a certain cache type
  27. * @arg name name of the cache type
  28. *
  29. * @return The cache operations or NULL if no operations
  30. * have been registered under the specified name.
  31. */
  32. struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
  33. {
  34. struct nl_cache_ops *ops;
  35. for (ops = cache_ops; ops; ops = ops->co_next)
  36. if (!strcmp(ops->co_name, name))
  37. return ops;
  38. return NULL;
  39. }
  40. /**
  41. * Associate a message type to a set of cache operations
  42. * @arg protocol netlink protocol
  43. * @arg msgtype netlink message type
  44. *
  45. * Associates the specified netlink message type with
  46. * a registered set of cache operations.
  47. *
  48. * @return The cache operations or NULL if no association
  49. * could be made.
  50. */
  51. struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
  52. {
  53. int i;
  54. struct nl_cache_ops *ops;
  55. for (ops = cache_ops; ops; ops = ops->co_next) {
  56. if (ops->co_protocol != protocol)
  57. continue;
  58. for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
  59. if (ops->co_msgtypes[i].mt_id == msgtype)
  60. return ops;
  61. }
  62. return NULL;
  63. }
  64. /**
  65. * Register a set of cache operations
  66. * @arg ops cache operations
  67. *
  68. * Called by users of caches to announce the avaibility of
  69. * a certain cache type.
  70. *
  71. * @return 0 on success or a negative error code.
  72. */
  73. int nl_cache_mngt_register(struct nl_cache_ops *ops)
  74. {
  75. if (!ops->co_name || !ops->co_obj_ops)
  76. return -NLE_INVAL;
  77. if (nl_cache_ops_lookup(ops->co_name))
  78. return -NLE_EXIST;
  79. ops->co_next = cache_ops;
  80. cache_ops = ops;
  81. NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
  82. return 0;
  83. }
  84. /**
  85. * Unregister a set of cache operations
  86. * @arg ops cache operations
  87. *
  88. * Called by users of caches to announce a set of
  89. * cache operations is no longer available. The
  90. * specified cache operations must have been registered
  91. * previously using nl_cache_mngt_register()
  92. *
  93. * @return 0 on success or a negative error code
  94. */
  95. int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
  96. {
  97. struct nl_cache_ops *t, **tp;
  98. for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
  99. if (t == ops)
  100. break;
  101. if (!t)
  102. return -NLE_NOCACHE;
  103. NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
  104. *tp = t->co_next;
  105. return 0;
  106. }
  107. /** @} */
  108. /** @} */