libsm: update to 1.2.5
[oi-userland.git] / components / x11 / x11vnc / files / svc-x11vnc
blob547bff65f1a46e77928e23d2a6e5f0fba6d0f30b
1 #!/bin/sh
4 # Method script to start x11vnc daemon connected to an X11 display
5 # and represents the system desktop session (or another display)
6 # for persistent remote access. Starts as root and changes to the
7 # currently appropriate account, selected as implemented below.
9 # The X11VNC program must run as the same account which runs the
10 # X11 server at the moment, to use the same magic cookies etc.,
11 # e.g. "lightdm" when waiting for login and as the "real user" if
12 # reconnecting after someone has logged in there. A VNC client
13 # disconnection as well as a session logout stops the daemon (and
14 # SMF then restarts it, connecting to the existing X11 session).
16 # This file and its contents are supplied under the terms of the
17 # Common Development and Distribution License ("CDDL"), version 1.0.
18 # You may only use this file in accordance with the terms of version
19 # 1.0 of the CDDL.
21 # A full copy of the text of the CDDL should have accompanied this
22 # source. A copy of the CDDL is also available via the Internet at
23 # http://www.illumos.org/license/CDDL.
25 # Copyright 2022 Jim Klimov
27 . /lib/svc/share/smf_include.sh
29 SMF_FMRI_BASENAME="`echo "${SMF_FMRI}" | sed 's,:[^:]*$,,'`"
30 SMF_FMRI_INSTANCE="`echo "${SMF_FMRI}" | sed 's,^.*:\([^:]*\)$,\1,'`"
32 getpropInst() {
33 OUT="`svcprop -p "$1" "$2" 2>/dev/null`" || return
34 if [ x"${OUT}" = x'""' ]; then OUT=""; fi
35 echo "$OUT"
38 getprop() {
39 getpropInst "$1" "$SMF_FMRI" \
40 || getpropInst "$1" "$SMF_FMRI_BASENAME"
43 getpropNumPositive() {
44 NUM="`getpropInst "$1" "$SMF_FMRI"`" \
45 && [ -n "${NUM}" ] && [ "${NUM}" -gt 0 ] \
46 && { echo "$NUM" ; return 0; }
48 NUM="`getpropInst "$1" "$SMF_FMRI_BASENAME"`" \
49 && [ -n "${NUM}" ] && [ "${NUM}" -gt 0 ] \
50 && { echo "$NUM" ; return 0; }
52 return 1
55 getpropStringNotEmpty() {
56 STR="`getpropInst "$1" "$SMF_FMRI"`" \
57 && [ -n "${STR}" ] \
58 && { echo "$STR" ; return 0; }
60 STR="`getpropInst "$1" "$SMF_FMRI_BASENAME"`" \
61 && [ -n "${STR}" ] \
62 && { echo "$STR" ; return 0; }
64 return 1
67 # Currently we support one way of determining the needed account,
68 # but just in case anticipate extensibility:
69 PERMISSION_MODEL="`getpropStringNotEmpty x11vnc/PERMISSION_MODEL`" \
70 && [ -n "${PERMISSION_MODEL}" ] \
71 || PERMISSION_MODEL="lightdm"
73 # Default a display number from settings or SMF instance name:
74 DISPLAY_NUMBER="`getpropInst x11vnc/DISPLAY_NUMBER $SMF_FMRI`" \
75 && [ -n "${DISPLAY_NUMBER}" ] && [ "${DISPLAY_NUMBER}" -gt 0 ] \
76 || { DISPLAY_NUMBER="`echo "${SMF_FMRI_INSTANCE}" | sed 's,^display-\([0-9]*\)$,\1,'`" \
77 && [ -n "${DISPLAY_NUMBER}" ] && [ "${DISPLAY_NUMBER}" -gt 0 ] \
78 || DISPLAY_NUMBER="0"
81 # Note: while port 5900 is standard for VNC on DISPLAY=":0" it may
82 # conflict with other enabled VNC server implementations (including
83 # an inetd-style listener) so we let it be configured:
84 BASE_PORT="`getpropNumPositive x11vnc/BASE_PORT`" \
85 || BASE_PORT="5900"
87 PORT="`getpropInst x11vnc/PORT $SMF_FMRI`" \
88 && [ -n "${PORT}" ] && [ "${PORT}" -gt 0 ] \
89 || PORT="`expr $BASE_PORT + $DISPLAY_NUMBER`"
91 ARGS="`getpropStringNotEmpty x11vnc/ARGS`" \
92 || ARGS=""
94 getUser() {
95 # Echo back the user name to run as:
96 case "${PERMISSION_MODEL}" in
97 lightdm)
98 if [ -s "/var/run/lightdm/root/:${DISPLAY_NUMBER}" ]; then
99 ls -la "/var/run/lightdm/root/:${DISPLAY_NUMBER}" \
100 | awk '{print $3}' \
101 && return 0
103 echo "Could not determine current lightdm/X11 user account for DISPLAY=:${DISPLAY_NUMBER}" >&2
105 *) echo "Unsupported x11vnc/PERMISSION_MODEL: ${PERMISSION_MODEL}" >&2
106 # TODO: Hardcode? Define in SMF? Look for running Xorg processes?
107 # echo "root"
109 esac
110 return 1
113 X11USER="`getUser`" || X11USER=""
114 if [ -n "${X11USER}" ] ; then
115 X11USER_HOME="`getent passwd "${X11USER}" | awk -F: '{print \$6}'`"
116 else
117 X11USER_HOME="`getent passwd \"\`id -u\`\" | awk -F: '{print \$6}'`"
120 case "$1" in
121 start)
122 # Password file may be created by `vncpasswd` executed by the user,
123 # or by the server binary like this:
124 # x11vnc -storepasswd yourVNCpasswordHERE ~/.vnc/passwd
125 case "${ARGS}" in
126 *rfbauth*|*storepasswd*) ;;
127 *) if [ -s "${X11USER_HOME}/.vnc/passwd" ] ; then
128 ARGS="$ARGS -rfbauth '${X11USER_HOME}/.vnc/passwd'"
131 esac
133 # set -x
134 if [ -n "${X11USER}" ] ; then
135 exec su - "${X11USER}" -c "/usr/bin/x11vnc -display ':${DISPLAY_NUMBER}' -rfbport '${PORT}' $ARGS"
136 else
137 exec /usr/bin/x11vnc -display ":${DISPLAY_NUMBER}" -rfbport "${PORT}" $ARGS
140 esac