<commit_before>/* -*- c-basic-offset:4; c-indentation-style:"k&r"; indent-tabs-mode:nil -*- */

/**
 * @file daemon.c
 * Pathfinder daemon and TCP/IP support.
 *
 * Copyright Notice:
 * -----------------
 *
 *  The contents of this file are subject to the MonetDB Public
 *  License Version 1.0 (the "License"); you may not use this file
 *  except in compliance with the License. You may obtain a copy of
 *  the License at http://monetdb.cwi.nl/Legal/MonetDBLicense-1.0.html
 *
 *  Software distributed under the License is distributed on an "AS
 *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 *  implied. See the License for the specific language governing
 *  rights and limitations under the License.
 *
 *  The Original Code is the ``Pathfinder'' system. The Initial
 *  Developer of the Original Code is the Database & Information
 *  Systems Group at the University of Konstanz, Germany. Portions
 *  created by U Konstanz are Copyright (C) 2000-2004 University
 *  of Konstanz. All Rights Reserved.
 *
 *  Contributors:
 *          <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * $Id$
 */

#include "pathfinder.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>

#include "daemon.h"

#include "oops.h"

#ifdef HAVE_SOCKLEN_T
#define SOCKLEN socklen_t
#else
#define SOCKLEN int
#endif

/**
 * Try to clean up if we are terminated via kill(2).
 */
void
terminate (int sig) 
{
    PFoops (OOPS_FATAL,
            "terminated (received signal %d)", sig);
}

/** 
 * Turn ourselves into a daemon. 
 */
/* HACK to pacify picky Intel compiler on Linux64 */
#if (defined(__INTEL_COMPILER) && (SIZEOF_VOID_P > SIZEOF_INT))  
#undef  SIG_IGN /*((__sighandler_t)1 )*/
#define SIG_IGN   ((__sighandler_t)1L)
#endif
void
daemonize (void)
{
    int child;
    int fd;

    /*
     * if we were started by init (pid 1) from the /etc/inittab file
     * there is no need to detach;
     * this test is unreliable due to an unavoidable ambiguity
     * if the process is started by some other process and orphaned
     * (i.e, if the parent process terminates before we are started)
     */
    if (getppid () == 1)
        goto in_background;

    /*
     * if we were not started in the background, fork and
     * let the parent exit; this also guarantees the first child
     * is not a process group leader.
     */	
    if ((child = fork ()) < 0)
        PFoops (OOPS_FATAL, "cannot fork first child");
    else if (child > 0)
        /* parent: return to caller (probably the shell) */
        exit (0); 

    /*
     * disassociate from controlling terminal and process group;
     * ensure the process can't reacquire a new controlling terminal
     */
    if (setpgid (0,0) == -1)
        PFoops (OOPS_FATAL, "cannot diassociate from controlling tty");

    /* be immune from pgrp leader death */
    signal (SIGHUP, SIG_IGN); 

    if ((child = fork ()) < 0)
        PFoops (OOPS_FATAL, "cannot fork second child");
    else if (child > 0)
        /* death of the first child */
        exit (0);

in_background:
    /* close all file descs but stderr (log output) */
    for (fd = 0; fd < FOPEN_MAX; fd++)
        if (fd != fileno (stderr))
            close (fd);<commit_msg>On IRIX 6.5, the GNU compiler complained about a `comparison between signed and unsigned' in daemon.c:121. According to the IRIX 6.5 man page (on the SGI website), fileno() should return an `int', which would match the declaration of `fd'. The error message is thus a bit strange.<commit_after>/* -*- c-basic-offset:4; c-indentation-style:"k&r"; indent-tabs-mode:nil -*- */

/**
 * @file daemon.c
 * Pathfinder daemon and TCP/IP support.
 *
 * Copyright Notice:
 * -----------------
 *
 *  The contents of this file are subject to the MonetDB Public
 *  License Version 1.0 (the "License"); you may not use this file
 *  except in compliance with the License. You may obtain a copy of
 *  the License at http://monetdb.cwi.nl/Legal/MonetDBLicense-1.0.html
 *
 *  Software distributed under the License is distributed on an "AS
 *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 *  implied. See the License for the specific language governing
 *  rights and limitations under the License.
 *
 *  The Original Code is the ``Pathfinder'' system. The Initial
 *  Developer of the Original Code is the Database & Information
 *  Systems Group at the University of Konstanz, Germany. Portions
 *  created by U Konstanz are Copyright (C) 2000-2004 University
 *  of Konstanz. All Rights Reserved.
 *
 *  Contributors:
 *          <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * $Id$
 */

#include "pathfinder.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>

#include "daemon.h"

#include "oops.h"

#ifdef HAVE_SOCKLEN_T
#define SOCKLEN socklen_t
#else
#define SOCKLEN int
#endif

/**
 * Try to clean up if we are terminated via kill(2).
 */
void
terminate (int sig) 
{
    PFoops (OOPS_FATAL,
            "terminated (received signal %d)", sig);
}

/** 
 * Turn ourselves into a daemon. 
 */
/* HACK to pacify picky Intel compiler on Linux64 */
#if (defined(__INTEL_COMPILER) && (SIZEOF_VOID_P > SIZEOF_INT))  
#undef  SIG_IGN /*((__sighandler_t)1 )*/
#define SIG_IGN   ((__sighandler_t)1L)
#endif
void
daemonize (void)
{
    int child;
    int fd;

    /*
     * if we were started by init (pid 1) from the /etc/inittab file
     * there is no need to detach;
     * this test is unreliable due to an unavoidable ambiguity
     * if the process is started by some other process and orphaned
     * (i.e, if the parent process terminates before we are started)
     */
    if (getppid () == 1)
        goto in_background;

    /*
     * if we were not started in the background, fork and
     * let the parent exit; this also guarantees the first child
     * is not a process group leader.
     */	
    if ((child = fork ()) < 0)
        PFoops (OOPS_FATAL, "cannot fork first child");
    else if (child > 0)
        /* parent: return to caller (probably the shell) */
        exit (0); 

    /*
     * disassociate from controlling terminal and process group;
     * ensure the process can't reacquire a new controlling terminal
     */
    if (setpgid (0,0) == -1)
        PFoops (OOPS_FATAL, "cannot diassociate from controlling tty");

    /* be immune from pgrp leader death */
    signal (SIGHUP, SIG_IGN); 

    if ((child = fork ()) < 0)
        PFoops (OOPS_FATAL, "cannot fork second child");
    else if (child > 0)
        /* death of the first child */
        exit (0);

in_background:
    /* close all file descs but stderr (log output) */
    for (fd = 0; fd < FOPEN_MAX; fd++)
        if (fd != (int) fileno (stderr))
            close (fd);
{'fraction_non_alphanumeric': 0.11761371687685986, 'fraction_numerical': 0.009494119314156156, 'mean_word_length': 2.7622601279317696, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1539994', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2249, 'n_words': 858}