diff options
Diffstat (limited to 'src/sys_zos.c')
| -rw-r--r-- | src/sys_zos.c | 238 |
1 files changed, 145 insertions, 93 deletions
diff --git a/src/sys_zos.c b/src/sys_zos.c index 9041edd..48c1315 100644 --- a/src/sys_zos.c +++ b/src/sys_zos.c @@ -15,6 +15,7 @@ #include <_Nascii.h> #include <errno.h> #include <fcntl.h> +#include <setjmp.h> #include <spawn.h> #include <stddef.h> #include <stdint.h> @@ -29,6 +30,12 @@ #include "charset.h" #include "sys.h" +/* IGGCSI00 is AMODE 31 and is called through a plain function pointer: + * that only works because this program is 31-bit too (bench/README.md). */ +#ifndef ZASH_ZOS_SHIM +_Static_assert(sizeof(void *) == 4, "the z/OS build must be -m32"); +#endif + /** * @brief Describe a signal. * @@ -89,6 +96,8 @@ const char *sys_strsignal(int sig) { */ pid_t sys_spawn(const spawn_req *rq) { int fd_map[SYS_NFD + 16]; + int dups[SYS_NFD]; + int ndups = 0; const int map_max = (int)(sizeof fd_map / sizeof fd_map[0]); int fd_count = 1; /* 0 would mean "inherit everything" */ struct inheritance inh = {0}; @@ -100,10 +109,31 @@ pid_t sys_spawn(const spawn_req *rq) { src = FD_CLOSED; if (src == FD_CLOSED) { fd_map[i] = SPAWN_FDCLOSED; - } else { - fd_map[i] = src; - fd_count = i + 1; + continue; + } + if (src != i) { + /* + * The shell keeps its pipe ends close-on-exec (sys_pipe()). A + * mapping onto the child's 0..2 has to clear that flag: the child + * is the one that execs the command, and close-on-exec would take + * its standard output with it -- ps and a #!/bin/sh shim both + * failed this way. dup2() clears the flag, but spawn()'s map + * gives the child the parent's descriptor as it is, so pass a + * plain duplicate, which F_DUPFD makes without the flag. + */ + src = fcntl(src, F_DUPFD, SYS_NFD); + if (src < 0) { + int saved; + saved = errno; + while (ndups) + close(dups[--ndups]); + errno = saved; + return -1; + } + dups[ndups++] = src; } + fd_map[i] = src; + fd_count = i + 1; } if (rq->sigdfl) { inh.flags |= SPAWN_SETSIGDEF; @@ -138,8 +168,14 @@ pid_t sys_spawn(const spawn_req *rq) { * runtime converts path, argv and envp, and that _BPX_SHAREAS, * _BPX_SPAWN_SCRIPT and STEPLIB are honoured from `envp`. */ - return spawn(rq->path, fd_count, fd_map, &inh, (const char **)rq->argv, - (const char **)rq->envp); + pid_t pid = spawn(rq->path, fd_count, fd_map, &inh, (const char **)rq->argv, + (const char **)rq->envp); + int saved; + saved = errno; + while (ndups) + close(dups[--ndups]); + errno = saved; + return pid; } /** @@ -541,70 +577,79 @@ const char *sys_info(sys_info_kind kind) { /* ---- the catalog ------------------------------------------------------- */ /* - * IGGCSI00 is AMODE 31 and takes a standard three-word parameter list. - * zash is AMODE 64, so the call goes through zos_csi.s, which switches - * addressing mode around a branch to the module. Everything the routine - * or the glue touches is in one csi_block in 31-bit storage, below 2 GB. - * The glue keeps the caller's registers there too, so it depends on - * nothing about the caller's save area or stack, and it writes the entry - * point of IGGCSI00 back there so later calls skip the directory search. + * IGGCSI00 is AMODE 31 and takes a standard OS parameter list of three + * addresses: reason area, selection criteria, work area, with the VL bit + * on the last. zash is 31-bit XPLINK too, so it is called directly: + * fetch() loads it once, and the OS linkage makes the compiler build the + * parameter list and give the module a save area. No mode switch, and + * no assembler. + * + * The linkage has to be OS_UPSTACK, not plain OS. Under -m32 (XPLINK), + * OS means OS_NOSTACK: no standard save area, so IGGCSI00 -- a + * traditional upward-stack OS routine -- takes a program check on the + * first call. OS_UPSTACK is what the compiler reference names for + * exactly this case; measured on z/OS, OS abends and OS_UPSTACK returns + * rc=0 with the work area filled. */ -/** @brief Size of the CSI return work area (IBM recommends 64000). */ -#define CSI_WORK 64000 - -/** @brief Returned when IGGCSI00 cannot be loaded (CSI uses 0-16). */ -#define CSI_NOLOAD 16 - -/** - * @brief 31-bit storage for CSI calls. The layout up to `rc` is shared - * with zos_csi.s (the offsets are checked below). +/** @brief Size of the CSI return work area. * - * Packed, so the compiler cannot insert padding the DSECT does not have, - * and aligned(8), so the block and its save area start on a doubleword. - * The name is kept as EBCDIC bytes and not as a C string, so that both - * character modes spell it the same way. + * IBM recommends 64000; 32K already holds more names than a completion + * screen shows, and it is taken per search rather than held. */ -typedef struct __attribute__((packed, aligned(8))) csi_block { - unsigned char *__ptr32 parm[3]; /**< +0: A(reason), A(field), - A(work) with the VL bit */ - uint32_t save[18]; /**< +12: save area for IGGCSI00 */ - unsigned char reason[4]; /**< +84 */ - uint64_t regs[16]; /**< +88: caller's R14-R13 */ - uint32_t ep; /**< +216: EPA from LOAD */ - int32_t rc; /**< +220: R15 from IGGCSI00 */ - unsigned char epname[8]; /**< +224: "IGGCSI00", IBM-1047 */ - unsigned char field[CSI_FIELD_LEN]; /**< +232: selection criteria */ - unsigned char work[CSI_WORK]; /**< return work area */ -} csi_block; +#define CSI_WORK 32768 + +/** @brief One search's storage: taken per search, freed before return. */ +typedef struct csi_req { + unsigned char reason[4]; + unsigned char field[CSI_FIELD_LEN]; + unsigned char work[CSI_WORK] __attribute__((aligned(8))); +} csi_req; + +typedef int iggcsi_fn(void *reason, void *field, void *work); +#pragma linkage(iggcsi_fn, OS_UPSTACK) + +static iggcsi_fn *csi_ep; /**< from fetch(), NULL until loaded */ +static bool csi_dead; /**< fetch failed or a call abended */ +static bool csi_in_flight; /**< one search at a time */ -_Static_assert(offsetof(csi_block, save) == 12, "zos_csi.s SAVE"); -_Static_assert(offsetof(csi_block, reason) == 84, "zos_csi.s REASON"); -_Static_assert(offsetof(csi_block, regs) == 88, "zos_csi.s REGS"); -_Static_assert(offsetof(csi_block, ep) == 216, "zos_csi.s EP"); -_Static_assert(offsetof(csi_block, rc) == 220, "zos_csi.s RC"); -_Static_assert(_Alignof(csi_block) == 8, "csi_block alignment"); -/* Held for the life of the shell: keep it small. */ -_Static_assert(sizeof(csi_block) <= 64 * 1024 + 512, "csi_block size"); +/** @brief Returned by csi_call() after an abend: > 4, so the search stops. */ +#define CSI_ABENDED 20 -#pragma linkage(ZASHCSI, OS) -int ZASHCSI(csi_block *block); +static sigjmp_buf csi_jb; + +static void csi_trap(int sig) { + (void)sig; + siglongjmp(csi_jb, 1); +} /** * @brief One IGGCSI00 call (a csi_call_fn). * - * The parameter list is rebuilt each time: IGGCSI00 is entitled to - * assume nothing else in the block survives a call. + * A program check or abend inside IGGCSI00 comes back as a signal. It is + * caught only for the length of the call, and the call then reports + * failure; the caller frees the block and retires the catalog. */ static int csi_call(csi_area *a, void *ctx) { - csi_block *b = ctx; - (void)a; - /* 64-bit to __ptr32 is an explicit cast; the block is below 2 GB, - * so nothing is lost. The VL bit goes on the last entry. */ - b->parm[0] = (unsigned char *__ptr32)b->reason; - b->parm[1] = (unsigned char *__ptr32)b->field; - b->parm[2] = (unsigned char *__ptr32)((uintptr_t)b->work | 0x80000000u); - return ZASHCSI(b); + bool *abended = ctx; + static const int sigs[] = {SIGABND, SIGSEGV, SIGILL, SIGFPE, SIGBUS}; + enum { NSIG_CSI = sizeof sigs / sizeof sigs[0] }; + struct sigaction sa = {.sa_handler = csi_trap}, old[NSIG_CSI]; + sigemptyset(&sa.sa_mask); + for (int i = 0; i < NSIG_CSI; i++) + sigaction(sigs[i], &sa, &old[i]); + volatile int rc; + if (sigsetjmp(csi_jb, 1) == 0) { + /* VL bit on the last address; harmless if the compiler sets it. */ + void *work = (void *)((uintptr_t)a->work | 0x80000000u); + rc = csi_ep(a->reason, a->field, work); + } else { + *abended = true; + rc = CSI_ABENDED; + } + for (int i = 0; i < NSIG_CSI; i++) + sigaction(sigs[i], &old[i], NULL); + return rc; } /** @@ -615,45 +660,40 @@ bool sys_has_catalog(void) { } /** - * @brief The one CSI block, in 31-bit storage, allocated on the first - * search and held for the life of the shell. - */ -static csi_block *csi_blk; - -/** @brief True while a search is using the block. */ -static bool csi_in_flight; - -/** * @brief List cataloged names that match a CSI filter key. * - * The block is 64 KB of 31-bit storage: IGGCSI00 is AMODE 31, so all - * three of its addresses have to be below 2 GB. Allocating one for - * the first Tab and keeping it is better than taking a fresh one per - * Tab, which is what the shell used to do. A nested search is - * refused rather than given a second block: the block holds the - * resume token of the search that is running. The module zash loads - * is never DELETEd; MVS drops it when the shell ends. + * The module is loaded once and, unless a call abends, stays loaded for + * the life of the shell: MVS drops it when the shell ends. A nested + * search is refused rather than given a second block, because the work + * area holds the resume token of the search that is running. * * @return false if the catalog could not be searched. */ bool sys_catalog_list(const char *filter, mvs_name_fn *fn, void *arg) { - if (csi_in_flight) + if (csi_dead || csi_in_flight) return false; - if (!csi_blk) { - /* Spelled in IBM-1047 whatever this program's own character - * mode is: LOAD takes the name as the system spells it. */ - static const unsigned char name[8] = {0xC9, 0xC7, 0xC7, 0xC3, - 0xE2, 0xC9, 0xF0, 0xF0}; - csi_blk = __malloc31(sizeof *csi_blk); - if (!csi_blk) + if (!csi_ep) { + csi_ep = (iggcsi_fn *)(void (*)(void))fetch("IGGCSI00"); + if (!csi_ep) { + csi_dead = true; /* never asked for again */ return false; - memset(csi_blk, 0, offsetof(csi_block, epname)); - memcpy(csi_blk->epname, name, sizeof name); + } } - csi_area a = {csi_blk->reason, csi_blk->field, csi_blk->work, CSI_WORK}; + csi_req *r = malloc(sizeof *r); + if (!r) + return false; /* not sticky: storage may come back */ + csi_area a = {r->reason, r->field, r->work, CSI_WORK}; + bool abended = false; csi_in_flight = true; - bool ok = csi_search(filter, &a, csi_call, csi_blk, fn, arg); + bool ok = csi_search(filter, &a, csi_call, &abended, fn, arg); csi_in_flight = false; + free(r); + if (abended) { + (void)release((void (*)(void))csi_ep); + csi_ep = NULL; + csi_dead = true; + ok = false; + } return ok; } @@ -668,6 +708,9 @@ bool sys_catalog_list(const char *filter, mvs_name_fn *fn, void *arg) { * PDS under BSAM. A PDSE directory is simulated). * * @param dsn Data set name, without quotes. + * @param fn Called for each member name. + * @param arg Passed to `fn`. + * @return false if the directory could not be read or was malformed. */ bool sys_pds_members(const char *dsn, mvs_name_fn *fn, void *arg) { char path[64]; @@ -708,7 +751,7 @@ bool sys_dd_names(mvs_name_fn *fn, void *arg) { /* ---- dynamic allocation ------------------------------------------------ */ typedef int bpxwdyn_fn(char *); -#pragma linkage(bpxwdyn_fn, OS) +#pragma linkage(bpxwdyn_fn, OS_UPSTACK) /** * @brief Issue a BPXWDYN request, such as `alloc dd(in) da(x.y) shr`. @@ -717,9 +760,6 @@ typedef int bpxwdyn_fn(char *); * names allocated this way belong to the address space, so a command * spawned with `_BPX_SHAREAS=YES` can open them as `//DD:NAME`. * - * VERIFY: calling BPXWDY2 from an AMODE 64 program through fetch(). It - * may need the same kind of glue as the catalog search. - * * @param request The request text. * @param msg Receives an explanation on failure. * @param msglen Size of `msg`. @@ -728,16 +768,19 @@ typedef int bpxwdyn_fn(char *); */ int sys_dynalloc(const char *request, char *msg, size_t msglen) { static bpxwdyn_fn *bpxwdyn; - if (!bpxwdyn) + static bool tried; + if (!bpxwdyn && !tried) { + tried = true; bpxwdyn = (bpxwdyn_fn *)(void (*)(void))fetch("BPXWDY2 "); + } if (!bpxwdyn) { snprintf(msg, msglen, "cannot load BPXWDYN"); return -1; } size_t len = strlen(request); - char *text = __malloc31(len + 1); + char *text = malloc(len + 1); if (!text) { - snprintf(msg, msglen, "out of storage below 2 GB"); + snprintf(msg, msglen, "out of storage"); return -1; } memcpy(text, request, len + 1); @@ -775,7 +818,8 @@ int sys_pipe(int fd[2]) { } int high = sys_dupfd(fd[i], SYS_NFD); if (high < 0) { - int err = errno; + int err; + err = errno; close(fd[0]); close(fd[1]); errno = err; @@ -826,6 +870,14 @@ int sys_open(const char *path, int flags, mode_t mode) { unsigned sys_init(void) { unsigned mask = 0; /* + * LE prints "CEE5208S The signal SIGxxx was received." on stderr when a + * process dies of a signal with the default action -- noise in a script's + * output, and /bin/sh does not do it. IBM's zoslib sets this too. The + * runtime reads it lazily, so this covers this shell and the children it + * forks; "0": a value the user set wins. + */ + (void)setenv("_EDC_SIG_DFLT", "1", 0); + /* * An ASCII build converts to and from tagged files and pipes itself; * it must not depend on the user having set _BPXK_AUTOCVT. An EBCDIC * build leaves the process setting alone, like /bin/sh. |
