Trying to see of you are a child of launchd
may actually be the sane way of doing but the solution I want needs to be conservative about the system calls you use.
Here are two different approaches. One is to to check whether one of the standard file descriptors is a tty (that won't work if they all redirected though). Sample code:
#include <unistd.h>
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
if (isatty (STDIN_FILENO) || isatty (STDOUT_FILENO) || isatty(STDERR_FILENO))
{ printf ("tty launch\n"); fflush (stdout); }
else
{ NSLog (@"Launch service"); }
}
the other one is too look if the environment variable TERM is unset or, for open
launches, if the environment variable _
is /usr/bin/open
):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSDictionary *env = [[NSProcessInfo processInfo] environment];
NSLog(@"%@",env);
NSString* term = [env objectForKey:@"TERM"];
NSString* underscore = [env objectForKey:@"_"];
if (!term || [underscore isEqualTo:@"/usr/bin/open"])
{ NSLog (@"Launch service"); }
else
{ printf ("tty launch\n"); fflush (stdout); }
}