root/opal/util/daemon_init.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_daemon_init

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2015      Research Organization for Information Science
  13  *                         and Technology (RIST). All rights reserved.
  14  * $COPYRIGHT$
  15  *
  16  * Additional copyrights may follow
  17  *
  18  * $HEADER$
  19  */
  20 
  21 
  22 #include "opal_config.h"
  23 #ifdef HAVE_SYS_TYPES_H
  24 #include <sys/types.h>
  25 #endif
  26 #include <sys/stat.h>
  27 #include <fcntl.h>
  28 #ifdef HAVE_UNISTD_H
  29 #include <unistd.h>
  30 #endif
  31 #include <stdlib.h>
  32 
  33 #include "opal/util/daemon_init.h"
  34 #include "opal/constants.h"
  35 
  36 
  37 int opal_daemon_init(char *working_dir)
  38 {
  39 #if defined(HAVE_FORK)
  40     pid_t pid;
  41     int fd;
  42 
  43     if ((pid = fork()) < 0) {
  44         return OPAL_ERROR;
  45     } else if (pid != 0) {
  46         exit(0);   /* parent goes bye-bye */
  47     }
  48 
  49     /* child continues */
  50 #if defined(HAVE_SETSID)
  51     setsid();  /* become session leader */
  52 #endif
  53 
  54     if (NULL != working_dir) {
  55         chdir(working_dir);  /* change working directory */
  56     }
  57 
  58     /* connect input to /dev/null */
  59     fd = open("/dev/null", O_RDONLY);
  60     if (0 > fd) {
  61         return OPAL_ERR_FATAL;
  62     }
  63     dup2(fd, STDIN_FILENO);
  64     if(fd != STDIN_FILENO) {
  65         close(fd);
  66     }
  67 
  68     /* connect outputs to /dev/null */
  69     fd = open("/dev/null", O_RDWR|O_CREAT|O_TRUNC, 0666);
  70     if (fd >= 0) {
  71         dup2(fd, STDOUT_FILENO);
  72         dup2(fd, STDERR_FILENO);
  73         /* just to be safe, make sure we aren't trying
  74          * to close stdout or stderr! since we dup'd both
  75          * of them to the same fd, we can't just close it
  76          * since one of the two would still be open and
  77          * someone could attempt to use it.
  78          */
  79         if(fd != STDOUT_FILENO && fd != STDERR_FILENO) {
  80            close(fd);
  81         }
  82     } else {
  83         return OPAL_ERR_FATAL;
  84     }
  85 
  86     return OPAL_SUCCESS;
  87 
  88 #else /* HAVE_FORK */
  89     return OPAL_ERR_NOT_SUPPORTED;
  90 #endif
  91 }

/* [<][>][^][v][top][bottom][index][help] */