loadlib.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. ** $Id: loadlib.c,v 1.8 2004/10/18 18:07:31 roberto Exp roberto $
  3. ** Dynamic library loader for Lua
  4. ** See Copyright Notice in lua.h
  5. *
  6. * This Lua library exports a single function, called loadlib, which
  7. * is called from Lua as loadlib(lib,init), where lib is the full
  8. * name of the library to be loaded (including the complete path) and
  9. * init is the name of a function to be called after the library is
  10. * loaded. Typically, this function will register other functions, thus
  11. * making the complete library available to Lua. The init function is
  12. * *not* automatically called by loadlib. Instead, loadlib returns the
  13. * init function as a Lua function that the client can call when it
  14. * thinks is appropriate. In the case of errors, loadlib returns nil and
  15. * two strings describing the error. The first string is supplied by
  16. * the operating system; it should be informative and useful for error
  17. * messages. The second string is "open", "init", or "absent" to identify
  18. * the error and is meant to be used for making decisions without having
  19. * to look into the first string (whose format is system-dependent).
  20. * This module contains an implementation of loadlib for Unix systems
  21. * that have dlfcn, an implementation for Darwin (Mac OS X), an
  22. * implementation for Windows, and a stub for other systems. See
  23. * the list at the end of this file for some links to available
  24. * implementations of dlfcn and interfaces to other native dynamic
  25. * loaders on top of which loadlib could be implemented.
  26. */
  27. #define loadlib_c
  28. #define LUA_LIB
  29. #include "lua.h"
  30. #include "lauxlib.h"
  31. #include "lualib.h"
  32. #define ERR_OPEN 1
  33. #define ERR_FUNCTION 2
  34. static void registerlib (lua_State *L, const void *lib);
  35. #if defined(USE_DLOPEN)
  36. /*
  37. * This is an implementation of loadlib based on the dlfcn interface.
  38. * The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  39. * NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  40. * as an emulation layer on top of native functions.
  41. */
  42. #include <dlfcn.h>
  43. #define freelib dlclose
  44. static int loadlib(lua_State *L, const char *path, const char *init)
  45. {
  46. void *lib=dlopen(path,RTLD_NOW);
  47. if (lib!=NULL)
  48. {
  49. lua_CFunction f=(lua_CFunction) dlsym(lib,init);
  50. if (f!=NULL)
  51. {
  52. registerlib(L, lib);
  53. lua_pushcfunction(L,f);
  54. return 0;
  55. }
  56. }
  57. /* else return appropriate error message */
  58. lua_pushstring(L,dlerror());
  59. if (lib!=NULL) {
  60. dlclose(lib);
  61. return ERR_FUNCTION; /* error loading function */
  62. }
  63. else return ERR_OPEN; /* error loading library */
  64. }
  65. #elif defined(USE_DLL)
  66. /*
  67. * This is an implementation of loadlib for Windows using native functions.
  68. */
  69. #include <windows.h>
  70. #define freelib(lib) FreeLibrary((HINSTANCE)lib)
  71. static void pusherror(lua_State *L)
  72. {
  73. int error=GetLastError();
  74. char buffer[128];
  75. if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  76. 0, error, 0, buffer, sizeof(buffer), 0))
  77. lua_pushstring(L,buffer);
  78. else
  79. lua_pushfstring(L,"system error %d\n",error);
  80. }
  81. static int loadlib(lua_State *L, const char *path, const char *init)
  82. {
  83. HINSTANCE lib=LoadLibrary(path);
  84. if (lib!=NULL)
  85. {
  86. lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init);
  87. if (f!=NULL)
  88. {
  89. registerlib(L, lib);
  90. lua_pushcfunction(L,f);
  91. return 1;
  92. }
  93. }
  94. pusherror(L);
  95. if (lib!=NULL) {
  96. FreeLibrary(lib);
  97. return ERR_OPEN;
  98. }
  99. else return ERR_FUNCTION;
  100. }
  101. /* Native Mac OS X / Darwin Implementation */
  102. #elif defined(USE_DYLD)
  103. #include <mach-o/dyld.h>
  104. /* Mach cannot unload images (?) */
  105. #define freelib(lib) ((void)lib)
  106. static void pusherror (lua_State *L)
  107. {
  108. const char *err_str;
  109. const char *err_file;
  110. NSLinkEditErrors err;
  111. int err_num;
  112. NSLinkEditError(&err, &err_num, &err_file, &err_str);
  113. lua_pushstring(L, err_str);
  114. }
  115. static int loadlib (lua_State *L, const char *path, const char *init) {
  116. const struct mach_header *lib;
  117. /* this would be a rare case, but prevents crashing if it happens */
  118. if(!_dyld_present()) {
  119. lua_pushstring(L,"dyld not present.");
  120. return ERR_OPEN;
  121. }
  122. lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  123. if(lib != NULL) {
  124. NSSymbol init_sym = NSLookupSymbolInImage(lib, init,
  125. NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
  126. NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  127. if(init_sym != NULL) {
  128. lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym);
  129. registerlib(L, lib);
  130. lua_pushcfunction(L,f);
  131. return 0;
  132. }
  133. }
  134. /* else an error ocurred */
  135. pusherror(L);
  136. /* Can't unload image */
  137. return (lib != NULL) ? ERR_FUNCTION : ERR_OPEN;
  138. }
  139. #else
  140. /* Fallback for other systems */
  141. #define freelib(lib) ((void)lib)
  142. static int loadlib(lua_State *L)
  143. {
  144. registerlib(L, NULL); /* to avoid warnings */
  145. lua_pushnil(L);
  146. lua_pushliteral(L,"`loadlib' not supported");
  147. lua_pushliteral(L,"absent");
  148. return 3;
  149. }
  150. #endif
  151. /*
  152. ** common code for all implementations: put a library into the registry
  153. ** so that, when Lua closes its state, the library's __gc metamethod
  154. ** will be called to unload the library.
  155. */
  156. static void registerlib (lua_State *L, const void *lib)
  157. {
  158. const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *));
  159. *plib = lib;
  160. luaL_getmetatable(L, "_LOADLIB");
  161. lua_setmetatable(L, -2);
  162. lua_pushboolean(L, 1);
  163. lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */
  164. }
  165. /*
  166. ** __gc tag method: calls library's `freelib' function with the lib
  167. ** handle
  168. */
  169. static int gctm (lua_State *L)
  170. {
  171. void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB");
  172. freelib(lib);
  173. return 0;
  174. }
  175. static int loadlib1 (lua_State *L) {
  176. const char *path=luaL_checkstring(L,1);
  177. const char *init=luaL_checkstring(L,2);
  178. int res = loadlib(L,path,init);
  179. if (res == 0) /* no error? */
  180. return 1; /* function is at stack top */
  181. else { /* error */
  182. lua_pushnil(L);
  183. lua_insert(L,-2); /* insert nil before error message */
  184. lua_pushstring(L, (res==ERR_OPEN)?"open":"init");
  185. return 3;
  186. }
  187. }
  188. LUALIB_API int luaopen_loadlib (lua_State *L)
  189. {
  190. luaL_newmetatable(L, "_LOADLIB");
  191. lua_pushcfunction(L, gctm);
  192. lua_setfield(L, -2, "__gc");
  193. lua_register(L,"loadlib",loadlib1);
  194. return 0;
  195. }
  196. /*
  197. * Here are some links to available implementations of dlfcn and
  198. * interfaces to other native dynamic loaders on top of which loadlib
  199. * could be implemented. Please send contributions and corrections to us.
  200. *
  201. * AIX
  202. * Starting with AIX 4.2, dlfcn is included in the base OS.
  203. * There is also an emulation package available.
  204. * http://www.faqs.org/faqs/aix-faq/part4/section-21.html
  205. *
  206. * HPUX
  207. * HPUX 11 has dlfcn. For HPUX 10 use shl_*.
  208. * http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html
  209. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  210. *
  211. * Macintosh, Windows
  212. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  213. *
  214. * GLIB has wrapper code for BeOS, OS2, Unix and Windows
  215. * http://cvs.gnome.org/lxr/source/glib/gmodule/
  216. *
  217. */