4d-plugin-purge

Privileged HelperTool to execute purge on macOS

View the Project on GitHub miyako/4d-plugin-purge

4d-plugin-purge

Purge runs macOS’s purge command from 4D, which flushes the disk cache (inactive file-backed memory). purge needs root privileges, so the plugin installs a privileged helper tool (com.4D.purge.HelperTool, via SMJobBless) the first time you use it, and asks that helper to start purge. The command returns an Object describing the installation and the process ID of the purge it started.

Command Returns Purpose
Purge disk cache Object Install the helper if needed, then start purge as root

Platforms: macOS only (the Windows binary in the bundle is a stub)


Requirements & platform notes


Purge disk cache

Syntax

Purge disk cache → status
Parameter Type Description
Result Object Installation status and the process ID of purge (see below)

Description

Purge disk cache takes no parameters. Each call:

  1. Checks whether /Library/PrivilegedHelperTools/com.4D.purge.HelperTool exists. If it doesn’t, it launches the installer app and continues without waiting.
  2. If the helper exists, runs the small command line tool com.4D.purge.proxy (inside the plugin bundle), which connects to the helper over XPC and asks it to start purge. The helper replies with the process ID.

Result object:

Property Type Description
helperToolPath Text Where the helper is (or will be) installed: /Library/PrivilegedHelperTools/com.4D.purge.HelperTool
purgePath Text Path of purge found by /usr/bin/which, or "" if not found
proxyPath Text Path of the com.4D.purge.proxy tool inside the plugin bundle
isHelperToolInstalled Boolean True if the helper file exists
pid Real Process ID of the purge process started as root, or 0 if nothing was started

isHelperToolInstalled only checks that the file exists, not that the helper is running or up to date.

With the fixed source in this repository, the command gives up waiting for the helper after 10 seconds and returns pid 0; earlier builds could block the calling process indefinitely if the helper never answered.

Example

From the plugin’s own test method (TEST.4dm):

$info:=Purge disk cache

Check the result:

$info:=Purge disk cache

Case of 
	: (Not($info.isHelperToolInstalled))
		ALERT("Approve the helper tool, then try again.")
	: ($info.purgePath="")
		ALERT("purge was not found on this system.")
	: ($info.pid=0)
		ALERT("The helper did not start purge.")
	Else 
		// purge is running as process $info.pid
End case 

Error handling & troubleshooting


Quick reference

$info:=Purge disk cache
If ($info.pid#0)
	// purge started
End if 

Build information

Based on EvenBetterAuthorizationSample.

The repository has two Xcode projects: Purge.xcodeproj at the root builds the plugin and the com.4D.purge.proxy tool; helper/Purge.xcodeproj builds the installer app (Purge.app) and the helper tool it embeds. Purge.app and com.4D.purge.proxy are committed prebuilt inside the plugin bundle; the release workflow rebuilds only the plugin binary and re-signs the whole bundle.

As explained here, it is imperative to run SMJobBlessUtil.py before building the application. Do not update the project version, or the python script will fail.

python3 version: https://gist.github.com/mikeyh/89a1e2ecc6849ff6056b7391c5216799

The syntax is

SMJobBlessUtil.py setreq {app} {App-Info.plist} {HelperTool-Info.plist}

This updates SMPrivilegedExecutables in App-Info.plist and SMAuthorizedClients in HelperTool-Info.plist.

It seems SMJobBless and launchd reads Info.plist of the main application, not of the plugin (which makes sense). The plugin uses a small app (hidden, LSBackgroundOnly) to install the helper. The “installer” app displays a simple user interface if the argument --debug is passed.

It seems like there is no easy way to call NSXPCConnection or NSXPCInterface from the plugin, especially if the plugin is reloaded without terminating the main application. As a workaround, a proxy console application is called each time to run purge.