Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / installer / mac / Scripts / remoting_preflight.sh
blobb1f347fb6d18d0062819a92bdc99c95523533b51
1 #!/bin/sh
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 # Version = @@VERSION@@
9 HELPERTOOLS=/Library/PrivilegedHelperTools
10 SERVICE_NAME=org.chromium.chromoting
11 CONFIG_FILE="$HELPERTOOLS/$SERVICE_NAME.json"
12 SCRIPT_FILE="$HELPERTOOLS/$SERVICE_NAME.me2me.sh"
13 USERS_TMP_FILE="$SCRIPT_FILE.users"
14 PLIST=/Library/LaunchAgents/org.chromium.chromoting.plist
15 ENABLED_FILE="$HELPERTOOLS/$SERVICE_NAME.me2me_enabled"
16 ENABLED_FILE_BACKUP="$ENABLED_FILE.backup"
18 # In case of errors, log the fact, but continue to unload launchd jobs as much
19 # as possible. When finished, this preflight script should exit successfully in
20 # case this is a Keystone-triggered update which must be allowed to proceed.
21 function on_error {
22 logger An error occurred during Chrome Remote Desktop setup.
25 function find_users_with_active_hosts {
26 ps -eo uid,command | awk -v script="$SCRIPT_FILE" '
27 $2 == "/bin/sh" && $3 == script && $4 == "--run-from-launchd" {
28 print $1
32 function find_login_window_for_user {
33 # This function mimics the behaviour of pgrep, which may not be installed
34 # on Mac OS X.
35 local user=$1
36 ps -ec -u "$user" -o comm,pid | awk '$1 == "loginwindow" { print $2; exit }'
39 # Return 0 (true) if the current OS is El Capitan (OS X 10.11) or newer.
40 function is_el_capitan_or_newer {
41 local full_version=$(sw_vers -productVersion)
43 # Split the OS version into an array.
44 local version
45 IFS='.' read -a version <<< "${full_version}"
46 local v0="${version[0]}"
47 local v1="${version[1]}"
48 if [[ $v0 -gt 10 || ( $v0 -eq 10 && $v1 -ge 11 ) ]]; then
49 return 0
50 else
51 return 1
55 trap on_error ERR
57 logger Running Chrome Remote Desktop preflight script @@VERSION@@
59 # If there is an _enabled file, rename it while upgrading.
60 if [[ -f "$ENABLED_FILE" ]]; then
61 mv "$ENABLED_FILE" "$ENABLED_FILE_BACKUP"
64 # Stop and unload the service for each user currently running the service, and
65 # record the user IDs so the service can be restarted for the same users in the
66 # postflight script.
67 rm -f "$USERS_TMP_FILE"
69 for uid in $(find_users_with_active_hosts); do
70 if [[ -n "$uid" ]]; then
71 echo "$uid" >> "$USERS_TMP_FILE"
72 if [[ "$uid" = "0" ]]; then
73 context="LoginWindow"
74 else
75 context="Aqua"
78 sudo_user="sudo -u #$uid"
79 stop="launchctl stop $SERVICE_NAME"
80 unload="launchctl unload -w -S $context $PLIST"
82 if is_el_capitan_or_newer; then
83 boostrap_user="launchctl asuser $uid"
84 else
85 # Load the launchd agent in the bootstrap context of user $uid's
86 # graphical session, so that screen-capture and input-injection can
87 # work. To do this, find the PID of a process which is running in that
88 # context. The loginwindow process is a good candidate since the user
89 # (if logged in to a session) will definitely be running it.
90 pid="$(find_login_window_for_user "$uid")"
91 if [[ ! -n "$pid" ]]; then
92 exit 1
94 bootstrap_user="launchctl bsexec $pid"
97 $bootstrap_user $sudo_user $stop
98 $bootstrap_user $sudo_user $unload
100 done
102 exit 0