mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 00:30:53 +00:00
* Tools/gdnc.m (main): Close any open file descriptors so we can
be a proper daemon. Fixes #4938. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@17580 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
7bc03ea631
commit
560f02f25b
2 changed files with 109 additions and 1 deletions
|
@ -1,6 +1,12 @@
|
||||||
|
2003-08-31 Adam Fedor <fedor@gnu.org>
|
||||||
|
|
||||||
|
* Tools/gdnc.m (main): Close any open file descriptors so we can
|
||||||
|
be a proper daemon.
|
||||||
|
Fixes #4938.
|
||||||
|
|
||||||
2003-08-29 Adam Fedor <fedor@gnu.org>
|
2003-08-29 Adam Fedor <fedor@gnu.org>
|
||||||
|
|
||||||
* config/procfs.m4: Disalbe procfs for solaris
|
* config/procfs.m4: Disable procfs for solaris
|
||||||
|
|
||||||
2003-08-26 Adam Fedor <fedor@gnu.org>
|
2003-08-26 Adam Fedor <fedor@gnu.org>
|
||||||
|
|
||||||
|
|
102
Tools/gdnc.m
102
Tools/gdnc.m
|
@ -28,12 +28,77 @@
|
||||||
#include "process.h"
|
#include "process.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#ifdef HAVE_SYSLOG_H
|
||||||
|
#include <syslog.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#ifndef NSIG
|
#ifndef NSIG
|
||||||
#define NSIG 32
|
#define NSIG 32
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static int is_daemon = 0; /* Currently running as daemon. */
|
||||||
|
static char ebuf[2048];
|
||||||
|
|
||||||
|
#ifdef HAVE_SYSLOG
|
||||||
|
|
||||||
|
static int log_priority;
|
||||||
|
|
||||||
|
static void
|
||||||
|
gdnc_log (int prio)
|
||||||
|
{
|
||||||
|
if (is_daemon)
|
||||||
|
{
|
||||||
|
syslog (log_priority | prio, ebuf);
|
||||||
|
}
|
||||||
|
else if (prio == LOG_INFO)
|
||||||
|
{
|
||||||
|
write (1, ebuf, strlen (ebuf));
|
||||||
|
write (1, "\n", 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
write (2, ebuf, strlen (ebuf));
|
||||||
|
write (2, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prio == LOG_CRIT)
|
||||||
|
{
|
||||||
|
if (is_daemon)
|
||||||
|
{
|
||||||
|
syslog (LOG_CRIT, "exiting.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf (stderr, "exiting.\n");
|
||||||
|
fflush (stderr);
|
||||||
|
}
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define LOG_CRIT 2
|
||||||
|
#define LOG_DEBUG 0
|
||||||
|
#define LOG_ERR 1
|
||||||
|
#define LOG_INFO 0
|
||||||
|
#define LOG_WARNING 0
|
||||||
|
void
|
||||||
|
gdnc_log (int prio)
|
||||||
|
{
|
||||||
|
write (2, ebuf, strlen (ebuf));
|
||||||
|
write (2, "\n", 1);
|
||||||
|
if (prio == LOG_CRIT)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "exiting.\n");
|
||||||
|
fflush (stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ihandler(int sig)
|
ihandler(int sig)
|
||||||
{
|
{
|
||||||
|
@ -909,6 +974,7 @@ ihandler(int sig)
|
||||||
int
|
int
|
||||||
main(int argc, char** argv, char** env)
|
main(int argc, char** argv, char** env)
|
||||||
{
|
{
|
||||||
|
int c;
|
||||||
GDNCServer *server;
|
GDNCServer *server;
|
||||||
NSString *str;
|
NSString *str;
|
||||||
BOOL shouldFork = YES;
|
BOOL shouldFork = YES;
|
||||||
|
@ -948,6 +1014,7 @@ main(int argc, char** argv, char** env)
|
||||||
#else
|
#else
|
||||||
if (shouldFork)
|
if (shouldFork)
|
||||||
{
|
{
|
||||||
|
is_daemon = 1;
|
||||||
switch (fork())
|
switch (fork())
|
||||||
{
|
{
|
||||||
case -1:
|
case -1:
|
||||||
|
@ -971,6 +1038,41 @@ main(int argc, char** argv, char** env)
|
||||||
}
|
}
|
||||||
#endif /* !MINGW */
|
#endif /* !MINGW */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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));
|
||||||
|
gdnc_log(LOG_CRIT);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (open("/dev/null", O_WRONLY) != 1)
|
||||||
|
{
|
||||||
|
sprintf(ebuf, "failed to open stdout from /dev/null (%s)\n",
|
||||||
|
strerror(errno));
|
||||||
|
gdnc_log(LOG_CRIT);
|
||||||
|
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));
|
||||||
|
gdnc_log(LOG_CRIT);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
#if GS_WITH_GC == 0
|
#if GS_WITH_GC == 0
|
||||||
CREATE_AUTORELEASE_POOL(pool);
|
CREATE_AUTORELEASE_POOL(pool);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue