-
Notifications
You must be signed in to change notification settings - Fork 2
/
nwuser.c
226 lines (175 loc) · 5.82 KB
/
nwuser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
*
* Hack to enable per user settings for NWN.
*
* Copyright 2004 - David Holland [email protected]
* Copyright 2008 - David Holland [email protected]
*
* Do what you want with the code, just maintain
* the copyright, and give credit
*
* Initialization functions.
*
* No, none of this code is particularly efficient, but it
* arguably doesn't have to be, as at worst, it's specifically
* targetted towards NWN, and hopefully you don't have more than
* a few hundred save games..
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <dlfcn.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef CRASH
#include <signal.h>
#include <execinfo.h>
#endif
#define _DEFINE_GLOBALS
#include "code.h"
#include "nwuser.h"
int (*__nwu_open)(const char *path, int flags, ...) = NULL;
int (*__nwu_open64)(const char *path, int flags, ...) = NULL;
FILE *(*__nwu_fopen)(const char *path, const char *mode) = NULL;
FILE *(*__nwu_fopen64)(const char *path, const char *mode) = NULL;
size_t (*__nwu_fread)(void *ptr, size_t size, size_t nmemb, FILE *stream) = NULL;
int (*__nwu_fclose)(FILE *stream) = NULL;
DIR *(*__nwu_opendir)(const char *name) = NULL;
int (*__nwu_closedir)(DIR *dir) = NULL;
int (*__nwu_mkdir)(const char *pathname, mode_t mode) = NULL;
int (*__nwu_rmdir)(const char *pathname ) = NULL;
int (*__nwu_unlink)(const char *pathname ) = NULL;
int (*__nwu_remove)(const char *pathname ) = NULL;
int (*__nwu_chdir)(const char *pathname ) = NULL;
struct dirent *(*__nwu_readdir)(DIR *dir) = NULL;
struct dirent64 *(*__nwu_readdir64)(DIR *dir) = NULL;
int (*__nwu_xstat)(int ver, const char *file_name, struct stat *buf) = NULL;
int (*__nwu_xstat64)(int ver, const char *file_name, struct stat64 *buf) = NULL;
int (*__nwu_lxstat)(int ver, const char *file_name, struct stat *buf) = NULL;
int (*__nwu_lxstat64)(int ver, const char *file_name, struct stat64 *buf) = NULL;
char *__nwu_basedir = NULL;
char __nwu_homedir[PATH_MAX] = "";
char __nwu_workingdir[PATH_MAX] = "";
int __nwu_loglevel = 0;
int __nwuser_initialized = 0;
#ifdef CRASH
void nwu_sighandler(int sig) {
int fd = -1 ;
char msg[1024];
void *array[1024]; /* large stack dump */
size_t size;
pid_t my_pid;
pid_t pid;
int (*__nwu_copen)(const char *path, int flags, ...) = NULL;
/* Write rudamentary crash log */
__nwu_copen = dlsym(RTLD_NEXT, "open");
if( __nwu_copen != NULL ) {
fd = __nwu_copen("/tmp/nwu_crash.log", O_CREAT | O_WRONLY, 0644 );
}
if( fd < 0 || __nwu_copen == NULL ) {
fd = fileno(stderr);
}
sprintf(msg, "Aieeeeeeee, NWUser Crashed: %d\n", sig);
write(fd, msg, strlen(msg));
size = backtrace(array, 1024);
backtrace_symbols_fd(array, size, fd);
close(fd);
/* Try for a more complex crash log */
if( __nwu_copen != NULL ) {
fd = __nwu_copen("/tmp/nwu_crash.cmd", O_CREAT | O_WRONLY, 0644 );
if( fd < 0 ) {
exit(-1);
}
sprintf(msg, "where\n");
write(fd, msg, strlen(msg));
sprintf(msg, "quit\n");
write(fd, msg, strlen(msg));
close(fd);
putenv("LD_PRELOAD"); /* Unset the preload variable */
my_pid = getpid();
switch( pid = fork() ) {
case -1:
/* fork failed */
exit(-1);
case 0:
/* child */
sprintf(msg, "gdb /proc/%d/exe %d < /tmp/nwu_crash.cmd > /tmp/nwu_crash2.log 2>&1", my_pid, my_pid);
system(msg);
_exit(0);
default:
/* parent */
sleep(10);
}
}
exit(-1);
}
#endif
/* Initailize this preload library */
void __nwu_initialize(void) __attribute__((constructor));
void __nwu_initialize(void) {
struct passwd *pwent;
char *level;
int sleeptime;
if( ! __nwuser_initialized ) {
/* sleep so we can attach a debugger */
if ((level = getenv("NWU_SLEEPTIME"))) {
sleeptime = atoi(level);
sleep(sleeptime);
}
#ifdef CRASH
signal(SIGBUS, nwu_sighandler);
signal(SIGSEGV, nwu_sighandler);
#endif
if ((level = getenv("NWU_VERBOSE"))) __nwu_loglevel = atoi(level);
__nwu_open = dlsym(RTLD_NEXT, "open");
__nwu_open64 = dlsym(RTLD_NEXT, "open64");
__nwu_fopen = dlsym(RTLD_NEXT, "fopen");
__nwu_fopen64 = dlsym(RTLD_NEXT, "fopen64");
__nwu_fread = dlsym(RTLD_NEXT, "fread");
__nwu_fclose = dlsym(RTLD_NEXT, "fclose");
/* Not really necessary, for NWN, but it makes LS happy, */
__nwu_xstat = dlsym(RTLD_NEXT, "__xstat");
__nwu_lxstat = dlsym(RTLD_NEXT, "__lxstat");
__nwu_xstat64 = dlsym(RTLD_NEXT, "__xstat64");
__nwu_lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");
__nwu_opendir = dlsym(RTLD_NEXT, "opendir");
__nwu_closedir = dlsym(RTLD_NEXT, "closedir");
__nwu_mkdir = dlsym(RTLD_NEXT, "mkdir");
__nwu_rmdir = dlsym(RTLD_NEXT, "rmdir");
__nwu_unlink = dlsym(RTLD_NEXT, "unlink");
__nwu_remove = dlsym(RTLD_NEXT, "remove");
__nwu_chdir = dlsym(RTLD_NEXT, "chdir");
__nwu_readdir = dlsym(RTLD_NEXT, "readdir");
__nwu_readdir64 = dlsym(RTLD_NEXT, "readdir64");
pwent = getpwuid(getuid());
if( pwent != NULL ) {
strncpy( __nwu_homedir, pwent->pw_dir, PATH_MAX - 100 ); /* leave some extra space */
strcat ( __nwu_homedir, "/.nwn" );
}
__nwu_basedir = (char *)malloc(PATH_MAX);
if( ! __nwu_basedir ) {
fprintf(stderr, "ERROR: Unable to allocate PATH_MAX memory: %d\n", errno);
exit(-1);
}
if( !getcwd( __nwu_basedir, PATH_MAX) ) {
fprintf(stderr, "ERROR: getcwd() failed. Toto, I don't think we're in Kansas anymore.\n");
exit(-1);
}
fprintf(stderr, "NOTICE: NWUser: Version %s Successfully loaded. (BaseDir = %s)\n", _NWUSER_VERSION, __nwu_basedir);
__nwuser_initialized = 1;
__nwu_loglevel = 1;
__nwu_log(1, "NOTICE: NWUser: Version %s Successfully loaded. (BaseDir = %s)\n", _NWUSER_VERSION, __nwu_basedir);
if ((level = getenv("NWU_VERBOSE"))) {
__nwu_loglevel = atoi(level);
} else {
__nwu_loglevel = 0;
}
}
}