VirtualBox

source: kBuild/trunk/src/kash/shfile.c@ 2302

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

kash: Merged the SH_STUB_MODE and SH_FORKED_MODE stuff and use the latter name/define.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 29.0 KB
Line 
1/* $Id: shfile.c 2302 2009-03-01 05:42:54Z bird $ */
2/** @file
3 *
4 * File management.
5 *
6 * Copyright (c) 2007-2009 knut st. osmundsen <[email protected]>
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/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "shfile.h"
31#include "shinstance.h" /* TRACE2 */
32#include <stdlib.h>
33#include <stdio.h>
34#include <assert.h>
35
36#if K_OS == K_OS_WINDOWS
37# include <limits.h>
38# ifndef PIPE_BUF
39# define PIPE_BUF 512
40# endif
41# include <Windows.h>
42#else
43# include <unistd.h>
44# include <fcntl.h>
45# include <dirent.h>
46#endif
47
48
49/*******************************************************************************
50* Defined Constants And Macros *
51*******************************************************************************/
52/** @def SHFILE_IN_USE
53 * Whether the file descriptor table stuff is actually in use or not.
54 */
55#if K_OS == K_OS_WINDOWS \
56 || !defined(SH_FORKED_MODE)
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_FORKED_MODE)
602 fd = open(name, flags, mode);
603#endif
604
605 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
606 return fd;
607}
608
609int shfile_pipe(shfdtab *pfdtab, int fds[2])
610{
611 int rc;
612 int s;
613#ifdef SHFILE_IN_USE
614# if K_OS == K_OS_WINDOWS
615 HANDLE hRead = INVALID_HANDLE_VALUE;
616 HANDLE hWrite = INVALID_HANDLE_VALUE;
617 SECURITY_ATTRIBUTES SecurityAttributes;
618
619 SecurityAttributes.nLength = sizeof(SecurityAttributes);
620 SecurityAttributes.lpSecurityDescriptor = NULL;
621 SecurityAttributes.bInheritHandle = TRUE;
622
623 fds[1] = fds[0] = -1;
624 if (CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096))
625 {
626 fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, -1, "shfile_pipe");
627 if (fds[0] != -1)
628 {
629 fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, -1, "shfile_pipe");
630 if (fds[1] != -1)
631 rc = 0;
632 }
633
634# else
635 int native_fds[2];
636
637 fds[1] = fds[0] = -1;
638 if (!pipe(native_fds))
639 {
640 fds[0] = shfile_insert(pfdtab, native_fds[0], O_RDONLY, -1, "shfile_pipe");
641 if (fds[0] != -1)
642 {
643 fds[1] = shfile_insert(pfdtab, native_fds[1], O_WRONLY, -1, "shfile_pipe");
644 if (fds[1] != -1)
645 rc = 0;
646 }
647# endif
648 s = errno;
649 if (fds[1] == -1)
650 {
651 if (fds[0] != -1)
652 {
653 shmtxtmp tmp;
654 shmtx_enter(&pfdtab->mtx, &tmp);
655 rc = fds[0];
656 pfdtab->tab[rc].fd = -1;
657 pfdtab->tab[rc].flags = 0;
658 pfdtab->tab[rc].native = -1;
659 shmtx_leave(&pfdtab->mtx, &tmp);
660 }
661
662# if K_OS == K_OS_WINDOWS
663 CloseHandle(hRead);
664 CloseHandle(hWrite);
665# else
666 close(native_fds[0]);
667 close(native_fds[1]);
668# endif
669 fds[0] = fds[1] = -1;
670 errno = s;
671 rc = -1;
672 }
673 }
674 else
675 {
676# if K_OS == K_OS_WINDOWS
677 errno = shfile_dos2errno(GetLastError());
678# endif
679 rc = -1;
680 }
681
682#elif defined(SH_FORKED_MODE)
683 rc = pipe(fds);
684#endif
685
686 TRACE2((NULL, "shfile_pipe() -> %d{%d,%d} [%d]\n", rc, fds[0], fds[1], errno));
687 return rc;
688}
689
690int shfile_dup(shfdtab *pfdtab, int fd)
691{
692 int rc;
693#if defined(SH_FORKED_MODE)
694 rc = dup(fd);
695
696#else
697#endif
698
699 TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
700 return rc;
701}
702
703/**
704 * close().
705 */
706int shfile_close(shfdtab *pfdtab, unsigned fd)
707{
708 int rc;
709#ifdef SHFILE_IN_USE
710 shmtxtmp tmp;
711 shfile *file = shfile_get(pfdtab, fd, &tmp);
712 if (file)
713 {
714 shfile_native_close(file->native, file->flags);
715
716 file->fd = -1;
717 file->flags = 0;
718 file->native = -1;
719
720 shfile_put(pfdtab, file, &tmp);
721 rc = 0;
722 }
723 else
724 rc = -1;
725
726#elif defined(SH_FORKED_MODE)
727 rc = close(fd);
728#endif
729
730 TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
731 return rc;
732}
733
734/**
735 * read().
736 */
737long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
738{
739 long rc;
740#ifdef SHFILE_IN_USE
741 shmtxtmp tmp;
742 shfile *file = shfile_get(pfdtab, fd, &tmp);
743 if (file)
744 {
745# if K_OS == K_OS_WINDOWS
746 DWORD dwRead = 0;
747 if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
748 rc = dwRead;
749 else
750 rc = shfile_dos2errno(GetLastError());
751# else
752 rc = read(file->native, buf, len);
753# endif
754
755 shfile_put(pfdtab, file, &tmp);
756 }
757 else
758 rc = -1;
759
760#elif defined(SH_FORKED_MODE)
761 rc = read(fd, buf, len);
762#endif
763 return rc;
764}
765
766/**
767 * write().
768 */
769long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
770{
771 long rc;
772#ifdef SHFILE_IN_USE
773 shmtxtmp tmp;
774 shfile *file = shfile_get(pfdtab, fd, &tmp);
775 if (file)
776 {
777# if K_OS == K_OS_WINDOWS
778 DWORD dwWritten = 0;
779 if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
780 rc = dwWritten;
781 else
782 rc = shfile_dos2errno(GetLastError());
783# else
784 rc = write(file->native, buf, len);
785# endif
786
787 shfile_put(pfdtab, file, &tmp);
788 }
789 else
790 rc = -1;
791
792#elif defined(SH_FORKED_MODE)
793 rc = write(fd, buf, len);
794#endif
795 return rc;
796}
797
798/**
799 * lseek().
800 */
801long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
802{
803 long rc;
804#ifdef SHFILE_IN_USE
805 shmtxtmp tmp;
806 shfile *file = shfile_get(pfdtab, fd, &tmp);
807 if (file)
808 {
809# if K_OS == K_OS_WINDOWS
810 assert(SEEK_SET == FILE_BEGIN);
811 assert(SEEK_CUR == FILE_CURRENT);
812 assert(SEEK_END == FILE_END);
813 rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
814 if (rc == INVALID_SET_FILE_POINTER)
815 rc = shfile_dos2errno(GetLastError());
816# else
817 rc = lseek(file->native, off, whench);
818# endif
819
820 shfile_put(pfdtab, file, &tmp);
821 }
822 else
823 rc = -1;
824
825#elif defined(SH_FORKED_MODE)
826 rc = lseek(fd, off, whench);
827#endif
828
829 return rc;
830}
831
832int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
833{
834 int rc;
835#ifdef SHFILE_IN_USE
836 shmtxtmp tmp;
837 shfile *file = shfile_get(pfdtab, fd, &tmp);
838 if (file)
839 {
840 switch (cmd)
841 {
842 case F_GETFL:
843 rc = file->flags;
844 break;
845
846 case F_SETFL:
847 {
848 unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
849# ifdef O_DIRECT
850 mask |= O_DIRECT;
851# endif
852# ifdef O_ASYNC
853 mask |= O_ASYNC;
854# endif
855# ifdef O_SYNC
856 mask |= O_SYNC;
857# endif
858 if ((file->flags & mask) == (arg & mask))
859 rc = 0;
860 else
861 {
862# if K_OS == K_OS_WINDOWS
863 assert(0);
864 errno = EINVAL;
865 rc = -1;
866# else
867 rc = fcntl(file->native, F_SETFL, arg);
868 if (rc != -1)
869 file->flags = (file->flags & ~mask) | (arg & mask);
870# endif
871 }
872 break;
873 }
874
875 case F_DUPFD:
876 {
877# if K_OS == K_OS_WINDOWS
878 HANDLE hNew = INVALID_HANDLE_VALUE;
879 if (DuplicateHandle(GetCurrentProcess(),
880 (HANDLE)file->native,
881 GetCurrentProcess(),
882 &hNew,
883 0,
884 TRUE /* bInheritHandle */,
885 DUPLICATE_SAME_ACCESS))
886 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->flags, arg, "shfile_fcntl");
887 else
888 rc = shfile_dos2errno(GetLastError());
889# else
890 int nativeNew = fcntl(file->native F_DUPFD, SHFILE_UNIX_MIN_FD);
891 if (nativeNew != -1)
892 rc = shfile_insert(pfdtab, nativeNew, file->flags, arg, "shfile_fcntl");
893# endif
894 break;
895 }
896
897 default:
898 errno = -EINVAL;
899 rc = -1;
900 break;
901 }
902
903 shfile_put(pfdtab, file, &tmp);
904 }
905 else
906 rc = -1;
907
908#elif defined(SH_FORKED_MODE)
909 rc = fcntl(fd, cmd, arg);
910#endif
911
912 switch (cmd)
913 {
914 case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
915 case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
916 case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
917 default: TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
918 }
919 return rc;
920}
921
922int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
923{
924#if defined(SH_FORKED_MODE)
925 return stat(path, pst);
926
927#else
928#endif
929}
930
931int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
932{
933#if defined(SH_FORKED_MODE)
934# ifdef _MSC_VER
935 return stat(link, pst);
936# else
937 return lstat(link, pst);
938# endif
939
940#else
941#endif
942}
943
944int shfile_chdir(shfdtab *pfdtab, const char *path)
945{
946#if defined(SH_FORKED_MODE)
947# ifdef _MSC_VER //???
948 return chdir(path);
949# else
950 return chdir(path);
951# endif
952
953#else
954#endif
955}
956
957char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
958{
959#if defined(SH_FORKED_MODE)
960 return getcwd(buf, len);
961
962#else
963#endif
964}
965
966int shfile_access(shfdtab *pfdtab, const char *path, int type)
967{
968#if defined(SH_FORKED_MODE)
969# ifdef _MSC_VER
970 type &= ~X_OK;
971 return access(path, type);
972# else
973 return access(path, type);
974# endif
975
976#else
977#endif
978}
979
980int shfile_isatty(shfdtab *pfdtab, int fd)
981{
982 int rc;
983
984#if defined(SH_FORKED_MODE)
985 rc = isatty(fd);
986#else
987#endif
988
989 TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
990 return rc;
991}
992
993
994int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
995{
996 int rc;
997
998#if defined(SH_FORKED_MODE)
999# ifdef _MSC_VER
1000 errno = ENOSYS;
1001 rc = -1;
1002# else
1003 rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
1004 | (closeit ? FD_CLOEXEC : 0));
1005# endif
1006
1007#else
1008#endif
1009
1010 TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
1011 return rc;
1012}
1013
1014
1015int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
1016{
1017 int rc;
1018
1019#if defined(SH_FORKED_MODE)
1020# ifdef _MSC_VER
1021 errno = ENOSYS;
1022 rc = -1;
1023# else
1024 rc = ioctl(fd, request, buf);
1025# endif
1026
1027#else
1028#endif
1029
1030 TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
1031 return rc;
1032}
1033
1034
1035mode_t shfile_get_umask(shfdtab *pfdtab)
1036{
1037#if defined(SH_FORKED_MODE)
1038 return 022;
1039
1040#else
1041#endif
1042}
1043
1044void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
1045{
1046 (void)mask;
1047}
1048
1049
1050shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
1051{
1052#if defined(SH_FORKED_MODE)
1053# ifdef _MSC_VER
1054 errno = ENOSYS;
1055 return NULL;
1056# else
1057 return (shdir *)opendir(dir);
1058# endif
1059
1060#else
1061#endif
1062}
1063
1064shdirent *shfile_readdir(struct shdir *pdir)
1065{
1066#if defined(SH_FORKED_MODE)
1067# ifdef _MSC_VER
1068 errno = ENOSYS;
1069 return NULL;
1070# else
1071 struct dirent *pde = readdir((DIR *)pdir);
1072 return pde ? (shdirent *)&pde->d_name[0] : NULL;
1073# endif
1074
1075#else
1076#endif
1077}
1078
1079void shfile_closedir(struct shdir *pdir)
1080{
1081#if defined(SH_FORKED_MODE)
1082# ifdef _MSC_VER
1083 errno = ENOSYS;
1084# else
1085 closedir((DIR *)pdir);
1086# endif
1087
1088#else
1089#endif
1090}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette