-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osfixes.c
325 lines (284 loc) · 7.5 KB
/
osfixes.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "osfixes.h"
#define _CRTIMP
#define WIN32_LEAN_AND_MEAN
#define _GL_CTYPE_H
#define _GL_CONFIG_H_INCLUDED 1
#include <windows.h>
#ifdef WLB_INCL_WLB_DEBUG_H
#include "wlb_debug.h"
#endif
#ifndef MAX_LONG_PATH
#define MAX_LONG_PATH 32768
#endif // !MAX_LONG_PATH
// #include "term.h"
// #include "general.h"
#include <../ucrt/stdlib.h>
#include <../ucrt/stdio.h>
#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif // !FALSE
int clearenv(void)
{
char* envp, * s;
char name[MAX_LONG_PATH];
while (environ && (envp = *environ)) {
if ((s = strchr(envp, '=')) != NULL) {
strncpy(name, envp, s - envp + 1);
strncpy_s(name, sizeof(name), envp, s - envp+1);
name[s - envp + 1+1] = 0;
if (_putenv(name) == -1) {
return -1;
}
}
else {
return -1;
}
}
return 0;
}
#if defined (WLB_DISABLE_DEBUG_ASSERT_POPUP_AT_EXIT) || defined(WLB_DISABLE_DEBUG_ASSERT_POPUP_AT_LAUNCH)
#if !defined(DisableDebugAssertPopup)
#include <crtdbg.h>
#include <debugapi.h>
static void DisableDebugAssertPopup(void) {
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
}
#endif
#if !defined(DisableDebugAssertAtExit)
void DisableDebugAssertAtExit() {
atexit(DisableDebugAssertPopup);
}
#endif
CONSTRUCTOR(wlb_at_startup) {
#ifdef WLB_DISABLE_DEBUG_ASSERT_POPUP_AT_LAUNCH
DisableDebugAssertPopup();
#else
DisableDebugAssertAtExit();
#endif
}
#endif
#ifdef WLB_EXEC_ADVANCED
#define ENV_ALLOC_CNT 1024
#define FAKE_EXIT_STATUS_UNDEFINED 2424242 //exit codes should be 1 byte only
static volatile long EXEC_FAKE_CUR_PID = FAKE_EXIT_STATUS_UNDEFINED;
HandleExec* ExecGetNew() {
HandleExec* exec = malloc(sizeof(HandleExec));
exec->actions = malloc(sizeof(posix_spawn_file_actions_t));
exec->attrp = malloc(sizeof(posix_spawnattr_t));
posix_spawn_file_actions_init(exec->actions);
posix_spawnattr_init(exec->attrp);
exec->__has_run = FALSE;
exec->__has_forked = FALSE;
exec->child_pid = 0;
exec->envp = NULL;
exec->cmd = 0;
exec->args = 0;
exec->___env_max = 0;
exec->___env_cur = 0;
exec->__had_fake_exit = FALSE;
exec->fake_exit_status = FAKE_EXIT_STATUS_UNDEFINED;
return exec;
}
BOOL ExecIsFakePid(pid_t pid) {
return (pid >= FAKE_EXIT_STATUS_UNDEFINED && pid <= EXEC_FAKE_CUR_PID);
}
BOOL ExecHasFakeExit(HandleExec* exec) {
return exec->__had_fake_exit;
}
void ExecSetCmdAndArgs(HandleExec* exec, const char* cmd, const char** args) {
exec->cmd = savestring(cmd);
if (args) {
int argCnt;
for (argCnt = 0; args[argCnt]; argCnt++) {
}
exec->args = malloc(sizeof(char*) * argCnt + 1);
for (argCnt = 0; args[argCnt]; argCnt++) {
exec->args[argCnt] = savestring(args[argCnt]);
}
exec->args[argCnt] = 0;
}
}
void ExecSetEnvVar(HandleExec* exec, const char** envp) {
int argCnt;
if (!envp)
return;
for (argCnt = 0; envp[argCnt]; argCnt++)
ExecDupeEnvVar(exec, envp[argCnt]);
}
void ExecSetFakeExit(HandleExec* exec, int ExitCode) {
if (exec->__has_run)
dlogfatal("ExecDoSpawn called but has already been run");
dlog("Set Fake Exit called for cmd: %s", exec->cmd);
exec->__has_run = TRUE;
exec->__had_fake_exit = TRUE;
exec->fake_exit_status = ExitCode;
exec->child_pid = InterlockedIncrement(&EXEC_FAKE_CUR_PID);
}
int ExecDoSpawn(HandleExec* exec) {
if (exec->__has_run)
dlogfatal("ExecDoSpawn called but has already been run");
exec->__has_run = TRUE;
if (ExecHasFakeExit(exec))
return 0;
dlog("Running spawn cmd: %s", exec->cmd);
return posix_spawnp(&exec->child_pid, exec->cmd, exec->actions, exec->attrp, exec->args, exec->envp);
}
void ExecDupeEnvVar(HandleExec* exec, const char* varLine) {
if (exec->___env_cur == exec->___env_max) {
dlog("IN DUMPENVVAR");
int newMax = exec->___env_max + ENV_ALLOC_CNT;
char** old = exec->envp;
exec->envp = malloc(sizeof(char*) * (newMax + 1));
if (old)
memcpy(exec->envp, old, sizeof(char*) * exec->___env_cur);
exec->___env_max = newMax;
}
exec->envp[exec->___env_cur++] = savestring(varLine);
exec->envp[exec->___env_cur] = 0;
}
static void __DupeOrUpdateEnvVarPair(HandleExec* exec, BOOL CHECK_EXISTING, const char* varName, const char* value) {
int sz = 1 + strlen(varName) + strlen(value) + 1;
char* tmp = strcpy(malloc(sz), varName);
strcat_s(tmp, sz, "=");
if (CHECK_EXISTING) {
char* findStr = tmp;
for (int x = 0; x < exec->___env_cur; x++) {
if (strcmp(exec->envp[x], findStr) == 0) {
strcat_s(tmp, sz, value);
free(exec->envp[x]);
exec->envp[x] = tmp;
return;
}
}
}
strcat_s(tmp, sz, value);
ExecDupeEnvVar(exec, tmp);
free(tmp);
}
void ExecDupeEnvVarPair(HandleExec* exec, const char* varName, const char* value) {
__DupeOrUpdateEnvVarPair(exec, FALSE, varName, value);
}
void ExecDupeOrUpdateEnvVarPair(HandleExec* exec, const char* varName, const char* value) {
__DupeOrUpdateEnvVarPair(exec, TRUE, varName, value);
}
//the original code would have already forked by now, aka this should be spanwed before the destroy
void ExecWouldHaveForked(HandleExec* exec) {
exec->__has_forked = true;
}
void ExecDestroy(HandleExec** exec_ptr) {
HandleExec* exec = *exec_ptr;
if (exec->__has_forked && ! exec->__has_run) {
dlogfatal("originally we would have forked by now but we have not yet run so shouldn't be destroying");
}
posix_spawnattr_destroy(exec->attrp);
posix_spawn_file_actions_destroy(exec->actions);
for (int x = 0; x < exec->___env_cur; x++)
free(exec->envp[x]);
if (exec->envp)
free(exec->envp);
if (exec->cmd)
free(exec->cmd);
if (exec->args) {
for (int argCnt = 0; exec->args[argCnt]; argCnt++)
free(exec->args[argCnt]);
free(exec->args);
}
free(exec);
exec_ptr = -1;
}
#endif
#ifdef WLB_DL_OPEN
#include <winerror.h>
#include <dlfcn.h>
static DWORD last_err;
void *
dlopen (const char *file, int mode)
{
char dllfn[MAX_PATH], *p;
HANDLE dllhandle;
if (mode != RTLD_LAZY)
{
errno = EINVAL;
last_err = ERROR_INVALID_PARAMETER;
return NULL;
}
/* MSDN says to be sure to use backslashes in the DLL file name. */
strcpy (dllfn, file);
for (p = dllfn; *p; p++)
if (*p == '/')
*p = '\\';
dllhandle = LoadLibrary (dllfn);
if (!dllhandle)
last_err = GetLastError ();
return dllhandle;
}
char *
dlerror (void)
{
static char errbuf[1024];
DWORD ret;
if (!last_err)
return NULL;
ret = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, last_err, 0, errbuf, sizeof (errbuf), NULL);
while (ret > 0 && (errbuf[ret - 1] == '\n' || errbuf[ret - 1] == '\r'))
--ret;
errbuf[ret] = '\0';
if (!ret)
sprintf (errbuf, "Error code %lu", last_err);
last_err = 0;
return errbuf;
}
int
dlclose (void *handle)
{
if (!handle || handle == INVALID_HANDLE_VALUE)
return -1;
if (!FreeLibrary (handle))
return -1;
return 0;
}
void *
dlsym (void *handle, const char *name)
{
FARPROC addr = NULL;
if (!handle || handle == INVALID_HANDLE_VALUE)
{
last_err = ERROR_INVALID_PARAMETER;
return NULL;
}
addr = GetProcAddress (handle, name);
if (!addr)
last_err = GetLastError ();
return (void *)addr;
}
#endif
#ifdef WLB_PROC_SIG_EXITS
int kill(pid_t pid, int sig) {
if (sig == SIGINT)
return GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid);//as long as we have created the process with CREATE_NEW_PROCESS_GROUP it should
else {
printf("kill for pid: %i and sig: %i", pid, sig);
return -1;
}
}
#endif
#ifdef WLB_FUNC_NOOPS
int fchmod(int fd, mode_t mode) {
return 0;
}
unsigned int alarm(unsigned int seconds) {
return -1;
}
pid_t getpgrp(pid_t pid) {
return -1;
}
pid_t getppid() {
return -1;
}
pid_t wait(int* wstatus) {
return -1;
}
#endif