🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #10080

Re: Programar daemon. Lenguaje C.

#10080
From: J.A. Gutierrez
Date: Fri, 22 Mar 2002 10:37
130 lines
3733 bytes
pmj <in@terra.es> wrote:

: � Donde puedo encontrar informaci�n de c�mo se programan los daemon
: (lenguaje C)  bajo Linux ?

#ifdef __sgi
#   define HAVE_DAEMONIZE
#endif

#ifndef HAVE_DAEMONIZE
/* _daemonize(3C) flags */
#define _DF_NOFORK      0x1     /* don't fork */
#define _DF_NOCHDIR     0x2     /* don't chdir to / */
#define _DF_NOCLOSE     0x4     /* close no files */

int my_daemonize(int flags, int fd1, int fd2, int fd3);
#define _daemonize my_daemonize
#endif

#define HAVE_SETSID 1 /* irix, linux, solaris */
...
if ( -1 == _daemonize(_DF_NOCHDIR,
	gprefs.f != NULL ? fileno(gprefs.f) : -1, -1,-1))
	perror("Can't daemonize");

...
int my_daemonize(int flags, int fd1, int fd2, int fd3)
{
    int status=0;
    int fd;

    debug_msg("daemonizing\n");

    if (!( flags & _DF_NOFORK ))
    {
        /* Fork and exit in the parent to disassociate from the
         * current process group and become the leader of a new
         * process group.
         */

        if ( -1 == (status = fork()))
        {
            perror("cant fork");
            exit(1);
        }
        else if ( status > 0 )
        {
            _exit(0);
        }

#if HAVE_SETSID
        setsid();
#endif


        if ( -1 == (status = fork()))
        {
            perror("cant fork again");
            exit(1);
        }
        else if ( status > 0 )
        {
            _exit(0);
        }
    }


#ifdef TIOCNOTTY
    if ( 0 < (fd = open("/dev/tty", O_RDWR)))
    {
        if (ioctl(fd, TIOCNOTTY, NULL) < 0)
        {
            perror("can't disassociate from the terminal");
            status=-1;
        }
        close(fd);
    }
#endif /* TIOCNOTTY */

    if (!(flags & _DF_NOCHDIR))
    {
        if (chdir("/.") < 0)
        {
            perror("can't chdir to /.");
            status=-1;
        }
    }

    if (!(flags & _DF_NOCLOSE))
    {
        long l;
        int i;

        l=sysconf(_SC_OPEN_MAX);

        while(l-->0)
        {
            if ((l!=fd1)&&(l!=fd2)&&(l!=fd3))
                close(l);
        }

        if ( -1 == ( i = open( "/dev/null", O_RDWR)))
        {
            perror("/dev/null");
            status=-1;
        }
        else
        {
            if ((1!=fd1)&&(1!=fd2)&&(1!=fd3))
                dup(i);
            if ((2!=fd1)&&(2!=fd2)&&(2!=fd3))
                dup(i);
        }
    }
    return(status);
}
#endif








-- 
PGP and other useless info at      \
http://www.cps.unizar.es/~spd/      \
finger://daphne.cps.unizar.es/spd    \       Timeo Danaos et dona ferentes
ftp://ivo.cps.unizar.es/pub/          \                         (Virgilio)   
Technical Details
Message-ID: <a7f1gm$hub$1@news.unizar.es>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.rediris.es!news.unizar.es!not-for-mail
References: <a7d735$344$1@talia.mad.ttd.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f86c8,37b04e8f072bbd74
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2002-03-22 02:06:59 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.rediris.es!news.unizar.es!not-for-mail
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: Programar daemon. Lenguaje C.
Date: 22 Mar 2002 10:37:10 GMT
Organization: Rank
Lines: 130
Message-ID: <a7f1gm$hub$1@news.unizar.es>
References: <a7d735$344$1@talia.mad.ttd.net>
NNTP-Posting-Host: daphne.cps.unizar.es
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: news.unizar.es 1016793430 18379 155.210.152.20 (22 Mar 2002 10:37:10 GMT)
X-Complaints-To: usenet@news.unizar.es
NNTP-Posting-Date: 22 Mar 2002 10:37:10 GMT
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (IRIX/6.2 (IP22))