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.
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" {
32 function find_login_window_for_user
{
33 # This function mimics the behaviour of pgrep, which may not be installed
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.
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
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
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
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"
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
94 bootstrap_user
="launchctl bsexec $pid"
97 $bootstrap_user $sudo_user $stop
98 $bootstrap_user $sudo_user $unload