Skip to content

Instantly share code, notes, and snippets.

@xrpdevs
Created July 6, 2021 17:53
Show Gist options
  • Save xrpdevs/b53c9962dd72ffce2cdd6bd9d4d87f28 to your computer and use it in GitHub Desktop.
Save xrpdevs/b53c9962dd72ffce2cdd6bd9d4d87f28 to your computer and use it in GitHub Desktop.

Revisions

  1. xrpdevs created this gist Jul 6, 2021.
    61 changes: 61 additions & 0 deletions Barrier
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/bin/sh

    ## Put this file in /Library/StartupItems/Barrier

    . /etc/rc.common

    BARRIER="/Library/StartupItems/Barrier/barrierc_prelogin"
    SERVER="192.168.7.101:24800"
    SCREEN_NAME="Jamies-iMac-Pro.local"

    cmd="$BARRIER --name $SCREEN_NAME --no-tray -l /tmp/barrier1.log $SERVER"

    echo $(date) " " $cmd >>/tmp/barrier2.log

    run=($cmd)

    KeepAlive ()
    {
    proc=${1##*/}

    echo "PID: $proc"

    while [ -x "$1" ]

    do
    echo $(date) " running test" >>/tmp/barrier2.log
    if ! ps axco command | grep -q "^${proc}\$"; then
    if ! ps axco command | grep -q "^barrierc\$"; then
    "$@"
    echo $(date) " restarting" >>/tmp/barrier2.log
    else
    echo $(date) " not restarting as user barrier process is running" >>/tmp/barrier2.log
    fi
    fi

    if [ -f /tmp/barrierc_user_running ]; then
    echo $(date) " killing root barrierc_prelogin" >>/tmp/barrier2.log
    killall barrierc_prelogin
    rm /tmp/barrierc_user_running
    fi
    sleep 3
    done
    }

    StartService ()
    {
    ConsoleMessage "Starting Barrier"
    KeepAlive "${run[@]}" &
    }

    StopService ()
    {
    return 0
    }

    RestartService ()
    {
    return 0
    }

    RunService "$1"
    22 changes: 22 additions & 0 deletions README.TXT
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    Open "Terminal" and login as root (type sudo su -)

    Put the two files "Barrier" and "StartupParameters.plist" in /Library/StartupItems/Barrier/
    create the Barrier directory if needed.

    Change BARRIER, SCREEN_NAME and SERVER to suit your setup.

    copy the barrierc binary from /Applications/Barrier.app/Contents/MacOS/barrierc to /Library/StartupItems/Barrier/barrierc_prelogin

    Create a launchd script which creates the file /tmp/barrierc_user_running when you log in.

    Barrier runs as root pre-login, checks for the presence of the "barrierc_user_running" file, and if so, terminates root's copy of
    barrierc (barrierc_prelogin) and deletes that file.

    When you log out, your (user) barrierc process stops, and barrierc_prelogin is restarted as root user.

    There are probably better ways to do this, I read about logout and login hooks, but their usage is not reccomended by Apple.

    You might want to get rid of the logging statements in the script, but the logs will go away on reboot anyways, they are
    just there to help with debugging.

    Give me a shout if you have any ideas for improvements.
    6 changes: 6 additions & 0 deletions StartupParameters.plist
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    {
    Description = "Barrier Client";
    Provides = ("Barrier");
    Requires = ("Network");
    OrderPreference = "None";
    }