1#!/bin/sh 2 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. 6 7# Version = @@VERSION@@ 8 9HELPERTOOLS=/Library/PrivilegedHelperTools 10SERVICE_NAME=org.chromium.chromoting 11CONFIG_FILE="$HELPERTOOLS/$SERVICE_NAME.json" 12SCRIPT_FILE="$HELPERTOOLS/$SERVICE_NAME.me2me.sh" 13USERS_TMP_FILE="$SCRIPT_FILE.users" 14PLIST=/Library/LaunchAgents/org.chromium.chromoting.plist 15ENABLED_FILE="$HELPERTOOLS/$SERVICE_NAME.me2me_enabled" 16ENABLED_FILE_BACKUP="$ENABLED_FILE.backup" 17 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. 21function on_error { 22 logger An error occurred during Chrome Remote Desktop setup. 23} 24 25function 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 29 }' 30} 31 32function 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 }' 37} 38 39trap on_error ERR 40 41logger Running Chrome Remote Desktop preflight script @@VERSION@@ 42 43# If there is an _enabled file, rename it while upgrading. 44if [[ -f "$ENABLED_FILE" ]]; then 45 mv "$ENABLED_FILE" "$ENABLED_FILE_BACKUP" 46fi 47 48# Stop and unload the service for each user currently running the service, and 49# record the user IDs so the service can be restarted for the same users in the 50# postflight script. 51rm -f "$USERS_TMP_FILE" 52 53for uid in $(find_users_with_active_hosts); do 54 if [[ -n "$uid" ]]; then 55 echo "$uid" >> "$USERS_TMP_FILE" 56 if [[ "$uid" = "0" ]]; then 57 context="LoginWindow" 58 else 59 context="Aqua" 60 fi 61 pid="$(find_login_window_for_user "$uid")" 62 if [[ -n "$pid" ]]; then 63 launchctl bsexec "$pid" sudo -u "#$uid" launchctl stop "$SERVICE_NAME" 64 launchctl bsexec "$pid" sudo -u "#$uid" launchctl unload -w -S \ 65 "$context" "$PLIST" 66 fi 67 fi 68done 69 70exit 0 71