source: trunk/src/kash/shfile.c@ 2297

Last change on this file since 2297 was 2297, checked in by bird, 16 years ago

kash: removed the stdio based logging.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 31.5 KB
Line 
1/* $Id: shfile.c 2297 2009-03-01 02:04:38Z bird $ */
2/** @file
3 *
4 * File management.
5 *
6 * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
7 *
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include "shfile.h"
28#include "shinstance.h" /* TRACE2 */
29#include <stdlib.h>
30#include <stdio.h>
31#include <assert.h>
32
33#if K_OS == K_OS_WINDOWS
34# include <limits.h>
35# ifndef PIPE_BUF
36# define PIPE_BUF 512
37# endif
38# include <Windows.h>
39#else
40# include <unistd.h>
41# include <fcntl.h>
42# include <dirent.h>
43#endif
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49/** @def SHFILE_IN_USE
50 * Whether the file descriptor table stuff is actually in use or not.
51 */
52#if !defined(SH_PURE_STUB_MODE) \
53 && ( K_OS == K_OS_WINDOWS \
54 || ( !defined(SH_STUB_MODE) \
55 && !defined(SH_FORKED_MODE)) \
56 )
57# define SHFILE_IN_USE
58#endif
59/** The max file table size. */
60#define SHFILE_MAX 1024
61/** The file table growth rate. */
62#define SHFILE_GROW 64
63/** The min native unix file descriptor. */
64#define SHFILE_UNIX_MIN_FD 32
65/** The path buffer size we use. */
66#define SHFILE_MAX_PATH 4096
67
68/** Set errno and return. Doing a trace in debug build. */
69#define RETURN_ERROR(rc, err, msg) \
70 do { \
71 TRACE2((NULL, "%s: " ## msg ## " - returning %d / %d\n", __FUNCTION__, (rc), (err))); \
72 errno = (err); \
73 return (rc); \
74 } while (0)
75
76
77#ifdef SHFILE_IN_USE
78
79/**
80 * Close the specified native handle.
81 *
82 * @param native The native file handle.
83 * @param flags The flags in case they might come in handy later.
84 */
85static void shfile_native_close(intptr_t native, unsigned flags)
86{
87#if K_OS == K_OS_WINDOWS
88 BOOL fRc = CloseHandle((HANDLE)native);
89 assert(fRc); (void)fRc;
90#else
91 int s = errno;
92 close(native);
93 errno = s;
94#endif
95 (void)flags;
96}
97
98/**
99 * Inserts the file into the descriptor table.
100 *
101 * If we're out of memory and cannot extend the table, we'll close the
102 * file, set errno to EMFILE and return -1.
103 *
104 * @returns The file descriptor number. -1 and errno on failure.
105 * @param pfdtab The file descriptor table.
106 * @param native The native file handle.
107 * @param flags The flags the it was created with.
108 * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
109 * @param who Who we're doing this for (for logging purposes).
110 */
111static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned flags, int fdMin, const char *who)
112{
113 shmtxtmp tmp;
114 int fd;
115 int i;
116
117 /*
118 * Fend of bad stuff.
119 */
120 if (fdMin >= SHFILE_MAX)
121 {
122 errno = EMFILE;
123 return -1;
124 }
125
126 shmtx_enter(&pfdtab->mtx, &tmp);
127
128 /*
129 * Search for a fitting unused location.
130 */
131 fd = -1;
132 for (i = 0; (unsigned)i < pfdtab->size; i++)
133 if ( i >= fdMin
134 && pfdtab->tab[i].fd == -1)
135 {
136 fd = i;
137 break;
138 }
139 if (fd == -1)
140 {
141 /*
142 * Grow the descriptor table.
143 */
144 shfile *new_tab;
145 int new_size = pfdtab->size + SHFILE_GROW;
146 while (new_size < fdMin)
147 new_size += SHFILE_GROW;
148 new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile));
149 if (new_tab)
150 {
151 for (i = pfdtab->size; i < new_size; i++)
152 {
153 new_tab[i].fd = -1;
154 new_tab[i].flags = 0;
155 new_tab[i].native = -1;
156 }
157
158 fd = pfdtab->size;
159 if (fd < fdMin)
160 fd = fdMin;
161
162 pfdtab->tab = new_tab;
163 pfdtab->size = new_size;
164 }
165 }
166
167 /*
168 * Fill in the entry if we've found one.
169 */
170 if (fd != -1)
171 {
172 pfdtab->tab[fd].fd = fd;
173 pfdtab->tab[fd].flags = flags;
174 pfdtab->tab[fd].cloexec = 0;
175 pfdtab->tab[fd].native = native;
176 }
177 else
178 shfile_native_close(native, flags);
179
180 shmtx_leave(&pfdtab->mtx, &tmp);
181
182 if (fd == -1)
183 errno = EMFILE;
184 (void)who;
185 return fd;
186}
187
188/**
189 * Gets a file descriptor and lock the file descriptor table.
190 *
191 * @returns Pointer to the file and table ownership on success,
192 * NULL and errno set to EBADF on failure.
193 * @param pfdtab The file descriptor table.
194 * @param fd The file descriptor number.
195 * @param ptmp See shmtx_enter.
196 */
197static shfile *shfile_get(shfdtab *pfdtab, int fd, shmtxtmp *ptmp)
198{
199 shfile *file = NULL;
200 if ( fd >= 0
201 && (unsigned)fd < pfdtab->size)
202 {
203 shmtx_enter(&pfdtab->mtx, ptmp);
204 if ((unsigned)fd < pfdtab->size
205 && pfdtab->tab[fd].fd != -1)
206 file = &pfdtab->tab[fd];
207 else
208 shmtx_leave(&pfdtab->mtx, ptmp);
209 }
210 if (!file)
211 errno = EBADF;
212 return file;
213}
214
215/**
216 * Puts back a file descriptor and releases the table ownership.
217 *
218 * @param pfdtab The file descriptor table.
219 * @param file The file.
220 * @param ptmp See shmtx_leave.
221 */
222static void shfile_put(shfdtab *pfdtab, shfile *file, shmtxtmp *ptmp)
223{
224 shmtx_leave(&pfdtab->mtx, ptmp);
225 assert(file);
226 (void)file;
227}
228
229/**
230 * Constructs a path from the current directory and the passed in path.
231 *
232 * @returns 0 on success, -1 and errno set to ENAMETOOLONG or EINVAL on failure.
233 *
234 * @param pfdtab The file descriptor table
235 * @param path The path the caller supplied.
236 * @param buf Where to put the path. This is assumed to be SHFILE_MAX_PATH
237 * chars in size.
238 */
239int shfile_make_path(shfdtab *pfdtab, const char *path, char *buf)
240{
241 size_t path_len = strlen(path);
242 if (path_len == 0)
243 {
244 errno = EINVAL;
245 return -1;
246 }
247 if (path_len >= SHFILE_MAX_PATH)
248 {
249 errno = ENAMETOOLONG;
250 return -1;
251 }
252 if ( *path == '/'
253#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
254 || *path == '\\'
255 || ( *path
256 && path[1] == ':'
257 && ( (*path >= 'A' && *path <= 'Z')
258 || (*path >= 'a' && *path <= 'z')))
259#endif
260 )
261 {
262 memcpy(buf, path, path_len + 1);
263 }
264 else
265 {
266 size_t cwd_len;
267 shmtxtmp tmp;
268
269 shmtx_enter(&pfdtab->mtx, &tmp);
270
271 cwd_len = strlen(pfdtab->cwd);
272 memcpy(buf, pfdtab->cwd, cwd_len);
273
274 shmtx_leave(&pfdtab->mtx, &tmp);
275
276 if (cwd_len + path_len + 1 >= SHFILE_MAX_PATH)
277 {
278 errno = ENAMETOOLONG;
279 return -1;
280 }
281 if ( !cwd_len
282 || buf[cwd_len - 1] != '/')
283 buf[cwd_len++] = '/';
284 memcpy(buf + cwd_len, path, path_len + 1);
285 }
286
287#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
288 if (!strcmp(buf, "/dev/null"))
289 strcpy(buf, "NUL");
290#endif
291 return 0;
292}
293
294# if K_OS == K_OS_WINDOWS
295/**
296 * Converts a DOS(/Windows) error code to errno,
297 * assigning it to errno.
298 *
299 * @returns -1
300 * @param err The DOS error.
301 */
302static int shfile_dos2errno(int err)
303{
304 switch (err)
305 {
306 case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
307 case ERROR_ACCESS_DENIED: errno = EACCES; break;
308 case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
309 case ERROR_LOCK_VIOLATION: errno = EACCES; break;
310 case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
311 case ERROR_CANNOT_MAKE: errno = EACCES; break;
312 case ERROR_FAIL_I24: errno = EACCES; break;
313 case ERROR_DRIVE_LOCKED: errno = EACCES; break;
314 case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
315 case ERROR_NOT_LOCKED: errno = EACCES; break;
316 case ERROR_LOCK_FAILED: errno = EACCES; break;
317 case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
318 case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
319 case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
320 case ERROR_INVALID_HANDLE: errno = EBADF; break;
321 case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
322 case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
323 case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
324 case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
325 case ERROR_FILE_EXISTS: errno = EEXIST; break;
326 case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
327 case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
328 case ERROR_INVALID_ACCESS: errno = EINVAL; break;
329 case ERROR_INVALID_DATA: errno = EINVAL; break;
330 case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
331 case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
332 case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
333 case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
334 case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
335 case ERROR_INVALID_DRIVE: errno = ENOENT; break;
336 case ERROR_NO_MORE_FILES: errno = ENOENT; break;
337 case ERROR_BAD_NETPATH: errno = ENOENT; break;
338 case ERROR_BAD_NET_NAME: errno = ENOENT; break;
339 case ERROR_BAD_PATHNAME: errno = ENOENT; break;
340 case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
341 case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
342 case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
343 case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
344 case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
345 case ERROR_NOT_ENOUGH_QUOTA: errno = ENOMEM; break;
346 case ERROR_DISK_FULL: errno = ENOSPC; break;
347 case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
348 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
349 case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
350 default: errno = EINVAL; break;
351 }
352 return -1;
353}
354# endif /* K_OS == K_OS_WINDOWS */
355
356#endif /* SHFILE_IN_USE */
357
358/**
359 * Initializes a file descriptor table.
360 *
361 * @returns 0 on success, -1 and errno on failure.
362 * @param pfdtab The table to initialize.
363 * @param inherit File descriptor table to inherit from. If not specified
364 * we will inherit from the current process as it were.
365 */
366int shfile_init(shfdtab *pfdtab, shfdtab *inherit)
367{
368 int rc;
369
370 pfdtab->cwd = NULL;
371 pfdtab->size = 0;
372 pfdtab->tab = NULL;
373 rc = shmtx_init(&pfdtab->mtx);
374 if (!rc)
375 {
376#ifdef SHFILE_IN_USE
377 char buf[SHFILE_MAX_PATH];
378 if (getcwd(buf, sizeof(buf)))
379 {
380 pfdtab->cwd = sh_strdup(NULL, buf);
381 if (!inherit)
382 {
383# if K_OS == K_OS_WINDOWS
384 static const struct
385 {
386 DWORD dwStdHandle;
387 unsigned flags;
388 } aStdHandles[3] =
389 {
390 { STD_INPUT_HANDLE, _O_RDONLY },
391 { STD_OUTPUT_HANDLE, _O_WRONLY },
392 { STD_ERROR_HANDLE, _O_WRONLY }
393 };
394 unsigned i;
395 STARTUPINFO Info;
396
397 rc = 0;
398
399 /* Try pick up the Visual C++ CRT file descriptor info. */
400 __try {
401 GetStartupInfo(&Info);
402 } __except (EXCEPTION_EXECUTE_HANDLER) {
403 memset(&Info, 0, sizeof(Info));
404 }
405 if ( Info.cbReserved2 <= sizeof(int)
406 && (uintptr_t)Info.lpReserved2 >= 0x1000
407 && *(int *)Info.lpReserved2 * (sizeof(int) + sizeof(intptr_t)) + 4 <= Info.cbReserved2
408 && *(int *)Info.lpReserved2 <= 2048
409 && *(int *)Info.lpReserved2 >= 1)
410 {
411 unsigned c = *(int *)Info.lpReserved2;
412 char *aosfile = (char *)Info.lpReserved2 + sizeof(int);
413 intptr_t *aosfhnd = (intptr_t *)(aosfile + c);
414
415 /** @todo process */
416
417 }
418
419 /* Check the three standard handles. */
420 for (i = 0; i < 3; i++)
421 {
422 HANDLE hFile = GetStdHandle(aStdHandles[i].dwStdHandle);
423 if ( hFile != INVALID_HANDLE_VALUE
424 && ( (unsigned)i >= pfdtab->size
425 || pfdtab->tab[i].fd == -1))
426 {
427 int fd2 = shfile_insert(pfdtab, (intptr_t)hFile, aStdHandles[i].flags, i, "shtab_init");
428 assert(fd2 == i); (void)fd2;
429 if (fd2 != i)
430 rc = -1;
431 }
432 }
433# else
434# endif
435 }
436 else
437 {
438 /** @todo */
439 errno = ENOSYS;
440 rc = -1;
441 }
442 }
443 else
444 rc = -1;
445#endif
446 }
447 return rc;
448}
449
450#if K_OS == K_OS_WINDOWS && defined(SHFILE_IN_USE) //&& defined(SH_FORKED_MODE)
451/**
452 * Helper for shfork.
453 *
454 * @param pfdtab The file descriptor table.
455 * @param set Whether to make all handles inheritable (1) or
456 * to restore them to the rigth state (0).
457 * @param hndls Where to store the three standard handles.
458 */
459void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls)
460{
461 shmtxtmp tmp;
462 unsigned i;
463
464 shmtx_enter(&pfdtab->mtx, &tmp);
465 TRACE2((NULL, "shfile_fork_win:\n"));
466
467 i = pfdtab->size;
468 while (i-- > 0)
469 {
470 if (pfdtab->tab[i].fd == i)
471 {
472 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
473 DWORD fFlag = (set || !pfdtab->tab[i].cloexec)
474 ? HANDLE_FLAG_INHERIT : 0;
475 if (set)
476 TRACE2((NULL, " #%d: native=%#x flags=%#x cloexec=%d fFlag=%#x\n",
477 i, pfdtab->tab[i].flags, hFile, pfdtab->tab[i].cloexec, fFlag));
478
479 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
480 {
481 DWORD err = GetLastError();
482 assert(0);
483 }
484 }
485 }
486
487 if (hndls)
488 {
489 for (i = 0; i < 3; i++)
490 {
491 if ( pfdtab->size > i
492 && pfdtab->tab[i].fd == 0)
493 hndls[i] = pfdtab->tab[i].native;
494 else
495 hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
496 }
497 }
498
499 shmtx_leave(&pfdtab->mtx, &tmp);
500}
501#endif
502
503
504/**
505 * open().
506 */
507int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
508{
509 int fd;
510#ifdef SHFILE_IN_USE
511 char absname[SHFILE_MAX_PATH];
512# if K_OS == K_OS_WINDOWS
513 HANDLE hFile;
514 DWORD dwDesiredAccess;
515 DWORD dwShareMode;
516 DWORD dwCreationDisposition;
517 DWORD dwFlagsAndAttributes;
518 SECURITY_ATTRIBUTES SecurityAttributes;
519
520# ifndef _O_ACCMODE
521# define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
522# endif
523 switch (flags & (_O_ACCMODE | _O_APPEND))
524 {
525 case _O_RDONLY: dwDesiredAccess = GENERIC_READ; break;
526 case _O_RDONLY | _O_APPEND: dwDesiredAccess = GENERIC_READ; break;
527 case _O_WRONLY: dwDesiredAccess = GENERIC_WRITE; break;
528 case _O_WRONLY | _O_APPEND: dwDesiredAccess = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
529 case _O_RDWR: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
530 case _O_RDWR | _O_APPEND: dwDesiredAccess = GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
531
532 default:
533 RETURN_ERROR(-1, EINVAL, "invalid mode");
534 }
535
536 dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
537
538 SecurityAttributes.nLength = sizeof(SecurityAttributes);
539 SecurityAttributes.lpSecurityDescriptor = NULL;
540 SecurityAttributes.bInheritHandle = (flags & _O_NOINHERIT) == 0;
541
542 if (flags & _O_CREAT)
543 {
544 if ((flags & (_O_EXCL | _O_TRUNC)) == (_O_EXCL | _O_TRUNC))
545 RETURN_ERROR(-1, EINVAL, "_O_EXCL | _O_TRUNC");
546
547 if (flags & _O_TRUNC)
548 dwCreationDisposition = CREATE_ALWAYS; /* not 100%, but close enough */
549 else if (flags & _O_EXCL)
550 dwCreationDisposition = CREATE_NEW;
551 else
552 dwCreationDisposition = OPEN_ALWAYS;
553 }
554 else if (flags & _O_TRUNC)
555 dwCreationDisposition = TRUNCATE_EXISTING;
556 else
557 dwCreationDisposition = OPEN_EXISTING;
558
559 if (!(flags & _O_CREAT) || (mode & 0222))
560 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
561 else
562 dwFlagsAndAttributes = FILE_ATTRIBUTE_READONLY;
563
564 fd = shfile_make_path(pfdtab, name, &absname[0]);
565 if (!fd)
566 {
567 SetLastError(0);
568 hFile = CreateFileA(absname,
569 dwDesiredAccess,
570 dwShareMode,
571 &SecurityAttributes,
572 dwCreationDisposition,
573 dwFlagsAndAttributes,
574 NULL /* hTemplateFile */);
575 if (hFile != INVALID_HANDLE_VALUE)
576 fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, -1, "shfile_open");
577 else
578 fd = shfile_dos2errno(GetLastError());
579 }
580
581# else /* K_OS != K_OS_WINDOWS */
582 fd = shfile_make_path(pfdtab, name, &absname[0]);
583 if (!fd)
584 {
585 fd = open(absname, flag, mode);
586 if (fd != -1)
587 {
588 int native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD);
589 int s = errno;
590 close(fd);
591 errno = s;
592 if (native != -1)
593 fd = shfile_insert(pfdtab, native, flags, -1, "shfile_open");
594 else
595 fd = -1;
596 }
597 }
598
599# endif /* K_OS != K_OS_WINDOWS */
600
601#elif defined(SH_PURE_STUB_MODE)
602 fd = -1;
603 errno = ENOSYS;
604
605#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
606 fd = open(name, flags, mode);
607#endif
608
609 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
610 return fd;
611}
612
613int shfile_pipe(shfdtab *pfdtab, int fds[2])
614{
615 int rc;
616 int s;
617#ifdef SHFILE_IN_USE
618# if K_OS == K_OS_WINDOWS
619 HANDLE hRead = INVALID_HANDLE_VALUE;
620 HANDLE hWrite = INVALID_HANDLE_VALUE;
621 SECURITY_ATTRIBUTES SecurityAttributes;
622
623 SecurityAttributes.nLength = sizeof(SecurityAttributes);
624 SecurityAttributes.lpSecurityDescriptor = NULL;
625 SecurityAttributes.bInheritHandle = TRUE;
626
627 if (!CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096))
628 {
629 fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, -1, "shfile_pipe");
630 if (fds[0] != -1)
631 {
632 fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, -1, "shfile_pipe");
633 if (fds[1] != -1)
634 rc = 0;
635 }
636
637# else
638 int native_fds[2];
639 if (!pipe(native_fds))
640 {
641 fds[1] = -1;
642 fds[0] = shfile_insert(pfdtab, native_fds[0], O_RDONLY, -1, "shfile_pipe");
643 if (fds[0] != -1)
644 {
645 fds[1] = shfile_insert(pfdtab, native_fds[1], O_WRONLY, -1, "shfile_pipe");
646 if (fds[1] != -1)
647 rc = 0;
648 }
649# endif
650 s = errno;
651 if (fds[1] == -1)
652 {
653 if (fds[0] != -1)
654 {
655 shmtxtmp tmp;
656 shmtx_enter(&pfdtab->mtx, &tmp);
657 rc = fds[0];
658 pfdtab->tab[rc].fd = -1;
659 pfdtab->tab[rc].flags = 0;
660 pfdtab->tab[rc].native = -1;
661 shmtx_leave(&pfdtab->mtx, &tmp);
662 }
663
664# if K_OS == K_OS_WINDOWS
665 CloseHandle(hRead);
666 CloseHandle(hWrite);
667# else
668 close(native_fds[0]);
669 close(native_fds[1]);
670# endif
671 fds[0] = fds[1] = -1;
672 errno = s;
673 rc = -1;
674 }
675 }
676
677#elif defined(SH_PURE_STUB_MODE)
678 rc = -1;
679 errno = ENOSYS;
680
681#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
682# ifdef _MSC_VER
683 rc = _pipe(fds, PIPE_BUF, O_BINARY);
684# else
685 rc = pipe(fds);
686# endif
687#endif
688
689 TRACE2((NULL, "shfile_pipe() -> %d{%d,%d} [%d]\n", rc, fds[0], fds[1], errno));
690 return rc;
691}
692
693int shfile_dup(shfdtab *pfdtab, int fd)
694{
695 int rc;
696#ifdef SH_PURE_STUB_MODE
697 rc = -1;
698
699#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
700 rc = dup(fd);
701
702#else
703#endif
704
705 TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
706 return rc;
707}
708
709/**
710 * close().
711 */
712int shfile_close(shfdtab *pfdtab, unsigned fd)
713{
714 int rc;
715#ifdef SHFILE_IN_USE
716 shmtxtmp tmp;
717 shfile *file = shfile_get(pfdtab, fd, &tmp);
718 if (file)
719 {
720 shfile_native_close(file->native, file->flags);
721
722 file->fd = -1;
723 file->flags = 0;
724 file->native = -1;
725
726 shfile_put(pfdtab, file, &tmp);
727 rc = 0;
728 }
729 else
730 rc = -1;
731
732#elif defined(SH_PURE_STUB_MODE)
733 rc = -1;
734
735#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
736 rc = close(fd);
737#endif
738
739 TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
740 return rc;
741}
742
743/**
744 * read().
745 */
746long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
747{
748 long rc;
749#ifdef SHFILE_IN_USE
750 shmtxtmp tmp;
751 shfile *file = shfile_get(pfdtab, fd, &tmp);
752 if (file)
753 {
754# if K_OS == K_OS_WINDOWS
755 DWORD dwRead = 0;
756 if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
757 rc = dwRead;
758 else
759 rc = shfile_dos2errno(GetLastError());
760# else
761 rc = read(file->native, buf, len);
762# endif
763
764 shfile_put(pfdtab, file, &tmp);
765 }
766 else
767 rc = -1;
768
769#elif defined(SH_PURE_STUB_MODE)
770 rc = -1;
771
772#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
773# ifdef _MSC_VER
774 rc = read(fd, buf, (unsigned)len);
775# else
776 rc = read(fd, buf, len);
777# endif
778#endif
779 return rc;
780}
781
782/**
783 * write().
784 */
785long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
786{
787 long rc;
788#ifdef SHFILE_IN_USE
789 shmtxtmp tmp;
790 shfile *file = shfile_get(pfdtab, fd, &tmp);
791 if (file)
792 {
793# if K_OS == K_OS_WINDOWS
794 DWORD dwWritten = 0;
795 if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
796 rc = dwWritten;
797 else
798 rc = shfile_dos2errno(GetLastError());
799# else
800 rc = write(file->native, buf, len);
801# endif
802
803 shfile_put(pfdtab, file, &tmp);
804 }
805 else
806 rc = -1;
807
808#elif defined(SH_PURE_STUB_MODE)
809 rc = -1;
810
811#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
812# ifdef _MSC_VER
813 rc = write(fd, buf, (unsigned)len);
814# else
815 rc = write(fd, buf, len);
816# endif
817#endif
818 return rc;
819}
820
821/**
822 * lseek().
823 */
824long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
825{
826 long rc;
827#ifdef SHFILE_IN_USE
828 shmtxtmp tmp;
829 shfile *file = shfile_get(pfdtab, fd, &tmp);
830 if (file)
831 {
832# if K_OS == K_OS_WINDOWS
833 assert(SEEK_SET == FILE_BEGIN);
834 assert(SEEK_CUR == FILE_CURRENT);
835 assert(SEEK_END == FILE_END);
836 rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
837 if (rc == INVALID_SET_FILE_POINTER)
838 rc = shfile_dos2errno(GetLastError());
839# else
840 rc = lseek(file->native, off, whench);
841# endif
842
843 shfile_put(pfdtab, file, &tmp);
844 }
845 else
846 rc = -1;
847
848#elif defined(SH_PURE_STUB_MODE)
849 rc = -1;
850
851#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
852 rc = lseek(fd, off, whench);
853#endif
854
855 return rc;
856}
857
858int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
859{
860 int rc;
861#ifdef SHFILE_IN_USE
862 shmtxtmp tmp;
863 shfile *file = shfile_get(pfdtab, fd, &tmp);
864 if (file)
865 {
866 switch (cmd)
867 {
868 case F_GETFL:
869 rc = file->flags;
870 break;
871
872 case F_SETFL:
873 {
874 unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
875# ifdef O_DIRECT
876 mask |= O_DIRECT;
877# endif
878# ifdef O_ASYNC
879 mask |= O_ASYNC;
880# endif
881# ifdef O_SYNC
882 mask |= O_SYNC;
883# endif
884 if ((file->flags & mask) == (arg & mask))
885 rc = 0;
886 else
887 {
888# if K_OS == K_OS_WINDOWS
889 assert(0);
890 errno = EINVAL;
891 rc = -1;
892# else
893 rc = fcntl(file->native, F_SETFL, arg);
894 if (rc != -1)
895 file->flags = (file->flags & ~mask) | (arg & mask);
896# endif
897 }
898 break;
899 }
900
901 case F_DUPFD:
902 {
903# if K_OS == K_OS_WINDOWS
904 HANDLE hNew = INVALID_HANDLE_VALUE;
905 if (DuplicateHandle(GetCurrentProcess(),
906 (HANDLE)file->native,
907 GetCurrentProcess(),
908 &hNew,
909 0,
910 TRUE /* bInheritHandle */,
911 DUPLICATE_SAME_ACCESS))
912 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->flags, arg, "shfile_fcntl");
913 else
914 rc = shfile_dos2errno(GetLastError());
915# else
916 int nativeNew = fcntl(file->native F_DUPFD, SHFILE_UNIX_MIN_FD);
917 if (nativeNew != -1)
918 rc = shfile_insert(pfdtab, nativeNew, file->flags, arg, "shfile_fcntl");
919# endif
920 break;
921 }
922
923 default:
924 errno = -EINVAL;
925 rc = -1;
926 break;
927 }
928
929 shfile_put(pfdtab, file, &tmp);
930 }
931 else
932 rc = -1;
933
934#elif defined(SH_PURE_STUB_MODE)
935 rc = -1;
936
937#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
938# ifdef _MSC_VER
939 switch (cmd)
940 {
941 /* Just enough F_GETFL/F_SETFL to get along with. */
942 case F_GETFL:
943 errno = 0;
944 rc = _isatty(fd);
945 if (errno == EBADF)
946 rc = -1;
947 break;
948
949 case F_SETFL:
950 errno = 0;
951 rc = _isatty(fd);
952 if (errno != EBADF)
953 {
954 if (!arg)
955 rc = 0;
956 else
957 {
958 errno = EINVAL;
959 rc = -1;
960 }
961 }
962 else
963 rc = -1;
964 break;
965
966 case F_DUPFD:
967 {
968 /* the brute force approach. */
969 int i = 0;
970 int fds[256];
971 for (i = 0; i < 256; i++)
972 {
973 fds[i] = -1;
974 rc = _dup(fd);
975 if (rc >= arg)
976 break;
977 fds[i] = rc;
978 }
979 while (i-- > 0)
980 close(fds[i]);
981 if (rc < arg)
982 {
983 errno = EMFILE;
984 rc = -1;
985 }
986 break;
987 }
988 }
989# else
990 rc = fcntl(fd, cmd, arg);
991# endif
992#endif
993
994 switch (cmd)
995 {
996 case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
997 case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
998 case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
999 default: TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
1000 }
1001 return rc;
1002}
1003
1004int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
1005{
1006#ifdef SH_PURE_STUB_MODE
1007 return -1;
1008
1009#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1010 return stat(path, pst);
1011
1012#else
1013#endif
1014}
1015
1016int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
1017{
1018#ifdef SH_PURE_STUB_MODE
1019 return -1;
1020
1021#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1022# ifdef _MSC_VER
1023 return stat(link, pst);
1024# else
1025 return lstat(link, pst);
1026# endif
1027
1028#else
1029#endif
1030}
1031
1032int shfile_chdir(shfdtab *pfdtab, const char *path)
1033{
1034#ifdef SH_PURE_STUB_MODE
1035 return -1;
1036
1037#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1038# ifdef _MSC_VER //???
1039 return chdir(path);
1040# else
1041 return chdir(path);
1042# endif
1043
1044#else
1045#endif
1046}
1047
1048char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
1049{
1050#ifdef SH_PURE_STUB_MODE
1051 return NULL;
1052
1053#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1054 return getcwd(buf, len);
1055
1056#else
1057#endif
1058}
1059
1060int shfile_access(shfdtab *pfdtab, const char *path, int type)
1061{
1062#ifdef SH_PURE_STUB_MODE
1063 return -1;
1064
1065#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1066# ifdef _MSC_VER
1067 type &= ~X_OK;
1068 return access(path, type);
1069# else
1070 return access(path, type);
1071# endif
1072
1073#else
1074#endif
1075}
1076
1077int shfile_isatty(shfdtab *pfdtab, int fd)
1078{
1079 int rc;
1080
1081#ifdef SH_PURE_STUB_MODE
1082 rc = 0;
1083#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1084 rc = isatty(fd);
1085#else
1086#endif
1087
1088 TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
1089 return rc;
1090}
1091
1092
1093int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
1094{
1095 int rc;
1096
1097#ifdef SH_PURE_STUB_MODE
1098 rc = -1;
1099
1100#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1101# ifdef _MSC_VER
1102 errno = ENOSYS;
1103 rc = -1;
1104# else
1105 rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
1106 | (closeit ? FD_CLOEXEC : 0));
1107# endif
1108
1109#else
1110#endif
1111
1112 TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
1113 return rc;
1114}
1115
1116
1117int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
1118{
1119 int rc;
1120
1121#ifdef SH_PURE_STUB_MODE
1122 rc = -1;
1123
1124#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1125# ifdef _MSC_VER
1126 errno = ENOSYS;
1127 rc = -1;
1128# else
1129 rc = ioctl(fd, request, buf);
1130# endif
1131
1132#else
1133#endif
1134
1135 TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
1136 return rc;
1137}
1138
1139
1140mode_t shfile_get_umask(shfdtab *pfdtab)
1141{
1142#ifdef SH_PURE_STUB_MODE
1143 return 022;
1144
1145#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1146 return 022;
1147
1148#else
1149#endif
1150}
1151
1152void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
1153{
1154 (void)mask;
1155}
1156
1157
1158shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
1159{
1160#ifdef SH_PURE_STUB_MODE
1161 return NULL;
1162
1163#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1164# ifdef _MSC_VER
1165 errno = ENOSYS;
1166 return NULL;
1167# else
1168 return (shdir *)opendir(dir);
1169# endif
1170
1171#else
1172#endif
1173}
1174
1175shdirent *shfile_readdir(struct shdir *pdir)
1176{
1177#ifdef SH_PURE_STUB_MODE
1178 return NULL;
1179
1180#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1181# ifdef _MSC_VER
1182 errno = ENOSYS;
1183 return NULL;
1184# else
1185 struct dirent *pde = readdir((DIR *)pdir);
1186 return pde ? (shdirent *)&pde->d_name[0] : NULL;
1187# endif
1188
1189#else
1190#endif
1191}
1192
1193void shfile_closedir(struct shdir *pdir)
1194{
1195#ifdef SH_PURE_STUB_MODE
1196 return NULL;
1197
1198#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1199# ifdef _MSC_VER
1200 errno = ENOSYS;
1201# else
1202 closedir((DIR *)pdir);
1203# endif
1204
1205#else
1206#endif
1207}
Note: See TracBrowser for help on using the repository browser.