- Timestamp:
- Oct 17, 2010 11:43:35 PM (14 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/Makefile.kmk
r2413 r2423 31 31 # 32 32 PROGRAMS += kash 33 kash_TEMPLATE = BIN 34 kash_NAME .win= kmk_ash33 kash_TEMPLATE = BIN-THREADED 34 kash_NAME = kmk_ash 35 35 kash_ASTOOL = YASM 36 36 kash_DEFS = lint SHELL SMALL … … 50 50 HAVE_SYS_SIGNAME HAVE_SYSCTL_H HAVE_SETPROGNAME 51 51 kash_DEFS.freebsd = \ 52 HAVE_SYS_SIGNAME HAVE_SYSCTL_H HAVE_SETPROGNAME 53 kash_DEFS.openbsd = \ 52 54 HAVE_SYS_SIGNAME HAVE_SYSCTL_H HAVE_SETPROGNAME 53 55 kash_INCS = $(kash_0_OUTDIR) . # (the last is because of error.h) -
trunk/src/kash/eval.c
r2387 r2423 45 45 #include <sys/types.h> 46 46 #ifdef HAVE_SYSCTL_H 47 # ifdef __OpenBSD__ /* joyful crap */ 48 # include <sys/param.h> 49 # undef psh 50 # endif 47 51 # include <sys/sysctl.h> 48 52 #endif -
trunk/src/kash/redir.c
r2290 r2423 383 383 return newfd; 384 384 } 385 386 387 /* 388 * Copy a file descriptor to be = to. Returns -1 389 * if the source file descriptor is closed, EMPTY if there are no unused 390 * file descriptors left. 391 */ 392 393 int 394 copyfd2(shinstance *psh, int from, int to) 395 { 396 int newfd; 397 398 /** @todo Use dup2()... */ 399 newfd = shfile_fcntl(&psh->fdtab, from, F_DUPFD, to); 400 if (newfd < 0) { 401 if (errno == EMFILE) 402 return EMPTY; 403 else 404 error(psh, "%d: %s", from, strerror(errno)); 405 } 406 else if (newfd != to) { 407 error(psh, "%d: F_DUPFD returned %d, expected %d", from, newfd, to); 408 close(newfd); 409 newfd = -1; 410 } 411 412 return newfd; 413 } 414 -
trunk/src/kash/shfile.c
r2417 r2423 32 32 #include <stdlib.h> 33 33 #include <stdio.h> 34 #include <string.h> 34 35 #include <assert.h> 35 36 … … 60 61 */ 61 62 #if K_OS == K_OS_WINDOWS \ 63 || K_OS == K_OS_OPENBSD /* because of ugly pthread library pipe hacks */ \ 62 64 || !defined(SH_FORKED_MODE) 63 65 # define SHFILE_IN_USE … … 201 203 return -1; 202 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 203 214 204 215 shmtx_enter(&pfdtab->mtx, &tmp); … … 264 275 return fd; 265 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 */ 266 308 267 309 /** … … 646 688 } 647 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 648 734 # endif 649 735 } … … 829 915 #endif /* K_OS_WINDOWS */ 830 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 */ 831 949 832 950 /** … … 911 1029 if (!fd) 912 1030 { 913 fd = open(absname, flag , mode);1031 fd = open(absname, flags, mode); 914 1032 if (fd != -1) 915 { 916 int native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD); 917 int s = errno; 918 close(fd); 919 errno = s; 920 if (native != -1) 921 fd = shfile_insert(pfdtab, native, flags, 0, -1, "shfile_open"); 922 else 923 fd = -1; 924 } 1033 fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open"); 925 1034 } 926 1035 … … 937 1046 int shfile_pipe(shfdtab *pfdtab, int fds[2]) 938 1047 { 939 int rc ;1048 int rc = -1; 940 1049 #ifdef SHFILE_IN_USE 941 1050 # if K_OS == K_OS_WINDOWS … … 965 1074 if (!pipe(native_fds)) 966 1075 { 967 fds[0] = shfile_ insert(pfdtab,native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");1076 fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe"); 968 1077 if (fds[0] != -1) 969 1078 { 970 fds[1] = shfile_ insert(pfdtab,native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");1079 fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe"); 971 1080 if (fds[1] != -1) 972 1081 rc = 0; … … 1049 1158 1050 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 } 1051 1166 rc = close(fd); 1052 1167 #endif … … 1115 1230 1116 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 1117 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 } 1118 1254 #endif 1119 1255 return rc; … … 1191 1327 rc = fcntl(file->native, F_SETFL, arg); 1192 1328 if (rc != -1) 1193 file-> flags = (file->flags & ~mask) | (arg & mask);1329 file->oflags = (file->oflags & ~mask) | (arg & mask); 1194 1330 # endif 1195 1331 } … … 1212 1348 rc = shfile_dos2errno(GetLastError()); 1213 1349 # else 1214 int nativeNew = fcntl(file->native F_DUPFD, SHFILE_UNIX_MIN_FD);1350 int nativeNew = fcntl(file->native, F_DUPFD, SHFILE_UNIX_MIN_FD); 1215 1351 if (nativeNew != -1) 1216 1352 rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, "shfile_fcntl"); 1353 else 1354 rc = -1; 1217 1355 # endif 1218 1356 break; … … 1352 1490 else 1353 1491 { 1354 if ( size < cwd_size)1492 if ((size_t)size < cwd_size) 1355 1493 size = (int)cwd_size; 1356 1494 ret = sh_malloc(shthread_get_shell(), size); … … 1445 1583 file->shflags &= ~SHFILE_FLAGS_CLOSE_ON_EXEC; 1446 1584 shfile_put(pfdtab, file, &tmp); 1585 rc = 0; 1447 1586 } 1448 1587 else … … 1501 1640 shdir *shfile_opendir(shfdtab *pfdtab, const char *dir) 1502 1641 { 1503 #if def SHFILE_IN_USE1642 #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS 1504 1643 shdir *pdir = NULL; 1505 1644 … … 1549 1688 shdirent *shfile_readdir(struct shdir *pdir) 1550 1689 { 1551 #if def SHFILE_IN_USE1690 #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS 1552 1691 if (pdir) 1553 1692 { … … 1617 1756 void shfile_closedir(struct shdir *pdir) 1618 1757 { 1619 #if def SHFILE_IN_USE1758 #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS 1620 1759 if (pdir) 1621 1760 { -
trunk/src/kash/shfile.h
r2416 r2423 132 132 void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls); 133 133 void *shfile_exec_win(shfdtab *pfdtab, int prepare, unsigned short *sizep, intptr_t *hndls); 134 int shfile_exec_unix(shfdtab *pfdtab); 134 135 135 136 int shfile_open(shfdtab *, const char *, unsigned, mode_t); -
trunk/src/kash/shinstance.c
r2416 r2423 1080 1080 } 1081 1081 rc = -1; 1082 # else 1083 rc = execve(exe, (char **)argv, (char **)envp); 1082 1083 # else 1084 rc = shfile_exec_unix(&psh->fdtab); 1085 if (!rc) 1086 rc = execve(exe, (char **)argv, (char **)envp); 1084 1087 # endif 1085 1088 -
trunk/src/kash/show.c
r2376 r2423 516 516 want_fd = ((want_fd + 1) / 2) - 1; 517 517 } 518 shfile_cloexec(&psh->fdtab, psh->tracefd, 1 /* close it */); 518 519 } 519 520 if (psh->tracefd == -1) { -
trunk/src/kash/tests/Makefile.kmk
r2413 r2423 24 24 # 25 25 26 SUB_DEPTH = ../.. 26 SUB_DEPTH = ../../.. 27 27 include $(KBUILD_PATH)/subheader.kmk 28 28 … … 33 33 TESTING += kash_tests 34 34 35 KASH_TEST_BIN = $(if $(kash_1_TARGET),$(kash_1_TARGET),$(PATH_INS)/$(TEMPLATE_BIN_INST)k ash$(SUFF_EXE))35 KASH_TEST_BIN = $(if $(kash_1_TARGET),$(kash_1_TARGET),$(PATH_INS)/$(TEMPLATE_BIN_INST)kmk_ash$(SUFF_EXE)) 36 36 KASH_TEST_DIR := $(PATH_SUB_CURRENT) 37 37 KASH_TESTCASES := $(addprefix $(KASH_TEST_DIR)/,\ … … 45 45 pipe-1 \ 46 46 pipe-2 \ 47 exec-1 \ 47 48 ) 48 49
Note:
See TracChangeset
for help on using the changeset viewer.