This source file includes following definitions.
- opal_daemon_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
47 }
48
49
50 #if defined(HAVE_SETSID)
51 setsid();
52 #endif
53
54 if (NULL != working_dir) {
55 chdir(working_dir);
56 }
57
58
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
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
74
75
76
77
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
89 return OPAL_ERR_NOT_SUPPORTED;
90 #endif
91 }