mirror of
https://github.com/gnustep/libs-back.git
synced 2025-02-24 04:11:28 +00:00
Use NSTask to run as daemon
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/back/trunk@20672 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
df49274a8d
commit
38df9be337
2 changed files with 41 additions and 96 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
2005-02-08 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
* Tools/gpbs.m: Use NSTask to re-execute as daemon.
|
||||||
|
|
||||||
2005-02-05 00:44 Alexander Malmberg <alexander@malmberg.org>
|
2005-02-05 00:44 Alexander Malmberg <alexander@malmberg.org>
|
||||||
|
|
||||||
* Source/art/path.m (-_clip_add_svp:): Handle the case where
|
* Source/art/path.m (-_clip_add_svp:): Handle the case where
|
||||||
|
|
115
Tools/gpbs.m
115
Tools/gpbs.m
|
@ -43,7 +43,7 @@
|
||||||
#define NSIG 32
|
#define NSIG 32
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int is_daemon = 0; /* Currently running as daemon. */
|
static BOOL is_daemon = NO; /* Currently running as daemon. */
|
||||||
static char ebuf[2048];
|
static char ebuf[2048];
|
||||||
|
|
||||||
#ifdef HAVE_SYSLOG
|
#ifdef HAVE_SYSLOG
|
||||||
|
@ -1126,10 +1126,13 @@ static void
|
||||||
init(int argc, char** argv, char **env)
|
init(int argc, char** argv, char **env)
|
||||||
{
|
{
|
||||||
NSUserDefaults *defs;
|
NSUserDefaults *defs;
|
||||||
NSArray *args = [[NSProcessInfo processInfo] arguments];
|
NSProcessInfo *pInfo;
|
||||||
unsigned count, c;
|
NSMutableArray *args;
|
||||||
|
unsigned count;
|
||||||
BOOL shouldFork = YES;
|
BOOL shouldFork = YES;
|
||||||
|
|
||||||
|
pInfo = [NSProcessInfo processInfo];
|
||||||
|
args = AUTORELEASE([[pInfo arguments] mutableCopy]);
|
||||||
for (count = 1; count < [args count]; count++)
|
for (count = 1; count < [args count]; count++)
|
||||||
{
|
{
|
||||||
NSString *a = [args objectAtIndex: count];
|
NSString *a = [args objectAtIndex: count];
|
||||||
|
@ -1143,6 +1146,11 @@ init(int argc, char** argv, char **env)
|
||||||
printf("--verbose\tMore verbose debug output\n");
|
printf("--verbose\tMore verbose debug output\n");
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
else if ([a isEqualToString: @"--daemon"] == YES)
|
||||||
|
{
|
||||||
|
is_daemon = YES;
|
||||||
|
shouldFork = NO;
|
||||||
|
}
|
||||||
else if ([a isEqualToString: @"--no-fork"] == YES)
|
else if ([a isEqualToString: @"--no-fork"] == YES)
|
||||||
{
|
{
|
||||||
shouldFork = NO;
|
shouldFork = NO;
|
||||||
|
@ -1184,98 +1192,31 @@ init(int argc, char** argv, char **env)
|
||||||
|
|
||||||
if (shouldFork == YES)
|
if (shouldFork == YES)
|
||||||
{
|
{
|
||||||
char **a = malloc((argc+2) * sizeof(char*));
|
NSFileHandle *null;
|
||||||
|
NSTask *t;
|
||||||
|
|
||||||
memcpy(a, argv, argc*sizeof(char*));
|
t = [NSTask new];
|
||||||
a[argc] = "--no-fork";
|
NS_DURING
|
||||||
a[argc+1] = 0;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Now fork off child process to run in background.
|
|
||||||
*/
|
|
||||||
#ifdef __MINGW__
|
|
||||||
if (_spawnv(_P_NOWAIT, argv[0], a) == -1)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "gpbs - spawn failed - bye.\n");
|
[args addObject: @"--daemon"];
|
||||||
exit(EXIT_FAILURE);
|
[t setLaunchPath: [[NSBundle mainBundle] executablePath]];
|
||||||
|
[t setArguments: args];
|
||||||
|
[t setEnvironment: [pInfo environment]];
|
||||||
|
null = [NSFileHandle fileHandleWithNullDevice];
|
||||||
|
[t setStandardInput: null];
|
||||||
|
[t setStandardOutput: null];
|
||||||
|
[t setStandardError: null];
|
||||||
|
[t launch];
|
||||||
|
DESTROY(t);
|
||||||
}
|
}
|
||||||
exit(EXIT_SUCCESS);
|
NS_HANDLER
|
||||||
#else
|
|
||||||
is_daemon = 1;
|
|
||||||
switch (fork())
|
|
||||||
{
|
{
|
||||||
case -1:
|
|
||||||
NSLog(@"gpbs - fork failed - bye.\n");
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
case 0:
|
|
||||||
/*
|
|
||||||
* Try to run in background.
|
|
||||||
*/
|
|
||||||
#ifdef NeXT
|
|
||||||
setpgrp(0, getpid());
|
|
||||||
#else
|
|
||||||
setsid();
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
if (verbose)
|
|
||||||
{
|
|
||||||
NSLog(@"Process backgrounded (running as daemon)\r\n");
|
|
||||||
}
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Ensure we don't have any open file descriptors which may refer
|
|
||||||
* to sockets bound to ports we may try to use.
|
|
||||||
*
|
|
||||||
* Use '/dev/null' for stdin and stdout.
|
|
||||||
*/
|
|
||||||
for (c = 0; c < FD_SETSIZE; c++)
|
|
||||||
{
|
|
||||||
if (is_daemon || (c != 2))
|
|
||||||
{
|
|
||||||
(void)close(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (open("/dev/null", O_RDONLY) != 0)
|
|
||||||
{
|
|
||||||
sprintf(ebuf, "failed to open stdin from /dev/null (%s)\n",
|
|
||||||
strerror(errno));
|
|
||||||
gpbs_log(LOG_CRIT);
|
gpbs_log(LOG_CRIT);
|
||||||
exit(EXIT_FAILURE);
|
DESTROY(t);
|
||||||
}
|
}
|
||||||
if (open("/dev/null", O_WRONLY) != 1)
|
NS_ENDHANDLER
|
||||||
{
|
|
||||||
sprintf(ebuf, "failed to open stdout from /dev/null (%s)\n",
|
|
||||||
strerror(errno));
|
|
||||||
gpbs_log(LOG_CRIT);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (is_daemon && open("/dev/null", O_WRONLY) != 2)
|
|
||||||
{
|
|
||||||
sprintf(ebuf, "failed to open stderr from /dev/null (%s)\n",
|
|
||||||
strerror(errno));
|
|
||||||
gpbs_log(LOG_CRIT);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Try to re-execute the program in case we are running using the
|
|
||||||
* pth library (which would be messed up by having closed descriptors).
|
|
||||||
*/
|
|
||||||
if ([[NSBundle mainBundle] executablePath] != nil)
|
|
||||||
{
|
|
||||||
execve([[[NSBundle mainBundle] executablePath] cString], a, env);
|
|
||||||
sprintf(ebuf, "failed to exec to '%s' (%s)\n",
|
|
||||||
argv[0], strerror(errno));
|
|
||||||
gpbs_log(LOG_CRIT);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Make gpbs logging go to syslog unless overridden by user.
|
* Make gpbs logging go to syslog unless overridden by user.
|
||||||
|
|
Loading…
Reference in a new issue