• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * Copyright 1999-2008  Brian Paul
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * The above copyright notice and this permission notice (including the
24  * next paragraph) shall be included in all copies or substantial portions
25  * of the Software.
26  *
27  **************************************************************************/
28 
29 
30 #include "detect_os.h"
31 
32 #if DETECT_OS_UNIX
33 #include <dlfcn.h>
34 #endif
35 #if DETECT_OS_WINDOWS
36 #include <windows.h>
37 #endif
38 
39 #include "u_dl.h"
40 #include "u_pointer.h"
41 
42 
43 struct util_dl_library *
util_dl_open(const char * filename)44 util_dl_open(const char *filename)
45 {
46 #if DETECT_OS_UNIX
47    return (struct util_dl_library *)dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
48 #elif DETECT_OS_WINDOWS
49    return (struct util_dl_library *)LoadLibraryA(filename);
50 #else
51    return NULL;
52 #endif
53 }
54 
55 
56 util_dl_proc
util_dl_get_proc_address(struct util_dl_library * library,const char * procname)57 util_dl_get_proc_address(struct util_dl_library *library,
58                          const char *procname)
59 {
60 #if DETECT_OS_UNIX
61    return (util_dl_proc) pointer_to_func(dlsym((void *)library, procname));
62 #elif DETECT_OS_WINDOWS
63    return (util_dl_proc)GetProcAddress((HMODULE)library, procname);
64 #else
65    return (util_dl_proc)NULL;
66 #endif
67 }
68 
69 
70 void
util_dl_close(struct util_dl_library * library)71 util_dl_close(struct util_dl_library *library)
72 {
73 #if DETECT_OS_UNIX
74    dlclose((void *)library);
75 #elif DETECT_OS_WINDOWS
76    FreeLibrary((HMODULE)library);
77 #else
78    (void)library;
79 #endif
80 }
81 
82 
83 const char *
util_dl_error(void)84 util_dl_error(void)
85 {
86 #if DETECT_OS_UNIX
87    return dlerror();
88 #elif DETECT_OS_WINDOWS
89    return "unknown error";
90 #else
91    return "unknown error";
92 #endif
93 }
94