summaryrefslogtreecommitdiffhomepage
path: root/src/exec.c
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-15 23:12:40 +0000
committerSean Kurtz <seankurtz75@gmail.com>2026-11-15 23:12:40 +0000
commit73397aa38958ddfe0b0cadbbf5e7e55ba88da644 (patch)
treed6d1f5453460aa0aec26f423be234ef5d04ca191 /src/exec.c
parente76ce7d25f28a8b9c811b727c1ca44654a546834 (diff)
downloadzash-73397aa38958ddfe0b0cadbbf5e7e55ba88da644.tar.gz
z/OS: file tagging, catalog completion, time, and per-command options
Tagging and conversion (sys_zos.c, redir.c, input.c, var.c, exec.c): - _TAG_REDIR_IN/OUT/ERR as /bin/sh documents them, cached by variable hooks so redirection tests a byte; honoured as a prefix on external commands and on pipeline stages. - ASCII builds tag new output and here-document files as text, tag pipes IBM-1047 (set +o pipetag to stop), and switch on autoconvert. - Scripts, . files, profile and ENV are read in either character set: tagged files get F_CONTROL_CVT, untagged ones are sniffed. - tag/chtag primitives (sys_tag_get/sys_tag_set) for a builtin to come. Data set completion (complete.c, mvsfmt.[ch], zos_csi.s): - //'HLQ.PART, "//'HLQ.PART, //NAME (user HLQ), //'DSN(MEM, //DD:NAME complete one qualifier at a time from the Catalog Search Interface, PDS directories and the TIOT. - IGGCSI00 is called through HLASM glue that switches to AMODE 31 and keeps every register and parameter below the bar (VERIFY). - The work-area, directory and TIOT decoders are portable and run on Linux against a stand-in catalog that produces the real EBCDIC bytes, including CSI resume and directory-block splits; fuzzed with libFuzzer (make fuzz). - A lone completion inside quotes now closes the quote. Other z/OS features: - time [-p] keyword with shell/children CPU split. - set -o jobnames (_BPX_JOBNAME per command), set -o shareas (_BPX_SHAREAS=YES for foreground commands only), set -o msgid (ZSHnnnnE/W message IDs), ulimit -M (MEMLIMIT), ZOS_SYSNAME, ZOS_SYSPLEX, ZOS_RELEASE, ZOS_ASID, ZOS_JOBNAME, prompt \S. - BPXWDYN entry point for an alloc builtin to come. Checking z/OS code off z/OS: - make zos-lint compiles sys_zos.c against declarations transcribed from the z/OS headers, and the rest with the MVS paths forced on. - make mvs-asan runs the suite with the MVS paths on, under ASan/UBSan. - make asan and make valgrind now check for leaks.
Diffstat (limited to 'src/exec.c')
-rw-r--r--src/exec.c228
1 files changed, 227 insertions, 1 deletions
diff --git a/src/exec.c b/src/exec.c
index e5cc009..15007fb 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -15,13 +15,17 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
+#include <sys/resource.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "expand.h"
#include "interact.h"
#include "parser.h"
+#include "sys.h"
/**
* @brief A `break`, `continue` or `return` is in progress.
@@ -146,6 +150,173 @@ void hash_print(void) {
}
/**
+ * @brief Add a difference of two timevals, in microseconds, to a total.
+ */
+static long long tv_diff(struct timeval a, struct timeval b) {
+ return (long long)(b.tv_sec - a.tv_sec) * 1000000 +
+ (long long)(b.tv_usec - a.tv_usec);
+}
+
+/**
+ * @brief Print one line of `time` output.
+ * @param label "real", "user" or "sys".
+ * @param us The time in microseconds.
+ * @param posix `-p`: seconds with two decimals; else `0m0.000s`.
+ */
+static void put_time(const char *label, long long us, bool posix) {
+ if (us < 0)
+ us = 0;
+ if (posix)
+ out_printf(&out2, "%s %lld.%02lld", label, us / 1000000,
+ us % 1000000 / 10000);
+ else
+ out_printf(&out2, "%s\t%lldm%lld.%03llds", label, us / 60000000,
+ us / 1000000 % 60, us % 1000000 / 1000);
+}
+
+/**
+ * @brief Run `time pipeline` and report where the time went.
+ *
+ * The report goes to standard error after the pipeline finishes:
+ *
+ * real 0m1.204s
+ * user 0m0.310s (shell 0m0.010s, children 0m0.300s)
+ * sys 0m0.052s (shell 0m0.002s, children 0m0.050s)
+ *
+ * The split is what an external `time` cannot give: CPU the shell itself
+ * used (builtins, expansion, loops) against CPU used by the processes it
+ * ran, which is the first question when a script costs too much. With
+ * `-p` the three POSIX lines are printed and nothing else.
+ *
+ * @param n The N_TIME node.
+ * @param flags Evaluation flags for the pipeline; `EV_EXIT` is dropped
+ * because the shell must survive to print the report.
+ * @return Status of the pipeline (0 if there is none).
+ */
+static int eval_time(const node *n, unsigned flags) {
+ struct rusage s0;
+ struct rusage c0;
+ struct rusage s1;
+ struct rusage c1;
+ struct timeval t0;
+ struct timeval t1;
+ getrusage(RUSAGE_SELF, &s0);
+ getrusage(RUSAGE_CHILDREN, &c0);
+ gettimeofday(&t0, NULL);
+ int status =
+ n->u.time.body ? eval_node(n->u.time.body, flags & ~EV_EXIT) : 0;
+ gettimeofday(&t1, NULL);
+ getrusage(RUSAGE_SELF, &s1);
+ getrusage(RUSAGE_CHILDREN, &c1);
+ bool posix = n->u.time.posix;
+ long long self[2] = {tv_diff(s0.ru_utime, s1.ru_utime),
+ tv_diff(s0.ru_stime, s1.ru_stime)};
+ long long kids[2] = {tv_diff(c0.ru_utime, c1.ru_utime),
+ tv_diff(c0.ru_stime, c1.ru_stime)};
+ static const char *const label[2] = {"user", "sys"};
+ out_flush(&out1);
+ put_time("real", tv_diff(t0, t1), posix);
+ out_putc(&out2, '\n');
+ for (int i = 0; i < 2; i++) {
+ put_time(label[i], self[i] + kids[i], posix);
+ if (!posix) {
+ put_time(" (shell", self[i], true);
+ put_time("s, children", kids[i], true);
+ out_puts(&out2, "s)");
+ }
+ out_putc(&out2, '\n');
+ }
+ out_flush(&out2);
+ return status;
+}
+
+/**
+ * @brief Is this byte allowed in an MVS job name?
+ */
+static bool jobname_char(char c) {
+ return (c >= 'A' && c <= 'I') || (c >= 'J' && c <= 'R') ||
+ (c >= 'S' && c <= 'Z') || (c >= '0' && c <= '9') || c == '@' ||
+ c == '#' || c == '$';
+}
+
+/**
+ * @brief Does an environment vector define a variable?
+ * @param envp The vector.
+ * @param name The name followed by `=`.
+ */
+static bool env_has(char **envp, const char *name) {
+ size_t n = strlen(name);
+ for (; *envp; envp++) {
+ if (!strncmp(*envp, name, n))
+ return true;
+ }
+ return false;
+}
+
+/**
+ * @brief Add the z/OS per-command variables to a command's environment.
+ *
+ * Called only when `set -o jobnames` or `set -o shareas` is on, so a
+ * shell that uses neither pays two byte tests per external command.
+ *
+ * - **jobnames:** `_BPX_JOBNAME` is set to the command's name in upper
+ * case (up to 8 characters valid in a job name), so that each command
+ * is visible under its own name in SDSF, SMF type 30 records and WLM
+ * classification, instead of as `USERID1` to `USERID9`. z/OS honours
+ * it only for a process that gets its own address space and only if
+ * the user may read `BPX.JOBNAME` in FACILITY (or is a superuser).
+ * - **shareas:** `_BPX_SHAREAS=YES` for a foreground command, which then
+ * runs in the shell's address space: no address space creation, which
+ * is most of the cost of starting a command on z/OS. Background
+ * commands and pipeline stages are left alone: they outlive or run
+ * beside the shell, and a shared address space serialises storage
+ * and is torn down with its owner.
+ *
+ * A variable the user has exported, or given on the command itself, is
+ * never overridden.
+ *
+ * @param envp The command's environment.
+ * @param name argv[0].
+ * @param foreground The shell waits for this command alone.
+ * @return The environment to use, in scratch memory.
+ */
+static char **zos_environ(char **envp, const char *name, bool foreground) {
+ char *add[2];
+ int nadd = 0;
+ if (opt.jobnames && !env_has(envp, "_BPX_JOBNAME=")) {
+ const char *base = strrchr(name, '/');
+ base = base ? base + 1 : name;
+ char *text = arena_alloc(&scratch, sizeof "_BPX_JOBNAME=" + 8);
+ char *p = stpcpy(text, "_BPX_JOBNAME=");
+ char *start = p;
+ for (; *base && p - start < 8; base++) {
+ char c = (char)toupper((unsigned char)*base);
+ if (!jobname_char(c) || (p == start && c >= '0' && c <= '9'))
+ continue;
+ *p++ = c;
+ }
+ *p = '\0';
+ if (p > start)
+ add[nadd++] = text;
+ }
+ if (opt.shareas && foreground && !env_has(envp, "_BPX_SHAREAS=")) {
+ static char shareas[] = "_BPX_SHAREAS=YES";
+ add[nadd++] = shareas;
+ }
+ if (!nadd)
+ return envp;
+ size_t n = 0;
+ while (envp[n])
+ n++;
+ char **env = arena_alloc(&scratch, (n + (size_t)nadd + 1) * sizeof *env);
+ memcpy(env, envp, n * sizeof *env);
+ for (int i = 0; i < nadd; i++)
+ env[n + (size_t)i] = add[i];
+ env[n + (size_t)nadd] = NULL;
+ return env;
+}
+
+/**
* @brief PATH hook: remembered paths are no longer valid.
*/
static void path_changed(const char *value) {
@@ -695,15 +866,67 @@ static void assign_temp(const simple_cmd *sc) {
* waited for (pipeline stages, `&`, command substitution).
* @return The exit status; 0 for an async launch.
*/
+/**
+ * @brief Apply `_TAG_REDIR_*` assignments given on an external command.
+ *
+ * Assignments before an external command go only into its environment,
+ * never into the shell's variables, so the hooks that keep #tag_redir
+ * current do not see `_TAG_REDIR_OUT=TXT cmd >f`. This sets #tag_redir
+ * from them for the duration of the redirections, and applies them to
+ * the pipe ends the command gets in a pipeline, as /bin/sh does.
+ *
+ * @param sc The command.
+ * @param saved Receives #tag_redir as it was.
+ * @return true if anything changed (call tag_prefix_end()).
+ */
+static bool tag_prefix_begin(const simple_cmd *sc, tag_mode saved[3]) {
+ static const char *const name[3] = {
+ "_TAG_REDIR_IN=", "_TAG_REDIR_OUT=", "_TAG_REDIR_ERR="};
+ bool any = false;
+ memcpy(saved, tag_redir, sizeof tag_redir);
+ for (int i = 0; i < sc->nassign; i++) {
+ const char *a = sc->atext[i];
+ if (strncmp(a, "_TAG_REDIR_", 11))
+ continue;
+ for (int k = 0; k < 3; k++) {
+ size_t n = strlen(name[k]);
+ if (!strncmp(a, name[k], n)) {
+ tag_redir[k] = tag_value(a + n);
+ any = true;
+ }
+ }
+ }
+ return any;
+}
+
+/**
+ * @brief Tag the pipe ends of a pipeline stage per its prefix, then put
+ * #tag_redir back.
+ */
+static void tag_prefix_end(const tag_mode saved[3], int in, int out) {
+ if (out >= 0 && tag_redir[TAG_OUT] != saved[TAG_OUT])
+ sys_redir_tag(out, TAG_OUT, tag_redir[TAG_OUT], true);
+ if (in >= 0 && tag_redir[TAG_IN] != saved[TAG_IN])
+ sys_redir_tag(in, TAG_IN, tag_redir[TAG_IN], false);
+ memcpy(tag_redir, saved, sizeof tag_redir);
+}
+
static int run_external(simple_cmd *sc, unsigned flags, int in, int out,
pid_t *async) {
const node *n = sc->n;
char **envp =
sc->nassign ? var_environ_with(sc->atext, sc->nassign) : var_environ();
+ if (opt.jobnames || opt.shareas)
+ envp = zos_environ(envp, sc->argv[0], !async && in < 0 && out < 0);
fdplan plan;
plan_init(&plan, in, out);
- if (!plan_redirs(&plan, n->redirs)) {
+ tag_mode saved_tags[3];
+ bool tags = SYS_MVS && sc->nassign && tag_prefix_begin(sc, saved_tags);
+ bool planned = plan_redirs(&plan, n->redirs);
+ if (tags)
+ tag_prefix_end(saved_tags, in, out);
+ if (!planned) {
plan_close(&plan);
return 2;
}
@@ -1669,6 +1892,9 @@ int eval_node(node *n, unsigned flags) {
case N_NOT:
status = !eval_node(n->u.body, EV_TESTED);
break;
+ case N_TIME:
+ status = eval_time(n, flags);
+ break;
case N_BG:
status = eval_background(n->u.body);
break;