summaryrefslogtreecommitdiffhomepage
path: root/README.md
blob: c2d3cacb77e4701f82e67a6cb5788696dd34b736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<p align="center">
  <img src="packaging/zash-logo.svg" alt="zash" width="220">
</p>

# zash

<p align="center">Sean Kurtz</p>

A small, fast POSIX shell for z/OS UNIX System Services, in the Almquist
(ash) tradition. zash aims to be for z/OS what FreeBSD's `sh` is for
FreeBSD: a lean, standards-following `/bin/sh` that is quick at running
scripts and still pleasant to type at.

It is written from scratch in C23. No code is taken from another shell.

## Contents

- [Why](#why)
- [Goals](#goals)
- [Non-goals](#non-goals)
- [Toolchain](#toolchain)
- [Building](#building)
- [Testing](#testing)
- [Status](#status)
- [Recommendations](#recommendations)
- [Roadmap](#roadmap)
- [License](#license)

## Why

The z/OS `/bin/sh` is dated, and the usual alternatives are bash and zsh.
Those are comfortable interactively but slow as scripting shells, large,
and encourage scripts that run nowhere else. On z/OS that cost is real:
CPU time is billed, and every `fork()` copies an address space.

zash is for shops that care about that:

- Low-MSU shops and anyone watching script CPU time. The shell does its
  work in-process wherever the language allows. When a process is needed it
  uses `spawn()`, so `_BPX_SHAREAS` can keep the child in the same address
  space.
- Shops that want a modern `/bin/sh`, not a different language. Scripts
  written for zash are POSIX `sh` scripts. They run under the existing
  `/bin/sh`, dash, FreeBSD sh and bash.
- People who type at it. File and command completion, history and line
  editing are in scope, provided they cost a script nothing.

It aims to be the dash of z/OS, but more usable interactively.

## Goals

1. A drop-in replacement for the z/OS `/bin/sh` for POSIX scripts,
   including the z/OS behaviours scripts rely on (`_BPX_SHAREAS`,
   `_BPX_SPAWN_SCRIPT`, `STEPLIB`, file tagging).
2. Script performance at least equal to `/bin/sh`, measured on z/OS,
   by using `spawn()` instead of `fork()` and by not creating a process at
   all for builtins, even in pipelines and command substitutions.
3. Interactive comfort that is free for scripts: job control, tab
   completion for files and commands, history, emacs- and vi-style
   editing. The keys are bash's, and `bind '^[[H'=beginning-of-line`
   moves one in the BSD shells' notation. A built-in editor is the
   default and libedit is a compile-time alternative. Neither is
   initialised by a non-interactive shell.
4. The Korn shell features z/OS scripts actually use: `print`,
   `whence`, `typeset`, `function name { }`, `(( ))`, `let`, aliases.
   They are accepted only where they cost other scripts nothing.
5. ASCII or EBCDIC, chosen at compile time. The source makes no
   assumption about the character set.
6. Small and readable. One directory, a plain POSIX Makefile, every
   function documented.
7. Heavily tested. See [Testing](#testing).

## Non-goals

- bash or ksh93 extensions: arrays, process substitution,
  `${var//a/b}`, brace expansion, coprocesses.
- Being buildable with the legacy z/OS XL C/C++ compiler (`xlc`).
- Portability to every UNIX. Linux is supported: most development and all
  sanitizer testing happens there. Other POSIX systems will probably work,
  but are not a target.

## Toolchain

| Platform | Compiler | Standard |
| --- | --- | --- |
| Linux | Clang | `-std=c23` |
| z/OS | IBM Open XL C/C++ (`ibm-clang`) | `-std=c23` |

The code is plain C23. It draws the line at the language itself: no
`<stdbit.h>`, `<stdckdint.h>` or other library additions newer than the
platforms' C libraries, so a system whose libc is older still builds it. See
[DESIGN.md](DESIGN.md#language-level) for the language level and the
coding style.

The z/OS build is 64-bit (`-m64`) XPLINK. It used to be 31-bit, because a
64-bit `fork()` cost 4.2 ms against 0.10 and a start 29.5 ms against 1.1;
both were the 64-bit C run-time being loaded out of the linklist on every
start, and with that run-time in the LPA the two widths cost the same. See
[bench/README.md](bench/README.md#is-it-the-address-mode-or-the-c-runtime)
for the measurements and [Recommendations](#recommendations) for the LPA
change on a z/OS system.

Where the z/OS code generator leaves measurable performance on the table,
a hot routine may be rewritten in HLASM. Any such routine keeps its C
version. That stays the reference, and it is what Linux builds use.

## Building

```sh
make                 # Linux, clang, -std=c23
make zash            # Linux: the same build, named exactly
make EDIT=libedit LDLIBS=-ledit   # use libedit, not the built-in editor
make check           # run the test suite
make asan            # build zash-asan (ASan + UBSan) and run the tests
make valgrind        # run the tests under Valgrind
make bench           # CPU time against dash, bash, ksh, zsh, oksh (or /bin/sh)
make compdb          # write compile_commands.json for clangd
make format tidy     # clang-format, clang-tidy
make install         # copy ./zash into $(DESTDIR)$(BINDIR)
make doc             # the source reference: docs/html, docs/zash-reference.pdf
make docs            # that, plus HTML and PDF of this file and DESIGN.md
make spec            # formal/zash-spec.pdf, typeset with typst
make other-docs      # spec and doc: the documentation tools z/OS lacks

make zos-ascii       # z/OS default (make, make zash): 64-bit ASCII ./zash
make zos-ebcdic      # z/OS EBCDIC (IBM-1047), writes ./zash-ebcdic
make zos-check       # z/OS: functional tests only, no sanitizers
make zos-heapcheck   # z/OS: the same tests with LE heap check zones on
make zos-lint        # z/OS code paths compiled on Linux, against shims
make mvs-asan        # the suite with SYS_MVS forced on, under ASan
make zos-debug       # z/OS: unoptimised ./zash-dbg for dbx
make zos-dbx         # z/OS: run ./zash-dbg under dbx
make zos-amblist     # z/OS: CSECT map of the program object
```

Set `CC`, `CLANG_FORMAT` and `CLANG_TIDY` to pick tool versions. The
Makefile is one flat, plain `.POSIX` makefile with no GNU extensions, so
any `make` can read it. On z/OS the default build, `make` or `make zash`, is
the ASCII `./zash`; only the EBCDIC variant gets a suffix (`./zash-ebcdic`). Variant builds
(sanitizers, the z/OS modes) are single compile-and-link commands over the
source list; the only places that re-enter `make` are the switchovers on z/OS
(`all`, `check`) and the LaTeX pass inside `doc`.

`make doc` needs Doxygen, and for the PDF a working `pdflatex` with the
packages Doxygen's LaTeX output uses (the Fedora list is a comment above
the `doc` target in the Makefile); without `pdflatex` it builds
`docs/html` and says so. `make docs` also needs `pandoc` and a
Chrome/Chromium to turn the two Markdown documents into HTML and PDF, and
`make spec` needs `typst` (the CLI, or the Python package from
`pip install typst`).

On z/OS the tree carries a zopen `buildenv`, so `zopen build -gp`
builds both test passes, installs into `$ZOPEN_INSTALL_DIR`, and writes a
pax under `install/`.  `zopen build -s` stops in the prepared shell.

## Testing

The project is tested at several levels:

- Behavioural tests for every feature, as shell scripts with expected
  output. Portable tests are also run under dash and the results compared,
  so a disagreement with an existing ash-family shell is always visible.
- AddressSanitizer and UndefinedBehaviorSanitizer builds run the whole
  suite on Linux.
- Valgrind runs the whole suite on Linux, including leak checking.
- Process accounting tests assert the exact process count of a
  construct (for example, `$(echo x)` creates none), so a performance
  regression fails the suite.
- Security tests (`tests/zos/security.sh`) check what the shell must
  never do: create a file more permissive than the umask allows, look for
  a command in the current directory, or run what an expansion or a file
  name produced.
- Interactive tests drive the shell through a pseudo-terminal: editor
  keys in both modes, completion, history, prompts and job control. They
  need python3 and are skipped without it.
- On z/OS `make zos-check` runs the same behavioural and
  process-accounting tests, plus a z/OS-only group (`tests/zos/`), in
  either character mode. There is no ASan, UBSan or Valgrind there, but
  Language Environment can put a check zone after every heap element and
  validate the heap on every request, so an overlay becomes a U4042
  abend: `make zos-heapcheck` runs the whole suite that way, and
  `tests/zos/fuzz.sh` runs its own cases under it. `zos-check` also puts a
  bundled `sleep` first in `PATH`, because `/bin/sleep` rejects a
  fractional interval and four tests of job ordering need one. Both
  character modes pass `zos-check`; under the heap checker two signal
  tests fail, as they do not in `zos-check` (see
  [DESIGN.md](DESIGN.md#testing)).
- A debugger is `lldb` on Linux (`make debug`, `make attach`) and
  `dbx` on z/OS (`make zos-debug`, `make zos-dbx`). `make zos-amblist`
  maps the CSECTs of the program object when a module-level question
  comes up. Break by function (`st in main`) or by address. On a build
  from ibm-clang, `st at "file":line` does not resolve the source. `next`
  and `where` report file and line correctly. The tests use nothing
  beyond POSIX utilities (`awk`, `sed`, `tr`, `wc`, `sort`, `cat`,
  `grep`, `sleep`), and the few that depend on ASCII code points are
  skipped automatically for an EBCDIC build.

See [tests/README.md](tests/README.md) for the layout.

Memory and undefined-behaviour bugs are rarely platform specific: a
use-after-free Valgrind finds on Linux is the same bug on z/OS, where
there is no Valgrind. The shell stays portable, with the z/OS-specific
code in one small file. That is what makes the Linux tooling useful.

## Status

On Linux the shell is feature-complete for its first release: the
scripting core, the FreeBSD and Korn extensions, job control, and the
interactive layer (built-in editor with emacs and vi modes, completion,
history and `fc`, libedit optional). It builds warning-free with Clang as
C23 and passes its suite plain, under ASan/UBSan, and under Valgrind.

On z/OS it builds in both character modes with Open XL, 64-bit, passes
its suite (including the `zos/` group) and debugs with `dbx`. The
LE heap-checker run is the one exception: two signal tests fail there and
pass in `zos-check`. `make zos-debug`, `zos-dbx`, `zos-heapcheck` and
`zos-amblist` are the entry points. Data set redirection and `tso` are
the parts still written from documentation. See
[DESIGN.md](DESIGN.md#roadmap).

CPU seconds (user+sys, children included) on Linux x86-64, `make bench`.
Timings vary by a few hundredths between runs, and the rows that create
processes by more:

| benchmark | zash | dash | bash | ksh93 | oksh |
| --- | --- | --- | --- | --- | --- |
| `$(builtin)` x 40000 | 0.01 | 8.20 | 17.99 | 0.11 | 25.24 |
| function calls | 0.03 | 0.07 | 0.25 | 0.13 | 0.12 |
| arithmetic loop | 0.07 | 0.19 | 0.53 | 0.24 | 0.31 |
| `let` and integer variables | 0.10 | n/a | 0.78 | 0.35 | 0.43 |
| `(( ))` as a command | 0.06 | n/a | 0.33 | 0.09 | 0.30 |
| `[[ ]]` in a loop | 0.04 | n/a | 0.29 | 0.12 | n/a |
| `[ ]` tests in a loop | 0.05 | 0.16 | 0.43 | 0.15 | 0.22 |
| `case` dispatch | 0.03 | 0.07 | 0.20 | 0.09 | 0.14 |
| small pipelines and `$(a \| b)` | 1.10 | 2.35 | 3.16 | 1.33 | 2.32 |
| 2000 external commands | 0.55 | 0.53 | 0.81 | 0.56 | 0.84 |
| subshells | 0.01 | 0.37 | 0.62 | 0.01 | 0.48 |
| background jobs | 0.03 | 0.03 | 0.06 | 0.04 | 0.03 |
| redirections | 0.07 | 0.08 | 0.11 | 0.09 | 5.85 |
| here-documents | 0.03 | 0.03 | 0.08 | 0.10 | 0.13 |
| `while read` over a file | 0.04 | 0.07 | 0.21 | 0.12 | 0.10 |
| recursion | 0.02 | 0.04 | 0.17 | 0.05 | 0.06 |
| `printf` | 0.09 | 0.12 | 0.23 | 0.16 | 19.70 |
| `getopts` | 0.02 | 0.03 | 0.09 | 0.03 | 0.05 |
| parameter trimming | 0.05 | 0.09 | 0.32 | 0.20 | 0.14 |
| `${v/a/b}` substitutions | n/a | n/a | 0.17 | 0.08 | n/a |
| IFS field splitting | 0.03 | 0.04 | 0.11 | 0.05 | 0.06 |
| pattern matching and `case` | 0.11 | 0.18 | 0.53 | 0.38 | 0.35 |
| count a log by level and hour | 0.04 | 0.05 | 0.13 | 0.07 | 0.08 |
| render and check 100 configs | 0.38 | 0.46 | 0.53 | 0.26 | 0.52 |

The `bench/` scripts double as tests: `bench/run.sh` compares each
shell's output with the reference shell's, so a wrong answer fails
instead of merely being timed. `bench/report.sh` (count a log by level
and hour) and `bench/deploy.sh` (render and check 100 configs) are the
two that look most like the scripts this shell is for.

Running an external command costs zash about what it costs dash on
Linux, where `fork()` is cheap. On z/OS it is not, and that is where the
gap opens. The same benchmarks on z/OS (ASCII build), run
as `sh bench/run.sh $(pwd)/zash /bin/sh bash zsh oksh`. The `z/OS sh`
column is IBM's `/bin/sh`. It is not ksh93 (its usage string is
`sh [-abCefhikKLmnprtuvx]`, and it rejects `${.sh.version}`), so `[[ ]]`,
`let` and `typeset -i` are measured against it, and the `(( ))` command,
which ksh93 added, is `n/a` there: IBM's `sh` reads `((1))` as two nested
subshells and reports `1: FSUM7351 not found`. `run.sh` gives that
column the label `z/OS sh` when it is handed `/bin/sh` on z/OS:

| benchmark | zash | z/OS sh | bash | zsh | oksh |
| --- | --- | --- | --- | --- | --- |
| `$(builtin)` x 40000 | 0.04 | 19.55 | 283.14 | 225.96 | port bug |
| function calls | 0.08 | 0.68 | 1.12 | 0.60 | 0.52 |
| arithmetic loop | 0.20 | 1.54 | 2.11 | 0.96 | 1.34 |
| `let` and integer variables | 0.25 | 2.24 | 3.16 | 1.47 | 1.79 |
| `(( ))` as a command | 0.13 | n/a | 1.30 | 0.38 | 1.15 |
| `[[ ]]` in a loop | 0.08 | 0.95 | 1.16 | 0.28 | n/a |
| `[ ]` tests in a loop | 0.15 | 1.39 | 1.97 | 1.07 | 0.96 |
| `case` dispatch | 0.08 | 0.64 | 0.80 | 0.33 | 0.59 |
| small pipelines and `$(a \| b)` | 0.77 | 1.69 | 34.78 | 28.00 | port bug |
| 2000 external commands | 0.57 | 0.52 | 14.22 | 11.64 | 10.12 |
| subshells | 0.00 | 0.79 | 20.90 | 16.87 | 14.57 |
| background jobs | 0.93 | 0.14 | 1.36 | 1.09 | 0.92 |
| redirections | 0.28 | 0.36 | 0.46 | 0.33 | 61.63 |
| here-documents | 0.29 | 1.20 | 0.49 | 1.15 | 0.85 |
| `while read` over a file | 0.30 | 0.91 | 0.84 | 3.31 | port bug |
| recursion | 0.03 | 0.24 | 0.63 | 0.25 | 0.20 |
| `printf` | 1.17 | 1.60 | 1.92 | 1.40 | 207.19 |
| `getopts` | 0.06 | 0.28 | 0.40 | 0.21 | 0.16 |
| parameter trimming | 0.12 | 0.99 | 1.07 | 0.57 | 0.55 |
| `${v/a/b}` substitutions | n/a | n/a | 0.66 | 0.28 | n/a |
| IFS field splitting | 0.05 | 0.32 | 0.43 | differs | 0.24 |
| pattern matching and `case` | 0.27 | 3.04 | 1.83 | 0.98 | 1.11 |
| count a log by level and hour | 0.17 | 0.55 | 0.51 | 1.21 | port bug |
| render and check 100 configs | 0.20 | 0.24 | 2.91 | 2.40 | port bug |
| file tagging and auto-conversion | 0.41 | 1.04 | 8.48 | 7.23 | n/a |

The gap is process creation: every command substitution and pipeline
stage costs a full z/OS spawn there, from 14 s (bash, 2000 commands) to
283 s (bash, 40000 substitutions). zash does those substitutions and
`print`/`echo` pipeline stages in-shell, so its rows stay near the
`z/OS sh` column's, which is what z/OS shops already pay. Plain
external commands cost zash what they cost `sh` (0.57 against 0.52), so
nothing was traded away for the rest.

A `fork()` costs more on z/OS than a `spawn()`, because the fork copies
the address space where the spawn can share it. That used to be visible
in the subshell row: 3000 `( : )` shells, each forking, took 14.2 s. A
subshell whose body is one output-only builtin now runs in the shell,
like a pipeline stage already did, so the same 3000 cost 0.00 s here
against the 0.79 of `sh`.

Both costs in that paragraph, fork and start, were the 64-bit C run-time
going to disk on every process: 4.2 ms per `fork()` and 29.5 ms per start
against 0.10 and 1.1. Put the run-time in the dynamic LPA and a 64-bit
`fork()` costs 0.10 ms and a start 1.5 ms, the 31-bit numbers, so zash is
built 64-bit and the rows above that move when it forks are measured that
way too: `background jobs` 0.93 is the 2.1-era 64-bit run, and the
`( : )` subshell row never forks at all, the in-shell fast path takes it.
[bench/README.md](bench/README.md) has the before-and-after table, and
[Recommendations](#recommendations) says what to change on the system.

The `port bug` entries are the third-party `oksh` port's, not zash's:
on there `echo hi | cat` prints two bytes of garbage, so those rows
measure nothing and the run reports a differing result. n/a means the
shell lacks the feature the benchmark uses, and `differs` means the
shell computed something else: zsh, which does not split fields on an
unquoted expansion even in sh mode, answers the IFS benchmark with one
word instead of eight.

## Recommendations

### z/OS: put the C run-time in the LPA

A z/OS system that runs 64-bit programs often should load the 64-bit C
run-time once, into the dynamic LPA, from the linklist library it already
lives in. Without it every program load pays to load and relocate the
same DLL for itself; it is also the one change that made a 64-bit `fork()`
as cheap as a 31-bit one on the machine these numbers come from. One
command:

```text
SETPROG LPA,ADD,MODNAME=CELQLIB,DSNAME=CEE.SCEERUN2
```

and the same for `CELQDCPP`, `CELQDEST`, `CELQDMNP`, `CELQDSNF`,
`CELQIOTD`, `CELQMCHK`, `CELQV004` and `IBMHV011` --- about 14 MB in all.
The permanent form is an `LPA ADD` in the PROGxx member that `PROG=` in
`IEASYSxx` names:

```text
LPA ADD DSNAME(CEE.SCEERUN2) MODNAME(CELQDCPP,CELQDEST,CELQDMNP,CELQDSNF,
                                       CELQIOTD,CELQLIB,CELQMCHK,
                                       CELQV004,IBMHV011)
```

The installer may want `CSAMIN=(4M,0)` on that statement: a dynamic LPA
add takes extended common storage, and this system's LPA runs with no
floor at all (`D PROG,LPA,CSAMIN` answers `0,0`). `notes/celq-lpa.rexx`
and `notes/celq-lpa.jcl` issue the command through SDSF if the operator
would rather not type it.

On the machine measured here PLPA is full, so the IPL-time LPA already
overflows into the common page data set (`ILR005E`). That is a system to
clean up rather than a reason to skip the add: the add put 12 MB into the
common area, which had 255 MB free.

## Roadmap

Linux is feature-complete for the first release (see [Status](#status)).
On z/OS, in order:

1. Manual pages: `zash(1)` and a page per builtin group, installed by
   `make install`, kept beside the code they document.
2. The editor on a real terminal: interactive tests on a 3270-style
   terminal, in both character modes.
3. `_BPX_SHAREAS`: the benchmark rows above re-run under
   `_BPX_SHAREAS=YES`, `NO` and `REUSE`.
4. The remaining `VERIFY` notes: `sys_zos.c`, `charset.h`, the EBCDIC
   `$'\cX'` codes and the data set paths, each checked on hardware and
   the note removed.
5. z/OS conveniences: `tag`, `alloc`/`free` and `tso` builtins, MVS
   data set redirection, job names, message IDs.

[DESIGN.md](DESIGN.md#roadmap) has the longer version, including why each
item is where it is.

## License

SPDX: `MIT OR Apache-2.0 OR BSD-2-Clause`, at your option. See
[LICENSE](LICENSE).