mrrl-logincaps

MRRL version of logincaps
git clone https://ccx.te2000.cz/git/mrrl-logincaps
Log | Files | Refs

login.capability.app-terminal (2259B)


      1 #!/bin/zsh
      2 setopt no_unset extended_glob warn_create_global
      3 
      4 typeset -g scriptname=${0:t}
      5 die_n() {
      6 	exitcode=$1
      7 	shift
      8 	printf >&2 '%s\n' "$scriptname: fatal: $@"
      9 	exit $exitcode
     10 }
     11 
     12 die100() {
     13 	die_n 100 "$@"
     14 }
     15 
     16 die111() {
     17 	die_n 111 "$@"
     18 }
     19 
     20 die_usage() {
     21 	die100 "$@" "usage: login.capability.app-terminal app_name"
     22 }
     23 
     24 launch-urxvt() {
     25 	local container cmd exec_sock urxvt_sock
     26 	container=$1
     27 	cmd=$2
     28 	exec_sock=/run/containers/$container/run/exec/exec
     29 	urxvt_sock=/run/containers/xsession.$X.$USER/run/exec/urxvt
     30 
     31 	[[ -S $exec_sock ]] ||
     32 		die111 "Socket for exec not present: ${(qqq)exec_sock}"
     33 
     34 	[[ -S $urxvt_sock ]] ||
     35 		die111 "Socket for urxvt not present: ${(qqq)urxvt_sock}"
     36 
     37 	local -a el_env=(
     38 		EXEC_SOCK=$exec_sock
     39 		URXVT_SOCK=$urxvt_sock
     40 		TERM_NAME=${container//./:}
     41 	)
     42 	local -a el_cmd=(
     43 		'multisubstitute {'
     44 		'  importas -i -u EXEC_SOCK EXEC_SOCK'
     45 		'  importas -i -u URXVT_SOCK URXVT_SOCK'
     46 		'  importas -i -u TERM_NAME TERM_NAME'
     47 		'}'
     48 		'socketpair 3 1'
     49 		'background -d {'
     50 		'  fdclose 3'
     51 		'  s6-sudo $EXEC_SOCK'
     52 		'  /mnt/ns/bin/spawn-pty rxvt-unicode-256color {'
     53 		"    $cmd"
     54 		'  } /mnt/ns/bin/fdsend 1 0 true'
     55 		'}'
     56 		'fdmove -c 1 2'
     57 		'fdrecvto 3 0'
     58 		'fdclose 3'
     59 		'backtick -E PTS_NAME { ptsname 0 }'
     60 		's6-sudo $URXVT_SOCK ${TERM_NAME}:${PTS_NAME}'
     61 	)
     62 	exec env $el_env execlineb -c "${(F)el_cmd}"
     63 }
     64 
     65 ensure-container-started() {
     66 	[[ $1 == */* ]] && die "Invalid container name: ${(qqq)1}"
     67 	s6-svc -wU -T 4000 -o /run/service/container.$1 || die111 "failed to start container ${(qqq)1}"
     68 }
     69 
     70 check_command() {
     71 	local arg
     72 	local -a run quoted
     73 	typeset -g container
     74 
     75 	case $1 in
     76 		(tinc:ccx)
     77 			container=tinc.tinc
     78 			run=( tinc -c /home/tinc/tinc/ccx -n ccx )
     79 			;;
     80 		(wpa_cli)
     81 			container=wpa_supplicant.wpa_supplicant
     82 			run=( wpa_cli )
     83 			;;
     84 		(networking)
     85 			container=networking.networking
     86 			run=( zsh -li )
     87 			;;
     88 		(*) die100 "app not permitted: ${(qqq)1}";;
     89 	esac
     90 
     91 	for arg in "$run[@]"; do
     92 		quoted+=( "$(s6-quote -- "$arg")" )
     93 	done
     94 
     95 	typeset -g quoted_cmd
     96 	quoted_cmd="${(j: :)quoted}"
     97 }
     98 
     99 main() {
    100 	[[ $# == 1 ]] || die_usage "Incorrect number of arguments"
    101 	check_command $1
    102 	ensure-container-started $container
    103 	launch-urxvt $container $quoted_cmd
    104 }
    105 typeset -f -t main
    106 
    107 main "$@"
    108 #  vim: ft=zsh noet ts=4 sts=4 sw=4