[PATCH 0/8] Change serial getty instances dynamically
The host system is supposed to provide a shell on whatever serial devices are configured as the active kernel consoles. Previously, the system made the assumption that the active console would never change after boot, but in reality devices can be added and removed later than that. One situation in which this happened is when a serial device was configured whose driver was not built in to the kernel, but loaded as a module after the console was checked by rc.init. This series fixes that by creating and destroying instances of the serial-getty service whenever the active kernel consoles change, and adds a regression test. Since this is the second test that involves running a full Spectrum system in a VM, most of the series has ended up being refactoring the first such test, so that code can be shared with this new one. Yureka, I don't know whether this is the issue you were encountering when you tried debugging the Weston issue over serial, but maybe worth a try? I suspect not though, because if I recall correctly you weren't even getting kernel logs? This was the only issue I could find trying to reproduce the problem in QEMU. Alyssa Ross (8): host/rootfs: poll active consoles for serial getty release/checks/integration: init from networking release/checks/integration: use default Meson timeout release/checks/integration: extract config struct release/checks/integration: run from tmpdir release/checks/integration: name QEMU arg pointers release/checks/integration: extract library release/checks/integration: test late serial host/rootfs/Makefile | 1 + host/rootfs/default.nix | 8 +- .../service/serial-getty-generator/run | 43 +++ host/rootfs/etc/s6-linux-init/scripts/rc.init | 9 - release/checks/default.nix | 4 +- release/checks/integration/default.nix | 88 +++++ release/checks/integration/late-serial.c | 15 + release/checks/integration/lib.c | 221 ++++++++++++ release/checks/integration/lib.h | 20 ++ release/checks/integration/meson.build | 21 ++ release/checks/integration/meson_options.txt | 6 + release/checks/integration/networking.c | 138 +++++++ release/checks/networking/default.nix | 62 ---- release/checks/networking/test.c | 337 ------------------ release/checks/pkg-tests.nix | 2 +- 15 files changed, 560 insertions(+), 415 deletions(-) create mode 100755 host/rootfs/etc/s6-linux-init/run-image/service/serial-getty-generator/run create mode 100644 release/checks/integration/default.nix create mode 100644 release/checks/integration/late-serial.c create mode 100644 release/checks/integration/lib.c create mode 100644 release/checks/integration/lib.h create mode 100644 release/checks/integration/meson.build create mode 100644 release/checks/integration/meson_options.txt create mode 100644 release/checks/integration/networking.c delete mode 100644 release/checks/networking/default.nix delete mode 100644 release/checks/networking/test.c base-commit: 42887a0331fd058ee0930fd5209fc3265565c3a8 -- 2.49.0
Previously, the active consoles were only checked once, so if a console appeared later, getty wouldn't be run for it. This meant that if the console was set to a serial device whose driver was not built-in, it might have no shell if the module was not loaded until after rc.init had already run. Also, if a console device was removed, its instance of the serial-getty would hang around. To fix this, replace the getty instantiation in rc.init with a service that will create and destroy serial-getty instances to match the currently configured consoles, which will rerun whenever the configured consoles change. Fixes: b096279 ("host/rootfs: run getty for active serial consoles") Signed-off-by: Alyssa Ross <hi@alyssa.is> --- host/rootfs/Makefile | 1 + host/rootfs/default.nix | 8 ++-- .../service/serial-getty-generator/run | 43 +++++++++++++++++++ host/rootfs/etc/s6-linux-init/scripts/rc.init | 9 ---- 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100755 host/rootfs/etc/s6-linux-init/run-image/service/serial-getty-generator/run diff --git a/host/rootfs/Makefile b/host/rootfs/Makefile index d54538b..d5b9b55 100644 --- a/host/rootfs/Makefile +++ b/host/rootfs/Makefile @@ -31,6 +31,7 @@ FILES = \ etc/s6-linux-init/run-image/service/getty-tty4/run \ etc/s6-linux-init/run-image/service/s6-svscan-log/notification-fd \ etc/s6-linux-init/run-image/service/s6-svscan-log/run \ + etc/s6-linux-init/run-image/service/serial-getty-generator/run \ etc/s6-linux-init/run-image/service/serial-getty/notification-fd \ etc/s6-linux-init/run-image/service/serial-getty/run \ etc/s6-linux-init/run-image/service/serial-getty/template/run \ diff --git a/host/rootfs/default.nix b/host/rootfs/default.nix index b50d090..bfc1ba8 100644 --- a/host/rootfs/default.nix +++ b/host/rootfs/default.nix @@ -9,8 +9,8 @@ pkgsStatic.callPackage ( { start-vmm , lib, stdenvNoCC, nixos, runCommand, writeClosure, erofs-utils, s6-rc , bcachefs-tools, busybox, cloud-hypervisor, cryptsetup, dbus, execline -, e2fsprogs, inkscape, jq, kmod, mdevd, s6, s6-linux-init, socat -, util-linuxMinimal, virtiofsd, xorg +, e2fsprogs, inkscape, inotify-tools, jq, kmod, mdevd, s6 +, s6-linux-init, socat, util-linuxMinimal, virtiofsd, xorg , xdg-desktop-portal-spectrum-host }: @@ -137,8 +137,8 @@ let foot = pkgsGui.foot.override { allowPgo = false; }; packages = [ - bcachefs-tools cloud-hypervisor dbus execline jq kmod mdevd s6 - s6-linux-init s6-rc socat start-vmm virtiofsd + bcachefs-tools cloud-hypervisor dbus execline inotify-tools jq + kmod mdevd s6 s6-linux-init s6-rc socat start-vmm virtiofsd xdg-desktop-portal-spectrum-host (cryptsetup.override { diff --git a/host/rootfs/etc/s6-linux-init/run-image/service/serial-getty-generator/run b/host/rootfs/etc/s6-linux-init/run-image/service/serial-getty-generator/run new file mode 100755 index 0000000..445604f --- /dev/null +++ b/host/rootfs/etc/s6-linux-init/run-image/service/serial-getty-generator/run @@ -0,0 +1,43 @@ +#!/bin/execlineb -P +# SPDX-License-Identifier: EUPL-1.2+ +# SPDX-FileCopyrightText: 2024-2025 Alyssa Ross <hi@alyssa.is> + +piperw 3 4 +background { + fdclose 3 + fdmove 2 4 + inotifywait -e MODIFY /sys/class/tty/console/active +} +fdclose 4 +importas -i inotifywait_pid ! + +foreground { + if { fdmove 0 3 grep -qx "Watches established." } + background { fdmove 0 3 cat } + fdclose 3 + + # Wait until inotifywait is ready before updating serial gettys, + # so that changes won't be missed in between updating and starting + # inotifywait. + pipeline { s6-instance-list /run/service/serial-getty } + pipeline { sort } + fdmove -c 3 0 + + redirfd -r 0 /sys/class/tty/console/active + pipeline { tr " " "\n" } + pipeline { sort } + + pipeline { comm -3 - /proc/self/fd/3 } + forstdin -Ep line + case -N $line { + " ?tty[0-9]*" { } + " (.*)" { + importas -i tty 1 + s6-instance-delete /run/service/serial-getty $tty + } + } + s6-instance-create /run/service/serial-getty $line +} + +# Block until the active consoles change, then let s6 restart us. +wait -- $inotifywait_pid diff --git a/host/rootfs/etc/s6-linux-init/scripts/rc.init b/host/rootfs/etc/s6-linux-init/scripts/rc.init index c778714..674fd38 100755 --- a/host/rootfs/etc/s6-linux-init/scripts/rc.init +++ b/host/rootfs/etc/s6-linux-init/scripts/rc.init @@ -2,15 +2,6 @@ # SPDX-License-Identifier: EUPL-1.2+ # SPDX-FileCopyrightText: 2020-2022, 2024 Alyssa Ross <hi@alyssa.is> -background { - redirfd -r 0 /sys/class/tty/console/active - withstdinas active - importas -isu active active - forx -po0 -E tty { $active } - case $tty { tty[0-9]* { } } - s6-instance-create /run/service/serial-getty $tty -} - if { s6-rc-init -c /etc/s6-rc /run/service } if { mount --make-shared /run } -- 2.49.0
This patch has been committed as f743f506698647deb8b193447b584018361c3010, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=f743f506698647deb8b193447b58.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
Combining integration tests into a single Meson project will allow easily sharing code between tests. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- release/checks/default.nix | 4 +- release/checks/integration/default.nix | 87 +++++++++++++++++++ release/checks/integration/meson.build | 16 ++++ release/checks/integration/meson_options.txt | 6 ++ .../test.c => integration/networking.c} | 4 +- release/checks/networking/default.nix | 62 ------------- release/checks/pkg-tests.nix | 2 +- 7 files changed, 114 insertions(+), 67 deletions(-) create mode 100644 release/checks/integration/default.nix create mode 100644 release/checks/integration/meson.build create mode 100644 release/checks/integration/meson_options.txt rename release/checks/{networking/test.c => integration/networking.c} (98%) delete mode 100644 release/checks/networking/default.nix diff --git a/release/checks/default.nix b/release/checks/default.nix index e8ade9f..4c5ea2a 100644 --- a/release/checks/default.nix +++ b/release/checks/default.nix @@ -14,9 +14,9 @@ import ../../lib/call-package.nix ({ callSpectrumPackage }: doc-anchors = callSpectrumPackage ./doc-anchors.nix {}; - pkg-tests = callSpectrumPackage ./pkg-tests.nix {}; + integration = callSpectrumPackage ./integration {}; - networking = callSpectrumPackage ./networking {}; + pkg-tests = callSpectrumPackage ./pkg-tests.nix {}; no-roothash = callSpectrumPackage ./no-roothash.nix {}; diff --git a/release/checks/integration/default.nix b/release/checks/integration/default.nix new file mode 100644 index 0000000..f2996fd --- /dev/null +++ b/release/checks/integration/default.nix @@ -0,0 +1,87 @@ +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: 2023, 2025 Alyssa Ross <hi@alyssa.is> + +import ../../../lib/call-package.nix ( +{ callSpectrumPackage, src, lib, stdenv, runCommand, writeShellScript +, clang-tools, meson, ninja, e2fsprogs, tar2ext4, libressl, qemu_kvm +}: + +let + live = callSpectrumPackage ../../live {}; + + ncVm = callSpectrumPackage ../../../vm/make-vm.nix {} { + providers.net = [ "sys.netvm" ]; + type = "nix"; + run = writeShellScript "run" '' + set -x + while ! echo hello | ${libressl.nc}/bin/nc -N 10.0.2.2 1234; do :; done + ''; + }; + + userData = runCommand "user-data.img" { + nativeBuildInputs = [ e2fsprogs tar2ext4 ]; + } '' + tar --transform=s,^${ncVm},vms/nc, -Pcvf root.tar ${ncVm} + tar2ext4 -i root.tar -o $out + tune2fs -U a7834806-2f82-4faf-8ac4-4f8fd8a474ca $out + ''; +in + +stdenv.mkDerivation (finalAttrs: { + name = "spectrum-integration-tests"; + + src = lib.fileset.toSource { + root = ../../..; + fileset = lib.fileset.union + (lib.fileset.intersection src ./.) + ../../../scripts/run-qemu.sh; + }; + sourceRoot = "source/release/checks/integration"; + + nativeBuildInputs = [ meson ninja ]; + nativeCheckInputs = [ qemu_kvm ]; + + mesonFlags = [ + "-Defi=${qemu_kvm}/share/qemu/edk2-${stdenv.hostPlatform.qemuArch}-code.fd" + "-Dimg=${live}" + "-Duser_data=${userData}" + ]; + + doCheck = true; + + installPhase = '' + runHook preInstall + cp meson-logs/testlog.txt $out + runHook postInstall + ''; + + shellHook = '' + unset QEMU_SYSTEM + ''; + + env = { + QEMU_SYSTEM = "qemu-system-${stdenv.hostPlatform.qemuArch} -nographic"; + }; + + passthru.tests = { + clang-tidy = finalAttrs.finalPackage.overrideAttrs ( + { name, src, nativeBuildInputs ? [], ... }: + { + name = "${name}-clang-tidy"; + + src = lib.fileset.toSource { + root = ../../..; + fileset = lib.fileset.union (lib.fileset.fromSource src) ../../../.clang-tidy; + }; + + nativeBuildInputs = nativeBuildInputs ++ [ clang-tools ]; + + buildPhase = '' + clang-tidy --warnings-as-errors='*' -p . ../*.c ../*.h + touch $out + exit 0 + ''; + } + ); + }; +})) (_: {}) diff --git a/release/checks/integration/meson.build b/release/checks/integration/meson.build new file mode 100644 index 0000000..3f273b4 --- /dev/null +++ b/release/checks/integration/meson.build @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: EUPL-1.2+ +# SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is> + +project('spectrum-integration-tests', 'c', + default_options : { 'c_std': 'c23' }, + meson_version : '>=1.6.0') + +add_project_arguments('-D_GNU_SOURCE', language : 'c') + +test('networking', executable('networking', 'networking.c'), + args : [ + find_program('../../../scripts/run-qemu.sh'), + get_option('efi'), + get_option('img'), + get_option('user_data'), + ]) diff --git a/release/checks/integration/meson_options.txt b/release/checks/integration/meson_options.txt new file mode 100644 index 0000000..2d290de --- /dev/null +++ b/release/checks/integration/meson_options.txt @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: EUPL-1.2+ +# SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is> + +option('efi', type : 'string') +option('img', type : 'string') +option('user_data', type : 'string') diff --git a/release/checks/networking/test.c b/release/checks/integration/networking.c similarity index 98% rename from release/checks/networking/test.c rename to release/checks/integration/networking.c index 2bf0eb4..44dccf3 100644 --- a/release/checks/networking/test.c +++ b/release/checks/integration/networking.c @@ -321,8 +321,8 @@ int main(int argc, char *argv[]) "mount \"$(findfs UUID=a7834806-2f82-4faf-8ac4-4f8fd8a474ca)\" /run/mnt\n" "s6-rc -bu change vmm-env\n" "vm-import user /run/mnt/vms\n" - "vm-start user.vm\n" - "tail -Fc +0 /run/log/current /run/user.vm.log\r\n", + "vm-start user.nc\n" + "tail -Fc +0 /run/log/current /run/user.nc.log\r\n", console) == EOF) { fputs("error writing to console\n", stderr); exit(EXIT_FAILURE); diff --git a/release/checks/networking/default.nix b/release/checks/networking/default.nix deleted file mode 100644 index 4280e7e..0000000 --- a/release/checks/networking/default.nix +++ /dev/null @@ -1,62 +0,0 @@ -# SPDX-License-Identifier: MIT -# SPDX-FileCopyrightText: 2023, 2025 Alyssa Ross <hi@alyssa.is> - -import ../../../lib/call-package.nix ( -{ callSpectrumPackage, lib, stdenv, runCommand, runCommandCC, writeShellScript -, clang-tools, e2fsprogs, tar2ext4, libressl, qemu_kvm -}: - -let - live = callSpectrumPackage ../../live {}; - - vm = callSpectrumPackage ../../../vm/make-vm.nix {} { - providers.net = [ "sys.netvm" ]; - type = "nix"; - run = writeShellScript "run" '' - set -x - while ! echo hello | ${libressl.nc}/bin/nc -N 10.0.2.2 1234; do :; done - ''; - }; - - userData = runCommand "user-data.img" { - nativeBuildInputs = [ e2fsprogs tar2ext4 ]; - } '' - tar --transform=s,^${vm},vms/vm, -Pcvf root.tar ${vm} - tar2ext4 -i root.tar -o $out - tune2fs -U a7834806-2f82-4faf-8ac4-4f8fd8a474ca $out - ''; - - cflags = [ "-std=c23" "-D_GNU_SOURCE" ]; - - test = runCommandCC "test" {} '' - $CC -o $out ${lib.escapeShellArgs cflags} ${./test.c} - ''; - - testScript = writeShellScript "spectrum-networking-test" '' - PATH=${lib.escapeShellArg (lib.makeBinPath [ qemu_kvm ])}:"$PATH" - exec ${test} \ - ${../../../scripts/run-qemu.sh} \ - ${qemu_kvm}/share/qemu/edk2-${stdenv.hostPlatform.qemuArch}-code.fd \ - ${live} \ - ${userData} - ''; -in - -runCommand "run-${testScript.name}" { - inherit testScript userData; - - passthru.tests = { - clang-tidy = runCommand "run-${testScript.name}-clang-tidy" { - nativeBuildInputs = [ clang-tools ]; - } '' - clang-tidy --config-file=${../../../.clang-tidy} --warnings-as-errors='*' \ - ${lib.escapeShellArgs (map (flag: "--extra-arg=${flag}") cflags)} \ - ${./test.c} - touch $out - ''; - }; -} '' - export QEMU_SYSTEM='qemu-system-${stdenv.hostPlatform.qemuArch} -nographic' - $testScript - touch $out -'') (_: {}) diff --git a/release/checks/pkg-tests.nix b/release/checks/pkg-tests.nix index 97baaf3..cf58603 100644 --- a/release/checks/pkg-tests.nix +++ b/release/checks/pkg-tests.nix @@ -9,7 +9,7 @@ import ../../lib/call-package.nix ( lseek = lib.recurseIntoAttrs lseek.tests; - networking = lib.recurseIntoAttrs (callSpectrumPackage ./networking {}).tests; + integration = lib.recurseIntoAttrs (callSpectrumPackage ./integration {}).tests; start-vmm = lib.recurseIntoAttrs start-vmm.tests; -- 2.49.0
This patch has been committed as 30b52f63f3cc8003b1a2d7df55519ed0a0daf8db, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=30b52f63f3cc8003b1a2d7df5551.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
The failure mode for integration tests is usually going to be that they get stuck, so it's important for the build to fail in that case. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- release/checks/integration/default.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/release/checks/integration/default.nix b/release/checks/integration/default.nix index f2996fd..592917c 100644 --- a/release/checks/integration/default.nix +++ b/release/checks/integration/default.nix @@ -48,6 +48,7 @@ stdenv.mkDerivation (finalAttrs: { ]; doCheck = true; + dontAddTimeoutMultiplier = true; installPhase = '' runHook preInstall -- 2.49.0
This will make it easier to share code between tests, especially as we add more things that tests might want to optionally customise. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- release/checks/integration/networking.c | 39 ++++++++++++++++--------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/release/checks/integration/networking.c b/release/checks/integration/networking.c index 44dccf3..efc7b44 100644 --- a/release/checks/integration/networking.c +++ b/release/checks/integration/networking.c @@ -176,14 +176,21 @@ static void wait_for_prompt(FILE *console) exit(EXIT_FAILURE); } -static FILE *start_qemu(const char *tmp_dir, const char *run_qemu, - const char *efi, const char *img, const char *user_data) +struct config { + const char *run_qemu; + + struct { + const char *efi, *img, *user_data; + } drives; +}; + +static FILE *start_qemu(const char *tmp_dir, struct config c) { FILE *console; struct utsname u; int console_listener, console_conn; char *arch, *args[] = { - (char *)run_qemu, + (char *)c.run_qemu, "-serial", nullptr, "-drive", nullptr, "-drive", nullptr, @@ -227,14 +234,14 @@ static FILE *start_qemu(const char *tmp_dir, const char *run_qemu, exit(EXIT_FAILURE); } - if (asprintf(&args[4], "file=%s,format=raw,if=pflash,readonly=true", efi) == -1 || - asprintf(&args[6], "file=%s,format=raw,if=virtio,readonly=true", img) == -1 || - asprintf(&args[8], "file=%s,format=raw,if=virtio,readonly=true", user_data) == -1) { + if (asprintf(&args[4], "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || + asprintf(&args[6], "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || + asprintf(&args[8], "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { perror("asprintf"); exit(EXIT_FAILURE); } - execv(run_qemu, args); + execv(c.run_qemu, args); perror("execv"); exit(EXIT_FAILURE); } @@ -296,12 +303,17 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - char *run_qemu = argv[1]; - char *efi = argv[2]; - char *img = argv[3]; - char *user_data = argv[4]; + struct config c = { + .run_qemu = argv[1], + .drives = { + .efi = argv[2], + .img = argv[3], + .user_data = argv[4], + }, + }; - if (strchr(efi, ',') || strchr(img, ',') || strchr(user_data, ',')) { + if (strchr(c.drives.efi, ',') || strchr(c.drives.img, ',') || + strchr(c.drives.user_data, ',')) { fputs("arguments contain commas\n", stderr); exit(EXIT_FAILURE); } @@ -313,8 +325,7 @@ int main(int argc, char *argv[]) int server = setup_server(); - FILE *console = start_qemu(make_tmp_dir(), run_qemu, efi, img, - user_data); + FILE *console = start_qemu(make_tmp_dir(), c); if (fputs("set -euxo pipefail\n" "mkdir /run/mnt\n" -- 2.49.0
This patch has been committed as 28bbd33a5d3414839771858b938b32ce0af79bd7, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=28bbd33a5d3414839771858b938b.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
This simplifies the code by avoiding an allocation and free. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- release/checks/integration/networking.c | 26 ++++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/release/checks/integration/networking.c b/release/checks/integration/networking.c index efc7b44..1e5983f 100644 --- a/release/checks/integration/networking.c +++ b/release/checks/integration/networking.c @@ -184,14 +184,13 @@ struct config { } drives; }; -static FILE *start_qemu(const char *tmp_dir, struct config c) +static FILE *start_qemu(struct config c) { FILE *console; struct utsname u; int console_listener, console_conn; char *arch, *args[] = { (char *)c.run_qemu, - "-serial", nullptr, "-drive", nullptr, "-drive", nullptr, "-drive", nullptr, @@ -207,6 +206,7 @@ static FILE *start_qemu(const char *tmp_dir, struct config c) "-monitor", "vc", "-vga", "none", "-smbios", "type=11,value=io.systemd.stub.kernel-cmdline-extra=console=ttyS0", + "-serial", "unix:console", nullptr, }; @@ -217,12 +217,7 @@ static FILE *start_qemu(const char *tmp_dir, struct config c) if (strcmp(arch, "x86_64")) args[sizeof args / sizeof *args - 3] = nullptr; - if (asprintf(&args[2], "unix:%s/console", tmp_dir) == -1) { - perror("asprintf"); - exit(EXIT_FAILURE); - } - - console_listener = setup_unix(args[2] + strlen("unix:")); + console_listener = setup_unix("console"); switch (fork()) { case -1: @@ -234,9 +229,9 @@ static FILE *start_qemu(const char *tmp_dir, struct config c) exit(EXIT_FAILURE); } - if (asprintf(&args[4], "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || - asprintf(&args[6], "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || - asprintf(&args[8], "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { + if (asprintf(&args[2], "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || + asprintf(&args[4], "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || + asprintf(&args[6], "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { perror("asprintf"); exit(EXIT_FAILURE); } @@ -246,8 +241,6 @@ static FILE *start_qemu(const char *tmp_dir, struct config c) exit(EXIT_FAILURE); } - free(args[2]); - if ((console_conn = accept(console_listener, nullptr, nullptr)) == -1) { perror("accept"); exit(EXIT_FAILURE); @@ -323,9 +316,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + if (chdir(make_tmp_dir()) == -1) { + perror("chdir"); + exit(EXIT_FAILURE); + } + int server = setup_server(); - FILE *console = start_qemu(make_tmp_dir(), c); + FILE *console = start_qemu(c); if (fputs("set -euxo pipefail\n" "mkdir /run/mnt\n" -- 2.49.0
Only hardcoding indices right next to the array initilizer makes it easier to understand the code where these pointers are referred to. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- release/checks/integration/networking.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/release/checks/integration/networking.c b/release/checks/integration/networking.c index 1e5983f..7331c4a 100644 --- a/release/checks/integration/networking.c +++ b/release/checks/integration/networking.c @@ -209,6 +209,8 @@ static FILE *start_qemu(struct config c) "-serial", "unix:console", nullptr, }; + char **efi_arg = &args[2], **img_arg = &args[4], + **user_data_arg = &args[6]; if (!(arch = getenv("ARCH"))) { uname(&u); @@ -229,9 +231,9 @@ static FILE *start_qemu(struct config c) exit(EXIT_FAILURE); } - if (asprintf(&args[2], "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || - asprintf(&args[4], "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || - asprintf(&args[6], "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { + if (asprintf(efi_arg, "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || + asprintf(img_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || + asprintf(user_data_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { perror("asprintf"); exit(EXIT_FAILURE); } -- 2.49.0
This patch has been committed as c8df67c69bb4e335dab453088e5a2e3f6e7df700, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=c8df67c69bb4e335dab453088e5a.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
This will ease writing more tests. The drain stuff could probably also be extracted, but the second test I'm currently writing won't use it, so I'm leaving it for now. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- .../integration/{networking.c => lib.c} | 139 +---------- release/checks/integration/lib.h | 16 ++ release/checks/integration/meson.build | 4 +- release/checks/integration/networking.c | 216 +----------------- 4 files changed, 26 insertions(+), 349 deletions(-) copy release/checks/integration/{networking.c => lib.c} (61%) create mode 100644 release/checks/integration/lib.h diff --git a/release/checks/integration/networking.c b/release/checks/integration/lib.c similarity index 61% copy from release/checks/integration/networking.c copy to release/checks/integration/lib.c index 7331c4a..b0b4507 100644 --- a/release/checks/integration/networking.c +++ b/release/checks/integration/lib.c @@ -1,20 +1,15 @@ // SPDX-License-Identifier: EUPL-1.2+ // SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is> -#include <errno.h> -#include <pthread.h> +#include "lib.h" + #include <sched.h> #include <signal.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> -#include <arpa/inet.h> -#include <net/if.h> - -#include <sys/ioctl.h> #include <sys/prctl.h> #include <sys/socket.h> #include <sys/un.h> @@ -38,31 +33,6 @@ static void exit_on_sigchld(void) exit(EXIT_FAILURE); } -static void *drain(void *arg) -{ - int c; - - while ((c = fgetc((FILE *)arg)) != EOF) - fputc(c, stderr); - - if (ferror((FILE *)arg)) - return (void *)(intptr_t)errno; - - return nullptr; -} - -static pthread_t start_drain(FILE *console) -{ - pthread_t thread; - int e; - - if (!(e = pthread_create(&thread, nullptr, drain, console))) - return thread; - - fprintf(stderr, "pthread_create: %s\n", strerror(e)); - exit(EXIT_FAILURE); -} - static char *make_tmp_dir(void) { char *dir, *run; @@ -81,48 +51,6 @@ static char *make_tmp_dir(void) return dir; } -static int setup_server(void) -{ - int fd; - struct ifreq ifr; - - struct sockaddr_in addr = { - .sin_family = AF_INET, - .sin_port = htons(1234), - .sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) }, - }; - - sprintf(ifr.ifr_name, "lo"); - - if ((fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) { - perror("socket"); - exit(EXIT_FAILURE); - } - - if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) { - perror("SIOCGIFFLAGS"); - exit(EXIT_FAILURE); - } - - ifr.ifr_flags |= IFF_UP; - if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) { - perror("SIOCSIFFLAGS"); - exit(EXIT_FAILURE); - } - - if (bind(fd, &addr, sizeof addr) == -1) { - perror("bind"); - exit(EXIT_FAILURE); - } - - if (listen(fd, 1) == -1) { - perror("listen"); - exit(EXIT_FAILURE); - } - - return fd; -} - static int setup_unix(const char *path) { int r, fd; @@ -176,15 +104,7 @@ static void wait_for_prompt(FILE *console) exit(EXIT_FAILURE); } -struct config { - const char *run_qemu; - - struct { - const char *efi, *img, *user_data; - } drives; -}; - -static FILE *start_qemu(struct config c) +FILE *start_qemu(struct config c) { FILE *console; struct utsname u; @@ -259,36 +179,6 @@ static FILE *start_qemu(struct config c) return console; } -static void expect_connection(int listener) -{ - int conn_fd; - FILE *conn; - char msg[7]; - size_t len; - - fputs("waiting for server connection\n", stderr); - if ((conn_fd = accept(listener, nullptr, nullptr)) == -1) { - perror("accept"); - exit(EXIT_FAILURE); - } - fputs("accepted connection!\n", stderr); - if (!(conn = fdopen(conn_fd, "r"))) { - perror("fdopen(server connection)"); - exit(EXIT_FAILURE); - } - - len = fread(msg, 1, sizeof msg, conn); - if (len != 6 || memcmp("hello\n", msg, 6)) { - if (ferror(conn)) - perror("fread(server connection)"); - else - fprintf(stderr, "unexpected connection data: %.*s", - (int)len, msg); - exit(EXIT_FAILURE); - } - fclose(conn); -} - int main(int argc, char *argv[]) { exit_on_sigchld(); @@ -323,26 +213,5 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - int server = setup_server(); - - FILE *console = start_qemu(c); - - if (fputs("set -euxo pipefail\n" - "mkdir /run/mnt\n" - "mount \"$(findfs UUID=a7834806-2f82-4faf-8ac4-4f8fd8a474ca)\" /run/mnt\n" - "s6-rc -bu change vmm-env\n" - "vm-import user /run/mnt/vms\n" - "vm-start user.nc\n" - "tail -Fc +0 /run/log/current /run/user.nc.log\r\n", - console) == EOF) { - fputs("error writing to console\n", stderr); - exit(EXIT_FAILURE); - } - if (fflush(console) == EOF) { - perror("fflush"); - exit(EXIT_FAILURE); - } - start_drain(console); - - expect_connection(server); + test(c); } diff --git a/release/checks/integration/lib.h b/release/checks/integration/lib.h new file mode 100644 index 0000000..959573d --- /dev/null +++ b/release/checks/integration/lib.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: EUPL-1.2+ +// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is> + +#include <stdio.h> + +struct config { + const char *run_qemu; + + struct { + const char *efi, *img, *user_data; + } drives; +}; + +extern void test(struct config); + +FILE *start_qemu(struct config config); diff --git a/release/checks/integration/meson.build b/release/checks/integration/meson.build index 3f273b4..7edc314 100644 --- a/release/checks/integration/meson.build +++ b/release/checks/integration/meson.build @@ -7,7 +7,9 @@ project('spectrum-integration-tests', 'c', add_project_arguments('-D_GNU_SOURCE', language : 'c') -test('networking', executable('networking', 'networking.c'), +lib = static_library('spectrum-integration-test', 'lib.c') + +test('networking', executable('networking', 'networking.c', link_with : lib), args : [ find_program('../../../scripts/run-qemu.sh'), get_option('efi'), diff --git a/release/checks/integration/networking.c b/release/checks/integration/networking.c index 7331c4a..f550edc 100644 --- a/release/checks/integration/networking.c +++ b/release/checks/integration/networking.c @@ -1,42 +1,18 @@ // SPDX-License-Identifier: EUPL-1.2+ // SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is> +#include "lib.h" + #include <errno.h> #include <pthread.h> -#include <sched.h> -#include <signal.h> #include <stdint.h> -#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <arpa/inet.h> #include <net/if.h> #include <sys/ioctl.h> -#include <sys/prctl.h> -#include <sys/socket.h> -#include <sys/un.h> -#include <sys/utsname.h> - -static void chld_handler(int) -{ - exit(EXIT_FAILURE); -} - -static void exit_on_sigchld(void) -{ - struct sigaction sa = { - .sa_handler = chld_handler, - .sa_flags = SA_NOCLDSTOP, - }; - if (!sigaction(SIGCHLD, &sa, nullptr)) - return; - - perror("sigaction"); - exit(EXIT_FAILURE); -} static void *drain(void *arg) { @@ -63,24 +39,6 @@ static pthread_t start_drain(FILE *console) exit(EXIT_FAILURE); } -static char *make_tmp_dir(void) -{ - char *dir, *run; - if (!(run = secure_getenv("XDG_RUNTIME_DIR"))) { - fputs("warning: XDG_RUNTIME_DIR unset\n", stderr); - run = "/tmp"; - } - if (asprintf(&dir, "%s/spectrum-test.XXXXXX", run) == -1) { - perror("asprintf"); - exit(EXIT_FAILURE); - } - if (!mkdtemp(dir)) { - perror("mkdtemp"); - exit(EXIT_FAILURE); - } - return dir; -} - static int setup_server(void) { int fd; @@ -123,142 +81,6 @@ static int setup_server(void) return fd; } -static int setup_unix(const char *path) -{ - int r, fd; - struct sockaddr_un addr = { .sun_family = AF_UNIX }; - - r = snprintf(addr.sun_path, sizeof addr.sun_path, "%s", path); - if (r >= sizeof addr.sun_path) { - fputs("XDG_RUNTIME_DIR too long\n", stderr); - exit(EXIT_FAILURE); - } - - if (r < 0) { - fputs("snprintf error\n", stderr); - exit(EXIT_FAILURE); - } - - if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) { - perror("socket"); - exit(EXIT_FAILURE); - } - - if (bind(fd, &addr, sizeof addr) == -1) { - perror("bind"); - exit(EXIT_FAILURE); - } - - if (listen(fd, 1) == -1) { - perror("listen"); - exit(EXIT_FAILURE); - } - - return fd; -} - -static void wait_for_prompt(FILE *console) -{ - char *needle = "~ # "; - size_t i = 0; - int c; - - fputs("waiting for character\n", stderr); - - while ((c = fgetc(console)) != EOF) { - fputc(c, stderr); - i = c == needle[i] ? i + 1 : 0; - if (!needle[i]) - return; - } - - fputs("unexpected EOF from console\n", stderr); - exit(EXIT_FAILURE); -} - -struct config { - const char *run_qemu; - - struct { - const char *efi, *img, *user_data; - } drives; -}; - -static FILE *start_qemu(struct config c) -{ - FILE *console; - struct utsname u; - int console_listener, console_conn; - char *arch, *args[] = { - (char *)c.run_qemu, - "-drive", nullptr, - "-drive", nullptr, - "-drive", nullptr, - "-m", "4G", - "-nodefaults", - "-machine", "virtualization=on", - "-cpu", "max", - "-device", "virtio-keyboard", - "-device", "virtio-mouse", - "-device", "virtio-gpu", - "-netdev", "user,id=net0", - "-device", "e1000e,netdev=net0", - "-monitor", "vc", - "-vga", "none", - "-smbios", "type=11,value=io.systemd.stub.kernel-cmdline-extra=console=ttyS0", - "-serial", "unix:console", - nullptr, - }; - char **efi_arg = &args[2], **img_arg = &args[4], - **user_data_arg = &args[6]; - - if (!(arch = getenv("ARCH"))) { - uname(&u); - arch = u.machine; - } - if (strcmp(arch, "x86_64")) - args[sizeof args / sizeof *args - 3] = nullptr; - - console_listener = setup_unix("console"); - - switch (fork()) { - case -1: - perror("fork"); - exit(EXIT_FAILURE); - case 0: - if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) { - perror("prctl"); - exit(EXIT_FAILURE); - } - - if (asprintf(efi_arg, "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || - asprintf(img_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || - asprintf(user_data_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { - perror("asprintf"); - exit(EXIT_FAILURE); - } - - execv(c.run_qemu, args); - perror("execv"); - exit(EXIT_FAILURE); - } - - if ((console_conn = accept(console_listener, nullptr, nullptr)) == -1) { - perror("accept"); - exit(EXIT_FAILURE); - } - if (!(console = fdopen(console_conn, "a+"))) { - perror("fdopen"); - exit(EXIT_FAILURE); - } - - fputs("waiting for console prompt\n", stderr); - - wait_for_prompt(console); - - return console; -} - static void expect_connection(int listener) { int conn_fd; @@ -289,40 +111,8 @@ static void expect_connection(int listener) fclose(conn); } -int main(int argc, char *argv[]) +void test(struct config c) { - exit_on_sigchld(); - - if (argc != 5) { - fputs("Usage: test efi spectrum user_data\n", stderr); - exit(EXIT_FAILURE); - } - - struct config c = { - .run_qemu = argv[1], - .drives = { - .efi = argv[2], - .img = argv[3], - .user_data = argv[4], - }, - }; - - if (strchr(c.drives.efi, ',') || strchr(c.drives.img, ',') || - strchr(c.drives.user_data, ',')) { - fputs("arguments contain commas\n", stderr); - exit(EXIT_FAILURE); - } - - if (unshare(CLONE_NEWUSER|CLONE_NEWNET) == -1) { - perror("unshare"); - exit(EXIT_FAILURE); - } - - if (chdir(make_tmp_dir()) == -1) { - perror("chdir"); - exit(EXIT_FAILURE); - } - int server = setup_server(); FILE *console = start_qemu(c); -- 2.49.0
This patch has been committed as ee3d9f74a05e04eedf9ea67501a6cf32db476ebb, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=ee3d9f74a05e04eedf9ea67501a6.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
By "late", I mean the serial device won't be configured by the kernel until full userspace is running. Here I'm doing this with a USB serial device, for which driver modules need to be loaded by the kernel. This is a regression test, because that didn't used to work, since serial consoles were only configured once at boot. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- release/checks/integration/late-serial.c | 15 +++++++++++++++ release/checks/integration/lib.c | 16 ++++++++++------ release/checks/integration/lib.h | 4 ++++ release/checks/integration/meson.build | 9 ++++++--- 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 release/checks/integration/late-serial.c diff --git a/release/checks/integration/late-serial.c b/release/checks/integration/late-serial.c new file mode 100644 index 0000000..46f312a --- /dev/null +++ b/release/checks/integration/late-serial.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: EUPL-1.2+ +// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is> + +#include "lib.h" + +void test(struct config c) +{ + c.serial = (typeof(c.serial)) { + .optname = "-device", + .optval = "usb-serial,chardev=socket", + .console = "ttyUSB0", + }; + + start_qemu(c); +} diff --git a/release/checks/integration/lib.c b/release/checks/integration/lib.c index b0b4507..bc2b9dd 100644 --- a/release/checks/integration/lib.c +++ b/release/checks/integration/lib.c @@ -114,10 +114,12 @@ FILE *start_qemu(struct config c) "-drive", nullptr, "-drive", nullptr, "-drive", nullptr, + "-smbios", nullptr, "-m", "4G", "-nodefaults", "-machine", "virtualization=on", "-cpu", "max", + "-device", "qemu-xhci", "-device", "virtio-keyboard", "-device", "virtio-mouse", "-device", "virtio-gpu", @@ -125,19 +127,20 @@ FILE *start_qemu(struct config c) "-device", "e1000e,netdev=net0", "-monitor", "vc", "-vga", "none", - "-smbios", "type=11,value=io.systemd.stub.kernel-cmdline-extra=console=ttyS0", - "-serial", "unix:console", + "-chardev", "socket,id=socket,path=console", + c.serial.optname ? (char *)c.serial.optname : "-serial", + c.serial.optval ? (char *)c.serial.optval : "chardev:socket", nullptr, }; char **efi_arg = &args[2], **img_arg = &args[4], - **user_data_arg = &args[6]; + **user_data_arg = &args[6], **console_arg = &args[8]; if (!(arch = getenv("ARCH"))) { uname(&u); arch = u.machine; } - if (strcmp(arch, "x86_64")) - args[sizeof args / sizeof *args - 3] = nullptr; + if (!c.serial.console && !strcmp(arch, "x86_64")) + c.serial.console = "ttyS0"; console_listener = setup_unix("console"); @@ -153,7 +156,8 @@ FILE *start_qemu(struct config c) if (asprintf(efi_arg, "file=%s,format=raw,if=pflash,readonly=true", c.drives.efi) == -1 || asprintf(img_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.img) == -1 || - asprintf(user_data_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1) { + asprintf(user_data_arg, "file=%s,format=raw,if=virtio,readonly=true", c.drives.user_data) == -1 || + asprintf(console_arg, "type=11,value=io.systemd.stub.kernel-cmdline-extra=%s%s", c.serial.console ? "console=" : "", c.serial.console) == -1) { perror("asprintf"); exit(EXIT_FAILURE); } diff --git a/release/checks/integration/lib.h b/release/checks/integration/lib.h index 959573d..743c906 100644 --- a/release/checks/integration/lib.h +++ b/release/checks/integration/lib.h @@ -9,6 +9,10 @@ struct config { struct { const char *efi, *img, *user_data; } drives; + + struct { + const char *optname, *optval, *console; + } serial; }; extern void test(struct config); diff --git a/release/checks/integration/meson.build b/release/checks/integration/meson.build index 7edc314..4a58cce 100644 --- a/release/checks/integration/meson.build +++ b/release/checks/integration/meson.build @@ -7,12 +7,15 @@ project('spectrum-integration-tests', 'c', add_project_arguments('-D_GNU_SOURCE', language : 'c') +run_qemu = find_program('../../../scripts/run-qemu.sh') + lib = static_library('spectrum-integration-test', 'lib.c') -test('networking', executable('networking', 'networking.c', link_with : lib), - args : [ - find_program('../../../scripts/run-qemu.sh'), +foreach test : ['networking', 'late-serial'] + test(test, executable(test, test + '.c', link_with : lib), args : [ + run_qemu, get_option('efi'), get_option('img'), get_option('user_data'), ]) +endforeach -- 2.49.0
This patch has been committed as 750b1525275dc3250d3fb0b7bf71aeba733d9d95, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=750b1525275dc3250d3fb0b7bf71.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
participants (2)
-
Alyssa Ross -
Alyssa Ross