intermodule.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Deprecated, do not use. Moved from module.c to here. --RR */
  2. /* Written by Keith Owens <kaos@ocs.com.au> Oct 2000 */
  3. #include <linux/module.h>
  4. #include <linux/kmod.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/slab.h>
  8. /* inter_module functions are always available, even when the kernel is
  9. * compiled without modules. Consumers of inter_module_xxx routines
  10. * will always work, even when both are built into the kernel, this
  11. * approach removes lots of #ifdefs in mainline code.
  12. */
  13. static struct list_head ime_list = LIST_HEAD_INIT(ime_list);
  14. static DEFINE_SPINLOCK(ime_lock);
  15. static int kmalloc_failed;
  16. struct inter_module_entry {
  17. struct list_head list;
  18. const char *im_name;
  19. struct module *owner;
  20. const void *userdata;
  21. };
  22. /**
  23. * inter_module_register - register a new set of inter module data.
  24. * @im_name: an arbitrary string to identify the data, must be unique
  25. * @owner: module that is registering the data, always use THIS_MODULE
  26. * @userdata: pointer to arbitrary userdata to be registered
  27. *
  28. * Description: Check that the im_name has not already been registered,
  29. * complain if it has. For new data, add it to the inter_module_entry
  30. * list.
  31. */
  32. void inter_module_register(const char *im_name, struct module *owner, const void *userdata)
  33. {
  34. struct list_head *tmp;
  35. struct inter_module_entry *ime, *ime_new;
  36. if (!(ime_new = kzalloc(sizeof(*ime), GFP_KERNEL))) {
  37. /* Overloaded kernel, not fatal */
  38. printk(KERN_ERR
  39. "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n",
  40. im_name);
  41. kmalloc_failed = 1;
  42. return;
  43. }
  44. ime_new->im_name = im_name;
  45. ime_new->owner = owner;
  46. ime_new->userdata = userdata;
  47. spin_lock(&ime_lock);
  48. list_for_each(tmp, &ime_list) {
  49. ime = list_entry(tmp, struct inter_module_entry, list);
  50. if (strcmp(ime->im_name, im_name) == 0) {
  51. spin_unlock(&ime_lock);
  52. kfree(ime_new);
  53. /* Program logic error, fatal */
  54. printk(KERN_ERR "inter_module_register: duplicate im_name '%s'", im_name);
  55. BUG();
  56. }
  57. }
  58. list_add(&(ime_new->list), &ime_list);
  59. spin_unlock(&ime_lock);
  60. }
  61. /**
  62. * inter_module_unregister - unregister a set of inter module data.
  63. * @im_name: an arbitrary string to identify the data, must be unique
  64. *
  65. * Description: Check that the im_name has been registered, complain if
  66. * it has not. For existing data, remove it from the
  67. * inter_module_entry list.
  68. */
  69. void inter_module_unregister(const char *im_name)
  70. {
  71. struct list_head *tmp;
  72. struct inter_module_entry *ime;
  73. spin_lock(&ime_lock);
  74. list_for_each(tmp, &ime_list) {
  75. ime = list_entry(tmp, struct inter_module_entry, list);
  76. if (strcmp(ime->im_name, im_name) == 0) {
  77. list_del(&(ime->list));
  78. spin_unlock(&ime_lock);
  79. kfree(ime);
  80. return;
  81. }
  82. }
  83. spin_unlock(&ime_lock);
  84. if (kmalloc_failed) {
  85. printk(KERN_ERR
  86. "inter_module_unregister: no entry for '%s', "
  87. "probably caused by previous kmalloc failure\n",
  88. im_name);
  89. return;
  90. }
  91. else {
  92. /* Program logic error, fatal */
  93. printk(KERN_ERR "inter_module_unregister: no entry for '%s'", im_name);
  94. BUG();
  95. }
  96. }
  97. /**
  98. * inter_module_get - return arbitrary userdata from another module.
  99. * @im_name: an arbitrary string to identify the data, must be unique
  100. *
  101. * Description: If the im_name has not been registered, return NULL.
  102. * Try to increment the use count on the owning module, if that fails
  103. * then return NULL. Otherwise return the userdata.
  104. */
  105. static const void *inter_module_get(const char *im_name)
  106. {
  107. struct list_head *tmp;
  108. struct inter_module_entry *ime;
  109. const void *result = NULL;
  110. spin_lock(&ime_lock);
  111. list_for_each(tmp, &ime_list) {
  112. ime = list_entry(tmp, struct inter_module_entry, list);
  113. if (strcmp(ime->im_name, im_name) == 0) {
  114. if (try_module_get(ime->owner))
  115. result = ime->userdata;
  116. break;
  117. }
  118. }
  119. spin_unlock(&ime_lock);
  120. return(result);
  121. }
  122. /**
  123. * inter_module_get_request - im get with automatic request_module.
  124. * @im_name: an arbitrary string to identify the data, must be unique
  125. * @modname: module that is expected to register im_name
  126. *
  127. * Description: If inter_module_get fails, do request_module then retry.
  128. */
  129. const void *inter_module_get_request(const char *im_name, const char *modname)
  130. {
  131. const void *result = inter_module_get(im_name);
  132. if (!result) {
  133. request_module("%s", modname);
  134. result = inter_module_get(im_name);
  135. }
  136. return(result);
  137. }
  138. /**
  139. * inter_module_put - release use of data from another module.
  140. * @im_name: an arbitrary string to identify the data, must be unique
  141. *
  142. * Description: If the im_name has not been registered, complain,
  143. * otherwise decrement the use count on the owning module.
  144. */
  145. void inter_module_put(const char *im_name)
  146. {
  147. struct list_head *tmp;
  148. struct inter_module_entry *ime;
  149. spin_lock(&ime_lock);
  150. list_for_each(tmp, &ime_list) {
  151. ime = list_entry(tmp, struct inter_module_entry, list);
  152. if (strcmp(ime->im_name, im_name) == 0) {
  153. if (ime->owner)
  154. module_put(ime->owner);
  155. spin_unlock(&ime_lock);
  156. return;
  157. }
  158. }
  159. spin_unlock(&ime_lock);
  160. printk(KERN_ERR "inter_module_put: no entry for '%s'", im_name);
  161. BUG();
  162. }
  163. EXPORT_SYMBOL(inter_module_register);
  164. EXPORT_SYMBOL(inter_module_unregister);
  165. EXPORT_SYMBOL(inter_module_get_request);
  166. EXPORT_SYMBOL(inter_module_put);
  167. MODULE_LICENSE("GPL");