#!/bin/sh ################################################################################ # # File: licensed_load.sh # # Contains: Shell script that loads the PACE Eden License Daemon. # This script must be run as root in order to work. # # Copyright: 2013 - 2023 by PACE Anti-Piracy, all rights reserved. # ################################################################################ #------------------------------------------------------------------------------- # Initialize script globals #------------------------------------------------------------------------------- # Define the daemon plist and path constants gDaemonLabel="com.paceap.eden.licensed" gDaemonPlistPath="/Library/LaunchDaemons/$gDaemonLabel.plist" gDaemonServiceTarget="system/$gDaemonLabel" # Define the version of the OS where we start supporting launchctl 2.0. Even though # launchctl 2.0 was introduced on 10.10 Yosemite, we only support the new commands on # 10.11 El Capitan and above because the Yosemite support was apparently incomplete # (i.e. the "unbootstrap" command wasn't implemented and was replaced with "bootout"). gLaunchctl2MajorVersion=15 # Get the OS version gOsVersion=`uname -r` gOsMajorVersion=${gOsVersion%%.*} #echo "Running OS vers $gOsVersion, major version $gOsMajorVersion." # Define the launchctl tool to use. For standalone scripts, this should include the # "sudo" command. gLaunchctlCommand="/bin/launchctl" #------------------------------------------------------------------------------- fatalError() { echo "Error: $1" exit 1 } #------------------------------------------------------------------------------- # Load LicenseD via launchctl if it's not already running. #------------------------------------------------------------------------------- loadLicenseDManually() { echo "Loading license daemon as needed..." # Check to see if the LicenseD plist file exists. If it doesn't then it's # an unrecoverable error. if [[ ! -f "$gDaemonPlistPath" ]]; then fatalError "Could not find the LicenseD plist file. Please reinstall License Support." fi # If this is an OS where we support launchctl 2.0, then we want to enable the service # before doing anything else. We do this to deal with the edge case where LicenseD # is in fact running but it's been disabled, which means it will be gone on the next # boot. # # It should always be safe to enable the LicenseD service, even if it's never been # bootstrapped or loaded. if [[ $gOsMajorVersion -ge $gLaunchctl2MajorVersion ]]; then echo "Enabling the license daemon service in the system domain..." `$gLaunchctlCommand enable $gDaemonServiceTarget` result=$? if [[ $result -ne 0 ]]; then fatalError "launchctl enable failed with error $result." fi fi # See if we LicenseD is already running under launchd control. If it is then there's # nothing to do. echo "Checking to see if license daemon is running..." `$gLaunchctlCommand list | /usr/bin/grep -q "$gDaemonLabel"` if [[ $? -ne 0 ]]; then # LicenseD is not running so we need to load it. If this is a new enough OS, then # use the launchctl 2.0 bootstrap command. Otherwise fall back and use the legacy # load command. if [[ $gOsMajorVersion -ge $gLaunchctl2MajorVersion ]]; then # Bootstrap the LicenseD service into the system domain. This will start the # service as root if it hasn't already been started. echo "Bootstrapping the license daemon service into the system domain..." `$gLaunchctlCommand bootstrap system "$gDaemonPlistPath"` result=$? if [[ $result -ne 0 ]]; then fatalError "launchctl bootstrap failed with error $result." fi else # We're on an OS version earlier than 10.11 El Cap, so use the original launchctl # load command. echo "Loading license daemon..." `$gLaunchctlCommand load "$gDaemonPlistPath"` result=$? if [[ $result -ne 0 ]]; then fatalError "launchctl load failed with error $result." fi # TODO: Determine if we really need to check if LicenseD is running. According # to the docs, only "remove" returns immediately. # Since the the load and unload launchctl commands always return 0 as long as the # request is well formed (see the launchctl man page for details on this behavior) # we have to separately check to make sure that LicenseD is actually loaded. # # To do this, we loop up to 5 times with a 1-second delay between checks. This # gives LicenseD 5 seconds to start up. daemonStarted=0 daemonCheckCount=0 for (( daemonCheckCount=0 ; $daemonCheckCount != 5 ; daemonCheckCount=daemonCheckCount+1 )) do `$gLaunchctlCommand list | /usr/bin/grep -q "$gDaemonLabel"` result=$? if [[ $result -eq 0 ]]; then daemonStarted=1 echo "The license daemon has started." break fi echo "Waiting for the license daemon to start..." sleep 1 # give it more time done # If we LicenseD was not started, report the error and fail if [[ $daemonStarted -eq 0 ]]; then fatalError "LicenseD was not found under launchctl control after $daemonCheckCount seconds." fi fi # If we get here, LicenseD was successfully loaded. echo "License daemon was successfully loaded." else echo "License daemon is already running. Nothing to do." fi echo } #------------------------------------------------------------------------------- # Script starts here #------------------------------------------------------------------------------- # Load LicenseD via launchctl if it's not already running. loadLicenseDManually # Exit with success exit 0