[launchd-dev] How to ensure that my job is launched through launchd and not directly?
Damien Sorresso
dsorresso at apple.com
Thu Apr 16 12:20:08 PDT 2015
There are a couple strategies you can use.
In your launchd.plist(5), you can define a certain environment variable, like LAUNCHED_BY_LAUNCHD or something. If you check for that variable with getenv(3) and it is not present, you know that you weren't launched properly.
Alternatively, as of Yosemite, launchd sets an environment variable called XPC_SERVICE_NAME whose value is the label you've given your job. You can check that too.
Do not do a getppid(3) check against 1. It will fail on older OSes if you're an agent, and the kernel may reparent processes to PID 1 under certain conditions, so having a parent PID of 1 is not necessarily indicative of being managed by launchd.
-damien
On 16 Apr, 2015, at 04:25, Raunak Poddar <raunak.poddar at gmail.com> wrote:
> I have a program and I want to ensure that it has been launched only by launchd (as a daemon) & it should terminate if the user launches it directly (double-click on Finder, terminal). Is there a nifty way of accomplishing this?
> _______________________________________________
> launchd-dev mailing list
> launchd-dev at lists.macosforge.org
> https://lists.macosforge.org/mailman/listinfo/launchd-dev
More information about the launchd-dev
mailing list