static int herein(struct ioword *iop, char **resbuf) { int fd = -1; struct shf *shf; struct temp *h; int i; /* ksh -c 'cat << EOF' can cause this... */ if (iop->heredoc == NULL) { warningf(true, "%s missing", "here document"); /* special to iosetup(): don't print error */ return (-2); } /* lexer substitution flags */ i = (iop->flag & IOEVAL) ? (ONEWORD | HEREDOC) : 0; /* skip all the fd setup if we just want the value */ if (resbuf != NULL) return (hereinval(iop->heredoc, i, resbuf, NULL)); /* * Create temp file to hold content (done before newenv * so temp doesn't get removed too soon). */ h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps); if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY, 0)) < 0) { i = errno; warningf(true, "can't %s temporary file %s: %s", !shf ? "create" : "open", h->tffn, cstrerror(i)); if (shf) shf_close(shf); /* special to iosetup(): don't print error */ return (-2); } if (hereinval(iop->heredoc, i, NULL, shf) == -2) { close(fd); /* special to iosetup(): don't print error */ return (-2); } if (shf_close(shf) == EOF) { i = errno; close(fd); warningf(true, "can't %s temporary file %s: %s", "write", h->tffn, cstrerror(i)); /* special to iosetup(): don't print error */ return (-2); } return (fd); }
void kshdebug_init_(void) { if (kshdebug_shf) shf_close(kshdebug_shf); kshdebug_shf = shf_open("/tmp/ksh-debug.log", O_WRONLY|O_APPEND|O_CREAT, 0600, SHF_WR|SHF_MAPHI); if (kshdebug_shf) { shf_fprintf(kshdebug_shf, "\nNew shell[pid %d]\n", getpid()); shf_flush(kshdebug_shf); } }
void quitenv(struct shf *shf) { struct env *ep = e; char *cp; int fd; yyrecursive_pop(true); while (ep->oenv && ep->oenv->loc != ep->loc) popblock(); if (ep->savefd != NULL) { for (fd = 0; fd < NUFILE; fd++) /* if ep->savefd[fd] < 0, means fd was closed */ if (ep->savefd[fd]) restfd(fd, ep->savefd[fd]); if (ep->savefd[2]) /* Clear any write errors */ shf_reopen(2, SHF_WR, shl_out); } /* * Bottom of the stack. * Either main shell is exiting or cleanup_parents_env() was called. */ if (ep->oenv == NULL) { #ifdef DEBUG_LEAKS int i; #endif if (ep->type == E_NONE) { /* Main shell exiting? */ #if HAVE_PERSISTENT_HISTORY if (Flag(FTALKING)) hist_finish(); #endif j_exit(); if (ep->flags & EF_FAKE_SIGDIE) { int sig = (exstat & 0xFF) - 128; /* * ham up our death a bit (AT&T ksh * only seems to do this for SIGTERM) * Don't do it for SIGQUIT, since we'd * dump a core.. */ if ((sig == SIGINT || sig == SIGTERM) && (kshpgrp == kshpid)) { setsig(&sigtraps[sig], SIG_DFL, SS_RESTORE_CURR | SS_FORCE); kill(0, sig); } } } if (shf) shf_close(shf); reclaim(); #ifdef DEBUG_LEAKS #ifndef MKSH_NO_CMDLINE_EDITING x_done(); #endif #ifndef MKSH_NOPROSPECTOFWORK /* block at least SIGCHLD during/after afreeall */ sigprocmask(SIG_BLOCK, &sm_sigchld, NULL); #endif afreeall(APERM); for (fd = 3; fd < NUFILE; fd++) if ((i = fcntl(fd, F_GETFD, 0)) != -1 && (i & FD_CLOEXEC)) close(fd); close(2); close(1); close(0); #endif exit(exstat & 0xFF); } if (shf) shf_close(shf); reclaim(); e = e->oenv; /* free the struct env - tricky due to the ALLOC_ITEM inside */ cp = (void *)ep; afree(cp + sizeof(ALLOC_ITEM), ATEMP); }