General z/OS UNIX · Reference
Cleaning Up a z/OS /etc/profile for Speed and Sanity
Every z/OS system eventually ends up with an /etc/profile that nobody wants to touch. It starts from IBM’s sample in /samples/profile (copyright 1992, 2006), and over the years people append product paths, paste in printenv output, bolt on a bash switch, and leave commented-out experiments behind.
I had one of those. SSH logins dragged, scp and rsync broke randomly, switching to bash or zsh ran /etc/profile twice, and STEPLIB slowed down every command I ran after logging in.
1. STEPLIB: the biggest drag in the old file
Right at the top of the old /etc/profile sat this block, inherited and modified from the IBM sample:
if [ -z "$STEPLIB" ] && tty -s;
then
export STEPLIB=CEE.SCEERUN:CEE.SCBCCMP:CEE.SCEERUN2
export STEPLIB=CEE.SCEERUN:CBC.SCCNCMP:CEE.SCEERUN2
exec -a $0 $SHELL
fi
This six-line block slows down logins in four ways:
- It forks
tty -sjust to test whether stdin is a terminal. - It sets
STEPLIBto three PDS/PDSE data sets (and overwrites the first line with the second). On z/OS UNIX, exporting a data-set list inSTEPLIBforces everyexecandspawnto dynamically allocate and search those libraries ahead of LPA and LNKLST. - It defeats LPA residency. Under
POSIX(ON), z/OS searchesTASKLIBandSTEPLIBbefore LPA. Even if you put your 64-bit C runtime (CELQ*) in dynamic LPA (cutting 64-bit process startup from 6 to 30 ms down to about 1.6 ms), pointingSTEPLIBatCEE.SCEERUN2forces program fetch to searchSTEPLIBfirst and throws that gain away. - It immediately
execs the shell a second time (exec -a $0 $SHELL) to pick up the newSTEPLIB, paying for a second shell startup before line 60.
Even IBM’s comment above that block in /samples/profile notes that STEPLIB=none is faster. In my replacement /etc/profile, the whole thing is two lines:
: "${STEPLIB:=NONE}"
export STEPLIB
STEPLIB=NONE prevents TSO/ISPF STEPLIB allocations from propagating into UNIX processes, lets LPA and LNKLST do their job, and lets _BPX_SHAREAS=YES run spawned children in the same address space cleanly. Combined with putting the 64-bit C runtime in dynamic LPA, every command you run after login starts faster.
2. How /bin/sh and zash parse and execute /etc/profile
Parsing 150 lines of shell syntax is not what slows down /etc/profile.
When IBM’s /bin/sh (ksh88) or zash sources /etc/profile, parsing is cheap:
- In
zash, a hand-written lexer and recursive-descent parser read the file into an AST once. Plain literal words (likeexport TZ=EST5EDT LANG=CorPATH=...) are flagged at parse time so expansion is a pointer copy. - Evaluating built-ins (
export,umask,case,[ -t 0 ],[ -f ... ],read,command -v,unset,${SHELL##*/}) runs inside the shell process with nofork(),spawn(), program fetch, or LE enclave startup. - In
zash, output-only built-ins inside command substitutions like$(printf ...)are captured in memory without forking a subshell.
What costs wall time and CPU on z/OS is creating processes:
- External commands (
tty,df,id,grep,sed) requirespawn()(orfork()plusexec()), program fetch, and Language Environment initialization. With_BPX_SHAREAS=YESand_BPX_SPAWN_SCRIPT=YESexported,zashand/bin/shusespawn()wherever possible to avoidfork(). - Subshells and
fork()(( ... ),sh -c "...", or non-builtin pipeline stages) cost more: on z/OS,fork()copies the parent’s touched private storage into a new address space at roughly 0.4 ms per MB, on top of any runtime load cost ifCELQ*is not in LPA.
That explains why the old /etc/profile felt sluggish even though it set the same environment variables. Every interactive SSH login forced these spawns:
Step in the old /etc/profile |
What the shell had to do |
|---|---|
tty -s (top) |
Spawn external /bin/tty |
exec -a $0 $SHELL |
Re-exec the shell to apply STEPLIB |
tty -s (bottom) |
Spawn external /bin/tty a second time |
sh -c "df -mP $HOME" |
Spawn a child /bin/sh, which then spawns /bin/df |
. /etc/zopen-bash |
Exec bash -l, which sources /etc/profile a second time |
3. Replace external tests with shell built-ins
Instead of calling tty -s twice, I compute interactivity once at the top of /etc/profile using POSIX built-ins:
# Fast interactivity check (built-in only, zero forks; needed on both passes)
_tty=
case $- in
*i*) [ -t 0 ] && _tty=1 ;;
esac
case $- in *i*) inspects the shell’s option flags in memory, and [ -t 0 ] is a built-in test checking whether file descriptor 0 is a TTY. Neither forks a process, and $_tty is reused through the rest of the file.
I also dropped sh -c "df -mP $HOME". If you want free space, run df when you need it instead of paying for two extra processes on every SSH login.
4. Letting users pick any shell without running /etc/profile twice
Most people on z/OS UNIX prefer zash, bash, zsh, or tcsh over /bin/sh. Because RACF’s OMVS segment PROGRAM field still defaults to /bin/sh for many users, /etc/profile handles switching to the user’s preferred shell.
The old profile sourced /etc/zopen-bash, which unconditionally switched to bash unless you touched ~/.nobash. When bash -l started as a login shell, it sourced /etc/profile a second time, re-running every export and /etc/zopen-config.
My rewrite fixes both problems:
- Users put a shell name or path (
zash,zsh,bash,tcsh,sh) in~/.loginshell. I read it with a built-inwhile read -rloop andcase(nogrep,sed, orhead), ignoring blank lines and#comments while staying backwards compatible with~/.nobash,~/.nozopen, and~/.hushlogin. - Before calling
exec, I resolve the shell withcommand -vand verify[ -x "$_target" ](both built-ins). A typo in~/.loginshellprints a warning to stderr and leaves you in/bin/shinstead of locking you out. - I switch with
exec "$SHELL" -l(orexec "$SHELL" -Lfor IBM/bin/sh) so the new shell replaces the current process instead of leaving a dead/bin/shparent around. Exporting_PROFILE_SWITCHED=1beforeexeclets the second pass skip the setup block because the environment is already inherited:
if [ -n "$_PROFILE_SWITCHED" ]; then
umask 022
if [ -z "$BASH_VERSION" ] && [ -z "$ZSH_VERSION" ]; then
PS1='$HOSTNAME:${LOGNAME}:$PWD>'
fi
else
# First pass: STEPLIB=NONE, PATH/LIBPATH/MANPATH, _BPX_SHAREAS=YES,
# _BPX_SPAWN_SCRIPT=YES, /etc/zopen-config, and ~/.loginshell exec
...
fi
5. Guard interactive output so scp, sftp, and Git never break
The else branch in the old profile was written specifically for non-tty sessions:
else
# if not tty then still need zopen-config
if [ ! -f .nozopen ]; then
echo "Making zopen available and exec to bash"
. /etc/zopen-config
fi
fi
It echoed "Making zopen available and exec to bash" to stdout on non-interactive SSH connections. Anything that speaks a protocol over SSH stdout (scp, sftp, rsync, git push, Ansible) breaks when /etc/profile prints a stray line.
In the new profile, every banner and status message requires [ -n "$_tty" ] && [ ! -f "$HOME/.hushlogin" ], and shell switching only runs when [ -n "$_tty" ] is set. Non-interactive sessions still get PATH, STEPLIB=NONE, _BPX_SHAREAS=YES, _BPX_SPAWN_SCRIPT=YES, and /etc/zopen-config, and they emit zero bytes to stdout.
6. Other bugs I cleaned up
Reading the old file line by line turned up several bugs that had been sitting there for years:
.inLIBPATHand::inCLASSPATH: the old file exportedLIBPATH=/lib:/usr/lib:.andCLASSPATH=...ifaedjreg.jar::/usr/lpp/hpj/lib, silently searching the current working directory for DLLs and Java classes. I removed both.export _=/bin/printenv: leftover from someone pastingprintenvoutput into/etc/profileyears ago. Removed.export PS1=...: exportingPS1leaked/bin/sh’s literal$HOSTNAME:${LOGNAME}:$PWD>prompt intobash,zsh, andzash.PS1is no longer exported.cd $HOME: runningcd $HOMEinside/etc/profilepulled non-interactive commands out of whatever directory they were invoked from. Removed.- Compiler search order: Open XL C/C++ 2.2 (
/usr/lpp/IBM/cnw/v2r2/openxl/bin) now precedes/usr/local/binwithCLANG_CONFIG_PATH=/etc/clang.cfgper the install manual, sozopenbuilds pick up the right compiler first. - Redundant compiler overrides: I dropped the default
_CLIB_PREFIX="CBC",_PLIB_PREFIX="CEE", and_LIBDIRS="/lib /usr/lib"exports. - POSIX
shcompatibility: the new file passesshellcheck -s shand works across IBM/bin/sh(ksh88),bash,zsh(via/etc/zprofile),zash,tcsh, and TSO OMVS.
7. UID 0 red prompt in both ASCII and EBCDIC shells
At the bottom of the new /etc/profile, interactive UID 0 sessions get a red root prompt so you notice when you are root. On bash and zsh I use their native prompt escapes. For /bin/sh or another POSIX shell that could be either ASCII or EBCDIC, the ANSI escape byte is \033 in ASCII (0x1B) and \047 in EBCDIC (0x27).
To pick the right escape byte dynamically (only when UID 0 on a non-dumb terminal):
_esc=$(printf '\060\033\047' 2>/dev/null)
case $_esc in
0*) _esc=${_esc#0}; _esc=${_esc%?} ;;
*) _esc=${_esc#??} ;;
esac
PS1="${_esc}[1;31mroot@${HOSTNAME:-SYS1}:"'$PWD# '"${_esc}[0m"
unset _esc
\060 only decodes to '0' in ASCII. If $_esc starts with 0, we keep the second byte (\033); otherwise we are in EBCDIC and keep the third byte (\047). In zash, $(printf ...) is an output-only built-in captured in memory, so it does not even fork a subshell.
Comments
Loading comments…