1 | /* $Id: shfile.c 2423 2010-10-17 23:43:35Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * File management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007-2010 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 <string.h>
|
---|
35 | #include <assert.h>
|
---|
36 |
|
---|
37 | #if K_OS == K_OS_WINDOWS
|
---|
38 | # include <limits.h>
|
---|
39 | # ifndef PIPE_BUF
|
---|
40 | # define PIPE_BUF 512
|
---|
41 | # endif
|
---|
42 | # include <ntstatus.h>
|
---|
43 | # define WIN32_NO_STATUS
|
---|
44 | # include <Windows.h>
|
---|
45 | # if !defined(_WIN32_WINNT)
|
---|
46 | # define _WIN32_WINNT 0x0502 /* Windows Server 2003 */
|
---|
47 | # endif
|
---|
48 | # include <winternl.h> //NTSTATUS
|
---|
49 | #else
|
---|
50 | # include <unistd.h>
|
---|
51 | # include <fcntl.h>
|
---|
52 | # include <dirent.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | /*******************************************************************************
|
---|
57 | * Defined Constants And Macros *
|
---|
58 | *******************************************************************************/
|
---|
59 | /** @def SHFILE_IN_USE
|
---|
60 | * Whether the file descriptor table stuff is actually in use or not.
|
---|
61 | */
|
---|
62 | #if K_OS == K_OS_WINDOWS \
|
---|
63 | || K_OS == K_OS_OPENBSD /* because of ugly pthread library pipe hacks */ \
|
---|
64 | || !defined(SH_FORKED_MODE)
|
---|
65 | # define SHFILE_IN_USE
|
---|
66 | #endif
|
---|
67 | /** The max file table size. */
|
---|
68 | #define SHFILE_MAX 1024
|
---|
69 | /** The file table growth rate. */
|
---|
70 | #define SHFILE_GROW 64
|
---|
71 | /** The min native unix file descriptor. */
|
---|
72 | #define SHFILE_UNIX_MIN_FD 32
|
---|
73 | /** The path buffer size we use. */
|
---|
74 | #define SHFILE_MAX_PATH 4096
|
---|
75 |
|
---|
76 | /** Set errno and return. Doing a trace in debug build. */
|
---|
77 | #define RETURN_ERROR(rc, err, msg) \
|
---|
78 | do { \
|
---|
79 | TRACE2((NULL, "%s: " ## msg ## " - returning %d / %d\n", __FUNCTION__, (rc), (err))); \
|
---|
80 | errno = (err); \
|
---|
81 | return (rc); \
|
---|
82 | } while (0)
|
---|
83 |
|
---|
84 | #if K_OS == K_OS_WINDOWS
|
---|
85 | /* See msdos.h for description. */
|
---|
86 | # define FOPEN 0x01
|
---|
87 | # define FEOFLAG 0x02
|
---|
88 | # define FCRLF 0x04
|
---|
89 | # define FPIPE 0x08
|
---|
90 | # define FNOINHERIT 0x10
|
---|
91 | # define FAPPEND 0x20
|
---|
92 | # define FDEV 0x40
|
---|
93 | # define FTEXT 0x80
|
---|
94 |
|
---|
95 | # define MY_ObjectBasicInformation 0
|
---|
96 | # define MY_FileNamesInformation 12
|
---|
97 |
|
---|
98 | typedef struct
|
---|
99 | {
|
---|
100 | ULONG Attributes;
|
---|
101 | ACCESS_MASK GrantedAccess;
|
---|
102 | ULONG HandleCount;
|
---|
103 | ULONG PointerCount;
|
---|
104 | ULONG PagedPoolUsage;
|
---|
105 | ULONG NonPagedPoolUsage;
|
---|
106 | ULONG Reserved[3];
|
---|
107 | ULONG NameInformationLength;
|
---|
108 | ULONG TypeInformationLength;
|
---|
109 | ULONG SecurityDescriptorLength;
|
---|
110 | LARGE_INTEGER CreateTime;
|
---|
111 | } MY_OBJECT_BASIC_INFORMATION;
|
---|
112 |
|
---|
113 | #if 0
|
---|
114 | typedef struct
|
---|
115 | {
|
---|
116 | union
|
---|
117 | {
|
---|
118 | LONG Status;
|
---|
119 | PVOID Pointer;
|
---|
120 | };
|
---|
121 | ULONG_PTR Information;
|
---|
122 | } MY_IO_STATUS_BLOCK;
|
---|
123 | #else
|
---|
124 | typedef IO_STATUS_BLOCK MY_IO_STATUS_BLOCK;
|
---|
125 | #endif
|
---|
126 | typedef MY_IO_STATUS_BLOCK *PMY_IO_STATUS_BLOCK;
|
---|
127 |
|
---|
128 | typedef struct
|
---|
129 | {
|
---|
130 | ULONG NextEntryOffset;
|
---|
131 | ULONG FileIndex;
|
---|
132 | ULONG FileNameLength;
|
---|
133 | WCHAR FileName[1];
|
---|
134 | } MY_FILE_NAMES_INFORMATION, *PMY_FILE_NAMES_INFORMATION;
|
---|
135 |
|
---|
136 | typedef NTSTATUS (NTAPI * PFN_NtQueryObject)(HANDLE, int, void *, size_t, size_t *);
|
---|
137 | typedef NTSTATUS (NTAPI * PFN_NtQueryDirectoryFile)(HANDLE, HANDLE, void *, void *, PMY_IO_STATUS_BLOCK, void *,
|
---|
138 | ULONG, int, int, PUNICODE_STRING, int);
|
---|
139 | typedef NTSTATUS (NTAPI * PFN_RtlUnicodeStringToAnsiString)(PANSI_STRING, PCUNICODE_STRING, int);
|
---|
140 |
|
---|
141 |
|
---|
142 | #endif /* K_OS_WINDOWS */
|
---|
143 |
|
---|
144 |
|
---|
145 | /*******************************************************************************
|
---|
146 | * Global Variables *
|
---|
147 | *******************************************************************************/
|
---|
148 | #if K_OS == K_OS_WINDOWS
|
---|
149 | static int g_shfile_globals_initialized = 0;
|
---|
150 | static PFN_NtQueryObject g_pfnNtQueryObject = NULL;
|
---|
151 | static PFN_NtQueryDirectoryFile g_pfnNtQueryDirectoryFile = NULL;
|
---|
152 | static PFN_RtlUnicodeStringToAnsiString g_pfnRtlUnicodeStringToAnsiString = NULL;
|
---|
153 | #endif /* K_OS_WINDOWS */
|
---|
154 |
|
---|
155 |
|
---|
156 | #ifdef SHFILE_IN_USE
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Close the specified native handle.
|
---|
160 | *
|
---|
161 | * @param native The native file handle.
|
---|
162 | * @param flags The flags in case they might come in handy later.
|
---|
163 | */
|
---|
164 | static void shfile_native_close(intptr_t native, unsigned flags)
|
---|
165 | {
|
---|
166 | #if K_OS == K_OS_WINDOWS
|
---|
167 | BOOL fRc = CloseHandle((HANDLE)native);
|
---|
168 | assert(fRc); (void)fRc;
|
---|
169 | #else
|
---|
170 | int s = errno;
|
---|
171 | close(native);
|
---|
172 | errno = s;
|
---|
173 | #endif
|
---|
174 | (void)flags;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Inserts the file into the descriptor table.
|
---|
179 | *
|
---|
180 | * If we're out of memory and cannot extend the table, we'll close the
|
---|
181 | * file, set errno to EMFILE and return -1.
|
---|
182 | *
|
---|
183 | * @returns The file descriptor number. -1 and errno on failure.
|
---|
184 | * @param pfdtab The file descriptor table.
|
---|
185 | * @param native The native file handle.
|
---|
186 | * @param oflags The flags the it was opened/created with.
|
---|
187 | * @param shflags The shell file flags.
|
---|
188 | * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
|
---|
189 | * @param who Who we're doing this for (for logging purposes).
|
---|
190 | */
|
---|
191 | static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin, const char *who)
|
---|
192 | {
|
---|
193 | shmtxtmp tmp;
|
---|
194 | int fd;
|
---|
195 | int i;
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Fend of bad stuff.
|
---|
199 | */
|
---|
200 | if (fdMin >= SHFILE_MAX)
|
---|
201 | {
|
---|
202 | errno = EMFILE;
|
---|
203 | return -1;
|
---|
204 | }
|
---|
205 | # if K_OS != K_OS_WINDOWS
|
---|
206 | if (fcntl((int)native, F_SETFD, fcntl((int)native, F_GETFD, 0) | FD_CLOEXEC) == -1)
|
---|
207 | {
|
---|
208 | int e = errno;
|
---|
209 | close((int)native);
|
---|
210 | errno = e;
|
---|
211 | return -1;
|
---|
212 | }
|
---|
213 | # endif
|
---|
214 |
|
---|
215 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Search for a fitting unused location.
|
---|
219 | */
|
---|
220 | fd = -1;
|
---|
221 | for (i = 0; (unsigned)i < pfdtab->size; i++)
|
---|
222 | if ( i >= fdMin
|
---|
223 | && pfdtab->tab[i].fd == -1)
|
---|
224 | {
|
---|
225 | fd = i;
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | if (fd == -1)
|
---|
229 | {
|
---|
230 | /*
|
---|
231 | * Grow the descriptor table.
|
---|
232 | */
|
---|
233 | shfile *new_tab;
|
---|
234 | int new_size = pfdtab->size + SHFILE_GROW;
|
---|
235 | while (new_size < fdMin)
|
---|
236 | new_size += SHFILE_GROW;
|
---|
237 | new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile));
|
---|
238 | if (new_tab)
|
---|
239 | {
|
---|
240 | for (i = pfdtab->size; i < new_size; i++)
|
---|
241 | {
|
---|
242 | new_tab[i].fd = -1;
|
---|
243 | new_tab[i].oflags = 0;
|
---|
244 | new_tab[i].shflags = 0;
|
---|
245 | new_tab[i].native = -1;
|
---|
246 | }
|
---|
247 |
|
---|
248 | fd = pfdtab->size;
|
---|
249 | if (fd < fdMin)
|
---|
250 | fd = fdMin;
|
---|
251 |
|
---|
252 | pfdtab->tab = new_tab;
|
---|
253 | pfdtab->size = new_size;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Fill in the entry if we've found one.
|
---|
259 | */
|
---|
260 | if (fd != -1)
|
---|
261 | {
|
---|
262 | pfdtab->tab[fd].fd = fd;
|
---|
263 | pfdtab->tab[fd].oflags = oflags;
|
---|
264 | pfdtab->tab[fd].shflags = shflags;
|
---|
265 | pfdtab->tab[fd].native = native;
|
---|
266 | }
|
---|
267 | else
|
---|
268 | shfile_native_close(native, oflags);
|
---|
269 |
|
---|
270 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
271 |
|
---|
272 | if (fd == -1)
|
---|
273 | errno = EMFILE;
|
---|
274 | (void)who;
|
---|
275 | return fd;
|
---|
276 | }
|
---|
277 |
|
---|
278 | #if K_OS != K_OS_WINDOWS
|
---|
279 | /**
|
---|
280 | * Makes a copy of the native file, closes the original, and inserts the copy
|
---|
281 | * into the descriptor table.
|
---|
282 | *
|
---|
283 | * If we're out of memory and cannot extend the table, we'll close the
|
---|
284 | * file, set errno to EMFILE and return -1.
|
---|
285 | *
|
---|
286 | * @returns The file descriptor number. -1 and errno on failure.
|
---|
287 | * @param pfdtab The file descriptor table.
|
---|
288 | * @param pnative The native file handle on input, -1 on output.
|
---|
289 | * @param oflags The flags the it was opened/created with.
|
---|
290 | * @param shflags The shell file flags.
|
---|
291 | * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
|
---|
292 | * @param who Who we're doing this for (for logging purposes).
|
---|
293 | */
|
---|
294 | static int shfile_copy_insert_and_close(shfdtab *pfdtab, int *pnative, unsigned oflags, unsigned shflags, int fdMin, const char *who)
|
---|
295 | {
|
---|
296 | int fd = -1;
|
---|
297 | int s = errno;
|
---|
298 | int native_copy = fcntl(*pnative, F_DUPFD, SHFILE_UNIX_MIN_FD);
|
---|
299 | close(*pnative);
|
---|
300 | *pnative = -1;
|
---|
301 | errno = s;
|
---|
302 |
|
---|
303 | if (native_copy != -1)
|
---|
304 | fd = shfile_insert(pfdtab, native_copy, oflags, shflags, fdMin, who);
|
---|
305 | return fd;
|
---|
306 | }
|
---|
307 | #endif /* !K_OS_WINDOWS */
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Gets a file descriptor and lock the file descriptor table.
|
---|
311 | *
|
---|
312 | * @returns Pointer to the file and table ownership on success,
|
---|
313 | * NULL and errno set to EBADF on failure.
|
---|
314 | * @param pfdtab The file descriptor table.
|
---|
315 | * @param fd The file descriptor number.
|
---|
316 | * @param ptmp See shmtx_enter.
|
---|
317 | */
|
---|
318 | static shfile *shfile_get(shfdtab *pfdtab, int fd, shmtxtmp *ptmp)
|
---|
319 | {
|
---|
320 | shfile *file = NULL;
|
---|
321 | if ( fd >= 0
|
---|
322 | && (unsigned)fd < pfdtab->size)
|
---|
323 | {
|
---|
324 | shmtx_enter(&pfdtab->mtx, ptmp);
|
---|
325 | if ((unsigned)fd < pfdtab->size
|
---|
326 | && pfdtab->tab[fd].fd != -1)
|
---|
327 | file = &pfdtab->tab[fd];
|
---|
328 | else
|
---|
329 | shmtx_leave(&pfdtab->mtx, ptmp);
|
---|
330 | }
|
---|
331 | if (!file)
|
---|
332 | errno = EBADF;
|
---|
333 | return file;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Puts back a file descriptor and releases the table ownership.
|
---|
338 | *
|
---|
339 | * @param pfdtab The file descriptor table.
|
---|
340 | * @param file The file.
|
---|
341 | * @param ptmp See shmtx_leave.
|
---|
342 | */
|
---|
343 | static void shfile_put(shfdtab *pfdtab, shfile *file, shmtxtmp *ptmp)
|
---|
344 | {
|
---|
345 | shmtx_leave(&pfdtab->mtx, ptmp);
|
---|
346 | assert(file);
|
---|
347 | (void)file;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Constructs a path from the current directory and the passed in path.
|
---|
352 | *
|
---|
353 | * @returns 0 on success, -1 and errno set to ENAMETOOLONG or EINVAL on failure.
|
---|
354 | *
|
---|
355 | * @param pfdtab The file descriptor table
|
---|
356 | * @param path The path the caller supplied.
|
---|
357 | * @param buf Where to put the path. This is assumed to be SHFILE_MAX_PATH
|
---|
358 | * chars in size.
|
---|
359 | */
|
---|
360 | int shfile_make_path(shfdtab *pfdtab, const char *path, char *buf)
|
---|
361 | {
|
---|
362 | size_t path_len = strlen(path);
|
---|
363 | if (path_len == 0)
|
---|
364 | {
|
---|
365 | errno = EINVAL;
|
---|
366 | return -1;
|
---|
367 | }
|
---|
368 | if (path_len >= SHFILE_MAX_PATH)
|
---|
369 | {
|
---|
370 | errno = ENAMETOOLONG;
|
---|
371 | return -1;
|
---|
372 | }
|
---|
373 | if ( *path == '/'
|
---|
374 | #if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
---|
375 | || *path == '\\'
|
---|
376 | || ( *path
|
---|
377 | && path[1] == ':'
|
---|
378 | && ( (*path >= 'A' && *path <= 'Z')
|
---|
379 | || (*path >= 'a' && *path <= 'z')))
|
---|
380 | #endif
|
---|
381 | )
|
---|
382 | {
|
---|
383 | memcpy(buf, path, path_len + 1);
|
---|
384 | }
|
---|
385 | else
|
---|
386 | {
|
---|
387 | size_t cwd_len;
|
---|
388 | shmtxtmp tmp;
|
---|
389 |
|
---|
390 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
391 |
|
---|
392 | cwd_len = strlen(pfdtab->cwd);
|
---|
393 | memcpy(buf, pfdtab->cwd, cwd_len);
|
---|
394 |
|
---|
395 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
396 |
|
---|
397 | if (cwd_len + path_len + 1 >= SHFILE_MAX_PATH)
|
---|
398 | {
|
---|
399 | errno = ENAMETOOLONG;
|
---|
400 | return -1;
|
---|
401 | }
|
---|
402 | if ( !cwd_len
|
---|
403 | || buf[cwd_len - 1] != '/')
|
---|
404 | buf[cwd_len++] = '/';
|
---|
405 | memcpy(buf + cwd_len, path, path_len + 1);
|
---|
406 | }
|
---|
407 |
|
---|
408 | #if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
---|
409 | if (!strcmp(buf, "/dev/null"))
|
---|
410 | strcpy(buf, "NUL");
|
---|
411 | #endif
|
---|
412 | return 0;
|
---|
413 | }
|
---|
414 |
|
---|
415 | # if K_OS == K_OS_WINDOWS
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Converts a DOS(/Windows) error code to errno,
|
---|
419 | * assigning it to errno.
|
---|
420 | *
|
---|
421 | * @returns -1
|
---|
422 | * @param err The DOS error.
|
---|
423 | */
|
---|
424 | static int shfile_dos2errno(int err)
|
---|
425 | {
|
---|
426 | switch (err)
|
---|
427 | {
|
---|
428 | case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
|
---|
429 | case ERROR_ACCESS_DENIED: errno = EACCES; break;
|
---|
430 | case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
|
---|
431 | case ERROR_LOCK_VIOLATION: errno = EACCES; break;
|
---|
432 | case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
|
---|
433 | case ERROR_CANNOT_MAKE: errno = EACCES; break;
|
---|
434 | case ERROR_FAIL_I24: errno = EACCES; break;
|
---|
435 | case ERROR_DRIVE_LOCKED: errno = EACCES; break;
|
---|
436 | case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
|
---|
437 | case ERROR_NOT_LOCKED: errno = EACCES; break;
|
---|
438 | case ERROR_LOCK_FAILED: errno = EACCES; break;
|
---|
439 | case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
|
---|
440 | case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
|
---|
441 | case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
|
---|
442 | case ERROR_INVALID_HANDLE: errno = EBADF; break;
|
---|
443 | case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
|
---|
444 | case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
|
---|
445 | case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
|
---|
446 | case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
|
---|
447 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
---|
448 | case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
|
---|
449 | case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
|
---|
450 | case ERROR_INVALID_ACCESS: errno = EINVAL; break;
|
---|
451 | case ERROR_INVALID_DATA: errno = EINVAL; break;
|
---|
452 | case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
|
---|
453 | case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
|
---|
454 | case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
|
---|
455 | case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
|
---|
456 | case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
|
---|
457 | case ERROR_INVALID_DRIVE: errno = ENOENT; break;
|
---|
458 | case ERROR_NO_MORE_FILES: errno = ENOENT; break;
|
---|
459 | case ERROR_BAD_NETPATH: errno = ENOENT; break;
|
---|
460 | case ERROR_BAD_NET_NAME: errno = ENOENT; break;
|
---|
461 | case ERROR_BAD_PATHNAME: errno = ENOENT; break;
|
---|
462 | case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
|
---|
463 | case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
|
---|
464 | case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
|
---|
465 | case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
|
---|
466 | case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
|
---|
467 | case ERROR_NOT_ENOUGH_QUOTA: errno = ENOMEM; break;
|
---|
468 | case ERROR_DISK_FULL: errno = ENOSPC; break;
|
---|
469 | case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
|
---|
470 | case ERROR_BROKEN_PIPE: errno = EPIPE; break;
|
---|
471 | case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
|
---|
472 | default: errno = EINVAL; break;
|
---|
473 | }
|
---|
474 | return -1;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Converts an NT status code to errno,
|
---|
479 | * assigning it to errno.
|
---|
480 | *
|
---|
481 | * @returns -1
|
---|
482 | * @param rcNt The NT status code.
|
---|
483 | */
|
---|
484 | static int shfile_nt2errno(NTSTATUS rcNt)
|
---|
485 | {
|
---|
486 | switch (rcNt)
|
---|
487 | {
|
---|
488 | default: errno = EINVAL; break;
|
---|
489 | }
|
---|
490 | return -1;
|
---|
491 | }
|
---|
492 |
|
---|
493 | DWORD shfile_query_handle_access_mask(HANDLE h, PACCESS_MASK pMask)
|
---|
494 | {
|
---|
495 | MY_OBJECT_BASIC_INFORMATION BasicInfo;
|
---|
496 | NTSTATUS rcNt;
|
---|
497 |
|
---|
498 | if (!g_pfnNtQueryObject)
|
---|
499 | return ERROR_NOT_SUPPORTED;
|
---|
500 |
|
---|
501 | rcNt = g_pfnNtQueryObject(h, MY_ObjectBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
|
---|
502 | if (rcNt >= 0)
|
---|
503 | {
|
---|
504 | *pMask = BasicInfo.GrantedAccess;
|
---|
505 | return NO_ERROR;
|
---|
506 | }
|
---|
507 | if (rcNt != STATUS_INVALID_HANDLE)
|
---|
508 | return ERROR_GEN_FAILURE;
|
---|
509 | return ERROR_INVALID_HANDLE;
|
---|
510 | }
|
---|
511 |
|
---|
512 | # endif /* K_OS == K_OS_WINDOWS */
|
---|
513 |
|
---|
514 | #endif /* SHFILE_IN_USE */
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * Initializes the global variables in this file.
|
---|
518 | */
|
---|
519 | static void shfile_init_globals(void)
|
---|
520 | {
|
---|
521 | #if K_OS == K_OS_WINDOWS
|
---|
522 | if (!g_shfile_globals_initialized)
|
---|
523 | {
|
---|
524 | HMODULE hNtDll = GetModuleHandle("NTDLL");
|
---|
525 | g_pfnNtQueryObject = (PFN_NtQueryObject) GetProcAddress(hNtDll, "NtQueryObject");
|
---|
526 | g_pfnNtQueryDirectoryFile = (PFN_NtQueryDirectoryFile)GetProcAddress(hNtDll, "NtQueryDirectoryFile");
|
---|
527 | g_pfnRtlUnicodeStringToAnsiString = (PFN_RtlUnicodeStringToAnsiString)GetProcAddress(hNtDll, "RtlUnicodeStringToAnsiString");
|
---|
528 | if ( !g_pfnRtlUnicodeStringToAnsiString
|
---|
529 | || !g_pfnNtQueryDirectoryFile)
|
---|
530 | {
|
---|
531 | /* fatal error */
|
---|
532 | }
|
---|
533 | g_shfile_globals_initialized = 1;
|
---|
534 | }
|
---|
535 | #endif
|
---|
536 | }
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Initializes a file descriptor table.
|
---|
540 | *
|
---|
541 | * @returns 0 on success, -1 and errno on failure.
|
---|
542 | * @param pfdtab The table to initialize.
|
---|
543 | * @param inherit File descriptor table to inherit from. If not specified
|
---|
544 | * we will inherit from the current process as it were.
|
---|
545 | */
|
---|
546 | int shfile_init(shfdtab *pfdtab, shfdtab *inherit)
|
---|
547 | {
|
---|
548 | int rc;
|
---|
549 |
|
---|
550 | shfile_init_globals();
|
---|
551 |
|
---|
552 | pfdtab->cwd = NULL;
|
---|
553 | pfdtab->size = 0;
|
---|
554 | pfdtab->tab = NULL;
|
---|
555 | rc = shmtx_init(&pfdtab->mtx);
|
---|
556 | if (!rc)
|
---|
557 | {
|
---|
558 | #ifdef SHFILE_IN_USE
|
---|
559 | char buf[SHFILE_MAX_PATH];
|
---|
560 | if (getcwd(buf, sizeof(buf)))
|
---|
561 | {
|
---|
562 | pfdtab->cwd = sh_strdup(NULL, buf);
|
---|
563 | if (!inherit)
|
---|
564 | {
|
---|
565 | # if K_OS == K_OS_WINDOWS
|
---|
566 | static const struct
|
---|
567 | {
|
---|
568 | DWORD dwStdHandle;
|
---|
569 | unsigned fFlags;
|
---|
570 | } aStdHandles[3] =
|
---|
571 | {
|
---|
572 | { STD_INPUT_HANDLE, _O_RDONLY },
|
---|
573 | { STD_OUTPUT_HANDLE, _O_WRONLY },
|
---|
574 | { STD_ERROR_HANDLE, _O_WRONLY }
|
---|
575 | };
|
---|
576 | int i;
|
---|
577 | STARTUPINFO Info;
|
---|
578 | ACCESS_MASK Mask;
|
---|
579 | DWORD dwErr;
|
---|
580 |
|
---|
581 | rc = 0;
|
---|
582 |
|
---|
583 | /* Try pick up the Visual C++ CRT file descriptor info. */
|
---|
584 | __try {
|
---|
585 | GetStartupInfo(&Info);
|
---|
586 | } __except (EXCEPTION_EXECUTE_HANDLER) {
|
---|
587 | memset(&Info, 0, sizeof(Info));
|
---|
588 | }
|
---|
589 |
|
---|
590 | if ( Info.cbReserved2 > sizeof(int)
|
---|
591 | && (uintptr_t)Info.lpReserved2 >= 0x1000
|
---|
592 | && (i = *(int *)Info.lpReserved2) >= 1
|
---|
593 | && i <= 2048
|
---|
594 | && ( Info.cbReserved2 == i * 5 + 4
|
---|
595 | //|| Info.cbReserved2 == i * 5 + 1 - check the cygwin sources.
|
---|
596 | || Info.cbReserved2 == i * 9 + 4))
|
---|
597 | {
|
---|
598 | uint8_t *paf = (uint8_t *)Info.lpReserved2 + sizeof(int);
|
---|
599 | int dwPerH = 1 + (Info.cbReserved2 == i * 9 + 4);
|
---|
600 | DWORD *ph = (DWORD *)(paf + i) + dwPerH * i;
|
---|
601 | HANDLE aStdHandles2[3];
|
---|
602 | int j;
|
---|
603 |
|
---|
604 | //if (Info.cbReserved2 == i * 5 + 1) - check the cygwin sources.
|
---|
605 | // i--;
|
---|
606 |
|
---|
607 | for (j = 0; j < 3; j++)
|
---|
608 | aStdHandles2[j] = GetStdHandle(aStdHandles[j].dwStdHandle);
|
---|
609 |
|
---|
610 | while (i-- > 0)
|
---|
611 | {
|
---|
612 | ph -= dwPerH;
|
---|
613 |
|
---|
614 | if ( (paf[i] & (FOPEN | FNOINHERIT)) == FOPEN
|
---|
615 | && *ph != (uint32_t)INVALID_HANDLE_VALUE)
|
---|
616 | {
|
---|
617 | HANDLE h = (HANDLE)(intptr_t)*ph;
|
---|
618 | int fd2;
|
---|
619 | int fFlags;
|
---|
620 | int fFlags2;
|
---|
621 |
|
---|
622 | if ( h == aStdHandles2[j = 0]
|
---|
623 | || h == aStdHandles2[j = 1]
|
---|
624 | || h == aStdHandles2[j = 2])
|
---|
625 | fFlags = aStdHandles[j].fFlags;
|
---|
626 | else
|
---|
627 | {
|
---|
628 | dwErr = shfile_query_handle_access_mask(h, &Mask);
|
---|
629 | if (dwErr == ERROR_INVALID_HANDLE)
|
---|
630 | continue;
|
---|
631 | else if (dwErr == NO_ERROR)
|
---|
632 | {
|
---|
633 | fFlags = 0;
|
---|
634 | if ( (Mask & (GENERIC_READ | FILE_READ_DATA))
|
---|
635 | && (Mask & (GENERIC_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA)))
|
---|
636 | fFlags |= O_RDWR;
|
---|
637 | else if (Mask & (GENERIC_READ | FILE_READ_DATA))
|
---|
638 | fFlags |= O_RDONLY;
|
---|
639 | else if (Mask & (GENERIC_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA))
|
---|
640 | fFlags |= O_WRONLY;
|
---|
641 | else
|
---|
642 | fFlags |= O_RDWR;
|
---|
643 | if ((Mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) == FILE_APPEND_DATA)
|
---|
644 | fFlags |= O_APPEND;
|
---|
645 | }
|
---|
646 | else
|
---|
647 | fFlags = O_RDWR;
|
---|
648 | }
|
---|
649 |
|
---|
650 | if (paf[i] & FPIPE)
|
---|
651 | fFlags2 = SHFILE_FLAGS_PIPE;
|
---|
652 | else if (paf[i] & FDEV)
|
---|
653 | fFlags2 = SHFILE_FLAGS_TTY;
|
---|
654 | else
|
---|
655 | fFlags2 = 0;
|
---|
656 |
|
---|
657 | fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init");
|
---|
658 | assert(fd2 == i); (void)fd2;
|
---|
659 | if (fd2 != i)
|
---|
660 | rc = -1;
|
---|
661 | }
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | /* Check the three standard handles. */
|
---|
666 | for (i = 0; i < 3; i++)
|
---|
667 | if ( (unsigned)i >= pfdtab->size
|
---|
668 | || pfdtab->tab[i].fd == -1)
|
---|
669 | {
|
---|
670 | HANDLE hFile = GetStdHandle(aStdHandles[i].dwStdHandle);
|
---|
671 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
672 | {
|
---|
673 | DWORD dwType = GetFileType(hFile);
|
---|
674 | unsigned fFlags = aStdHandles[i].fFlags;
|
---|
675 | unsigned fFlags2;
|
---|
676 | int fd2;
|
---|
677 | if (dwType == FILE_TYPE_CHAR)
|
---|
678 | fFlags2 = SHFILE_FLAGS_TTY;
|
---|
679 | else if (dwType == FILE_TYPE_PIPE)
|
---|
680 | fFlags2 = SHFILE_FLAGS_PIPE;
|
---|
681 | else
|
---|
682 | fFlags2 = SHFILE_FLAGS_FILE;
|
---|
683 | fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init");
|
---|
684 | assert(fd2 == i); (void)fd2;
|
---|
685 | if (fd2 != i)
|
---|
686 | rc = -1;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | # else
|
---|
690 | /*
|
---|
691 | * Annoying...
|
---|
692 | */
|
---|
693 | int fd;
|
---|
694 |
|
---|
695 | for (fd = 0; fd < 10; fd++)
|
---|
696 | {
|
---|
697 | int oflags = fcntl(fd, F_GETFL, 0);
|
---|
698 | if (oflags != -1)
|
---|
699 | {
|
---|
700 | int cox = fcntl(fd, F_GETFD, 0);
|
---|
701 | struct stat st;
|
---|
702 | if ( cox != -1
|
---|
703 | && fstat(fd, &st) != -1)
|
---|
704 | {
|
---|
705 | int native;
|
---|
706 | int fd2;
|
---|
707 | int fFlags2 = 0;
|
---|
708 | if (cox & FD_CLOEXEC)
|
---|
709 | fFlags2 |= SHFILE_FLAGS_CLOSE_ON_EXEC;
|
---|
710 | if (S_ISREG(st.st_mode))
|
---|
711 | fFlags2 |= SHFILE_FLAGS_FILE;
|
---|
712 | else if (S_ISDIR(st.st_mode))
|
---|
713 | fFlags2 |= SHFILE_FLAGS_DIR;
|
---|
714 | else if (S_ISCHR(st.st_mode))
|
---|
715 | fFlags2 |= SHFILE_FLAGS_TTY;
|
---|
716 | else if (S_ISFIFO(st.st_mode))
|
---|
717 | fFlags2 |= SHFILE_FLAGS_PIPE;
|
---|
718 | else
|
---|
719 | fFlags2 |= SHFILE_FLAGS_TTY;
|
---|
720 |
|
---|
721 | native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD);
|
---|
722 | if (native == -1)
|
---|
723 | native = fd;
|
---|
724 | fd2 = shfile_insert(pfdtab, native, oflags, fFlags2, fd, "shtab_init");
|
---|
725 | assert(fd2 == fd); (void)fd2;
|
---|
726 | if (fd2 != fd)
|
---|
727 | rc = -1;
|
---|
728 | if (native != fd)
|
---|
729 | close(fd);
|
---|
730 | }
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | # endif
|
---|
735 | }
|
---|
736 | else
|
---|
737 | {
|
---|
738 | /** @todo */
|
---|
739 | errno = ENOSYS;
|
---|
740 | rc = -1;
|
---|
741 | }
|
---|
742 | }
|
---|
743 | else
|
---|
744 | rc = -1;
|
---|
745 | #endif
|
---|
746 | }
|
---|
747 | return rc;
|
---|
748 | }
|
---|
749 |
|
---|
750 | #if K_OS == K_OS_WINDOWS && defined(SHFILE_IN_USE)
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Helper for shfork.
|
---|
754 | *
|
---|
755 | * @param pfdtab The file descriptor table.
|
---|
756 | * @param set Whether to make all handles inheritable (1) or
|
---|
757 | * to restore them to the rigth state (0).
|
---|
758 | * @param hndls Where to store the three standard handles.
|
---|
759 | */
|
---|
760 | void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls)
|
---|
761 | {
|
---|
762 | shmtxtmp tmp;
|
---|
763 | unsigned i;
|
---|
764 | DWORD fFlag = set ? HANDLE_FLAG_INHERIT : 0;
|
---|
765 |
|
---|
766 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
767 | TRACE2((NULL, "shfile_fork_win: set=%d\n", set));
|
---|
768 |
|
---|
769 | i = pfdtab->size;
|
---|
770 | while (i-- > 0)
|
---|
771 | {
|
---|
772 | if (pfdtab->tab[i].fd == i)
|
---|
773 | {
|
---|
774 | HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
|
---|
775 | if (set)
|
---|
776 | TRACE2((NULL, " #%d: native=%#x oflags=%#x shflags=%#x\n",
|
---|
777 | i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
|
---|
778 | if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
|
---|
779 | {
|
---|
780 | DWORD err = GetLastError();
|
---|
781 | assert(0);
|
---|
782 | }
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 | if (hndls)
|
---|
787 | {
|
---|
788 | for (i = 0; i < 3; i++)
|
---|
789 | {
|
---|
790 | if ( pfdtab->size > i
|
---|
791 | && pfdtab->tab[i].fd == i)
|
---|
792 | hndls[i] = pfdtab->tab[i].native;
|
---|
793 | else
|
---|
794 | hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
|
---|
795 | TRACE2((NULL, "shfile_fork_win: i=%d size=%d fd=%d native=%d hndls[%d]=%p\n",
|
---|
796 | i, pfdtab->size, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
|
---|
797 | }
|
---|
798 | }
|
---|
799 |
|
---|
800 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
801 | }
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Helper for sh_execve.
|
---|
805 | *
|
---|
806 | * This is called before and after CreateProcess. On the first call it
|
---|
807 | * will mark the non-close-on-exec handles as inheritable and produce
|
---|
808 | * the startup info for the CRT. On the second call, after CreateProcess,
|
---|
809 | * it will restore the handle inheritability properties.
|
---|
810 | *
|
---|
811 | * @returns Pointer to CRT data if prepare is 1, NULL if prepare is 0.
|
---|
812 | * @param pfdtab The file descriptor table.
|
---|
813 | * @param prepare Which call, 1 if before and 0 if after.
|
---|
814 | * @param sizep Where to store the size of the returned data.
|
---|
815 | * @param hndls Where to store the three standard handles.
|
---|
816 | */
|
---|
817 | void *shfile_exec_win(shfdtab *pfdtab, int prepare, unsigned short *sizep, intptr_t *hndls)
|
---|
818 | {
|
---|
819 | void *pvRet;
|
---|
820 | shmtxtmp tmp;
|
---|
821 | unsigned count;
|
---|
822 | unsigned i;
|
---|
823 |
|
---|
824 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
825 | TRACE2((NULL, "shfile_exec_win: prepare=%p\n", prepare));
|
---|
826 |
|
---|
827 | count = pfdtab->size < (0x10000-4) / (1 + sizeof(HANDLE))
|
---|
828 | ? pfdtab->size
|
---|
829 | : (0x10000-4) / (1 + sizeof(HANDLE));
|
---|
830 | while (count > 3 && pfdtab->tab[count - 1].fd == -1)
|
---|
831 | count--;
|
---|
832 |
|
---|
833 | if (prepare)
|
---|
834 | {
|
---|
835 | size_t cbData = sizeof(int) + count * (1 + sizeof(HANDLE));
|
---|
836 | uint8_t *pbData = sh_malloc(shthread_get_shell(), cbData);
|
---|
837 | uint8_t *paf = pbData + sizeof(int);
|
---|
838 | HANDLE *pah = (HANDLE *)(paf + count);
|
---|
839 |
|
---|
840 | *(int *)pbData = count;
|
---|
841 |
|
---|
842 | i = count;
|
---|
843 | while (i-- > 0)
|
---|
844 | {
|
---|
845 | if ( pfdtab->tab[i].fd == i
|
---|
846 | && !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
|
---|
847 | {
|
---|
848 | HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
|
---|
849 | TRACE2((NULL, " #%d: native=%#x oflags=%#x shflags=%#x\n",
|
---|
850 | i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
|
---|
851 |
|
---|
852 | if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
|
---|
853 | {
|
---|
854 | DWORD err = GetLastError();
|
---|
855 | assert(0);
|
---|
856 | }
|
---|
857 | paf[i] = FOPEN;
|
---|
858 | if (pfdtab->tab[i].oflags & _O_APPEND)
|
---|
859 | paf[i] |= FAPPEND;
|
---|
860 | if (pfdtab->tab[i].oflags & _O_TEXT)
|
---|
861 | paf[i] |= FTEXT;
|
---|
862 | switch (pfdtab->tab[i].shflags & SHFILE_FLAGS_TYPE_MASK)
|
---|
863 | {
|
---|
864 | case SHFILE_FLAGS_TTY: paf[i] |= FDEV; break;
|
---|
865 | case SHFILE_FLAGS_PIPE: paf[i] |= FPIPE; break;
|
---|
866 | }
|
---|
867 | pah[i] = hFile;
|
---|
868 | }
|
---|
869 | else
|
---|
870 | {
|
---|
871 | paf[i] = 0;
|
---|
872 | pah[i] = INVALID_HANDLE_VALUE;
|
---|
873 | }
|
---|
874 | }
|
---|
875 |
|
---|
876 | for (i = 0; i < 3; i++)
|
---|
877 | {
|
---|
878 | if ( i < count
|
---|
879 | && pfdtab->tab[i].fd == i)
|
---|
880 | hndls[i] = pfdtab->tab[i].native;
|
---|
881 | else
|
---|
882 | hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
|
---|
883 | TRACE2((NULL, "shfile_exec_win: i=%d count=%d fd=%d native=%d hndls[%d]=\n",
|
---|
884 | i, count, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
|
---|
885 | }
|
---|
886 |
|
---|
887 | *sizep = (unsigned short)cbData;
|
---|
888 | pvRet = pbData;
|
---|
889 | }
|
---|
890 | else
|
---|
891 | {
|
---|
892 | assert(!hndls);
|
---|
893 | assert(!sizep);
|
---|
894 | i = count;
|
---|
895 | while (i-- > 0)
|
---|
896 | {
|
---|
897 | if ( pfdtab->tab[i].fd == i
|
---|
898 | && !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
|
---|
899 | {
|
---|
900 | HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
|
---|
901 | if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, 0))
|
---|
902 | {
|
---|
903 | DWORD err = GetLastError();
|
---|
904 | assert(0);
|
---|
905 | }
|
---|
906 | }
|
---|
907 | }
|
---|
908 | pvRet = NULL;
|
---|
909 | }
|
---|
910 |
|
---|
911 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
912 | return pvRet;
|
---|
913 | }
|
---|
914 |
|
---|
915 | #endif /* K_OS_WINDOWS */
|
---|
916 |
|
---|
917 | #if K_OS != K_OS_WINDOWS
|
---|
918 | /**
|
---|
919 | * Prepare file handles for inherting before a execve call.
|
---|
920 | *
|
---|
921 | * This is only used in the normal mode, so we've forked and need not worry
|
---|
922 | * about cleaning anything up after us. Nor do we need think about locking.
|
---|
923 | *
|
---|
924 | * @returns 0 on success, -1 on failure.
|
---|
925 | */
|
---|
926 | int shfile_exec_unix(shfdtab *pfdtab)
|
---|
927 | {
|
---|
928 | int rc = 0;
|
---|
929 | # ifdef SHFILE_IN_USE
|
---|
930 | unsigned fd;
|
---|
931 |
|
---|
932 | for (fd = 0; fd < pfdtab->size; fd++)
|
---|
933 | {
|
---|
934 | if ( pfdtab->tab[fd].fd != -1
|
---|
935 | && !(pfdtab->tab[fd].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC) )
|
---|
936 | {
|
---|
937 | TRACE2((NULL, "shfile_exec_unix: %d => %d\n", pfdtab->tab[fd].native, fd));
|
---|
938 | if (dup2(pfdtab->tab[fd].native, fd) < 0)
|
---|
939 | {
|
---|
940 | /* fatal_error(NULL, "shfile_exec_unix: failed to move %d to %d", pfdtab->tab[fd].fd, fd); */
|
---|
941 | rc = -1;
|
---|
942 | }
|
---|
943 | }
|
---|
944 | }
|
---|
945 | # endif
|
---|
946 | return rc;
|
---|
947 | }
|
---|
948 | #endif /* !K_OS_WINDOWS */
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * open().
|
---|
952 | */
|
---|
953 | int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
|
---|
954 | {
|
---|
955 | int fd;
|
---|
956 | #ifdef SHFILE_IN_USE
|
---|
957 | char absname[SHFILE_MAX_PATH];
|
---|
958 | # if K_OS == K_OS_WINDOWS
|
---|
959 | HANDLE hFile;
|
---|
960 | DWORD dwDesiredAccess;
|
---|
961 | DWORD dwShareMode;
|
---|
962 | DWORD dwCreationDisposition;
|
---|
963 | DWORD dwFlagsAndAttributes;
|
---|
964 | SECURITY_ATTRIBUTES SecurityAttributes;
|
---|
965 |
|
---|
966 | # ifndef _O_ACCMODE
|
---|
967 | # define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
|
---|
968 | # endif
|
---|
969 | switch (flags & (_O_ACCMODE | _O_APPEND))
|
---|
970 | {
|
---|
971 | case _O_RDONLY: dwDesiredAccess = GENERIC_READ; break;
|
---|
972 | case _O_RDONLY | _O_APPEND: dwDesiredAccess = GENERIC_READ; break;
|
---|
973 | case _O_WRONLY: dwDesiredAccess = GENERIC_WRITE; break;
|
---|
974 | case _O_WRONLY | _O_APPEND: dwDesiredAccess = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
|
---|
975 | case _O_RDWR: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
|
---|
976 | case _O_RDWR | _O_APPEND: dwDesiredAccess = GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
|
---|
977 |
|
---|
978 | default:
|
---|
979 | RETURN_ERROR(-1, EINVAL, "invalid mode");
|
---|
980 | }
|
---|
981 |
|
---|
982 | dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
|
---|
983 |
|
---|
984 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
---|
985 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
---|
986 | SecurityAttributes.bInheritHandle = FALSE;
|
---|
987 |
|
---|
988 | if (flags & _O_CREAT)
|
---|
989 | {
|
---|
990 | if ((flags & (_O_EXCL | _O_TRUNC)) == (_O_EXCL | _O_TRUNC))
|
---|
991 | RETURN_ERROR(-1, EINVAL, "_O_EXCL | _O_TRUNC");
|
---|
992 |
|
---|
993 | if (flags & _O_TRUNC)
|
---|
994 | dwCreationDisposition = CREATE_ALWAYS; /* not 100%, but close enough */
|
---|
995 | else if (flags & _O_EXCL)
|
---|
996 | dwCreationDisposition = CREATE_NEW;
|
---|
997 | else
|
---|
998 | dwCreationDisposition = OPEN_ALWAYS;
|
---|
999 | }
|
---|
1000 | else if (flags & _O_TRUNC)
|
---|
1001 | dwCreationDisposition = TRUNCATE_EXISTING;
|
---|
1002 | else
|
---|
1003 | dwCreationDisposition = OPEN_EXISTING;
|
---|
1004 |
|
---|
1005 | if (!(flags & _O_CREAT) || (mode & 0222))
|
---|
1006 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
|
---|
1007 | else
|
---|
1008 | dwFlagsAndAttributes = FILE_ATTRIBUTE_READONLY;
|
---|
1009 |
|
---|
1010 | fd = shfile_make_path(pfdtab, name, &absname[0]);
|
---|
1011 | if (!fd)
|
---|
1012 | {
|
---|
1013 | SetLastError(0);
|
---|
1014 | hFile = CreateFileA(absname,
|
---|
1015 | dwDesiredAccess,
|
---|
1016 | dwShareMode,
|
---|
1017 | &SecurityAttributes,
|
---|
1018 | dwCreationDisposition,
|
---|
1019 | dwFlagsAndAttributes,
|
---|
1020 | NULL /* hTemplateFile */);
|
---|
1021 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
1022 | fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open");
|
---|
1023 | else
|
---|
1024 | fd = shfile_dos2errno(GetLastError());
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | # else /* K_OS != K_OS_WINDOWS */
|
---|
1028 | fd = shfile_make_path(pfdtab, name, &absname[0]);
|
---|
1029 | if (!fd)
|
---|
1030 | {
|
---|
1031 | fd = open(absname, flags, mode);
|
---|
1032 | if (fd != -1)
|
---|
1033 | fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open");
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | # endif /* K_OS != K_OS_WINDOWS */
|
---|
1037 |
|
---|
1038 | #else
|
---|
1039 | fd = open(name, flags, mode);
|
---|
1040 | #endif
|
---|
1041 |
|
---|
1042 | TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
|
---|
1043 | return fd;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | int shfile_pipe(shfdtab *pfdtab, int fds[2])
|
---|
1047 | {
|
---|
1048 | int rc = -1;
|
---|
1049 | #ifdef SHFILE_IN_USE
|
---|
1050 | # if K_OS == K_OS_WINDOWS
|
---|
1051 | HANDLE hRead = INVALID_HANDLE_VALUE;
|
---|
1052 | HANDLE hWrite = INVALID_HANDLE_VALUE;
|
---|
1053 | SECURITY_ATTRIBUTES SecurityAttributes;
|
---|
1054 |
|
---|
1055 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
---|
1056 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
---|
1057 | SecurityAttributes.bInheritHandle = FALSE;
|
---|
1058 |
|
---|
1059 | fds[1] = fds[0] = -1;
|
---|
1060 | if (CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096))
|
---|
1061 | {
|
---|
1062 | fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
---|
1063 | if (fds[0] != -1)
|
---|
1064 | {
|
---|
1065 | fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
---|
1066 | if (fds[1] != -1)
|
---|
1067 | rc = 0;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | # else
|
---|
1071 | int native_fds[2];
|
---|
1072 |
|
---|
1073 | fds[1] = fds[0] = -1;
|
---|
1074 | if (!pipe(native_fds))
|
---|
1075 | {
|
---|
1076 | fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
---|
1077 | if (fds[0] != -1)
|
---|
1078 | {
|
---|
1079 | fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
---|
1080 | if (fds[1] != -1)
|
---|
1081 | rc = 0;
|
---|
1082 | }
|
---|
1083 | # endif
|
---|
1084 | if (fds[1] == -1)
|
---|
1085 | {
|
---|
1086 | int s = errno;
|
---|
1087 | if (fds[0] != -1)
|
---|
1088 | {
|
---|
1089 | shmtxtmp tmp;
|
---|
1090 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
1091 | rc = fds[0];
|
---|
1092 | pfdtab->tab[rc].fd = -1;
|
---|
1093 | pfdtab->tab[rc].oflags = 0;
|
---|
1094 | pfdtab->tab[rc].shflags = 0;
|
---|
1095 | pfdtab->tab[rc].native = -1;
|
---|
1096 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | # if K_OS == K_OS_WINDOWS
|
---|
1100 | CloseHandle(hRead);
|
---|
1101 | CloseHandle(hWrite);
|
---|
1102 | # else
|
---|
1103 | close(native_fds[0]);
|
---|
1104 | close(native_fds[1]);
|
---|
1105 | # endif
|
---|
1106 | fds[0] = fds[1] = -1;
|
---|
1107 | errno = s;
|
---|
1108 | rc = -1;
|
---|
1109 | }
|
---|
1110 | }
|
---|
1111 | else
|
---|
1112 | {
|
---|
1113 | # if K_OS == K_OS_WINDOWS
|
---|
1114 | errno = shfile_dos2errno(GetLastError());
|
---|
1115 | # endif
|
---|
1116 | rc = -1;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | #else
|
---|
1120 | rc = pipe(fds);
|
---|
1121 | #endif
|
---|
1122 |
|
---|
1123 | TRACE2((NULL, "shfile_pipe() -> %d{%d,%d} [%d]\n", rc, fds[0], fds[1], errno));
|
---|
1124 | return rc;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | /**
|
---|
1128 | * dup().
|
---|
1129 | */
|
---|
1130 | int shfile_dup(shfdtab *pfdtab, int fd)
|
---|
1131 | {
|
---|
1132 | return shfile_fcntl(pfdtab,fd, F_DUPFD, 0);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | /**
|
---|
1136 | * close().
|
---|
1137 | */
|
---|
1138 | int shfile_close(shfdtab *pfdtab, unsigned fd)
|
---|
1139 | {
|
---|
1140 | int rc;
|
---|
1141 | #ifdef SHFILE_IN_USE
|
---|
1142 | shmtxtmp tmp;
|
---|
1143 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1144 | if (file)
|
---|
1145 | {
|
---|
1146 | shfile_native_close(file->native, file->oflags);
|
---|
1147 |
|
---|
1148 | file->fd = -1;
|
---|
1149 | file->oflags = 0;
|
---|
1150 | file->shflags = 0;
|
---|
1151 | file->native = -1;
|
---|
1152 |
|
---|
1153 | shfile_put(pfdtab, file, &tmp);
|
---|
1154 | rc = 0;
|
---|
1155 | }
|
---|
1156 | else
|
---|
1157 | rc = -1;
|
---|
1158 |
|
---|
1159 | #else
|
---|
1160 | fsync(fd);
|
---|
1161 | {
|
---|
1162 | struct stat s;
|
---|
1163 | fstat(fd, &s);
|
---|
1164 | TRACE2((NULL, "shfile_close(%d) - %lu bytes\n", fd, (long)s.st_size));
|
---|
1165 | }
|
---|
1166 | rc = close(fd);
|
---|
1167 | #endif
|
---|
1168 |
|
---|
1169 | TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
1170 | return rc;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /**
|
---|
1174 | * read().
|
---|
1175 | */
|
---|
1176 | long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
|
---|
1177 | {
|
---|
1178 | long rc;
|
---|
1179 | #ifdef SHFILE_IN_USE
|
---|
1180 | shmtxtmp tmp;
|
---|
1181 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1182 | if (file)
|
---|
1183 | {
|
---|
1184 | # if K_OS == K_OS_WINDOWS
|
---|
1185 | DWORD dwRead = 0;
|
---|
1186 | if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
|
---|
1187 | rc = dwRead;
|
---|
1188 | else
|
---|
1189 | rc = shfile_dos2errno(GetLastError());
|
---|
1190 | # else
|
---|
1191 | rc = read(file->native, buf, len);
|
---|
1192 | # endif
|
---|
1193 |
|
---|
1194 | shfile_put(pfdtab, file, &tmp);
|
---|
1195 | }
|
---|
1196 | else
|
---|
1197 | rc = -1;
|
---|
1198 |
|
---|
1199 | #else
|
---|
1200 | rc = read(fd, buf, len);
|
---|
1201 | #endif
|
---|
1202 | return rc;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | /**
|
---|
1206 | * write().
|
---|
1207 | */
|
---|
1208 | long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
|
---|
1209 | {
|
---|
1210 | long rc;
|
---|
1211 | #ifdef SHFILE_IN_USE
|
---|
1212 | shmtxtmp tmp;
|
---|
1213 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1214 | if (file)
|
---|
1215 | {
|
---|
1216 | # if K_OS == K_OS_WINDOWS
|
---|
1217 | DWORD dwWritten = 0;
|
---|
1218 | if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
|
---|
1219 | rc = dwWritten;
|
---|
1220 | else
|
---|
1221 | rc = shfile_dos2errno(GetLastError());
|
---|
1222 | # else
|
---|
1223 | rc = write(file->native, buf, len);
|
---|
1224 | # endif
|
---|
1225 |
|
---|
1226 | shfile_put(pfdtab, file, &tmp);
|
---|
1227 | }
|
---|
1228 | else
|
---|
1229 | rc = -1;
|
---|
1230 |
|
---|
1231 | #else
|
---|
1232 | if (fd != shthread_get_shell()->tracefd)
|
---|
1233 | {
|
---|
1234 | struct stat s;
|
---|
1235 | int x;
|
---|
1236 | x = fstat(fd, &s);
|
---|
1237 | TRACE2((NULL, "shfile_write(%d) - %lu bytes (%d) - pos %lu - before; %o\n",
|
---|
1238 | fd, (long)s.st_size, x, (long)lseek(fd, 0, SEEK_CUR), s.st_mode ));
|
---|
1239 | errno = 0;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | rc = write(fd, buf, len);
|
---|
1243 | #endif
|
---|
1244 |
|
---|
1245 | #ifdef DEBUG
|
---|
1246 | if (fd != shthread_get_shell()->tracefd)
|
---|
1247 | {
|
---|
1248 | struct stat s;
|
---|
1249 | int x;
|
---|
1250 | TRACE2((NULL, "shfile_write(%d,,%d) -> %d [%d]\n", fd, len, rc, errno));
|
---|
1251 | x=fstat(fd, &s);
|
---|
1252 | TRACE2((NULL, "shfile_write(%d) - %lu bytes (%d) - pos %lu - after\n", fd, (long)s.st_size, x, (long)lseek(fd, 0, SEEK_CUR) ));
|
---|
1253 | }
|
---|
1254 | #endif
|
---|
1255 | return rc;
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | /**
|
---|
1259 | * lseek().
|
---|
1260 | */
|
---|
1261 | long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
|
---|
1262 | {
|
---|
1263 | long rc;
|
---|
1264 | #ifdef SHFILE_IN_USE
|
---|
1265 | shmtxtmp tmp;
|
---|
1266 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1267 | if (file)
|
---|
1268 | {
|
---|
1269 | # if K_OS == K_OS_WINDOWS
|
---|
1270 | assert(SEEK_SET == FILE_BEGIN);
|
---|
1271 | assert(SEEK_CUR == FILE_CURRENT);
|
---|
1272 | assert(SEEK_END == FILE_END);
|
---|
1273 | rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
|
---|
1274 | if (rc == INVALID_SET_FILE_POINTER)
|
---|
1275 | rc = shfile_dos2errno(GetLastError());
|
---|
1276 | # else
|
---|
1277 | rc = lseek(file->native, off, whench);
|
---|
1278 | # endif
|
---|
1279 |
|
---|
1280 | shfile_put(pfdtab, file, &tmp);
|
---|
1281 | }
|
---|
1282 | else
|
---|
1283 | rc = -1;
|
---|
1284 |
|
---|
1285 | #else
|
---|
1286 | rc = lseek(fd, off, whench);
|
---|
1287 | #endif
|
---|
1288 |
|
---|
1289 | return rc;
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
|
---|
1293 | {
|
---|
1294 | int rc;
|
---|
1295 | #ifdef SHFILE_IN_USE
|
---|
1296 | shmtxtmp tmp;
|
---|
1297 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1298 | if (file)
|
---|
1299 | {
|
---|
1300 | switch (cmd)
|
---|
1301 | {
|
---|
1302 | case F_GETFL:
|
---|
1303 | rc = file->oflags;
|
---|
1304 | break;
|
---|
1305 |
|
---|
1306 | case F_SETFL:
|
---|
1307 | {
|
---|
1308 | unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
|
---|
1309 | # ifdef O_DIRECT
|
---|
1310 | mask |= O_DIRECT;
|
---|
1311 | # endif
|
---|
1312 | # ifdef O_ASYNC
|
---|
1313 | mask |= O_ASYNC;
|
---|
1314 | # endif
|
---|
1315 | # ifdef O_SYNC
|
---|
1316 | mask |= O_SYNC;
|
---|
1317 | # endif
|
---|
1318 | if ((file->oflags & mask) == (arg & mask))
|
---|
1319 | rc = 0;
|
---|
1320 | else
|
---|
1321 | {
|
---|
1322 | # if K_OS == K_OS_WINDOWS
|
---|
1323 | assert(0);
|
---|
1324 | errno = EINVAL;
|
---|
1325 | rc = -1;
|
---|
1326 | # else
|
---|
1327 | rc = fcntl(file->native, F_SETFL, arg);
|
---|
1328 | if (rc != -1)
|
---|
1329 | file->oflags = (file->oflags & ~mask) | (arg & mask);
|
---|
1330 | # endif
|
---|
1331 | }
|
---|
1332 | break;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | case F_DUPFD:
|
---|
1336 | {
|
---|
1337 | # if K_OS == K_OS_WINDOWS
|
---|
1338 | HANDLE hNew = INVALID_HANDLE_VALUE;
|
---|
1339 | if (DuplicateHandle(GetCurrentProcess(),
|
---|
1340 | (HANDLE)file->native,
|
---|
1341 | GetCurrentProcess(),
|
---|
1342 | &hNew,
|
---|
1343 | 0,
|
---|
1344 | FALSE /* bInheritHandle */,
|
---|
1345 | DUPLICATE_SAME_ACCESS))
|
---|
1346 | rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg, "shfile_fcntl");
|
---|
1347 | else
|
---|
1348 | rc = shfile_dos2errno(GetLastError());
|
---|
1349 | # else
|
---|
1350 | int nativeNew = fcntl(file->native, F_DUPFD, SHFILE_UNIX_MIN_FD);
|
---|
1351 | if (nativeNew != -1)
|
---|
1352 | rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, "shfile_fcntl");
|
---|
1353 | else
|
---|
1354 | rc = -1;
|
---|
1355 | # endif
|
---|
1356 | break;
|
---|
1357 | }
|
---|
1358 |
|
---|
1359 | default:
|
---|
1360 | errno = -EINVAL;
|
---|
1361 | rc = -1;
|
---|
1362 | break;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | shfile_put(pfdtab, file, &tmp);
|
---|
1366 | }
|
---|
1367 | else
|
---|
1368 | rc = -1;
|
---|
1369 |
|
---|
1370 | #else
|
---|
1371 | rc = fcntl(fd, cmd, arg);
|
---|
1372 | #endif
|
---|
1373 |
|
---|
1374 | switch (cmd)
|
---|
1375 | {
|
---|
1376 | case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
|
---|
1377 | case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
|
---|
1378 | case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
|
---|
1379 | default: TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
|
---|
1380 | }
|
---|
1381 | return rc;
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
---|
1385 | {
|
---|
1386 | #ifdef SHFILE_IN_USE
|
---|
1387 | char abspath[SHFILE_MAX_PATH];
|
---|
1388 | int rc;
|
---|
1389 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
---|
1390 | if (!rc)
|
---|
1391 | {
|
---|
1392 | # if K_OS == K_OS_WINDOWS
|
---|
1393 | rc = stat(abspath, pst); /** @todo re-implement stat. */
|
---|
1394 | # else
|
---|
1395 | rc = stat(abspath, pst);
|
---|
1396 | # endif
|
---|
1397 | }
|
---|
1398 | TRACE2((NULL, "shfile_stat(,%s,) -> %d [%d]\n", path, rc, errno));
|
---|
1399 | return rc;
|
---|
1400 | #else
|
---|
1401 | return stat(path, pst);
|
---|
1402 | #endif
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | int shfile_lstat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
---|
1406 | {
|
---|
1407 | int rc;
|
---|
1408 | #ifdef SHFILE_IN_USE
|
---|
1409 | char abspath[SHFILE_MAX_PATH];
|
---|
1410 |
|
---|
1411 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
---|
1412 | if (!rc)
|
---|
1413 | {
|
---|
1414 | # if K_OS == K_OS_WINDOWS
|
---|
1415 | rc = stat(abspath, pst); /** @todo implement lstat. */
|
---|
1416 | # else
|
---|
1417 | rc = lstat(abspath, pst);
|
---|
1418 | # endif
|
---|
1419 | }
|
---|
1420 | #else
|
---|
1421 | rc = stat(path, pst);
|
---|
1422 | #endif
|
---|
1423 | TRACE2((NULL, "shfile_stat(,%s,) -> %d [%d]\n", path, rc, errno));
|
---|
1424 | return rc;
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | /**
|
---|
1428 | * chdir().
|
---|
1429 | */
|
---|
1430 | int shfile_chdir(shfdtab *pfdtab, const char *path)
|
---|
1431 | {
|
---|
1432 | shinstance *psh = shthread_get_shell();
|
---|
1433 | int rc;
|
---|
1434 | #ifdef SHFILE_IN_USE
|
---|
1435 | char abspath[SHFILE_MAX_PATH];
|
---|
1436 |
|
---|
1437 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
---|
1438 | if (!rc)
|
---|
1439 | {
|
---|
1440 | char *abspath_copy = sh_strdup(psh, abspath);
|
---|
1441 | char *free_me = abspath_copy;
|
---|
1442 | rc = chdir(path);
|
---|
1443 | if (!rc)
|
---|
1444 | {
|
---|
1445 | shmtxtmp tmp;
|
---|
1446 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
1447 |
|
---|
1448 | free_me = pfdtab->cwd;
|
---|
1449 | pfdtab->cwd = abspath_copy;
|
---|
1450 |
|
---|
1451 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
1452 | }
|
---|
1453 | sh_free(psh, free_me);
|
---|
1454 | }
|
---|
1455 | else
|
---|
1456 | rc = -1;
|
---|
1457 | #else
|
---|
1458 | rc = chdir(path);
|
---|
1459 | #endif
|
---|
1460 |
|
---|
1461 | TRACE2((psh, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno));
|
---|
1462 | return rc;
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | /**
|
---|
1466 | * getcwd().
|
---|
1467 | */
|
---|
1468 | char *shfile_getcwd(shfdtab *pfdtab, char *buf, int size)
|
---|
1469 | {
|
---|
1470 | char *ret;
|
---|
1471 | #ifdef SHFILE_IN_USE
|
---|
1472 |
|
---|
1473 | ret = NULL;
|
---|
1474 | if (buf && !size)
|
---|
1475 | errno = -EINVAL;
|
---|
1476 | else
|
---|
1477 | {
|
---|
1478 | size_t cwd_size;
|
---|
1479 | shmtxtmp tmp;
|
---|
1480 | shmtx_enter(&pfdtab->mtx, &tmp);
|
---|
1481 |
|
---|
1482 | cwd_size = strlen(pfdtab->cwd) + 1;
|
---|
1483 | if (buf)
|
---|
1484 | {
|
---|
1485 | if (cwd_size <= (size_t)size)
|
---|
1486 | ret = memcpy(buf, pfdtab->cwd, cwd_size);
|
---|
1487 | else
|
---|
1488 | errno = ERANGE;
|
---|
1489 | }
|
---|
1490 | else
|
---|
1491 | {
|
---|
1492 | if ((size_t)size < cwd_size)
|
---|
1493 | size = (int)cwd_size;
|
---|
1494 | ret = sh_malloc(shthread_get_shell(), size);
|
---|
1495 | if (ret)
|
---|
1496 | ret = memcpy(ret, pfdtab->cwd, cwd_size);
|
---|
1497 | else
|
---|
1498 | errno = ENOMEM;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | shmtx_leave(&pfdtab->mtx, &tmp);
|
---|
1502 | }
|
---|
1503 | #else
|
---|
1504 | ret = getcwd(buf, size);
|
---|
1505 | #endif
|
---|
1506 |
|
---|
1507 | TRACE2((NULL, "shfile_getcwd(,%p,%d) -> %s [%d]\n", buf, size, ret, errno));
|
---|
1508 | return ret;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | /**
|
---|
1512 | * access().
|
---|
1513 | */
|
---|
1514 | int shfile_access(shfdtab *pfdtab, const char *path, int type)
|
---|
1515 | {
|
---|
1516 | int rc;
|
---|
1517 | #ifdef SHFILE_IN_USE
|
---|
1518 | char abspath[SHFILE_MAX_PATH];
|
---|
1519 |
|
---|
1520 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
---|
1521 | if (!rc)
|
---|
1522 | {
|
---|
1523 | # ifdef _MSC_VER
|
---|
1524 | if (type & X_OK)
|
---|
1525 | type = (type & ~X_OK) | R_OK;
|
---|
1526 | # endif
|
---|
1527 | rc = access(abspath, type);
|
---|
1528 | }
|
---|
1529 | #else
|
---|
1530 | # ifdef _MSC_VER
|
---|
1531 | if (type & X_OK)
|
---|
1532 | type = (type & ~X_OK) | R_OK;
|
---|
1533 | # endif
|
---|
1534 | rc = access(path, type);
|
---|
1535 | #endif
|
---|
1536 |
|
---|
1537 | TRACE2((NULL, "shfile_access(,%s,%#x) -> %d [%d]\n", path, type, rc, errno));
|
---|
1538 | return rc;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | /**
|
---|
1542 | * isatty()
|
---|
1543 | */
|
---|
1544 | int shfile_isatty(shfdtab *pfdtab, int fd)
|
---|
1545 | {
|
---|
1546 | int rc;
|
---|
1547 | #ifdef SHFILE_IN_USE
|
---|
1548 | shmtxtmp tmp;
|
---|
1549 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1550 | if (file)
|
---|
1551 | {
|
---|
1552 | # if K_OS == K_OS_WINDOWS
|
---|
1553 | rc = (file->shflags & SHFILE_FLAGS_TYPE_MASK) == SHFILE_FLAGS_TTY;
|
---|
1554 | # else
|
---|
1555 | rc = isatty(file->native);
|
---|
1556 | # endif
|
---|
1557 | shfile_put(pfdtab, file, &tmp);
|
---|
1558 | }
|
---|
1559 | else
|
---|
1560 | rc = 0;
|
---|
1561 | #else
|
---|
1562 | rc = isatty(fd);
|
---|
1563 | #endif
|
---|
1564 |
|
---|
1565 | TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
1566 | return rc;
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | /**
|
---|
1570 | * fcntl F_SETFD / FD_CLOEXEC.
|
---|
1571 | */
|
---|
1572 | int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
|
---|
1573 | {
|
---|
1574 | int rc;
|
---|
1575 | #ifdef SHFILE_IN_USE
|
---|
1576 | shmtxtmp tmp;
|
---|
1577 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1578 | if (file)
|
---|
1579 | {
|
---|
1580 | if (closeit)
|
---|
1581 | file->shflags |= SHFILE_FLAGS_CLOSE_ON_EXEC;
|
---|
1582 | else
|
---|
1583 | file->shflags &= ~SHFILE_FLAGS_CLOSE_ON_EXEC;
|
---|
1584 | shfile_put(pfdtab, file, &tmp);
|
---|
1585 | rc = 0;
|
---|
1586 | }
|
---|
1587 | else
|
---|
1588 | rc = -1;
|
---|
1589 | #else
|
---|
1590 | rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
|
---|
1591 | | (closeit ? FD_CLOEXEC : 0));
|
---|
1592 | #endif
|
---|
1593 |
|
---|
1594 | TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
|
---|
1595 | return rc;
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 |
|
---|
1599 | int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
|
---|
1600 | {
|
---|
1601 | int rc;
|
---|
1602 | #ifdef SHFILE_IN_USE
|
---|
1603 | shmtxtmp tmp;
|
---|
1604 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
---|
1605 | if (file)
|
---|
1606 | {
|
---|
1607 | # if K_OS == K_OS_WINDOWS
|
---|
1608 | rc = -1;
|
---|
1609 | errno = ENOSYS;
|
---|
1610 | # else
|
---|
1611 | rc = ioctl(file->native, request, buf);
|
---|
1612 | # endif
|
---|
1613 | shfile_put(pfdtab, file, &tmp);
|
---|
1614 | }
|
---|
1615 | else
|
---|
1616 | rc = -1;
|
---|
1617 | #else
|
---|
1618 | rc = ioctl(fd, request, buf);
|
---|
1619 | #endif
|
---|
1620 |
|
---|
1621 | TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
|
---|
1622 | return rc;
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 |
|
---|
1626 | mode_t shfile_get_umask(shfdtab *pfdtab)
|
---|
1627 | {
|
---|
1628 | /** @todo */
|
---|
1629 | return 022;
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 | void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
|
---|
1633 | {
|
---|
1634 | /** @todo */
|
---|
1635 | (void)mask;
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 |
|
---|
1639 |
|
---|
1640 | shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
|
---|
1641 | {
|
---|
1642 | #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS
|
---|
1643 | shdir *pdir = NULL;
|
---|
1644 |
|
---|
1645 | if (g_pfnNtQueryDirectoryFile)
|
---|
1646 | {
|
---|
1647 | char abspath[SHFILE_MAX_PATH];
|
---|
1648 | if (shfile_make_path(pfdtab, dir, &abspath[0]) == 0)
|
---|
1649 | {
|
---|
1650 | HANDLE hFile;
|
---|
1651 | SECURITY_ATTRIBUTES SecurityAttributes;
|
---|
1652 |
|
---|
1653 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
---|
1654 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
---|
1655 | SecurityAttributes.bInheritHandle = FALSE;
|
---|
1656 |
|
---|
1657 | hFile = CreateFileA(abspath,
|
---|
1658 | GENERIC_READ,
|
---|
1659 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
1660 | &SecurityAttributes,
|
---|
1661 | OPEN_EXISTING,
|
---|
1662 | FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS,
|
---|
1663 | NULL /* hTemplateFile */);
|
---|
1664 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
1665 | {
|
---|
1666 | pdir = (shdir *)sh_malloc(shthread_get_shell(), sizeof(*pdir));
|
---|
1667 | if (pdir)
|
---|
1668 | {
|
---|
1669 | pdir->pfdtab = pfdtab;
|
---|
1670 | pdir->native = hFile;
|
---|
1671 | pdir->off = ~(size_t)0;
|
---|
1672 | }
|
---|
1673 | else
|
---|
1674 | CloseHandle(hFile);
|
---|
1675 | }
|
---|
1676 | else
|
---|
1677 | shfile_dos2errno(GetLastError());
|
---|
1678 | }
|
---|
1679 | }
|
---|
1680 | else
|
---|
1681 | errno = ENOSYS;
|
---|
1682 | return pdir;
|
---|
1683 | #else
|
---|
1684 | return (shdir *)opendir(dir);
|
---|
1685 | #endif
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | shdirent *shfile_readdir(struct shdir *pdir)
|
---|
1689 | {
|
---|
1690 | #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS
|
---|
1691 | if (pdir)
|
---|
1692 | {
|
---|
1693 | NTSTATUS rcNt;
|
---|
1694 |
|
---|
1695 | if ( pdir->off == ~(size_t)0
|
---|
1696 | || pdir->off + sizeof(MY_FILE_NAMES_INFORMATION) >= pdir->cb)
|
---|
1697 | {
|
---|
1698 | MY_IO_STATUS_BLOCK Ios;
|
---|
1699 |
|
---|
1700 | memset(&Ios, 0, sizeof(Ios));
|
---|
1701 | rcNt = g_pfnNtQueryDirectoryFile(pdir->native,
|
---|
1702 | NULL /*Event*/,
|
---|
1703 | NULL /*ApcRoutine*/,
|
---|
1704 | NULL /*ApcContext*/,
|
---|
1705 | &Ios,
|
---|
1706 | &pdir->buf[0],
|
---|
1707 | sizeof(pdir->buf),
|
---|
1708 | MY_FileNamesInformation,
|
---|
1709 | FALSE /*ReturnSingleEntry*/,
|
---|
1710 | NULL /*FileName*/,
|
---|
1711 | pdir->off == ~(size_t)0 /*RestartScan*/);
|
---|
1712 | if (rcNt >= 0 && rcNt != STATUS_PENDING)
|
---|
1713 | {
|
---|
1714 | pdir->cb = Ios.Information;
|
---|
1715 | pdir->off = 0;
|
---|
1716 | }
|
---|
1717 | else if (rcNt == STATUS_NO_MORE_FILES)
|
---|
1718 | errno = 0; /* wrong? */
|
---|
1719 | else
|
---|
1720 | shfile_nt2errno(rcNt);
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | if ( pdir->off != ~(size_t)0
|
---|
1724 | && pdir->off + sizeof(MY_FILE_NAMES_INFORMATION) <= pdir->cb)
|
---|
1725 | {
|
---|
1726 | PMY_FILE_NAMES_INFORMATION pcur = (PMY_FILE_NAMES_INFORMATION)&pdir->buf[pdir->off];
|
---|
1727 | ANSI_STRING astr;
|
---|
1728 | UNICODE_STRING ustr;
|
---|
1729 |
|
---|
1730 | astr.Length = astr.MaximumLength = sizeof(pdir->ent.name);
|
---|
1731 | astr.Buffer = &pdir->ent.name[0];
|
---|
1732 |
|
---|
1733 | ustr.Length = ustr.MaximumLength = pcur->FileNameLength < ~(USHORT)0 ? (USHORT)pcur->FileNameLength : ~(USHORT)0;
|
---|
1734 | ustr.Buffer = &pcur->FileName[0];
|
---|
1735 |
|
---|
1736 | rcNt = g_pfnRtlUnicodeStringToAnsiString(&astr, &ustr, 0/*AllocateDestinationString*/);
|
---|
1737 | if (rcNt < 0)
|
---|
1738 | sprintf(pdir->ent.name, "conversion-failed-%08x-rcNt=%08x-len=%u",
|
---|
1739 | pcur->FileIndex, rcNt, pcur->FileNameLength);
|
---|
1740 | if (pcur->NextEntryOffset)
|
---|
1741 | pdir->off += pcur->NextEntryOffset;
|
---|
1742 | else
|
---|
1743 | pdir->off = pdir->cb;
|
---|
1744 | return &pdir->ent;
|
---|
1745 | }
|
---|
1746 | }
|
---|
1747 | else
|
---|
1748 | errno = EINVAL;
|
---|
1749 | return NULL;
|
---|
1750 | #else
|
---|
1751 | struct dirent *pde = readdir((DIR *)pdir);
|
---|
1752 | return pde ? (shdirent *)&pde->d_name[0] : NULL;
|
---|
1753 | #endif
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | void shfile_closedir(struct shdir *pdir)
|
---|
1757 | {
|
---|
1758 | #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS
|
---|
1759 | if (pdir)
|
---|
1760 | {
|
---|
1761 | CloseHandle(pdir->native);
|
---|
1762 | pdir->pfdtab = NULL;
|
---|
1763 | pdir->native = INVALID_HANDLE_VALUE;
|
---|
1764 | sh_free(shthread_get_shell(), pdir);
|
---|
1765 | }
|
---|
1766 | else
|
---|
1767 | errno = EINVAL;
|
---|
1768 | #else
|
---|
1769 | closedir((DIR *)pdir);
|
---|
1770 | #endif
|
---|
1771 | }
|
---|
1772 |
|
---|