Skip to content

Latest commit

 

History

History
3737 lines (3113 loc) · 119 KB

doscalls.c

File metadata and controls

3737 lines (3113 loc) · 119 KB
 
Feb 26, 2018
Feb 26, 2018
1
2
3
4
5
6
7
8
/**
* 2ine; an OS/2 emulator for Linux.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Nov 2, 2016
Nov 2, 2016
9
#include "os2native16.h"
Oct 27, 2016
Oct 27, 2016
10
11
#include "doscalls.h"
Sep 30, 2016
Sep 30, 2016
12
13
#include <unistd.h>
#include <limits.h>
Sep 30, 2016
Sep 30, 2016
14
#include <time.h>
Oct 6, 2016
Oct 6, 2016
15
16
#include <dirent.h>
#include <fcntl.h>
Sep 30, 2016
Sep 30, 2016
17
#include <sys/types.h>
Oct 14, 2016
Oct 14, 2016
18
#include <sys/wait.h>
Oct 6, 2016
Oct 6, 2016
19
#include <sys/stat.h>
Sep 30, 2016
Sep 30, 2016
20
21
#include <sys/sysinfo.h>
#include <sys/time.h>
Oct 2, 2016
Oct 2, 2016
22
#include <sys/resource.h>
Oct 9, 2016
Oct 9, 2016
23
#include <sys/stat.h>
Sep 30, 2016
Sep 30, 2016
24
Feb 27, 2018
Feb 27, 2018
25
#include "doscalls-lx.h"
Sep 30, 2016
Sep 30, 2016
26
Oct 14, 2016
Oct 14, 2016
27
static pthread_mutex_t GMutexDosCalls;
Jul 10, 2018
Jul 10, 2018
28
static pthread_mutex_t GMutexDosBeep;
Oct 14, 2016
Oct 14, 2016
29
Sep 30, 2016
Sep 30, 2016
30
31
typedef struct ExitListItem
{
Sep 30, 2016
Sep 30, 2016
32
PFNEXITLIST fn;
Sep 30, 2016
Sep 30, 2016
33
34
35
36
37
38
uint32 priority;
struct ExitListItem *next;
} ExitListItem;
static ExitListItem *GExitList = NULL;
Oct 5, 2016
Oct 5, 2016
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
// Flags returned for character devices from DosQueryHType()...
enum {
DAW_STDIN = (1 << 0),
DAW_STDOUT = (1 << 1),
DAW_NUL = (1 << 2),
DAW_CLOCK = (1 << 3),
DAW_SPEC = (1 << 4),
DAW_ADD_ON = (1 << 5),
DAW_GIOCTL = (1 << 6),
DAW_LEVEL0 = 0,
DAW_LEVEL1 = (1 << 7),
DAW_LEVEL2 = (1 << 8),
DAW_LEVEL3 = (1 << 7) | (1 << 8),
DAW_FCNLEV = (1 << 7) | (1 << 8) | (1 << 9),
// ((1 << 10) is reserved, I think.)
DAW_OPENCLOSE = (1 << 11),
DAW_SHARE = (1 << 12),
DAW_NONIBM = (1 << 13),
DAW_IDC = (1 << 14),
DAW_CHARACTER = (1 << 15)
};
typedef struct HFileInfo
{
Oct 27, 2016
Oct 27, 2016
66
67
68
69
int fd; // unix file descriptor.
ULONG type; // file, pipe, device, etc.
ULONG attr; // for character devices, DAW_*
ULONG flags; // OPEN_FLAGS_*
Oct 5, 2016
Oct 5, 2016
70
} HFileInfo;
Oct 2, 2016
Oct 2, 2016
71
Oct 5, 2016
Oct 5, 2016
72
73
static HFileInfo *HFiles = NULL;
static uint32 MaxHFiles = 0;
Oct 2, 2016
Oct 2, 2016
74
Oct 14, 2016
Oct 14, 2016
75
76
77
78
79
80
typedef struct Thread
{
pthread_t thread;
size_t stacklen;
PFNTHREAD fn;
ULONG fnarg;
Jan 19, 2018
Jan 19, 2018
81
uint16 selector;
Oct 14, 2016
Oct 14, 2016
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
struct Thread *prev;
struct Thread *next;
} Thread;
static Thread *GDeadThreads = NULL;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t condition;
volatile int posted;
volatile int waiting;
} EventSem;
Oct 17, 2016
Oct 17, 2016
97
98
99
100
101
102
103
104
105
106
107
typedef struct
{
DIR *dirp;
ULONG attr;
ULONG level;
char pattern[CCHMAXPATHCOMP];
} DirFinder;
static DirFinder GHDir1;
Jun 18, 2018
Jun 18, 2018
108
109
110
111
112
113
static GINFOSEG *ginfo;
static SEL ginfosel;
static LINFOSEG *linfo;
static SEL linfosel;
static HDIR *hdir16 = 0;
Oct 17, 2016
Oct 17, 2016
114
Oct 14, 2016
Oct 14, 2016
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
static int grabLock(pthread_mutex_t *lock)
{
return (pthread_mutex_lock(lock) == 0);
} // grabLock
static void ungrabLock(pthread_mutex_t *lock)
{
pthread_mutex_unlock(lock);
} // ungrabLock
static void timespecNowPlusMilliseconds(struct timespec *waittime, const ULONG ulTimeout)
{
clock_gettime(CLOCK_REALTIME, waittime);
waittime->tv_sec += ulTimeout / 1000;
waittime->tv_nsec += (ulTimeout % 1000) * 1000000;
if (waittime->tv_nsec >= 1000000000) {
waittime->tv_sec++;
waittime->tv_nsec -= 1000000000;
assert(waittime->tv_nsec < 1000000000);
} // if
} // timespecNowPlusMilliseconds
Jun 8, 2018
Jun 8, 2018
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
static void initHFileInfoFromUnixFd(const int fd, HFileInfo *info)
{
struct stat statbuf;
if ((fd != -1) && (fstat(fd, &statbuf) == 0)) {
ULONG type = 0;
switch (statbuf.st_mode & S_IFMT) {
case S_IFREG:
type = 0;
break;
case S_IFBLK:
case S_IFCHR:
type = 1;
break;
case S_IFIFO:
case S_IFSOCK:
default:
type = 2;
break;
} // switch
info->fd = fd;
info->type = type;
FIXME("What should this be?");
info->attr = DAW_STDIN | DAW_STDOUT | DAW_LEVEL1 | DAW_CHARACTER;
info->flags = 0;
} else {
info->fd = -1;
info->type = 0;
info->attr = 0;
info->flags = 0;
} // else
} // initHFileInfoFromUnixFd
Sep 30, 2016
Sep 30, 2016
170
APIRET DosGetInfoBlocks(PTIB *pptib, PPIB *pppib)
Sep 30, 2016
Sep 30, 2016
171
{
Sep 30, 2016
Sep 30, 2016
172
173
TRACE_NATIVE("DosGetInfoBlocks(%p, %p)", pptib, pppib);
Oct 17, 2016
Oct 17, 2016
174
if (pptib != NULL)
Feb 28, 2018
Feb 28, 2018
175
*pptib = (PTIB) LX_GETTIB();
Sep 30, 2016
Sep 30, 2016
176
Oct 17, 2016
Oct 17, 2016
177
if (pppib != NULL)
Feb 28, 2018
Feb 28, 2018
178
*pppib = (PPIB) &GLoaderState.pib;
Sep 30, 2016
Sep 30, 2016
179
180
181
182
return 0;
} // DosGetInfoBlocks
Sep 30, 2016
Sep 30, 2016
183
APIRET DosQuerySysInfo(ULONG first, ULONG last, PVOID _buf, ULONG buflen)
Sep 30, 2016
Sep 30, 2016
184
{
Sep 30, 2016
Sep 30, 2016
185
186
TRACE_NATIVE("DosQuerySysInfo(%u, %u, %p, %u)", (uint) first, (uint) last, _buf, (uint) buflen);
Sep 30, 2016
Sep 30, 2016
187
uint32 *buf = (uint32 *) _buf;
Sep 30, 2016
Sep 30, 2016
188
189
if (last < first) return ERROR_INVALID_PARAMETER;
if ( (buflen / sizeof (uint32)) < ((last - first) + 1) ) return ERROR_BUFFER_OVERFLOW;
Sep 30, 2016
Sep 30, 2016
190
191
192
193
194
195
196
197
198
199
200
201
for (uint32 varid = first; varid <= last; varid++) {
switch (varid) {
case QSV_MAX_PATH_LENGTH: *(buf++) = PATH_MAX; break;
case QSV_MAX_TEXT_SESSIONS: *(buf++) = 999999; break;
case QSV_MAX_PM_SESSIONS: *(buf++) = 999999; break;
case QSV_MAX_VDM_SESSIONS: *(buf++) = 0; break;
case QSV_BOOT_DRIVE: *(buf++) = 3; break; // "C:"
case QSV_DYN_PRI_VARIATION: *(buf++) = 1; break;
case QSV_MAX_WAIT: *(buf++) = 1; break;
case QSV_MIN_SLICE: *(buf++) = 1; break;
case QSV_MAX_SLICE: *(buf++) = 10; break;
case QSV_PAGE_SIZE: *(buf++) = 4096; break;
Sep 30, 2016
Sep 30, 2016
202
// !!! FIXME: change the version number in some way so apps can know this isn't actually OS/2.
Jul 10, 2018
Jul 10, 2018
203
204
205
case QSV_VERSION_MAJOR: *(buf++) = 20; break; // OS/2 Warp 4.5
case QSV_VERSION_MINOR: *(buf++) = 45; break; // OS/2 Warp 4.5
case QSV_VERSION_REVISION: *(buf++) = 0; break; // OS/2 Warp 4.5
Sep 30, 2016
Sep 30, 2016
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
case QSV_MS_COUNT: {
static long startoffset = 0;
if (startoffset == 0) {
struct timespec ts;
struct sysinfo info;
sysinfo(&info);
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
startoffset = info.uptime - ((long) ts.tv_sec);
} // if
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
*(buf++) = (uint32) (((long) ts.tv_sec) - startoffset);
break;
} // case
case QSV_TIME_LOW: *(buf++) = (uint32) time(NULL); break;
case QSV_TIME_HIGH: *(buf++) = 0; break; // !!! FIXME: 32-bit Linux time_t is 32-bits!
case QSV_TOTPHYSMEM: {
struct sysinfo info;
sysinfo(&info);
*(buf++) = (uint32) info.totalram;
break;
} // case
case QSV_TOTRESMEM: {
struct sysinfo info;
sysinfo(&info);
*(buf++) = (uint32) info.totalram - info.freeram; // !!! FIXME: probably not right?
break;
} // case
case QSV_TOTAVAILMEM: {
struct sysinfo info;
sysinfo(&info);
*(buf++) = (uint32) info.freeram;
break;
} // case
case QSV_MAXPRMEM: *(buf++) = 0x7FFFFFFF; break; // !!! FIXME: should I list all 4 gigs? Also: what about machines with < 2 gigs?
case QSV_MAXSHMEM: *(buf++) = 0x7FFFFFFF; break; // !!! FIXME: should I list all 4 gigs? Also: what about machines with < 2 gigs?
case QSV_TIMER_INTERVAL: *(buf++) = 10; break;
case QSV_MAX_COMP_LENGTH: *(buf++) = NAME_MAX; break;
case QSV_FOREGROUND_FS_SESSION: *(buf++) = 1; break; // !!! FIXME
case QSV_FOREGROUND_PROCESS: *(buf++) = 1; break; // !!! FIXME
Sep 30, 2016
Sep 30, 2016
255
default: return ERROR_INVALID_PARAMETER;
Sep 30, 2016
Sep 30, 2016
256
257
258
} // switch
} // for
Sep 30, 2016
Sep 30, 2016
259
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
260
261
262
} // DosQuerySysInfo
Sep 30, 2016
Sep 30, 2016
263
APIRET DosQueryModuleName(HMODULE hmod, ULONG buflen, PCHAR buf)
Sep 30, 2016
Sep 30, 2016
264
{
Sep 30, 2016
Sep 30, 2016
265
266
TRACE_NATIVE("DosQueryModuleName(%u, %u, %p)", (uint) hmod, (uint) buflen, buf);
Sep 30, 2016
Sep 30, 2016
267
268
const LxModule *lxmod = (LxModule *) hmod;
// !!! FIXME: error 6 ERROR_INVALID_HANDLE
Oct 27, 2016
Oct 27, 2016
269
if (strlen(lxmod->os2path) >= buflen)
Sep 30, 2016
Sep 30, 2016
270
return ERROR_BAD_LENGTH;
Sep 30, 2016
Sep 30, 2016
271
strcpy(buf, lxmod->os2path);
Sep 30, 2016
Sep 30, 2016
272
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
273
274
} // DosQueryModuleName
Sep 30, 2016
Sep 30, 2016
275
276
APIRET DosScanEnv(PSZ name, PSZ *outval)
Sep 30, 2016
Sep 30, 2016
277
{
Sep 30, 2016
Sep 30, 2016
278
279
TRACE_NATIVE("DosScanEnv('%s', %p)", name, outval);
Feb 28, 2018
Feb 28, 2018
280
char *env = GLoaderState.pib.pib_pchenv;
Sep 30, 2016
Sep 30, 2016
281
282
283
284
const size_t len = strlen(name);
while (*env) {
if ((strncmp(env, name, len) == 0) && (env[len] == '=')) {
*outval = env + len + 1;
Sep 30, 2016
Sep 30, 2016
285
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
286
287
288
289
} // if
env += strlen(env) + 1;
} // while
Sep 30, 2016
Sep 30, 2016
290
return ERROR_ENVVAR_NOT_FOUND;
Sep 30, 2016
Sep 30, 2016
291
292
} // DosScanEnv
Oct 14, 2016
Oct 14, 2016
293
294
295
296
297
298
299
300
301
static int getHFileUnixDescriptor(HFILE h)
{
grabLock(&GMutexDosCalls);
const int fd = (h < MaxHFiles) ? HFiles[h].fd : -1;
ungrabLock(&GMutexDosCalls);
return fd;
} // getHFileUnixDescriptor
Jun 8, 2018
Jun 8, 2018
302
static APIRET DosWrite_implementation(HFILE h, PVOID buf, ULONG buflen, PULONG actual)
Sep 30, 2016
Sep 30, 2016
303
{
Oct 14, 2016
Oct 14, 2016
304
305
const int fd = getHFileUnixDescriptor(h);
if (fd == -1)
Oct 5, 2016
Oct 5, 2016
306
307
return ERROR_INVALID_HANDLE;
Oct 14, 2016
Oct 14, 2016
308
309
// !!! FIXME: writing to a terminal should probably convert CR/LF to LF.
const int rc = write(fd, buf, buflen);
Oct 5, 2016
Oct 5, 2016
310
311
if (rc < 0) {
*actual = 0;
Sep 30, 2016
Sep 30, 2016
312
return ERROR_DISK_FULL; // !!! FIXME: map these errors.
Oct 5, 2016
Oct 5, 2016
313
} // if
Sep 30, 2016
Sep 30, 2016
314
315
*actual = (uint32) rc;
Sep 30, 2016
Sep 30, 2016
316
return NO_ERROR;
Jun 8, 2018
Jun 8, 2018
317
318
319
320
321
322
} // DosWrite_implementation
APIRET DosWrite(HFILE h, PVOID buf, ULONG buflen, PULONG actual)
{
TRACE_NATIVE("DosWrite(%u, %p, %u, %p)", (uint) h, buf, (uint) buflen, actual);
return DosWrite_implementation(h, buf, buflen, actual);
Sep 30, 2016
Sep 30, 2016
323
324
325
326
327
328
329
330
} // DosWrite
static void runDosExitList(const uint32 why)
{
ExitListItem *next = NULL;
for (ExitListItem *item = GExitList; item; item = next) {
// don't run any of these more than once:
// http://www.verycomputer.com/3_c1f6e02c06ed108e_1.htm
Sep 30, 2016
Sep 30, 2016
331
PFNEXITLIST fn = item->fn;
Sep 30, 2016
Sep 30, 2016
332
333
334
next = item->next;
GExitList = next;
free(item);
Jan 20, 2018
Jan 20, 2018
335
//printf("calling dosexitlist item %p\n", fn); fflush(stdout);
Sep 30, 2016
Sep 30, 2016
336
fn(why);
Jan 20, 2018
Jan 20, 2018
337
//printf("dosexitlist item %p returned!\n", fn); fflush(stdout);
Sep 30, 2016
Sep 30, 2016
338
339
340
} // for
} // runDosExitList
Jun 6, 2018
Jun 6, 2018
341
static VOID DosExit_implementation(ULONG action, ULONG exitcode)
Sep 30, 2016
Sep 30, 2016
342
343
{
// !!! FIXME: what does a value other than 0 or 1 do here?
Sep 30, 2016
Sep 30, 2016
344
if (action == EXIT_THREAD) {
Jan 19, 2018
Jan 19, 2018
345
346
347
// !!! FIXME: If last thread: terminate process.
FIXME("if last thread, treat as DosExit(1)"); // right now it will terminate process like exit(0), without running the exit list or reporting exitcode.
pthread_exit(NULL); // DosWaitThread doesn't report exit codes.
Sep 30, 2016
Sep 30, 2016
348
349
350
} // if
// terminate the process.
Sep 30, 2016
Sep 30, 2016
351
runDosExitList(TC_EXIT);
Sep 30, 2016
Sep 30, 2016
352
Feb 28, 2018
Feb 28, 2018
353
GLoaderState.terminate(exitcode);
Jun 6, 2018
Jun 6, 2018
354
355
356
357
358
359
} // DosExit_implementation
VOID DosExit(ULONG action, ULONG exitcode)
{
TRACE_NATIVE("DosExit(%u, %u)", (uint) action, (uint) exitcode);
DosExit_implementation(action, exitcode);
Sep 30, 2016
Sep 30, 2016
360
361
} // DosExit
Sep 30, 2016
Sep 30, 2016
362
APIRET DosExitList(ULONG ordercode, PFNEXITLIST fn)
Sep 30, 2016
Sep 30, 2016
363
{
Sep 30, 2016
Sep 30, 2016
364
365
TRACE_NATIVE("DosExitList(%u, %p)", (uint) ordercode, fn);
Sep 30, 2016
Sep 30, 2016
366
if (fn == NULL)
Sep 30, 2016
Sep 30, 2016
367
return ERROR_INVALID_FUNCTION;
Sep 30, 2016
Sep 30, 2016
368
369
370
371
372
373
374
const uint8 cmd = ordercode & 0xFF;
const uint8 arg = (ordercode >> 8) & 0xFF;
ExitListItem *prev = NULL;
ExitListItem *item = NULL;
// !!! FIXME: docs say this is illegal, but have to check what OS/2 actually does here.
Jan 20, 2018
Jan 20, 2018
375
if ((cmd != EXLST_ADD) && (arg != 0))
Sep 30, 2016
Sep 30, 2016
376
return ERROR_INVALID_DATA;
Sep 30, 2016
Sep 30, 2016
377
378
switch (cmd) {
Sep 30, 2016
Sep 30, 2016
379
case EXLST_ADD: {
Sep 30, 2016
Sep 30, 2016
380
381
ExitListItem *newitem = (ExitListItem *) malloc(sizeof (ExitListItem));
if (!newitem)
Sep 30, 2016
Sep 30, 2016
382
return ERROR_NOT_ENOUGH_MEMORY;
Oct 2, 2016
Oct 2, 2016
383
newitem->fn = fn;
Sep 30, 2016
Sep 30, 2016
384
385
386
387
388
389
390
391
392
393
394
395
for (item = GExitList; item; item = item->next) {
if (item->priority >= ((uint32) arg))
break;
prev = item;
} // for
if (prev) {
prev->next = newitem;
newitem->next = item;
} else {
newitem->next = GExitList;
GExitList = newitem;
} // else
Sep 30, 2016
Sep 30, 2016
396
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
397
398
} // case
Sep 30, 2016
Sep 30, 2016
399
case EXLST_REMOVE: {
Sep 30, 2016
Sep 30, 2016
400
401
402
403
404
405
406
for (item = GExitList; item; item = item->next) {
if (item->fn == fn) {
if (prev)
prev->next = item->next;
else
GExitList = item->next;
free(item);
Sep 30, 2016
Sep 30, 2016
407
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
408
409
410
} // if
prev = item;
} // for
Sep 30, 2016
Sep 30, 2016
411
return ERROR_INVALID_FUNCTION; // !!! FIXME: yeah?
Sep 30, 2016
Sep 30, 2016
412
413
} // case
Sep 30, 2016
Sep 30, 2016
414
415
case EXLST_EXIT:
return NO_ERROR; // ... just treat this as a no-op, I guess...?
Sep 30, 2016
Sep 30, 2016
416
Sep 30, 2016
Sep 30, 2016
417
default: return ERROR_INVALID_DATA;
Sep 30, 2016
Sep 30, 2016
418
419
} // switch
Sep 30, 2016
Sep 30, 2016
420
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
421
422
} // DosExitList
Sep 30, 2016
Sep 30, 2016
423
APIRET DosCreateEventSem(PSZ name, PHEV phev, ULONG attr, BOOL32 state)
Sep 30, 2016
Sep 30, 2016
424
{
Sep 30, 2016
Sep 30, 2016
425
TRACE_NATIVE("DosCreateEventSem('%s', %p, %u, %u)", name, phev, (uint) attr, (uint) state);
Oct 14, 2016
Oct 14, 2016
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
if (name != NULL) {
FIXME("implement named semaphores");
return ERROR_NOT_ENOUGH_MEMORY;
} // if
if (attr & DC_SEM_SHARED) {
FIXME("implement shared semaphores");
} // if
assert(sizeof (HEV) == sizeof (EventSem *));
EventSem *sem = (EventSem *) malloc(sizeof (EventSem));
if (sem == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
if (pthread_mutex_init(&sem->mutex, NULL) == -1) {
free(sem);
return ERROR_NOT_ENOUGH_MEMORY;
} // if
if (pthread_cond_init(&sem->condition, NULL) == -1) {
pthread_mutex_destroy(&sem->mutex);
free(sem);
return ERROR_NOT_ENOUGH_MEMORY;
} // if
sem->posted = state ? 1 : 0;
*phev = (HEV) sem;
Sep 30, 2016
Sep 30, 2016
456
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
457
458
} // DosCreateEventSem
Sep 30, 2016
Sep 30, 2016
459
APIRET DosCreateMutexSem(PSZ name, PHMTX phmtx, ULONG attr, BOOL32 state)
Sep 30, 2016
Sep 30, 2016
460
{
Sep 30, 2016
Sep 30, 2016
461
TRACE_NATIVE("DosCreateMutexSem('%s', %p, %u, %u)", name, phmtx, (uint) attr, (uint) state);
Oct 14, 2016
Oct 14, 2016
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
if (name != NULL) {
FIXME("implement named mutexes");
return ERROR_NOT_ENOUGH_MEMORY;
} // if
if (attr & DC_SEM_SHARED) {
FIXME("implement shared mutexes");
} // if
assert(sizeof (HMTX) == sizeof (pthread_mutex_t *));
pthread_mutex_t *mtx = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
if (mtx == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
// OS/2 mutexes are recursive.
pthread_mutexattr_t muxattr;
pthread_mutexattr_init(&muxattr);
pthread_mutexattr_settype(&muxattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_setrobust(&muxattr, PTHREAD_MUTEX_ROBUST);
const int rc = pthread_mutex_init(mtx, &muxattr);
pthread_mutexattr_destroy(&muxattr);
if (rc == -1) {
free(mtx);
return ERROR_NOT_ENOUGH_MEMORY;
} // if
if (state)
pthread_mutex_lock(mtx);
*phmtx = (HMTX) mtx;
Sep 30, 2016
Sep 30, 2016
496
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
497
498
499
} // DosCreateMutexSem
// !!! FIXME: this is obviously not correct.
Sep 30, 2016
Sep 30, 2016
500
APIRET DosSetExceptionHandler(PEXCEPTIONREGISTRATIONRECORD rec)
Sep 30, 2016
Sep 30, 2016
501
{
Sep 30, 2016
Sep 30, 2016
502
503
TRACE_NATIVE("DosSetExceptionHandler(%p)", rec);
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
504
505
} // DosSetExceptionHandler
Jan 4, 2018
Jan 4, 2018
506
507
508
509
510
511
512
// !!! FIXME: this is obviously not correct.
APIRET DosUnsetExceptionHandler(PEXCEPTIONREGISTRATIONRECORD rec)
{
TRACE_NATIVE("DosUnsetExceptionHandler(%p)", rec);
return NO_ERROR;
} // DosSetExceptionHandler
Feb 28, 2018
Feb 28, 2018
513
514
515
#if !LX_LEGACY
ULONG DosFlatToSel(VOID) { return 0; }
#else
Oct 27, 2016
Oct 27, 2016
516
ULONG _DosFlatToSel(PVOID ptr)
Sep 30, 2016
Sep 30, 2016
517
{
Oct 9, 2016
Oct 9, 2016
518
TRACE_NATIVE("DosFlatToSel(%p)", ptr);
Feb 28, 2018
Feb 28, 2018
519
return GLoaderState.convert32to1616(ptr);
Oct 27, 2016
Oct 27, 2016
520
521
522
} // _DosFlatToSel
// DosFlatToSel() passes its argument in %eax, so a little asm to bridge that...
Jan 4, 2018
Jan 4, 2018
523
524
525
// Note that this isn't syscall calling conventions at all; CSet/2 generates
// thunking code that expects this function to preserve %ecx, so we'll just
// go ahead and save a bunch of stuff, just in case.
Oct 27, 2016
Oct 27, 2016
526
527
528
529
__asm__ (
".globl DosFlatToSel \n\t"
".type DosFlatToSel, @function \n\t"
"DosFlatToSel: \n\t"
Jan 4, 2018
Jan 4, 2018
530
531
532
533
534
535
" pushl %ebx \n\t" // save off a bunch of stuff. Better safe than sorry.
" pushl %ecx \n\t"
" pushl %edx \n\t"
" pushl %edi \n\t"
" pushl %esi \n\t"
" pushl %eax \n\t" // the actual argument to DosFlatToSel.
Oct 27, 2016
Oct 27, 2016
536
" call _DosFlatToSel \n\t"
Jan 4, 2018
Jan 4, 2018
537
538
539
540
541
542
" addl $4, %esp \n\t" // clear argument to DosFlatToSel.
" popl %esi \n\t"
" popl %edi \n\t"
" popl %edx \n\t" // save off a bunch of stuff.
" popl %ecx \n\t"
" popl %ebx \n\t"
Oct 27, 2016
Oct 27, 2016
543
544
545
" ret \n\t"
".size _DosFlatToSel, .-_DosFlatToSel \n\t"
);
Feb 28, 2018
Feb 28, 2018
546
#endif
Sep 30, 2016
Sep 30, 2016
547
Sep 30, 2016
Sep 30, 2016
548
APIRET DosSetSignalExceptionFocus(BOOL32 flag, PULONG pulTimes)
Sep 30, 2016
Sep 30, 2016
549
{
Sep 30, 2016
Sep 30, 2016
550
551
TRACE_NATIVE("DosSetSignalExceptionFocus(%u, %p)", (uint) flag, pulTimes);
Sep 30, 2016
Sep 30, 2016
552
if (flag == 0) {
Feb 28, 2018
Feb 28, 2018
553
if (GLoaderState.main_module->signal_exception_focus_count == 0)
Sep 30, 2016
Sep 30, 2016
554
return ERROR_ALREADY_RESET;
Feb 28, 2018
Feb 28, 2018
555
GLoaderState.main_module->signal_exception_focus_count--;
Sep 30, 2016
Sep 30, 2016
556
} else if (flag == 1) {
Feb 28, 2018
Feb 28, 2018
557
if (GLoaderState.main_module->signal_exception_focus_count == 0xFFFFFFFF)
Sep 30, 2016
Sep 30, 2016
558
return ERROR_NESTING_TOO_DEEP;
Feb 28, 2018
Feb 28, 2018
559
GLoaderState.main_module->signal_exception_focus_count++;
Sep 30, 2016
Sep 30, 2016
560
561
562
563
564
} else {
// !!! FIXME: does OS/2 do something if flag != 0 or 1?
} // else
if (pulTimes)
Feb 28, 2018
Feb 28, 2018
565
*pulTimes = GLoaderState.main_module->signal_exception_focus_count;
Sep 30, 2016
Sep 30, 2016
566
567
568
// !!! FIXME: I guess enable/disable SIGINT handler here?
Sep 30, 2016
Sep 30, 2016
569
return NO_ERROR;
Sep 30, 2016
Sep 30, 2016
570
571
} // DosSetSignalExceptionFocus
Oct 5, 2016
Oct 5, 2016
572
APIRET DosSetRelMaxFH(PLONG pincr, PULONG pcurrent)
Oct 2, 2016
Oct 2, 2016
573
{
Oct 5, 2016
Oct 5, 2016
574
TRACE_NATIVE("DosSetRelMaxFH(%p, %p)", pincr, pcurrent);
Oct 2, 2016
Oct 2, 2016
575
Oct 5, 2016
Oct 5, 2016
576
#if 0
Oct 2, 2016
Oct 2, 2016
577
578
579
580
581
582
struct rlimit rlim;
int rc = getrlimit(RLIMIT_NOFILE, &rlim);
assert(rc == 0); // this shouldn't fail...right?
if (pcurrent)
*pcurrent = (ULONG) rlim.rlim_cur;
Oct 5, 2016
Oct 5, 2016
583
584
if (pincr) {
rlim.rlim_cur = (rlim_t) (((LONG) rlim.rlim_cur) + *pincr);
Oct 2, 2016
Oct 2, 2016
585
586
587
588
589
if (setrlimit(RLIMIT_NOFILE, &rlim) == 0) {
if (pcurrent)
*pcurrent = (ULONG) rlim.rlim_cur;
} // if
} // if
Oct 5, 2016
Oct 5, 2016
590
591
592
593
594
595
596
#endif
if (pincr != NULL) {
const LONG incr = *pincr;
// OS/2 API docs say reductions in file handles are advisory, so we
// ignore them outright. Fight me.
if (incr > 0) {
Oct 14, 2016
Oct 14, 2016
597
grabLock(&GMutexDosCalls);
Oct 5, 2016
Oct 5, 2016
598
599
600
HFileInfo *info = (HFileInfo *) realloc(HFiles, sizeof (HFileInfo) * (MaxHFiles + incr));
if (info != NULL) {
HFiles = info;
Jun 8, 2018
Jun 8, 2018
601
FIXME("this needs to initialize the end of the array, not the front");
Oct 5, 2016
Oct 5, 2016
602
603
604
605
for (LONG i = 0; i < incr; i++, info++) {
info->fd = -1;
info->type = 0;
info->attr = 0;
Oct 27, 2016
Oct 27, 2016
606
info->flags = 0;
Oct 5, 2016
Oct 5, 2016
607
608
609
} // for
MaxHFiles += incr;
} // if
Oct 14, 2016
Oct 14, 2016
610
ungrabLock(&GMutexDosCalls);
Oct 5, 2016
Oct 5, 2016
611
612
613
614
} // if
} // if
if (pcurrent != NULL) {
Oct 14, 2016
Oct 14, 2016
615
grabLock(&GMutexDosCalls);
Oct 5, 2016
Oct 5, 2016
616
*pcurrent = MaxHFiles;
Oct 14, 2016
Oct 14, 2016
617
ungrabLock(&GMutexDosCalls);
Oct 5, 2016
Oct 5, 2016
618
} // if
Oct 2, 2016
Oct 2, 2016
619
620
621
622
623
624
625
626
627
628
return NO_ERROR; // always returns NO_ERROR even if it fails.
} // DosSetRelMaxFH
APIRET DosAllocMem(PPVOID ppb, ULONG cb, ULONG flag)
{
TRACE_NATIVE("DosAllocMem(%p, %u, %u)", ppb, (uint) cb, (uint) flag);
// !!! FIXME: this API is actually much more complicated than this.
Dec 21, 2017
Dec 21, 2017
629
*ppb = calloc(1, cb);
Oct 2, 2016
Oct 2, 2016
630
631
632
633
634
if (!*ppb)
return ERROR_NOT_ENOUGH_MEMORY;
return NO_ERROR;
} // DosAllocMem
Oct 2, 2016
Oct 2, 2016
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
APIRET DosSubSetMem(PVOID pbBase, ULONG flag, ULONG cb)
{
TRACE_NATIVE("DosSubSetMem(%p, %u, %u)", pbBase, (uint) flag, (uint) cb);
return NO_ERROR; // !!! FIXME: obviously wrong
} // DosSubSetMem
APIRET DosSubAllocMem(PVOID pbBase, PPVOID ppb, ULONG cb)
{
TRACE_NATIVE("DosSubAllocMem(%p, %p, %u)", pbBase, ppb, (uint) cb);
*ppb = malloc(cb); // !!! FIXME: obviously wrong
if (!*ppb)
return ERROR_NOT_ENOUGH_MEMORY;
return NO_ERROR;
} // DosSubAllocMem
Jun 8, 2018
Jun 8, 2018
652
static APIRET DosQueryHType_implementation(HFILE hFile, PULONG pType, PULONG pAttr)
Oct 2, 2016
Oct 2, 2016
653
{
Oct 14, 2016
Oct 14, 2016
654
APIRET retval = NO_ERROR;
Oct 5, 2016
Oct 5, 2016
655
Oct 14, 2016
Oct 14, 2016
656
657
658
659
660
661
662
663
664
grabLock(&GMutexDosCalls);
if ((hFile >= MaxHFiles) || (HFiles[hFile].fd == -1))
retval = ERROR_INVALID_HANDLE;
else {
// OS/2 will dereference a NULL pointer here, but I can't do it...!!!
if (pType) *pType = HFiles[hFile].type;
if (pAttr) *pAttr = HFiles[hFile].attr;
} // else
ungrabLock(&GMutexDosCalls);
Oct 5, 2016
Oct 5, 2016
665
Oct 14, 2016
Oct 14, 2016
666
return retval;
Jun 8, 2018
Jun 8, 2018
667
668
669
670
671
672
} // DosQueryHType_implementation
APIRET DosQueryHType(HFILE hFile, PULONG pType, PULONG pAttr)
{
TRACE_NATIVE("DosQueryHType(%u, %p, %p)", (uint) hFile, pType, pAttr);
return DosQueryHType_implementation(hFile, pType, pAttr);
Oct 2, 2016
Oct 2, 2016
673
674
675
676
677
678
679
} // DosQueryHType
APIRET DosSetMem(PVOID pb, ULONG cb, ULONG flag)
{
TRACE_NATIVE("DosSetMem(%p, %u, %u)", pb, (uint) cb, (uint) flag);
return NO_ERROR; // !!! FIXME: obviously wrong.
} // DosSetMem
Oct 6, 2016
Oct 6, 2016
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
APIRET DosGetDateTime(PDATETIME pdt)
{
TRACE_NATIVE("DosGetDateTime(%p)", pdt);
// we can't use time() for this, since we need sub-second resolution.
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm ltm;
localtime_r(&tv.tv_sec, &ltm);
pdt->hours = ltm.tm_hour;
pdt->minutes = ltm.tm_min;
pdt->seconds = (ltm.tm_sec == 60) ? 59 : ltm.tm_sec; // !!! FIXME: I assume OS/2 doesn't support leap seconds...
pdt->hundredths = tv.tv_usec / 10000000; // microseconds to hundreths.
pdt->day = ltm.tm_mday;
pdt->month = ltm.tm_mon + 1;
pdt->year = ltm.tm_year + 1900;
pdt->timezone = -(ltm.tm_gmtoff / 60); // OS/2 is minutes west of GMT, Unix is seconds east. Convert and flip!
pdt->weekday = ltm.tm_wday;
return NO_ERROR; // never returns failure.
} // DosGetDateTime
static char *makeUnixPath(const char *os2path, APIRET *err)
{
Feb 28, 2018
Feb 28, 2018
707
return GLoaderState.makeUnixPath(os2path, (uint32 *) err);
Oct 6, 2016
Oct 6, 2016
708
709
} // makeUnixPath
Oct 16, 2016
Oct 16, 2016
710
static APIRET doDosOpen(PSZ pszFileName, PHFILE pHf, PULONG pulAction, LONGLONG cbFile, ULONG ulAttribute, ULONG fsOpenFlags, ULONG fsOpenMode, PEAOP2 peaop2)
Oct 6, 2016
Oct 6, 2016
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
{
int isReadOnly = 0;
int flags = 0;
const ULONG access_mask = OPEN_ACCESS_READONLY | OPEN_ACCESS_WRITEONLY | OPEN_ACCESS_READWRITE;
switch (fsOpenMode & access_mask) {
case OPEN_ACCESS_READONLY: isReadOnly = 1; flags |= O_RDONLY; break;
case OPEN_ACCESS_WRITEONLY: flags |= O_WRONLY; break;
case OPEN_ACCESS_READWRITE: flags |= O_RDWR; break;
default: return ERROR_INVALID_PARAMETER;
} // switch
if (isReadOnly && (cbFile != 0))
return ERROR_INVALID_PARAMETER;
else if (ulAttribute & FILE_DIRECTORY)
return ERROR_INVALID_PARAMETER; // !!! FIXME: I assume you can't create a directory with DosOpen...right?
else if (isReadOnly && ((fsOpenFlags & 0xF0) == OPEN_ACTION_CREATE_IF_NEW))
return ERROR_INVALID_PARAMETER; // !!! FIXME: is this invalid on OS/2?
else if (isReadOnly && ((fsOpenFlags & 0x0F) == OPEN_ACTION_REPLACE_IF_EXISTS))
return ERROR_INVALID_PARAMETER; // !!! FIXME: is this invalid on OS/2?
else if (fsOpenFlags & 0xfffffe00) // bits (1<<8) through (1<<31) are reserved, must be zero.
return ERROR_INVALID_PARAMETER;
else if (fsOpenFlags & OPEN_FLAGS_DASD) // no good is going to come of this.
return ERROR_OPEN_FAILED;
else if (fsOpenMode & 0xffff0000) // bits (1<<16) through (1<<31) are reserved, must be zero.
return ERROR_INVALID_PARAMETER;
if (peaop2 != NULL) {
FIXME("EAs not yet implemented");
return ERROR_OPEN_FAILED;
}
switch (fsOpenFlags & 0xF0) {
case OPEN_ACTION_FAIL_IF_NEW:
break; // just don't O_CREAT and you're fine.
case OPEN_ACTION_CREATE_IF_NEW:
flags |= O_CREAT | O_EXCL; // the O_EXCL is intentional! see below.
break;
} // switch
int mustNotExist = 0;
int isExclusive = 0;
int isReplacing = 0;
switch (fsOpenFlags & 0x0F) {
case OPEN_ACTION_FAIL_IF_EXISTS:
if (isReadOnly)
mustNotExist = 1;
else {
isExclusive = 1;
flags |= O_CREAT | O_EXCL;
} // else
break;
case OPEN_ACTION_OPEN_IF_EXISTS:
break; // nothing to do here.
case OPEN_ACTION_REPLACE_IF_EXISTS: // right now, we already failed above if readonly.
isReplacing = 1;
flags |= O_TRUNC; // have the open call nuke it, and then we'll ftruncate to grow it if necessary.
break;
} // switch
if (fsOpenMode & OPEN_FLAGS_WRITE_THROUGH)
flags |= O_SYNC; // !!! FIXME: this flag is supposed to drop when inherited by children, but fcntl() can't do that on Linux.
if (fsOpenMode & OPEN_FLAGS_NOINHERIT)
flags |= O_CLOEXEC;
if (fsOpenMode & OPEN_FLAGS_FAIL_ON_ERROR)
FIXME("need other file i/o APIs to do a system dialog box if this handle has i/o failures!");
// O_DIRECT isn't supported on ZFS-on-Linux, so I'm already not on board,
// but also: do we care if programs intended to run on a system with a
// few megabytes of RAM--on an OS that didn't have a resizeable file
// cache!--writes into a multi-gigabyte machine's flexible cache? I don't!
//if (fsOpenMode & OPEN_FLAGS_NO_CACHE)
// flags |= O_DIRECT;
FIXME("There are fsOpenMode flags we currently ignore (like sharing support)");
const mode_t mode = S_IRUSR | ((ulAttribute & FILE_READONLY) ? 0 : S_IWUSR);
FIXME("Most of the file attributes don't make sense on Unix, but we could stuff them in EAs");
HFileInfo *info = HFiles;
uint32 i;
Oct 14, 2016
Oct 14, 2016
798
799
800
if (!grabLock(&GMutexDosCalls))
return ERROR_SYS_INTERNAL;
Oct 6, 2016
Oct 6, 2016
801
for (i = 0; i < MaxHFiles; i++, info++) {
Oct 14, 2016
Oct 14, 2016
802
803
if (info->fd == -1) { // available?
info->fd = -2;
Oct 6, 2016
Oct 6, 2016
804
break;
Oct 14, 2016
Oct 14, 2016
805
} // if
Oct 6, 2016
Oct 6, 2016
806
807
} // for
Oct 14, 2016
Oct 14, 2016
808
809
810
ungrabLock(&GMutexDosCalls);
if (i == MaxHFiles) {
Oct 6, 2016
Oct 6, 2016
811
return ERROR_TOO_MANY_OPEN_FILES;
Oct 14, 2016
Oct 14, 2016
812
}
Oct 6, 2016
Oct 6, 2016
813
814
815
816
817
818
819
820
821
822
const HFILE hf = i;
APIRET err = NO_ERROR;
char *unixpath = makeUnixPath(pszFileName, &err);
if (!unixpath) {
info->fd = -1;
return err;
} // if
Oct 14, 2016
Oct 14, 2016
823
//if (strcmp(pszFileName, unixpath) != 0) { fprintf(stderr, "DosOpen: converted '%s' to '%s'\n", pszFileName, unixpath); }
Oct 6, 2016
Oct 6, 2016
824
Oct 14, 2016
Oct 14, 2016
825
int fd = open(unixpath, flags, mode);
Oct 6, 2016
Oct 6, 2016
826
827
828
829
830
831
// if failed trying exclusive-create because file already exists, but we
// didn't explicitly _need_ exclusivity, retry without O_EXCL. We always
// try O_EXCL at first when using O_CREAT, so we can tell atomically if
// we were the one that created the file, to satisfy pulAction.
int existed = 0;
Oct 14, 2016
Oct 14, 2016
832
if ((fd == -1) && (flags & (O_CREAT|O_EXCL)) && (errno == EEXIST) && !isExclusive) {
Oct 6, 2016
Oct 6, 2016
833
existed = 1;
Oct 14, 2016
Oct 14, 2016
834
835
fd = open(unixpath, flags & ~O_EXCL, mode);
} else if (fd != -1) {
Oct 6, 2016
Oct 6, 2016
836
837
838
839
existed = 1;
} // else if
free(unixpath);
Oct 14, 2016
Oct 14, 2016
840
if (fd != -1) {
Oct 6, 2016
Oct 6, 2016
841
if (mustNotExist) {
Oct 14, 2016
Oct 14, 2016
842
close(fd);
Oct 6, 2016
Oct 6, 2016
843
844
845
846
847
info->fd = -1;
return ERROR_OPEN_FAILED; // !!! FIXME: what error does OS/2 return for this?
} else if ( (!existed || isReplacing) && (cbFile > 0) ) {
if (ftruncate(info->fd, cbFile) == -1) {
const int e = errno;
Oct 14, 2016
Oct 14, 2016
848
close(fd);
Oct 6, 2016
Oct 6, 2016
849
850
851
852
853
854
info->fd = -1;
errno = e; // let the switch below sort out the OS/2 error code.
} // if
} // else if
} // if
Oct 14, 2016
Oct 14, 2016
855
if (fd != -1) {
Oct 6, 2016
Oct 6, 2016
856
857
858
859
if (isReplacing)
FIXME("Replacing a file should delete all its EAs, too");
} // if
Jun 8, 2018
Jun 8, 2018
860
initHFileInfoFromUnixFd(fd, info);
Oct 14, 2016
Oct 14, 2016
861
862
if (fd == -1) {
Oct 6, 2016
Oct 6, 2016
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
switch (errno) {
case EACCES: return ERROR_ACCESS_DENIED;
case EDQUOT: return ERROR_CANNOT_MAKE;
case EEXIST: return ERROR_CANNOT_MAKE;
case EISDIR: return ERROR_ACCESS_DENIED;
case EMFILE: return ERROR_TOO_MANY_OPEN_FILES;
case ENFILE: return ERROR_TOO_MANY_OPEN_FILES;
case ENAMETOOLONG: return ERROR_FILENAME_EXCED_RANGE;
case ENOSPC: return ERROR_DISK_FULL;
case ENOMEM: return ERROR_NOT_ENOUGH_MEMORY;
case ENOENT: return ERROR_FILE_NOT_FOUND; // !!! FIXME: could be PATH_NOT_FOUND too, depending on circumstances.
case ENOTDIR: return ERROR_PATH_NOT_FOUND;
case EPERM: return ERROR_ACCESS_DENIED;
case EROFS: return ERROR_ACCESS_DENIED;
case ETXTBSY: return ERROR_ACCESS_DENIED;
default: return ERROR_OPEN_FAILED; // !!! FIXME: debug logging about missin errno case.
} // switch
__builtin_unreachable();
} // if
Oct 27, 2016
Oct 27, 2016
883
884
info->flags = fsOpenFlags;
Oct 6, 2016
Oct 6, 2016
885
886
887
888
889
890
891
892
893
if (isReplacing)
*pulAction = FILE_TRUNCATED;
else if (existed)
*pulAction = FILE_EXISTED;
else
*pulAction = FILE_CREATED;
*pHf = hf;
return NO_ERROR;
Oct 16, 2016
Oct 16, 2016
894
895
896
897
898
899
} // doDosOpen
APIRET DosOpen(PSZ pszFileName, PHFILE pHf, PULONG pulAction, ULONG cbFile, ULONG ulAttribute, ULONG fsOpenFlags, ULONG fsOpenMode, PEAOP2 peaop2)
{
TRACE_NATIVE("DosOpen('%s', %p, %p, %u, %u, %u, %u, %p)", pszFileName, pHf, pulAction, (uint) cbFile, (uint) ulAttribute, (uint) fsOpenFlags, (uint) fsOpenMode, peaop2);
return doDosOpen(pszFileName, pHf, pulAction, (LONGLONG) cbFile, ulAttribute, fsOpenFlags, fsOpenMode, peaop2);
Oct 6, 2016
Oct 6, 2016
900
901
} // DosOpen
Oct 9, 2016
Oct 9, 2016
902
903
904
APIRET DosRequestMutexSem(HMTX hmtx, ULONG ulTimeout)
{
TRACE_NATIVE("DosRequestMutexSem(%u, %u)", (uint) hmtx, (uint) ulTimeout);
Oct 14, 2016
Oct 14, 2016
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
if (!hmtx)
return ERROR_INVALID_HANDLE;
pthread_mutex_t *mutex = (pthread_mutex_t *) hmtx;
int rc;
if (ulTimeout == SEM_IMMEDIATE_RETURN) {
rc = pthread_mutex_trylock(mutex);
} else if (ulTimeout == SEM_INDEFINITE_WAIT) {
rc = pthread_mutex_lock(mutex);
} else {
struct timespec waittime;
timespecNowPlusMilliseconds(&waittime, ulTimeout);
rc = pthread_mutex_timedlock(mutex, &waittime);
} // else
switch (rc) {
case 0: return NO_ERROR;
case EAGAIN: return ERROR_TOO_MANY_SEM_REQUESTS;
case EOWNERDEAD: return ERROR_SEM_OWNER_DIED;
case ETIMEDOUT: return ERROR_TIMEOUT;
default: break;
} // switch
return ERROR_INVALID_HANDLE; // oh well.
Oct 9, 2016
Oct 9, 2016
931
932
933
934
935
} // DosRequestMutexSem
APIRET DosReleaseMutexSem(HMTX hmtx)
{
TRACE_NATIVE("DosReleaseMutexSem(%u)", (uint) hmtx);
Oct 14, 2016
Oct 14, 2016
936
937
938
939
940
941
942
943
944
945
946
947
if (!hmtx)
return ERROR_INVALID_HANDLE;
pthread_mutex_t *mutex = (pthread_mutex_t *) hmtx;
const int rc = pthread_mutex_unlock(mutex);
if (rc == 0)
return NO_ERROR;
else if (rc == EPERM)
return ERROR_NOT_OWNER;
return ERROR_INVALID_HANDLE;
Oct 9, 2016
Oct 9, 2016
948
949
} // DosReleaseMutexSem
Jun 18, 2018
Jun 18, 2018
950
static APIRET DosSetFilePtr_implementation(HFILE hFile, LONG ib, ULONG method, PULONG ibActual)
Oct 9, 2016
Oct 9, 2016
951
952
953
954
955
956
957
958
959
{
int whence;
switch (method) {
case FILE_BEGIN: whence = SEEK_SET; break;
case FILE_CURRENT: whence = SEEK_CUR; break;
case FILE_END: whence = SEEK_END; break;
default: return ERROR_INVALID_FUNCTION;
} // switch
Oct 14, 2016
Oct 14, 2016
960
961
const int fd = getHFileUnixDescriptor(hFile);
if (fd == -1)
Oct 9, 2016
Oct 9, 2016
962
963
return ERROR_INVALID_HANDLE;
Oct 14, 2016
Oct 14, 2016
964
const off_t pos = lseek(fd, (off_t) ib, whence);
Oct 9, 2016
Oct 9, 2016
965
966
967
968
969
970
971
972
973
974
if (pos == -1) {
if (errno == EINVAL)
return ERROR_NEGATIVE_SEEK;
return ERROR_INVALID_FUNCTION; // !!! FIXME: ?
} // if
if (ibActual)
*ibActual = (ULONG) pos;
return NO_ERROR;
Jun 18, 2018
Jun 18, 2018
975
} // DosSetFilePtr_implementation
Oct 9, 2016
Oct 9, 2016
976
Jun 18, 2018
Jun 18, 2018
977
APIRET DosSetFilePtr(HFILE hFile, LONG ib, ULONG method, PULONG ibActual)
Oct 9, 2016
Oct 9, 2016
978
{
Jun 18, 2018
Jun 18, 2018
979
980
981
TRACE_NATIVE("DosSetFilePtr(%u, %d, %u, %p)", (uint) hFile, (int) ib, (uint) method, ibActual);
return DosSetFilePtr_implementation(hFile, ib, method, ibActual);
} // DosSetFilePtr
Oct 9, 2016
Oct 9, 2016
982
Jun 18, 2018
Jun 18, 2018
983
984
static APIRET DosRead_implementation(HFILE hFile, PVOID pBuffer, ULONG cbRead, PULONG pcbActual)
{
Oct 14, 2016
Oct 14, 2016
985
986
const int fd = getHFileUnixDescriptor(hFile);
if (fd == -1)
Oct 9, 2016
Oct 9, 2016
987
988
return ERROR_INVALID_HANDLE;
Oct 14, 2016
Oct 14, 2016
989
const ssize_t br = read(fd, pBuffer, cbRead);
Oct 9, 2016
Oct 9, 2016
990
991
992
993
994
995
996
if (br == -1)
return ERROR_INVALID_FUNCTION; // !!! FIXME: ?
if (pcbActual)
*pcbActual = (ULONG) br;
return NO_ERROR;
Jun 18, 2018
Jun 18, 2018
997
} // DosRead_implementation
Oct 9, 2016
Oct 9, 2016
998
Jun 18, 2018
Jun 18, 2018
999
APIRET DosRead(HFILE hFile, PVOID pBuffer, ULONG cbRead, PULONG pcbActual)
Oct 9, 2016
Oct 9, 2016
1000
{