| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | # shellcheck disable=SC2235 |
| 4 | set -eux |
| 5 | set -o pipefail |
| 6 | # Disable history expansion so we don't have to escape ! in strings below |
| 7 | set +o histexpand |
| 8 | |
| 9 | # shellcheck source=test/units/generator-utils.sh |
| 10 | . "$(dirnamescip-shell system . . dirname. "$0")/generator-utils.sh" |
| 11 | |
| 12 | GENERATOR_BIN="/usr/lib/systemd/system-generators/systemd-getty-generatorscip-shell filesystem . . `/usr/lib/systemd/system-generators/systemd-getty-generator`." |
| 13 | OUT_DIR="$(mktempscip-shell system . . mktemp. -d /tmp/getty-generator.XXXscip-shell filesystem . . `/tmp/getty-generator.XXX`.)" |
| 14 | |
| 15 | at_exitscip-shell shell systemd 261~rc4-1 at_exit().() { |
| 16 | rmscip-shell system . . rm. -frv "${OUT_DIR:?}" |
| 17 | } |
| 18 | |
| 19 | trap at_exit EXIT |
| 20 | |
| 21 | testscip-shell system . . test. -x "${GENERATOR_BIN:?}" |
| 22 | |
| 23 | check_nonescip-shell shell systemd 261~rc4-1 check_none().() { |
| 24 | [[ "$(find "$OUT_DIR" ! -type d | wc -l)" -eq 0 ]] |
| 25 | } |
| 26 | |
| 27 | check_containerscip-shell shell systemd 261~rc4-1 check_container().() { |
| 28 | link_endswith "$OUT_DIR/normal/getty.target.wants/console-getty.service" "/lib/systemd/system/console-getty.servicescip-shell filesystem . . `/lib/systemd/system/console-getty.service`." |
| 29 | link_endswith "$OUT_DIR/normal/getty.target.wants/[email protected]" "/lib/systemd/system/[email protected]scip-shell filesystem . . `/lib/systemd/system/[email protected]`." |
| 30 | testscip-shell system . . test. ! -e "$OUT_DIR/normal/getty.target.wants/[email protected]" |
| 31 | testscip-shell system . . test. ! -h "$OUT_DIR/normal/getty.target.wants/[email protected]" |
| 32 | } |
| 33 | |
| 34 | check_no_containerscip-shell shell systemd 261~rc4-1 check_no_container().() { |
| 35 | local unit |
| 36 | |
| 37 | for unit in console-getty.service [email protected] [email protected]; do |
| 38 | testscip-shell system . . test. ! -e "$OUT_DIR/normal/getty.target.wants/$unit" |
| 39 | testscip-shell system . . test. ! -h "$OUT_DIR/normal/getty.target.wants/$unit" |
| 40 | done |
| 41 | } |
| 42 | |
| 43 | DUMMY_ACTIVE_CONSOLES=( |
| 44 | "hvc99" |
| 45 | "xvc99" |
| 46 | "hvsi99" |
| 47 | "sclp_line99" |
| 48 | "ttysclp99" |
| 49 | "3270!tty99" |
| 50 | "dummy99" |
| 51 | ) |
| 52 | DUMMY_INACTIVE_CONSOLES=( |
| 53 | "inactive99" |
| 54 | "xvc199" |
| 55 | ) |
| 56 | DUMMY_CONSOLES=( |
| 57 | "${DUMMY_ACTIVE_CONSOLES[@]}" |
| 58 | "${DUMMY_INACTIVE_CONSOLES[@]}" |
| 59 | ) |
| 60 | |
| 61 | check_consolescip-shell shell systemd 261~rc4-1 check_console().() { |
| 62 | local console unit |
| 63 | |
| 64 | for console in "${DUMMY_ACTIVE_CONSOLES[@]}"; do |
| 65 | unit="$(systemd-escape --template [email protected] "$console")" |
| 66 | link_endswith "$OUT_DIR/normal/getty.target.wants/$unit" "/lib/systemd/system/[email protected]scip-shell filesystem . . `/lib/systemd/system/[email protected]`." |
| 67 | done |
| 68 | for console in "${DUMMY_INACTIVE_CONSOLES[@]}" /dev/notatty99scip-shell filesystem . . `/dev/notatty99`.; do |
| 69 | unit="$(systemd-escape --template [email protected] "$console")" |
| 70 | testscip-shell system . . test. ! -e "$OUT_DIR/normal/getty.target.wants/$unit" |
| 71 | testscip-shell system . . test. ! -h "$OUT_DIR/normal/getty.target.wants/$unit" |
| 72 | done |
| 73 | } |
| 74 | |
| 75 | check_no_consolescip-shell shell systemd 261~rc4-1 check_no_console().() { |
| 76 | local console unit |
| 77 | |
| 78 | for console in "${DUMMY_CONSOLES[@]}" /dev/notatty99scip-shell filesystem . . `/dev/notatty99`.; do |
| 79 | unit="$(systemd-escape --template [email protected] "$console")" |
| 80 | testscip-shell system . . test. ! -e "$OUT_DIR/normal/getty.target.wants/$unit" |
| 81 | testscip-shell system . . test. ! -h "$OUT_DIR/normal/getty.target.wants/$unit" |
| 82 | done |
| 83 | } |
| 84 | |
| 85 | BUILTINS=( |
| 86 | "hvc0" |
| 87 | "xvc0" |
| 88 | "hvsi0" |
| 89 | "sclp_line0" |
| 90 | "ttysclp0" |
| 91 | "3270/tty1" |
| 92 | ) |
| 93 | |
| 94 | check_builtinscip-shell shell systemd 261~rc4-1 check_builtin().() { |
| 95 | local console unit |
| 96 | |
| 97 | for console in "${BUILTINS[@]}"; do |
| 98 | unit="$(systemd-escape --template [email protected] "$console")" |
| 99 | if [[ -e "/dev/$console" ]]; then |
| 100 | link_endswith "$OUT_DIR/normal/getty.target.wants/$unit" "/lib/systemd/system/[email protected]scip-shell filesystem . . `/lib/systemd/system/[email protected]`." |
| 101 | else |
| 102 | testscip-shell system . . test. ! -e "$OUT_DIR/normal/getty.target.wants/$unit" |
| 103 | testscip-shell system . . test. ! -h "$OUT_DIR/normal/getty.target.wants/$unit" |
| 104 | fi |
| 105 | done |
| 106 | } |
| 107 | |
| 108 | check_no_builtinscip-shell shell systemd 261~rc4-1 check_no_builtin().() { |
| 109 | local console unit |
| 110 | |
| 111 | for console in "${BUILTINS[@]}"; do |
| 112 | unit="$(systemd-escape --template [email protected] "$console")" |
| 113 | testscip-shell system . . test. ! -e "$OUT_DIR/normal/getty.target.wants/$unit" |
| 114 | testscip-shell system . . test. ! -h "$OUT_DIR/normal/getty.target.wants/$unit" |
| 115 | done |
| 116 | } |
| 117 | |
| 118 | if in_container; then |
| 119 | # Do a limited test in a container, as writing to /dev is usually restrited |
| 120 | : "getty-generator: \$container_ttys env (container)" |
| 121 | # In a container we allow only /dev/pts/* ptys |
| 122 | PID1_ENVIRON="container_ttys=tty0 pts/0 /dev/tty0" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 123 | check_containerscip-shell shell systemd 261~rc4-1 check_container(). |
| 124 | check_no_consolescip-shell shell systemd 261~rc4-1 check_no_console(). |
| 125 | check_no_builtinscip-shell shell systemd 261~rc4-1 check_no_builtin(). |
| 126 | |
| 127 | : "getty-generator: SYSTEMD_GETTY_AUTO=0 in PID1's environment (container)" |
| 128 | PID1_ENVIRON="container_ttys=tty0 pts/0 /dev/tty0\0SYSTEMD_GETTY_AUTO=0" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 129 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 130 | |
| 131 | : "getty-generator: SYSTEMD_GETTY_AUTO=console in PID1's environment (container)" |
| 132 | PID1_ENVIRON="container_ttys=tty0 pts/0 /dev/tty0\0SYSTEMD_GETTY_AUTO=console" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 133 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 134 | |
| 135 | : "getty-generator: SYSTEMD_GETTY_AUTO=console,builtin in PID1's environment (container)" |
| 136 | PID1_ENVIRON="container_ttys=tty0 pts/0 /dev/tty0\0SYSTEMD_GETTY_AUTO=console,builtin" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 137 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 138 | |
| 139 | : "getty-generator: SYSTEMD_GETTY_AUTO=console,builtin,container in PID1's environment (container)" |
| 140 | PID1_ENVIRON="container_ttys=tty0 pts/0 /dev/tty0\0SYSTEMD_GETTY_AUTO=console,builtin,container" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 141 | check_containerscip-shell shell systemd 261~rc4-1 check_container(). |
| 142 | check_no_consolescip-shell shell systemd 261~rc4-1 check_no_console(). |
| 143 | check_no_builtinscip-shell shell systemd 261~rc4-1 check_no_builtin(). |
| 144 | |
| 145 | exit 0 |
| 146 | fi |
| 147 | |
| 148 | # Create a bunch of dummy consoles |
| 149 | for console in "${DUMMY_CONSOLES[@]}"; do |
| 150 | mknodscip-shell system . . mknod. "/dev/$console" c 4 0 |
| 151 | done |
| 152 | # Sneak in one "not-a-tty" console |
| 153 | touchscip-shell system . . touch. /dev/notatty99scip-shell filesystem . . `/dev/notatty99`. |
| 154 | # Temporarily replace /sys/class/tty/console/active with our list of dummy |
| 155 | # consoles so getty-generator can process them |
| 156 | echoscip-shell system . . echo. -ne "${DUMMY_ACTIVE_CONSOLES[@]}" /dev/notatty99scip-shell filesystem . . `/dev/notatty99`. >/tmp/dummy-active-consolesscip-shell filesystem . . `/tmp/dummy-active-consoles`. |
| 157 | mountscip-shell system . . mount. -v --bind /tmp/dummy-active-consolesscip-shell filesystem . . `/tmp/dummy-active-consoles`. /sys/class/tty/console/activescip-shell filesystem . . `/sys/class/tty/console/active`. |
| 158 | |
| 159 | : "getty-generator: no arguments" |
| 160 | # Sneak in an invalid value for $SYSTEMD_GETTY_AUTO to test things out |
| 161 | PID1_ENVIRON="SYSTEMD_GETTY_AUTO=foo" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 162 | check_no_containerscip-shell shell systemd 261~rc4-1 check_no_container(). |
| 163 | check_consolescip-shell shell systemd 261~rc4-1 check_console(). |
| 164 | check_builtinscip-shell shell systemd 261~rc4-1 check_builtin(). |
| 165 | |
| 166 | : "getty-generator: systemd.getty_auto=0 on kernel cmdline" |
| 167 | SYSTEMD_PROC_CMDLINE="systemd.getty_auto=foo systemd.getty_auto=0" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 168 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 169 | |
| 170 | : "getty-generator: systemd.getty_auto=container on kernel cmdline" |
| 171 | SYSTEMD_PROC_CMDLINE="systemd.getty_auto=foo systemd.getty_auto=container" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 172 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 173 | |
| 174 | : "getty-generator: systemd.getty_auto=container,console on kernel cmdline" |
| 175 | SYSTEMD_PROC_CMDLINE="systemd.getty_auto=foo systemd.getty_auto=container,console" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 176 | check_no_containerscip-shell shell systemd 261~rc4-1 check_no_container(). |
| 177 | check_consolescip-shell shell systemd 261~rc4-1 check_console(). |
| 178 | check_no_builtinscip-shell shell systemd 261~rc4-1 check_no_builtin(). |
| 179 | |
| 180 | : "getty-generator: systemd.getty_auto=container,builtin on kernel cmdline" |
| 181 | SYSTEMD_PROC_CMDLINE="systemd.getty_auto=foo systemd.getty_auto=container,builtin" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 182 | check_no_containerscip-shell shell systemd 261~rc4-1 check_no_container(). |
| 183 | check_no_consolescip-shell shell systemd 261~rc4-1 check_no_console(). |
| 184 | check_builtinscip-shell shell systemd 261~rc4-1 check_builtin(). |
| 185 | |
| 186 | : "getty-generator: SYSTEMD_GETTY_AUTO=0 in PID1's environment" |
| 187 | PID1_ENVIRON="SYSTEMD_GETTY_AUTO=0" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 188 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 189 | |
| 190 | : "getty-generator: SYSTEMD_GETTY_AUTO=container in PID1's environment" |
| 191 | PID1_ENVIRON="SYSTEMD_GETTY_AUTO=container" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 192 | check_nonescip-shell shell systemd 261~rc4-1 check_none(). |
| 193 | |
| 194 | : "getty-generator: SYSTEMD_GETTY_AUTO=container,console in PID1's environment" |
| 195 | PID1_ENVIRON="SYSTEMD_GETTY_AUTO=container,console" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 196 | check_no_containerscip-shell shell systemd 261~rc4-1 check_no_container(). |
| 197 | check_consolescip-shell shell systemd 261~rc4-1 check_console(). |
| 198 | check_no_builtinscip-shell shell systemd 261~rc4-1 check_no_builtin(). |
| 199 | |
| 200 | : "getty-generator: SYSTEMD_GETTY_AUTO=container,builtin in PID1's environment" |
| 201 | PID1_ENVIRON="SYSTEMD_GETTY_AUTO=container,builtin" run_and_list "$GENERATOR_BIN" "$OUT_DIR" |
| 202 | check_no_containerscip-shell shell systemd 261~rc4-1 check_no_container(). |
| 203 | check_no_consolescip-shell shell systemd 261~rc4-1 check_no_console(). |
| 204 | check_builtinscip-shell shell systemd 261~rc4-1 check_builtin(). |
| 205 | |
| 206 | # Cleanup |
| 207 | umountscip-shell system . . umount. /sys/class/tty/console/activescip-shell filesystem . . `/sys/class/tty/console/active`. --lazy |
| 208 | rmscip-shell system . . rm. -f "${DUMMY_CONSOLES[@]/#//dev/}" /dev/notatty99scip-shell filesystem . . `/dev/notatty99`. |