Home | History | Annotate | Download | only in debian
      1 #!/bin/bash
      2 
      3 ### BEGIN INIT INFO
      4 # Provides:             chrome-remote-desktop
      5 # Required-Start:       $remote_fs $syslog
      6 # Required-Stop:        $remote_fs $syslog
      7 # Default-Start:        2 3 4 5
      8 # Default-Stop:         0 1 6
      9 # Short-Description:    Chrome Remote Desktop service
     10 ### END INIT INFO
     11 
     12 # /etc/init.d/chrome-remote-desktop: Start and stop Chrome Remote Desktop host daemon.
     13 
     14 HOST_PATH=/opt/google/chrome-remote-desktop/chrome-remote-desktop
     15 
     16 # Group of users for which Chrome Remote Desktop is enabled. Users are added
     17 # to that group when they start the host for the first time.
     18 CHROME_REMOTING_GROUP=chrome-remote-desktop
     19 
     20 test -x $HOST_PATH || exit 0
     21 
     22 . /lib/lsb/init-functions
     23 
     24 if [ "$(whoami)" = "root" ]; then
     25   # Extract list of users in the chrome-remoting group.
     26   USERS=$(getent group $CHROME_REMOTING_GROUP |
     27           awk -F ':' '{ gsub(",", " ", $4); print $4 }')
     28 else
     29   USERS=$(whoami)
     30 fi
     31 
     32 # Tries to wait for 10 seconds until specified command exits and then kills it.
     33 run_with_timeout() {
     34   "$@" &
     35   pid=$!
     36   local time_left=10
     37   while [ $time_left -gt 0 ]; do
     38     (kill -0 $pid 2>/dev/null) || return `wait $pid`
     39     sleep 1
     40     time_left=$((time_left - 1))
     41   done
     42   (kill -0 $pid 2>/dev/null) || return `wait $pid`
     43 
     44   echo command \"$@\" has timed out >&2
     45   kill $pid
     46   return 1
     47 }
     48 
     49 # Usage: run_and_ignore_error [--login] user program [args...]
     50 #   --login:
     51 #     Run program in a clean login shell. This requires backgrounding, since
     52 #     the user's .profile or .login script might be run, which might contain
     53 #     blocking commands.
     54 run_and_ignore_error() {
     55   login_options=""
     56   if [ "$1" = "--login" ]; then
     57     login_options="-b -i"
     58     shift
     59   fi
     60 
     61   user=$1
     62   shift
     63 
     64   set +e
     65   if [ "$(whoami)" = "$user" ]; then
     66     "$@"
     67   else
     68     sudo -u "$user" $login_options "$@"
     69   fi
     70 }
     71 
     72 do_start() {
     73   log_begin_msg "Starting Chrome Remote Desktop host for $1..."
     74   run_and_ignore_error --login $1 "$HOST_PATH" --start
     75   log_end_msg $?
     76 }
     77 
     78 do_stop() {
     79   log_begin_msg "Stopping Chrome Remote Desktop host for $1..."
     80   run_with_timeout run_and_ignore_error $1 "$HOST_PATH" --stop
     81   log_end_msg $?
     82 }
     83 
     84 do_reload() {
     85   log_begin_msg "Reloading Chrome Remote Desktop host configuration for $1..."
     86   run_and_ignore_error $1 "$HOST_PATH" --reload
     87   log_end_msg $?
     88 }
     89 
     90 do_restart() {
     91   log_begin_msg "Restarting Chrome Remote Desktop host for $1..."
     92   run_and_ignore_error $1 "$HOST_PATH" --stop
     93   run_and_ignore_error --login $1 "$HOST_PATH" --start
     94   log_end_msg $?
     95 }
     96 
     97 for_each_user() {
     98   for user in $USERS; do
     99     $1 $user
    100   done
    101 }
    102 
    103 case "$1" in
    104   start)
    105     for_each_user do_start
    106     ;;
    107 
    108   stop)
    109     for_each_user do_stop
    110     ;;
    111 
    112   reload|force-reload)
    113     for_each_user do_reload
    114     ;;
    115 
    116   restart)
    117     for_each_user do_restart
    118     ;;
    119 
    120   *)
    121     log_success_msg "Usage: /etc/init.d/chrome-remote-desktop" \
    122         "{start|stop|reload|force-reload|restart}"
    123     exit 1
    124 esac
    125 
    126 exit 0
    127