|
darope@0
|
1 |
// vi: set ts=4 sw=4 : |
|
darope@0
|
2 |
// vim: set tw=75 : |
|
darope@0
|
3 |
|
|
darope@0
|
4 |
// osdep.cpp - routines for operating system differences |
|
darope@0
|
5 |
|
|
darope@0
|
6 |
/* |
|
darope@0
|
7 |
* Copyright (c) 2001-2003 Will Day <[email protected]> |
|
darope@0
|
8 |
* |
|
darope@0
|
9 |
* This file is part of Metamod. |
|
darope@0
|
10 |
* |
|
darope@0
|
11 |
* Metamod is free software; you can redistribute it and/or modify it |
|
darope@0
|
12 |
* under the terms of the GNU General Public License as published by the |
|
darope@0
|
13 |
* Free Software Foundation; either version 2 of the License, or (at |
|
darope@0
|
14 |
* your option) any later version. |
|
darope@0
|
15 |
* |
|
darope@0
|
16 |
* Metamod is distributed in the hope that it will be useful, but |
|
darope@0
|
17 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
darope@0
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
darope@0
|
19 |
* General Public License for more details. |
|
darope@0
|
20 |
* |
|
darope@0
|
21 |
* You should have received a copy of the GNU General Public License |
|
darope@0
|
22 |
* along with Metamod; if not, write to the Free Software Foundation, |
|
darope@0
|
23 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
darope@0
|
24 |
* |
|
darope@0
|
25 |
* In addition, as a special exception, the author gives permission to |
|
darope@0
|
26 |
* link the code of this program with the Half-Life Game Engine ("HL |
|
darope@0
|
27 |
* Engine") and Modified Game Libraries ("MODs") developed by Valve, |
|
darope@0
|
28 |
* L.L.C ("Valve"). You must obey the GNU General Public License in all |
|
darope@0
|
29 |
* respects for all of the code used other than the HL Engine and MODs |
|
darope@0
|
30 |
* from Valve. If you modify this file, you may extend this exception |
|
darope@0
|
31 |
* to your version of the file, but you are not obligated to do so. If |
|
darope@0
|
32 |
* you do not wish to do so, delete this exception statement from your |
|
darope@0
|
33 |
* version. |
|
darope@0
|
34 |
* |
|
darope@0
|
35 |
*/ |
|
darope@0
|
36 |
|
|
darope@0
|
37 |
#ifdef linux |
|
darope@0
|
38 |
// enable extra routines in system header files, like dladdr |
|
darope@242
|
39 |
# ifndef _GNU_SOURCE |
|
darope@242
|
40 |
# define _GNU_SOURCE |
|
darope@242
|
41 |
# endif |
|
darope@0
|
42 |
#include <dlfcn.h> // dlopen, dladdr, etc |
|
darope@0
|
43 |
#include <signal.h> // sigaction, etc |
|
darope@0
|
44 |
#include <setjmp.h> // sigsetjmp, longjmp, etc |
|
darope@0
|
45 |
#endif /* linux */ |
|
darope@0
|
46 |
|
|
darope@242
|
47 |
#if defined(_MSC_VER) && (_MSC_VER < 1300) |
|
darope@242
|
48 |
# include <new.h> // set_new_handler() |
|
darope@242
|
49 |
#else |
|
darope@242
|
50 |
# include <new> // set_new_handler() |
|
darope@242
|
51 |
#endif |
|
darope@242
|
52 |
|
|
darope@242
|
53 |
|
|
darope@0
|
54 |
#include <string.h> // strpbrk, etc |
|
darope@0
|
55 |
|
|
darope@0
|
56 |
#include <extdll.h> // always |
|
darope@0
|
57 |
|
|
darope@0
|
58 |
#include "osdep.h" // me |
|
darope@0
|
59 |
#include "mreg.h" // RG_VALID, etc |
|
darope@0
|
60 |
#include "log_meta.h" // META_ERROR, etc |
|
darope@0
|
61 |
#include "types_meta.h" // mBOOL |
|
darope@0
|
62 |
#include "support_meta.h" // MAX_STRBUF_LEN |
|
darope@0
|
63 |
|
|
darope@248
|
64 |
mBOOL dlclose_handle_invalid; |
|
darope@248
|
65 |
|
|
darope@242
|
66 |
// To keep the rest of the sources clean and keep not only OS but also |
|
darope@242
|
67 |
// compiler dependant differences in this file, we define a local function |
|
darope@242
|
68 |
// to set the new handler. |
|
darope@242
|
69 |
void mm_set_new_handler( void ) |
|
darope@242
|
70 |
{ |
|
darope@242
|
71 |
#if defined(_MSC_VER) && (_MSC_VER < 1300) |
|
darope@242
|
72 |
_set_new_handler(meta_new_handler); |
|
darope@242
|
73 |
#else |
|
darope@242
|
74 |
std::set_new_handler(meta_new_handler); |
|
darope@242
|
75 |
#endif |
|
darope@242
|
76 |
} |
|
darope@242
|
77 |
|
|
darope@0
|
78 |
#ifdef _WIN32 |
|
darope@0
|
79 |
// Since windows doesn't provide a verison of strtok_r(), we include one |
|
darope@0
|
80 |
// here. This may or may not operate exactly like strtok_r(), but does |
|
darope@0
|
81 |
// what we need it it do. |
|
darope@0
|
82 |
char *my_strtok_r(char *s, const char *delim, char **ptrptr) { |
|
darope@0
|
83 |
char *begin=NULL; |
|
darope@0
|
84 |
char *end=NULL; |
|
darope@0
|
85 |
char *rest=NULL; |
|
darope@0
|
86 |
if(s) |
|
darope@0
|
87 |
begin=s; |
|
darope@0
|
88 |
else |
|
darope@0
|
89 |
begin=*ptrptr; |
|
darope@0
|
90 |
if(!begin) |
|
darope@0
|
91 |
return(NULL); |
|
darope@0
|
92 |
end=strpbrk(begin, delim); |
|
darope@0
|
93 |
if(end) { |
|
darope@0
|
94 |
*end='\0'; |
|
darope@0
|
95 |
rest=end+1; |
|
darope@0
|
96 |
*ptrptr=rest+strspn(rest, delim); |
|
darope@0
|
97 |
} |
|
darope@0
|
98 |
else |
|
darope@0
|
99 |
*ptrptr=NULL; |
|
darope@0
|
100 |
return(begin); |
|
darope@0
|
101 |
} |
|
darope@0
|
102 |
#endif /* _WIN32 */ |
|
darope@0
|
103 |
|
|
darope@0
|
104 |
|
|
darope@0
|
105 |
#ifdef _WIN32 |
|
darope@0
|
106 |
// Windows doesn't provide a functon analagous to dlerr() that returns a |
|
darope@0
|
107 |
// string describing the error, so we include one here, as exampled at: |
|
darope@0
|
108 |
// http://msdn.microsoft.com/library/en-us/debug/errors_0sdh.asp |
|
darope@0
|
109 |
// except without FORMAT_MESSAGE_ALLOCATE_BUFFER, since we use a local |
|
darope@0
|
110 |
// static buffer. |
|
darope@0
|
111 |
char *str_GetLastError(void) { |
|
darope@0
|
112 |
static char buf[MAX_STRBUF_LEN]; |
|
darope@0
|
113 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
|
darope@0
|
114 |
NULL, GetLastError(), |
|
darope@0
|
115 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //! Default language |
|
darope@0
|
116 |
(LPTSTR) &buf, 0, NULL); |
|
darope@0
|
117 |
return(buf); |
|
darope@0
|
118 |
} |
|
darope@0
|
119 |
#endif /* _WIN32 */ |
|
darope@0
|
120 |
|
|
darope@0
|
121 |
|
|
darope@0
|
122 |
// Find the filename of the DLL/shared-lib where the given memory location |
|
darope@0
|
123 |
// exists. |
|
darope@0
|
124 |
#ifdef linux |
|
darope@0
|
125 |
// Errno values: |
|
darope@0
|
126 |
// - ME_NOTFOUND couldn't find a sharedlib that contains memory location |
|
darope@0
|
127 |
const char *DLFNAME(void *memptr) { |
|
darope@0
|
128 |
Dl_info dli; |
|
darope@0
|
129 |
memset(&dli, 0, sizeof(dli)); |
|
darope@0
|
130 |
if(dladdr(memptr, &dli)) |
|
darope@0
|
131 |
return(dli.dli_fname); |
|
darope@0
|
132 |
else |
|
darope@0
|
133 |
RETURN_ERRNO(NULL, ME_NOTFOUND); |
|
darope@0
|
134 |
} |
|
darope@0
|
135 |
#elif defined(_WIN32) |
|
darope@0
|
136 |
// Implementation for win32 provided by Jussi Kivilinna <[email protected]>: |
|
darope@0
|
137 |
// |
|
darope@0
|
138 |
// 1. Get memory location info on memptr with VirtualQuery. |
|
darope@0
|
139 |
// 2. Check if memory location info is valid and use MBI.AllocationBase |
|
darope@0
|
140 |
// as module start point. |
|
darope@0
|
141 |
// 3. Get module file name with GetModuleFileName. |
|
darope@0
|
142 |
// |
|
darope@0
|
143 |
// Simple and should work pretty much same way as 'dladdr' in linux. |
|
darope@0
|
144 |
// VirtualQuery and GetModuleFileName work even with win32s. |
|
darope@0
|
145 |
// |
|
darope@0
|
146 |
// Note that GetModuleFileName returns longfilenames rather than 8.3. |
|
darope@0
|
147 |
// |
|
darope@0
|
148 |
// Note also, the returned filename is local static storage, and should be |
|
darope@0
|
149 |
// copied by caller if it needs to keep it around. |
|
darope@0
|
150 |
// |
|
darope@0
|
151 |
// Also note, normalize_pathname() should really be done by the caller, but |
|
darope@0
|
152 |
// is done here to preserve "const char *" return consistent with linux |
|
darope@0
|
153 |
// version. |
|
darope@0
|
154 |
// |
|
darope@0
|
155 |
// Errno values: |
|
darope@0
|
156 |
// - ME_NOTFOUND couldn't find a DLL that contains memory location |
|
darope@0
|
157 |
const char *DLFNAME(void *memptr) { |
|
darope@0
|
158 |
MEMORY_BASIC_INFORMATION MBI; |
|
darope@0
|
159 |
static char fname[PATH_MAX]; |
|
darope@0
|
160 |
|
|
darope@0
|
161 |
memset(fname, 0, sizeof(fname)); |
|
darope@0
|
162 |
|
|
darope@0
|
163 |
if (!VirtualQuery(memptr, &MBI, sizeof(MBI))) |
|
darope@0
|
164 |
RETURN_ERRNO(NULL, ME_NOTFOUND); |
|
darope@0
|
165 |
if (MBI.State != MEM_COMMIT) |
|
darope@0
|
166 |
RETURN_ERRNO(NULL, ME_NOTFOUND); |
|
darope@0
|
167 |
if(!MBI.AllocationBase) |
|
darope@0
|
168 |
RETURN_ERRNO(NULL, ME_NOTFOUND); |
|
darope@0
|
169 |
|
|
darope@0
|
170 |
// MSDN indicates that GetModuleFileName will leave string |
|
darope@0
|
171 |
// null-terminated, even if it's truncated because buffer is too small. |
|
darope@0
|
172 |
if(!GetModuleFileName((HMODULE)MBI.AllocationBase, fname, |
|
darope@0
|
173 |
sizeof(fname)-1)) |
|
darope@0
|
174 |
RETURN_ERRNO(NULL, ME_NOTFOUND); |
|
darope@0
|
175 |
if(!fname[0]) |
|
darope@0
|
176 |
RETURN_ERRNO(NULL, ME_NOTFOUND); |
|
darope@0
|
177 |
|
|
darope@0
|
178 |
normalize_pathname(fname); |
|
darope@0
|
179 |
return(fname); |
|
darope@0
|
180 |
} |
|
darope@0
|
181 |
#endif /* _WIN32 */ |
|
darope@0
|
182 |
|
|
darope@0
|
183 |
|
|
darope@0
|
184 |
// Determine whether the given memory location is valid (ie whether we |
|
darope@0
|
185 |
// should expect to be able to reference strings or functions at this |
|
darope@0
|
186 |
// location without segfaulting). |
|
darope@0
|
187 |
#ifdef linux |
|
darope@0
|
188 |
// Simulate this with dladdr. I'm not convinced this will be as generally |
|
darope@0
|
189 |
// applicable as the native windows routine below, but it should do what |
|
darope@0
|
190 |
// we need it for in this particular situation. |
|
darope@0
|
191 |
// meta_errno values: |
|
darope@0
|
192 |
// - ME_NOTFOUND couldn't find a matching sharedlib for this ptr |
|
darope@0
|
193 |
mBOOL IS_VALID_PTR(void *memptr) { |
|
darope@0
|
194 |
Dl_info dli; |
|
darope@0
|
195 |
memset(&dli, 0, sizeof(dli)); |
|
darope@0
|
196 |
if(dladdr(memptr, &dli)) |
|
darope@0
|
197 |
return(mTRUE); |
|
darope@0
|
198 |
else |
|
darope@0
|
199 |
RETURN_ERRNO(mFALSE, ME_NOTFOUND); |
|
darope@0
|
200 |
} |
|
darope@0
|
201 |
#elif defined(_WIN32) |
|
darope@0
|
202 |
// Use the native windows routine IsBadCodePtr. |
|
darope@0
|
203 |
// meta_errno values: |
|
darope@0
|
204 |
// - ME_BADMEMPTR not a valid memory pointer |
|
darope@0
|
205 |
mBOOL IS_VALID_PTR(void *memptr) { |
|
darope@0
|
206 |
if(IsBadCodePtr((FARPROC) memptr)) |
|
darope@0
|
207 |
RETURN_ERRNO(mFALSE, ME_BADMEMPTR); |
|
darope@0
|
208 |
else |
|
darope@0
|
209 |
return(mTRUE); |
|
darope@0
|
210 |
} |
|
darope@0
|
211 |
#endif /* _WIN32 */ |
|
darope@0
|
212 |
|
|
darope@0
|
213 |
// This used to be OS-dependent, as it used a SEGV signal handler under |
|
darope@0
|
214 |
// linux, but that was removed because (a) it masked legitimate segfaults |
|
darope@0
|
215 |
// in plugin commands and produced confusing output ("plugin has been |
|
darope@0
|
216 |
// unloaded", when really it segfaultd), and (b) wasn't necessary since |
|
darope@0
|
217 |
// IS_VALID_PTR() should cover the situation. |
|
darope@0
|
218 |
mBOOL os_safe_call(REG_CMD_FN pfn) { |
|
darope@0
|
219 |
// try and see if this is a valid memory location |
|
darope@0
|
220 |
if(!IS_VALID_PTR((void *) pfn)) |
|
darope@0
|
221 |
// meta_errno should be already set in is_valid_ptr() |
|
darope@0
|
222 |
return(mFALSE); |
|
darope@0
|
223 |
|
|
darope@0
|
224 |
pfn(); |
|
darope@0
|
225 |
return(mTRUE); |
|
darope@0
|
226 |
} |
|
darope@0
|
227 |
|
|
darope@0
|
228 |
// See comments in osdep.h. |
|
darope@242
|
229 |
#if defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 1300)) |
|
darope@242
|
230 |
void MM_CDECL meta_new_handler(void) { |
|
darope@0
|
231 |
// This merely because we don't want the program to exit if new() |
|
darope@0
|
232 |
// fails.. |
|
darope@0
|
233 |
return; |
|
darope@0
|
234 |
} |
|
darope@0
|
235 |
#elif defined(_MSC_VER) |
|
darope@0
|
236 |
int meta_new_handler(size_t size) { |
|
darope@0
|
237 |
// This merely because we don't want the program to exit if new() |
|
darope@0
|
238 |
// fails.. |
|
darope@0
|
239 |
return(0); |
|
darope@0
|
240 |
} |
|
darope@0
|
241 |
#endif /* _MSC_VER */ |