06-26-04 03:11 PM
> - When I start it from the shell : #my_http_serv I would like to get
> the same behaviour than apache.
> example : #my_http_serv start
> # <- here i come back
> to the original shell.
> How Can I do that please ?
>
> Thanks.
>
> p.s :I have written all the information ( pid , ...) into files ....\
This could be a good start.
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#ifdef USE_PROTOTYPES
int makeDaemon(int argc, char **argv,int (*parent)())
#else
int makeDaemon(argc, argv, parent)
int argc;
char **argv;
int (*parent)();
#endif
{
int ii,noctty,noblock,fd[3];
long parent_pid, child_pid;
parent_pid = (long)getpid();
if (0L > (child_pid=(long)fork())) /* failed */
return(-1);
else if (child_pid) /* parent */
{
if (parent) (void) parent(parent_pid,argc,argv);
exit(0);
}
/* child */
for(ii=0;3 > ii;ii++) /* detach terminal */
{
if (isatty(ii))
{
fd[ii] = -1;
close(ii);
}
else
fd[ii] = ii;
}
setsid(); /* become session leader */
if (0 > (child_pid=(long)fork())) /* failed */
return(-1);
else if (child_pid) /* parent */
exit(0);
umask(000); /* allow all users access to files created */
noctty = noblock = 0; /* reset file mode mask */
#ifdef O_NOCTTY
noctty |= O_NOCTTY;
#endif
#ifdef O_NDELAY
noblock |= O_NDELAY;
#endif
#ifdef O_NONBLOCK
noblock |= O_NONBLOCK;
#endif
/* open handles to nul device */
for(ii=0;3 > ii;ii++) /* detach terminal */
{
if (0 <= ii) continue;
switch(ii)
{
case 0:
open("/dev/null",O_RDONLY|noctty); /* stdin */
break;
case 1:
open("/dev/null",O_APPEND|noctty); /* stdout */
break;
case 2:
open("/dev/null",O_APPEND|noblock|noctty); /* stderr */
break;
}
}
return(0); /* daemon should be installed */
} /* int makeDaemon(int argc, char **argv,int (*parent)()) */
[ Post a follow-up to this message ]
|