2004-08-24 01:39:22 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-08-24 01:39:22 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2004-08-24 01:39:22 +04:00
|
|
|
#include <sys/types.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-08-24 01:39:22 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-08-24 01:39:22 +04:00
|
|
|
#include <unistd.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-08-24 12:44:37 +04:00
|
|
|
#include <stdlib.h>
|
2004-08-24 01:39:22 +04:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/daemon_init.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2005-08-13 00:46:25 +04:00
|
|
|
|
2004-08-24 01:39:22 +04:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_daemon_init(char *working_dir)
|
2004-08-24 01:39:22 +04:00
|
|
|
{
|
2005-07-14 22:05:30 +04:00
|
|
|
#if defined(HAVE_FORK)
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifndef __WINDOWS__
|
2004-10-22 20:06:05 +04:00
|
|
|
/* it seems that there is an entirely different way to write daemons in
|
|
|
|
WINDOWS land. Firstly, they are called services and the way to
|
|
|
|
go about it is to get a service handle annd then call CreateService()
|
|
|
|
So, I am guessing that this piece of code is called only by UNIX versions */
|
|
|
|
|
2004-08-24 01:39:22 +04:00
|
|
|
pid_t pid;
|
2005-05-13 01:44:23 +04:00
|
|
|
int fd;
|
2004-08-24 01:39:22 +04:00
|
|
|
|
|
|
|
if ((pid = fork()) < 0) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-08-24 01:39:22 +04:00
|
|
|
} else if (pid != 0) {
|
2005-05-13 01:44:23 +04:00
|
|
|
exit(0); /* parent goes bye-bye */
|
2004-08-24 01:39:22 +04:00
|
|
|
}
|
2005-05-13 01:44:23 +04:00
|
|
|
|
2004-08-24 01:39:22 +04:00
|
|
|
/* child continues */
|
2005-07-14 22:05:30 +04:00
|
|
|
#if defined(HAVE_SETSID)
|
2004-08-24 01:39:22 +04:00
|
|
|
setsid(); /* become session leader */
|
2005-07-14 22:05:30 +04:00
|
|
|
#endif
|
2004-08-24 01:39:22 +04:00
|
|
|
|
|
|
|
if (NULL != working_dir) {
|
2005-05-13 01:44:23 +04:00
|
|
|
chdir(working_dir); /* change working directory */
|
2004-08-24 01:39:22 +04:00
|
|
|
}
|
|
|
|
|
2005-05-13 01:44:23 +04:00
|
|
|
/* connect input to /dev/null */
|
|
|
|
fd = open("/dev/null", O_RDONLY);
|
|
|
|
if(fd > STDIN_FILENO) {
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* connect outputs to /dev/null */
|
|
|
|
fd = open("/dev/null", O_RDWR|O_CREAT|O_TRUNC, 0666);
|
|
|
|
if (fd >= 0) {
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
/* just to be safe, make sure we aren't trying
|
|
|
|
* to close stdout or stderr! since we dup'd both
|
|
|
|
* of them to the same fd, we can't just close it
|
|
|
|
* since one of the two would still be open and
|
|
|
|
* someone could attempt to use it.
|
|
|
|
*/
|
|
|
|
if(fd != STDOUT_FILENO && fd != STDERR_FILENO) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
} else {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_FATAL;
|
2005-05-13 01:44:23 +04:00
|
|
|
}
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-10-22 20:06:05 +04:00
|
|
|
#else
|
|
|
|
printf ("This function has not been implemented in windows yet, file %s line %d\n", __FILE__, __LINE__);
|
|
|
|
abort();
|
|
|
|
#endif
|
2005-07-14 22:05:30 +04:00
|
|
|
|
|
|
|
#else /* HAVE_FORK */
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
#endif
|
2004-08-24 01:39:22 +04:00
|
|
|
}
|