=== modified file 'xpra-skel/homedir/bin/start-xpra-for-container' --- old/xpra-skel/homedir/bin/start-xpra-for-container 2024-07-23 21:41:52 +0000 +++ new/xpra-skel/homedir/bin/start-xpra-for-container 2024-05-07 20:36:32 +0000 @@ -5,7 +5,7 @@ typeset -g map=/run/display-container-map typeset -g scandir=/run/service -typeset -g build=/run/xpra.service +typeset -g build=/run/tmp/service main() { typeset -g container client_display @@ -55,204 +55,69 @@ } ensure_services() { - typeset -ga new_services + typeset -g signal_svscan=0 typeset -g sv_name_xorg=xpra-xorg.$display typeset -g sv_name_server=xpra-server.$display typeset -g sv_name_client=xpra-client.$display.for.$client_display - write_sv_definitions - # if (($#new_services)); then - # - s6-svscanctl -a /run/service - # # TODO: wait for s6-supervises to start - # fi -} - -### Color and text definition {{{1 - -typeset -g hl_fatal hl_reset - -# die messages -if (( $terminfo[colors] >= 8 )); then - hl_fatal='%F{red}%B'; hl_fatal=${(%)hl_fatal} - hl_warn='%F{yellow}%B'; hl_warn=${(%)hl_warn} - hl_reset='%b%f'; hl_reset=${(%)hl_reset} -fi - -### Utility functions {{{1 -err_msg() { - local first=$1 - shift - printf >&2 '%s%s%s%s\n' "$hl_fatal" "$first" "$hl_reset" "$*" -} - -warn_msg() { - local first=$1 - shift - printf >&2 '%s%s%s%s\n' "$hl_warn" "$first" "$hl_reset" "$*" -} - -# helper that prints out stack, error message and exits -die_ret() { - set +x - local ret n - ret=$1 - shift - print -r - >&2 "${hl_fatal}Fatal$hl_reset error occurend in:" - for n in {${#funcfiletrace}..1}; do - printf >&2 '%d> %s (%s)\n' $n "$funcfiletrace[$n]" "$functrace[$n]" - done - printf >&2 '%s\n' "${hl_fatal}*$hl_reset $^@" - exit $ret -} - -die() { - set +x - die_ret 1 "$@" -} -die100() { # 100: wrong usage - set +x - die_ret 100 "$@" -} -die111() { # 111: system call failed - set +x - die_ret 111 "$@" -} - --() { # Run command and die on nonzero exitcode - "$@" || die_ret $? "command failed with exitcode $?: ${(j: :)${(q)@}}" -} - -# service builder - -sv_start() { - (($+sv_name)) && \ - die100 "sv_start: previous service definition ${(qqq)sv_name} wasn't ended properly" - (( $# != 1 )) && die100 "sv_start: incorrect arguments" - [[ $1 == */* ]] && die100 "sv_start: incorrect argument" - - typeset -g sv_name=$1 - typeset -g sv_dir=$build/$1 - - mkdir -p $sv_dir - - touch $sv_dir/down -} - -sv_end() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - (( $# != 0 )) && die100 "$0: incorrect arguments" - - if ! [[ -L $scandir/$sv_name ]]; then - - s6-svlink -t 3000 $scandir $sv_dir - # - s6-mkfifodir $sv_dir/event - # - ln -s -T $sv_dir $scandir/$sv_name - # new_services+=( $scandir/$sv_name ) + check_sv_xorg + check_sv_server + check_sv_client + if ((signal_svscan)); then + s6-svscanctl -a /run/service || exit $? fi - unset sv_name sv_dir -} - -sv_link_command() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - (( $# != 2 )) && die100 "$0: incorrect arguments" - - local script_path=$sv_dir/$1 - - ln -s -f -T ${commands[$2]} $script_path -} - -sv_write_lines() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - (( $# < 2 )) && die100 "$0: incorrect arguments" - - local file_path=$sv_dir/$1 - shift - printf '%s\n' >$file_path "$@" || \ - die111 "Error writing to ${(qqq)file_path}" -} - -sv_el_script() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - - - sv_write_lines $1 '#!/bin/execlineb -P' "$@[2,-1]" - - chmod +x $sv_dir/$1 -} - -sv_notification_fd() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - (( $# != 1 )) && die100 "$0: incorrect arguments" - - - sv_write_lines notification-fd $1 -} - -sv_env() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - (( $# < 2 )) && die100 "$0: incorrect arguments" - - - mkdir -p $sv_dir/env - local k v - for k v in "$@"; do - [[ $k == */* ]] && die100 "$0: invalid env variable ${(qqq)k}" - - sv_write_lines env/$k $v - done -} - -sv_mkdir() { - (($+sv_name)) || die100 "$0: no service definition started" - (($+sv_dir)) || die100 "$0: inconsistent state for service ${(qqq)sv_name}" - (( $# < 1 )) && die100 "$0: incorrect arguments" - - - mkdir -p $sv_dir/${^@} -} - -# end service builder - -sv_common_finish() { - local -a lines=( - 'backtick -E up { s6-svstat -o up ../'${(qqq)sv_name_server}' }' - 'backtick -E wantedup { s6-svstat -o wantedup ../'${(qqq)sv_name_server}' }' - ## down this service if server is neither up nor wanted up - 'if -n { if { $up } $wantedup }' - # 'ifelse { eltest true == $up -o true == $wantedup } { }' - 's6-svc -d .' - ) - - sv_el_script finish $lines -} - -write_sv_xorg() { - - sv_start $sv_name_xorg - - sv_notification_fd 3 - - sv_link_command run service.xpra-xorg.run - - sv_common_finish - - sv_end -} - -write_sv_server() { - - sv_start $sv_name_server - - sv_el_script run \ +} + + +mksvcdir(){ + local name tmpsv f data + name=$1 + shift || exit $? + mkdir -p $build || exit $? + tmpsv=$build/$name + mkdir $tmpsv || exit $? + touch $tmpsv/run || exit $? + printf '%s\n' "$@" >$tmpsv/run || exit $? + chmod +x $tmpsv/run || exit $? + mv $tmpsv $scandir/ || exit $? + (( signal_svscan++ )) + return 0 +} + +check_sv_xorg() { + local sv=$scandir/$sv_name_xorg + [[ -d $sv ]] && return + + local tmpsv=$build/$sv_name_xorg + mkdir -p $tmpsv/{env,data} || exit $? + touch $tmpsv/down || exit $? + echo 3 >$tmpsv/notification-fd || exit $? + ln -s -f ${commands[service.xpra-xorg.run]} $tmpsv/run || exit $? + mv $tmpsv $scandir/ || exit $? + (( signal_svscan++ )) +} + +check_sv_server() { + local sv=$scandir/$sv_name_server + [[ -d $sv ]] && return + + mksvcdir $sv:t \ + '#!/bin/execlineb -P' \ xpra-server $display --displayfd=3 - - sv_notification_fd 3 - - sv_end + echo 3 >$sv/notification-fd || exit $? } -write_sv_client() { - - sv_start $sv_name_client - - sv_el_script run \ +check_sv_client() { + local sv=$scandir/$sv_name_client + [[ -d $sv ]] && return + + mksvcdir $sv:t \ + '#!/bin/execlineb -P' \ 's6-envdir env' \ xpra-client "$display" \ "--title=\"[${container}] @title@ «s=@server-machine@ c=@client-machine@ d=@hostinfo@ w=@xid@\"" - - sv_common_finish - - sv_env DISPLAY $DISPLAY - - sv_end -} - -write_sv_definitions() { - - write_sv_xorg - - write_sv_server - - write_sv_client -} - + mkdir -p $sv/env || exit $? + printf '%s\n' $DISPLAY >$sv/env/DISPLAY +} do_unlock() { zsystem flock -u $lockfd @@ -260,8 +125,8 @@ } start_services() { - - s6-svc -wU -T 5000 -o $scandir/$sv_name_server - - s6-svc -o $scandir/$sv_name_client + s6-svc -wU -T 5000 -o $scandir/$sv_name_server || exit $? + s6-svc -o $scandir/$sv_name_client || exit $? } print_cookie() {