1 |
#include "eef_library.h" |
2 |
#include "eef_xacml_parser.h" |
3 |
#include "pdl.h" |
4 |
#include "plugin_manager.h" |
5 |
|
6 |
#include <unistd.h> |
7 |
|
8 |
#ifdef BSD |
9 |
#include <sys/ioctl.h> |
10 |
#endif |
11 |
|
12 |
static const char* _modules_path; /*! Holds the global path to plug-in modules */ |
13 |
void (*EEF_log)(int, const char*, ...); |
14 |
|
15 |
void fork_and_exit_parent_with_logging(void){ |
16 |
switch(fork()){ |
17 |
case 0: |
18 |
break; |
19 |
case -1: |
20 |
EEF_log(LOG_ERR, "Error forking: %s\n", strerror(errno)); |
21 |
exit(1); |
22 |
default: |
23 |
exit(0); |
24 |
} |
25 |
} |
26 |
|
27 |
unsigned int EEF_getVersion(void){ |
28 |
return SVN_REVISION; |
29 |
} |
30 |
/** |
31 |
* Tries to daemonize the process |
32 |
*/ |
33 |
EES_RC EEF_daemonize(void){ |
34 |
#ifdef BSD |
35 |
pid_t pid; |
36 |
#endif |
37 |
int fd = 0; |
38 |
|
39 |
/* If launched by init there's no need to detach |
40 |
* Note: this test is unreliable due to an unavoidable race |
41 |
* condition if the process is orphaned. |
42 |
*/ |
43 |
if(getpid() == 1){ |
44 |
goto out; |
45 |
} |
46 |
|
47 |
#ifdef SIGTTOU |
48 |
signal(SIGTTOU, SIG_IGN); |
49 |
#endif |
50 |
|
51 |
#ifdef SIGTTIN |
52 |
signal(SIGTTIN, SIG_IGN); |
53 |
#endif |
54 |
|
55 |
#ifdef SIGTSTP |
56 |
signal(SIGTSTP, SIG_IGN); |
57 |
#endif |
58 |
|
59 |
/* allow parent shell to continue - ensure process is not processgroup leader*/ |
60 |
fork_and_exit_parent_with_logging(); |
61 |
|
62 |
|
63 |
#ifdef BSD |
64 |
pid = getpid(); |
65 |
if(setpgid(0, pid) != -1){ |
66 |
if((fd = open("/dev/tty", O_RDWR)) >=0){ |
67 |
ioctl(fd, TIOCNOTTY, 0); |
68 |
close(fd); |
69 |
} |
70 |
} |
71 |
#else |
72 |
setpgrp(); |
73 |
signal(SIGHUP, SIG_IGN); |
74 |
fork_and_exit_parent_with_logging(); |
75 |
#endif |
76 |
|
77 |
out: |
78 |
|
79 |
/* change dir to '/' */ |
80 |
if(chdir("/") != 0){ |
81 |
return EES_FAILURE; |
82 |
} |
83 |
|
84 |
/* redirect output of stdin to /dev/null */ |
85 |
fd = open("/dev/null", O_RDONLY); |
86 |
if(fd != 0){ |
87 |
dup2(fd, 0); |
88 |
close(fd); |
89 |
} |
90 |
|
91 |
/* redirect output of stdout to /dev/null */ |
92 |
fd = open("/dev/null", O_WRONLY); |
93 |
if(fd != 1){ |
94 |
dup2(fd, 1); |
95 |
close(fd); |
96 |
} |
97 |
|
98 |
/* redirect output of stderr to /dev/null */ |
99 |
fd = open("/dev/null", O_WRONLY); |
100 |
if(fd != 2){ |
101 |
dup2(fd, 2); |
102 |
close(fd); |
103 |
} |
104 |
|
105 |
/* set file creation mask to '750' */ |
106 |
umask(027); |
107 |
|
108 |
return EES_SUCCESS; |
109 |
|
110 |
} |
111 |
|
112 |
/** |
113 |
* Try to initialize the AOS, enable logging, initialize the plug-in manager and try to parse the config file and ascertain the module path |
114 |
*/ |
115 |
EES_RC EEF_init(char *config_file, void (*log_func)(int, const char*, ...)){ |
116 |
if(log_func != NULL){ |
117 |
EEF_log = log_func; |
118 |
} else { |
119 |
EEF_log = syslog; |
120 |
} |
121 |
EEF_log(LOG_NOTICE, "EEF initializing version %i...", EEF_getVersion()); |
122 |
|
123 |
if (AOS_Init() == EES_SUCCESS){ |
124 |
if(start_plugin_manager() == EES_SUCCESS){ |
125 |
return start_pdl_parser(config_file, 0, NULL); |
126 |
} |
127 |
} |
128 |
return EES_FAILURE; |
129 |
} |
130 |
|
131 |
/** |
132 |
* Terminates the parser, stops the plug-in manager, terminates the AOS and closes the log. |
133 |
*/ |
134 |
EES_RC EEF_term(){ |
135 |
|
136 |
EEF_log(LOG_INFO, "Cleaning up..."); |
137 |
if((pdl_term() == EES_SUCCESS) && |
138 |
(stop_plugin_manager() == EES_SUCCESS) && |
139 |
(AOS_Term() == EES_SUCCESS)){ |
140 |
return EES_SUCCESS; |
141 |
} |
142 |
EEF_log(LOG_ERR, "EEF: Errors detected\n"); |
143 |
EEF_log(LOG_NOTICE, "EEF terminating..."); |
144 |
return EES_FAILURE; |
145 |
} |
146 |
|
147 |
/** |
148 |
* instructs the AOS to go into threaded mode, trying to store new data in thread-local storage |
149 |
*/ |
150 |
EES_RC EEF_startThreading(void){ |
151 |
aos_start_threading(); |
152 |
plugin_manager_start_threading(); |
153 |
return EES_SUCCESS; |
154 |
} |
155 |
|
156 |
/** |
157 |
* Sets the global modules path to load plug-ins from |
158 |
*/ |
159 |
void EEF_set_modules_path(const char* path){ |
160 |
_modules_path = path; |
161 |
} |
162 |
|
163 |
/** |
164 |
* Returns the global modules path to load plug-ins from |
165 |
* This can be either the _modules_path as set by the set_modules_path() function or the MODULE_DIR path macro generated by the configure script. |
166 |
*/ |
167 |
const char* EEF_get_modules_path(void){ |
168 |
if(_modules_path != NULL){ |
169 |
if(strlen(_modules_path)!=0){ |
170 |
return _modules_path; |
171 |
} |
172 |
} |
173 |
return MODULE_DIR; |
174 |
} |
175 |
|
176 |
char* EEF_getRunningPluginName(void){ |
177 |
return get_running_plugin_name(); |
178 |
} |