commit ec663264d531740c67db64aca2bf17e728b40224
parent 920d19652f71ca7fa3c5913216e3ab34caa099ac
Author: ccx <ccx@te2000.cz>
Date: Fri, 22 Mar 2024 02:53:01 +0000
WIP spawn-pty rewrite into C
Diffstat:
12 files changed, 245 insertions(+), 23 deletions(-)
diff --git a/files/spawn-pty.c b/files/spawn-pty.c
@@ -0,0 +1,103 @@
+#include <pty.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <errno.h>
+#include <unistd.h> /* fork(), getpid() */
+#include <sys/wait.h>
+#include <sys/select.h>
+#include <assert.h>
+
+#include <skalibs/exec.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/strerr2.h>
+
+#define USAGE "spawn-pty term_name { master } prog..."
+#define dieusage() strerr_dieusage(100, USAGE)
+
+#define resetsig(S) if(signal(S, SIG_DFL) == SIG_ERR) { \
+ strerr_dief1sys(111, "resetting signal handler"); \
+ }
+
+void exec_terminal(const char *term_env, const char **ptmx_argv, const char **pty_argv) {
+ int ptmx_fd, pty_fd;
+ if (openpty(&ptmx_fd, &pty_fd, NULL, NULL, NULL) != 0) {
+ strerr_dief1sys(111, "openpty() failed");
+ }
+ int fork_pid = fork();
+ if(fork_pid < 0) {
+ strerr_dief1sys(111, "fork()");
+ }
+ if(fork_pid == 0) {
+ /* child */
+ resetsig(SIGHUP);
+ resetsig(SIGINT);
+ resetsig(SIGQUIT);
+ resetsig(SIGPIPE);
+ resetsig(SIGALRM);
+ resetsig(SIGTERM);
+ resetsig(SIGCHLD);
+ resetsig(SIGCONT);
+ resetsig(SIGTSTP);
+ resetsig(SIGTTIN);
+ resetsig(SIGTTOU);
+ if(close(ptmx_fd) != 0) {
+ strerr_dief1sys(111, "close(ptmx_fd) in child");
+ }
+ if(dup2(pty_fd, 0) < 0) {
+ strerr_dief1sys(111, "dup2(pty_fd, 0) in child");
+ }
+ if(dup2(pty_fd, 1) < 0) {
+ strerr_dief1sys(111, "dup2(pty_fd, 1) in child");
+ }
+ if(dup2(pty_fd, 2) < 0) {
+ strerr_dief1sys(111, "dup2(pty_fd, 2) in child");
+ }
+ if(close(pty_fd) != 0) {
+ strerr_dief1sys(111, "close(pty_fd) in child");
+ }
+ char envstr[6 + strlen(term_env)];
+ memcpy(envstr, "TERM=", 5);
+ strcpy(&envstr[5], term_env);
+ xmexec_n(pty_argv, envstr, strlen(envstr), 1);
+ } else {
+ /* parent */
+ if(close(pty_fd) != 0) {
+ strerr_dief1sys(111, "close(pty_fd) in parent");
+ }
+ if(dup2(ptmx_fd, 0) < 0) {
+ strerr_dief1sys(111, "dup2(ptmx_fd, 0) in parent");
+ }
+ if(close(ptmx_fd) != 0) {
+ strerr_dief1sys(111, "close(ptmx_fd) in parent");
+ }
+ // # parent
+ // env = dict(os.environ)
+ // env['PTY_FD'] = "0"
+ // execve(terminal, env)
+ xexec(ptmx_argv);
+ }
+}
+
+int main(const int argc, const char **argv) {
+ char ptmx_argv[argc];
+ const char *term_env = argv[1];
+ int n = 2;
+ while(n < argc - 1) {
+ switch(argv[n][0]) {
+ case 0:
+ ptmx_argv[n - 2] = 0;
+ exec_terminal(term_env, ptmx_argv, &argv[n + 1]);
+ return 111;
+ case ' ':
+ ptmx_argv[n - 2] = &argv[n++][1];
+ break;
+ default:
+ strerr_dieusage(100, "spawn-pty: improperly terminated block");
+ return 100;
+ }
+ }
+ strerr_dieusage(100, "spawn-pty: unterminated block");
+ return 100;
+}
+
+/* vim ft=c sts=2 sw=2 et */
diff --git a/packages/containers b/packages/containers
@@ -3,7 +3,7 @@
#+zsh.762204e14953017be79e356d4bf9f7681625a750ecc951f3510bf144ea7b51bb
#+pthbs-banginstall.d1e43ec62e6d2b3a2a03a2d9ce487f6fe768fb7131c09ce0d2dd9e165233cff1
#+execline.9d9d14aad09d9643f578727ab1ed3363b975772358c4ac8e756abdad4fca7a3d
-#@git:d633847b6662b8789a6b13a48c9bf6210f33b807:containers
+#@git:a43ba149e373c838e6e237ba95d103b42d4d0093:containers
: ${JOBS:=1}
prefix=/versions/$pthbs_package
diff --git a/packages/containers.environment b/packages/containers.environment
@@ -6,7 +6,7 @@
#+s6-linux-utils.198acb1d9ef7c0d4eade19a06d07864bfda68d89e4d65990af8bc1026c069885
#+zsh.762204e14953017be79e356d4bf9f7681625a750ecc951f3510bf144ea7b51bb
#+confz.6658089fa0f90962e97d41b3fd8400d7ea37c6872828f5f7bd122a2d07a9c467
-#+containers.730ca0b68af01c71b9638c53c2b6b478bdf3d9b40db26d64182611f5ba358995
+#+containers.4293ba352fa6d85ddc3fe5cafa3fff76a81a0223f03aa5b6482bed7b1df9cf72
#+xbps.9831b74b8d46bb8c240d6ba606b13d3a80d67804ddbce5e828bef08377d0f9b0
#+zstd.ab99ab40fa027a935e6012884c3988d7161dc0c6ff10c06566c0a92c694bca07
#+apk-tools.c9cdb867562a26201a1618e0b1bf2147e6cee6aff1a6901909ee911a4462327a
diff --git a/packages/default.environment b/packages/default.environment
@@ -1,7 +1,7 @@
-#+system-config.422c1237211cbd19990915a0a9cefd3c05731b109d6ca8fd5bbc22d9a80d8757
-#+system-config-rc.2f69365766309c8cd07fc56e9a1ba3ddb76bb15df22367010f25a97702fa951b
+#+system-config.9e22a1e41800fd6c2e289d12e964fcb06b50724a6321ee1edd5af0ed622c1667
+#+system-config-rc.debb34bff96677bb1165c223046bb8a7f9ac4679b1097e63c112c69e052667c9
#+system-config-scripts.ac300ce9214b7e302ac630c3335cd537000ec030b409ee951e53c563d9f71891
-#+system-config-init.1b4e1d765b98eb0935603f16a01ab9df322f5730664705671619413d1bd10763
+#+system-config-init.f4b1edebb482644bfccfff6a2d70ff51a4a77fb28c58c1cbc5451614362d70ec
#+system-config-zsh.1f09219345bc5468ae634bec90576c270cf60dc0b7c71ebaecb6d61d019072d9
#+busybox.e60885fe93ee85c01831673bb29f0e62a64903f4ce3094e3dc35bc8ec8887ad9
#+gnu-make.782c9e6625fd7420e2cd38b847afed19db3b3844cae8a0426a0dbf73e10d78e5
@@ -25,9 +25,9 @@
#+pthbs-banginstall.d1e43ec62e6d2b3a2a03a2d9ce487f6fe768fb7131c09ce0d2dd9e165233cff1
#+aat.e073f6b23349ea7153987ad737fc80f909241ae4a0c0a22adc12392fcad71597
#+confz.6658089fa0f90962e97d41b3fd8400d7ea37c6872828f5f7bd122a2d07a9c467
-#+containers.730ca0b68af01c71b9638c53c2b6b478bdf3d9b40db26d64182611f5ba358995
+#+containers.4293ba352fa6d85ddc3fe5cafa3fff76a81a0223f03aa5b6482bed7b1df9cf72
#+fileset.22d9abf3009dbbfe507bd7ff93fdd0bf7f86ee0bc317b37b423b89d70ab950a2
-#+logincaps.d32a07761238e4e49d2a968d33ac171ae20dfcb70504b3735d42758d2a459f67
+#+logincaps.ab41dbf1ff5851875ca90ba6889df9da8b8a5e86ed2789e3fec0f6d805b61882
#+snaprep.42b7b97b7530a211dd0f01680c9b15362aa2e7249bc27cea0deb0632f7535bb1
#+curl.b82f818e85570a561ca20ca417adba2f1c3214ef5c3e01626c0a4aa099b90f0c
#+git.1839db7305bbee32d184adc0ece64d40ab58132d3c7749b426c4d680659bd4eb
@@ -38,4 +38,5 @@
#+iconv.0b381a2e57a5e7b3df6b6a68d72230eec4e5ef2f6a460509c94d3d502fe1f2cf
#+libcap.3528f93ae9873fceae62855cb7c0fa521ac2c3cfabe71bf58ba01483136bf5fd
#+applyuidgid-caps.e36af59c076e13332a716eb4cdf6571c93d9cde48fb0bdb98acb5a770e25bb30
-#+nosuid.ae35bf3fa1605f01e99b2c4c54b11da0156890c3ce38de1e26618f5db06b84f6-
\ No newline at end of file
+#+nosuid.ae35bf3fa1605f01e99b2c4c54b11da0156890c3ce38de1e26618f5db06b84f6
+#+spawn-pty.a75672830ff10360bfe41044c2a3582eb81a34472e40c39ed6a7e1c6fc263c74+
\ No newline at end of file
diff --git a/packages/logincaps b/packages/logincaps
@@ -4,7 +4,7 @@
#+pthbs-banginstall.d1e43ec62e6d2b3a2a03a2d9ce487f6fe768fb7131c09ce0d2dd9e165233cff1
#+execline.9d9d14aad09d9643f578727ab1ed3363b975772358c4ac8e756abdad4fca7a3d
#+musl-cross-make.65f98305f5666435bf0c6b9ccedffae2179ff1b3286752756886f760cf7771d2
-#@git:5f3921f7d4f134de4b4cb47b26a6fe444b452261:logincaps
+#@git:74a5987415c09fdac410eaeb26a8fd79be49906a:logincaps
: ${JOBS:=1}
prefix=/versions/$pthbs_package
diff --git a/packages/spawn-pty b/packages/spawn-pty
@@ -0,0 +1,92 @@
+#!/usr/bin/env pthbs-build
+#+musl-cross-make.65f98305f5666435bf0c6b9ccedffae2179ff1b3286752756886f760cf7771d2
+#+gnu-make.782c9e6625fd7420e2cd38b847afed19db3b3844cae8a0426a0dbf73e10d78e5
+#+busybox.e60885fe93ee85c01831673bb29f0e62a64903f4ce3094e3dc35bc8ec8887ad9
+#+skalibs.9c4a42aba2c3b6d3622a04a17fb8dbb75d51805220f45823c26fe7a73e842b5e
+#@sha256:a4d063770b6765c3e14a74d97d3879910ac399303c2a699fee756522531f85f8:spawn-pty.c
+
+name=spawn-pty
+check_static() {
+ local exe || true
+ exe=$pthbs_destdir/'/versions'/$pthbs_package/$1
+ if ! test -f $exe; then
+ printf '%s\n' "Error: file '$1' doesn't exist!"
+ exit 1
+ fi
+ interp_info=$(readelf --string-dump=.interp "$exe") || exit $?
+ if test x '!=' "x$interp_info"; then
+ printf '%s\n' "Error: '$1' is a dynamic binary!"
+ exit 1
+ fi
+}
+build_env_static() {
+ export LD_LIBRARY_PATH="$pthbs_build_environment/library"
+ export CPATH="$pthbs_build_environment/include"
+ export LDFLAGS="-static -L$pthbs_build_environment/library $LDFLAGS"
+}
+def_prefix() {
+ prefix=/versions/$pthbs_package
+}
+def_dest() {
+ dest=${pthbs_destdir%/}//versions/$pthbs_package
+}
+build_env_static
+def_prefix
+
+gcc -D_GNU_SOURCE -static -o $name $name.c $LDFLAGS -lskarnet
+
+install -d "$pthbs_destdir/$prefix/command"
+install -m 755 $name "$pthbs_destdir/$prefix/command"
+check_static command/$name
+
+cd "$pthbs_destdir/versions/$pthbs_package"
+find -type d -o -print | awk -F/ '
+BEGIN {
+ x["./command/spawn-pty"]=1}
+
+function r1(s) {
+ sub("^[.]/[^/]*", ".", s)
+ return s
+}
+function s1(repl, s) {
+ sub("^[.]/[^/]*", "./"repl, s)
+ return s
+}
+function link(src) {
+ x[$0]=0
+ printf "%s\t%s\n", $0, src
+ printf "genlinks >>%s\t%s<<\n", $0, src >>"/dev/stderr"
+}
+$1!="."{exit 1}
+
+
+$2 == "command" { link($0); next }
+$2 == "bin" { link(s1("command", $0)); next }
+
+$2 == "library.so" { link($0); next }
+$2 == "library" { link($0); next }
+$2 == "lib" && $NF ~ /\.l?a$/ { link(s1("library", $0)); next }
+$2 == "lib" && $NF ~ /\.so(|\..*)$/ { link(s1("library.so", $0)); next }
+
+$2 == "share" && $3 ~ /^(info|man|doc|icons|terminfo)$/ { link(r1($0)); next }
+
+$2 == "man" { link($0); next }
+$2 == "info" { link($0); next }
+$2 == "doc" { link($0); next }
+$2 == "icons" { link($0); next }
+$2 == "terminfo" { link($0); next }
+$2 == "data" { link($0); next }
+$2 == "include" { link($0); next }
+
+{ printf "genlinks ##%s## skipped\n", $0 >>"/dev/stderr" }
+
+END {
+ for(fname in x) { printf "DEBUG: x[\"%s\"]=\"%s\"\n", fname, x[fname] >"/dev/stderr" }
+ for(fname in x) {
+ if(x[fname]) {
+ printf "ERROR: missing expected file \"%s\"\n", fname >"/dev/stderr"
+ exit 3
+ }
+ }
+}' >.install-links.new
+mv .install-links.new .install-links
diff --git a/packages/system-config b/packages/system-config
@@ -13,8 +13,8 @@
#+s6.bb3974d7b49c2034b5064bac508beae2d4bfc1a7b6543aefcbc1af9e9e94f1a7
#+zsh.762204e14953017be79e356d4bf9f7681625a750ecc951f3510bf144ea7b51bb
#+system-config-scripts.ac300ce9214b7e302ac630c3335cd537000ec030b409ee951e53c563d9f71891
-#+logincaps.d32a07761238e4e49d2a968d33ac171ae20dfcb70504b3735d42758d2a459f67
-#@git:a2b599ada2bec1724ab4b049ce73fb66a0f720a4:system-config
+#+logincaps.ab41dbf1ff5851875ca90ba6889df9da8b8a5e86ed2789e3fec0f6d805b61882
+#@git:4363567448b3ab6940f68178d6c2201e681333ca:system-config
def_prefix() {
prefix=/versions/$pthbs_package
@@ -43,8 +43,8 @@ cd 'system-config'
printf '%s\n' >config/etc/motd \
"Welcome to $(cat ./config/hostname)!" \
- 'Current running configuration was generated from system-config:a2b599ada2bec1724ab4b049ce73fb66a0f720a4' \
- 'Last change on 2024-03-18 18:19:17 +0000 by ccx'
+ 'Current running configuration was generated from system-config:4363567448b3ab6940f68178d6c2201e681333ca' \
+ 'Last change on 2024-03-22 01:14:03 +0000 by ccx'
printf '%s\n' >config/etc/skel/loginexec \
"#!$(which execlineb) -S0" \
@@ -52,7 +52,7 @@ printf '%s\n' >config/etc/skel/loginexec \
chmod +x config/etc/skel/loginexec
env 'pthbs_path_system-config'="$prefix" \
- 'pthbs_path_containers=/versions/env.6cf50a7cf9122a11073b0fe4942db9c050f0100ff90c1171a9a02b92978c8b67' \
+ 'pthbs_path_containers=/versions/env.c219f69c3045c2d079b30ca9ffe589ef85c3a1fc83fd19cc808dce1b0e0045c7' \
'pthbs_path_mdevd=/versions/env.9ff4ae1aa76157d331d77b75c4dec8d6fd85395776d90a931bfb285a671a5c32' \
make -j${JOBS:-1} -l$((1+${JOBS:-1})) all
diff --git a/packages/system-config-init b/packages/system-config-init
@@ -5,9 +5,9 @@
#+s6-portable-utils.39f4babb8cd30769f1a931708e5442244b0305c36ddafe32348c7dc30c000b00
#+s6-linux-init.396413eed542c149346f51a569f2d686c03b2104f4d062f52422e444ad9b502d
#+execline.9d9d14aad09d9643f578727ab1ed3363b975772358c4ac8e756abdad4fca7a3d
-#+system-config-rc.2f69365766309c8cd07fc56e9a1ba3ddb76bb15df22367010f25a97702fa951b
+#+system-config-rc.debb34bff96677bb1165c223046bb8a7f9ac4679b1097e63c112c69e052667c9
-s6rcdb=/versions/system-config-rc.2f69365766309c8cd07fc56e9a1ba3ddb76bb15df22367010f25a97702fa951b/config/s6-rc-db
+s6rcdb=/versions/system-config-rc.debb34bff96677bb1165c223046bb8a7f9ac4679b1097e63c112c69e052667c9/config/s6-rc-db
prefix=/versions/$pthbs_package
pkgdir="$pthbs_destdir/$prefix"
diff --git a/packages/system-config-rc b/packages/system-config-rc
@@ -3,7 +3,7 @@
#+busybox-diffutils.4f5a07b29246414b77a7d71b103263af8f1249d75ddcbd9864e00def3d6feded
#+s6-rc.6cefe2791153127e0a783521ab97f3b124f023f21cdc2eddadbb865496ad0b45
#+fileset.22d9abf3009dbbfe507bd7ff93fdd0bf7f86ee0bc317b37b423b89d70ab950a2
-#+system-config.422c1237211cbd19990915a0a9cefd3c05731b109d6ca8fd5bbc22d9a80d8757
+#+system-config.9e22a1e41800fd6c2e289d12e964fcb06b50724a6321ee1edd5af0ed622c1667
def_prefix() {
prefix=/versions/$pthbs_package
@@ -13,7 +13,7 @@ def_dest() {
}
def_dest
-src=/versions/system-config.422c1237211cbd19990915a0a9cefd3c05731b109d6ca8fd5bbc22d9a80d8757/config/s6-rc-source
+src=/versions/system-config.9e22a1e41800fd6c2e289d12e964fcb06b50724a6321ee1edd5af0ed622c1667/config/s6-rc-source
s6-rc-compile ./s6-rc-db "$src"
mkdir -p "$dest/config"
mv -v s6-rc-db "$dest/config/"
diff --git a/packages/userspace.environment b/packages/userspace.environment
@@ -20,9 +20,9 @@
#+pthbs-banginstall.d1e43ec62e6d2b3a2a03a2d9ce487f6fe768fb7131c09ce0d2dd9e165233cff1
#+aat.e073f6b23349ea7153987ad737fc80f909241ae4a0c0a22adc12392fcad71597
#+confz.6658089fa0f90962e97d41b3fd8400d7ea37c6872828f5f7bd122a2d07a9c467
-#+containers.730ca0b68af01c71b9638c53c2b6b478bdf3d9b40db26d64182611f5ba358995
+#+containers.4293ba352fa6d85ddc3fe5cafa3fff76a81a0223f03aa5b6482bed7b1df9cf72
#+fileset.22d9abf3009dbbfe507bd7ff93fdd0bf7f86ee0bc317b37b423b89d70ab950a2
-#+logincaps.d32a07761238e4e49d2a968d33ac171ae20dfcb70504b3735d42758d2a459f67
+#+logincaps.ab41dbf1ff5851875ca90ba6889df9da8b8a5e86ed2789e3fec0f6d805b61882
#+snaprep.42b7b97b7530a211dd0f01680c9b15362aa2e7249bc27cea0deb0632f7535bb1
#+curl.b82f818e85570a561ca20ca417adba2f1c3214ef5c3e01626c0a4aa099b90f0c
#+git.1839db7305bbee32d184adc0ece64d40ab58132d3c7749b426c4d680659bd4eb
@@ -33,4 +33,5 @@
#+iconv.0b381a2e57a5e7b3df6b6a68d72230eec4e5ef2f6a460509c94d3d502fe1f2cf
#+libcap.3528f93ae9873fceae62855cb7c0fa521ac2c3cfabe71bf58ba01483136bf5fd
#+applyuidgid-caps.e36af59c076e13332a716eb4cdf6571c93d9cde48fb0bdb98acb5a770e25bb30
-#+nosuid.ae35bf3fa1605f01e99b2c4c54b11da0156890c3ce38de1e26618f5db06b84f6-
\ No newline at end of file
+#+nosuid.ae35bf3fa1605f01e99b2c4c54b11da0156890c3ce38de1e26618f5db06b84f6
+#+spawn-pty.a75672830ff10360bfe41044c2a3582eb81a34472e40c39ed6a7e1c6fc263c74+
\ No newline at end of file
diff --git a/templates/pkg/spawn-pty b/templates/pkg/spawn-pty
@@ -0,0 +1,24 @@
+{% extends "genlinks" %}
+{%- block script %}
+#+{{pkg_install_name("musl-cross-make")}}
+#+{{pkg_install_name("gnu-make")}}
+#+{{pkg_install_name("busybox")}}
+#+{{pkg_install_name("skalibs")}}
+#@sha256:{{files["spawn-pty.c"]}}:spawn-pty.c
+
+name=spawn-pty
+{% include "functions/check_static" %}
+{% include "functions/build_env_static" %}
+{% include "functions/vars" %}
+build_env_static
+def_prefix
+
+gcc -D_GNU_SOURCE -static -o $name $name.c $LDFLAGS -lskarnet
+
+install -d "$pthbs_destdir/$prefix/command"
+install -m 755 $name "$pthbs_destdir/$prefix/command"
+check_static command/$name
+{% endblock %}
+{% block genlinks_begin %}
+ x["./command/spawn-pty"]=1
+{%- endblock %}
diff --git a/templates/pkg/userspace.environment b/templates/pkg/userspace.environment
@@ -34,3 +34,4 @@
#+{{pkg_install_name("libcap")}}
#+{{pkg_install_name("applyuidgid-caps")}}
#+{{pkg_install_name("nosuid")}}
+#+{{pkg_install_name("spawn-pty")}}