Skip to content

Commit da5bf73

Browse files
committed
Core: add an option to run as a standalone daemon process
1 parent 10713cb commit da5bf73

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/core/main.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ print_help()
8181
{
8282
std::cout
8383
<< "IPFIX Collector daemon\n"
84-
<< "Usage: ipfixcol [-c FILE] [-d PATH] [-vVh]\n"
84+
<< "Usage: ipfixcol [-c FILE] [-p PATH] [-e DIR] [-P FILE] [-vVhd]\n"
8585
<< " -c FILE Path to the startup configuration file\n"
8686
<< " (default: " << IPX_DEFAULT_STARTUP_CONFIG << ")\n"
8787
<< " -p PATH Add path to a directory with plugins or to a file\n"
8888
<< " (default: " << IPX_DEFAULT_PLUGINS_DIR << ")\n"
8989
<< " -e DIR Path to a directory with definitions of IPFIX Information Elements\n"
9090
<< " (default: " << fds_api_cfg_dir() << ")\n"
91+
<< " -P FILE Path to a PID file (without this option, no PID file is created)\n"
92+
<< " -d Run as a standalone daemon process\n"
9193
<< " -h Show this help message and exit\n"
9294
<< " -V Show version information and exit\n"
93-
<< " -v Be verbose (in addition, show warning messages)\n"
94-
<< " -vv Be more verbose (like previous + info messages)\n"
95-
<< " -vvv Be even more verbose (like previous + debug messages)\n"
96-
<< " -P FILE Path to a PID file (without this option, no PID file is created)\n";
95+
<< " -v Increase verbosity level (by default, show only error messages)\n"
96+
<< " (can be used up to 3 times: adds warnings/infos/debug messages)\n";
9797
}
9898

9999
/**
@@ -165,12 +165,13 @@ int main(int argc, char *argv[])
165165
const char *cfg_startup = nullptr;
166166
const char *cfg_iedir = nullptr;
167167
const char *pid_file = nullptr;
168+
bool daemon_en = false;
168169
ipx_configurator conf;
169170

170171
// Parse configuration
171172
int opt;
172173
opterr = 0; // Disable default error messages
173-
while ((opt = getopt(argc, argv, "c:vVhp:e:P:")) != -1) {
174+
while ((opt = getopt(argc, argv, "c:vVhdp:e:P:")) != -1) {
174175
switch (opt) {
175176
case 'c': // Configuration file
176177
cfg_startup = optarg;
@@ -184,6 +185,9 @@ int main(int argc, char *argv[])
184185
case 'h': // Help
185186
print_help();
186187
return EXIT_SUCCESS;
188+
case 'd':
189+
daemon_en = true;
190+
break;
187191
case 'p': // Plugin search path
188192
conf.finder.path_add(std::string(optarg));
189193
break;
@@ -209,6 +213,17 @@ int main(int argc, char *argv[])
209213
cfg_iedir = fds_api_cfg_dir();
210214
}
211215

216+
if (daemon_en) {
217+
// Run as a standalone daemon process
218+
ipx_verb_syslog(true);
219+
if (daemon(1, 0) == -1) {
220+
const char *err_str;
221+
ipx_strerror(errno, err_str);
222+
IPX_ERROR(module, "Failed to start as a standalone daemon: %s", err_str);
223+
return EXIT_FAILURE;
224+
}
225+
}
226+
212227
// Always use the default directory for looking for plugins, but with the lowest priority
213228
conf.finder.path_add(IPX_DEFAULT_PLUGINS_DIR);
214229
conf.iemgr_set_dir(cfg_iedir);

0 commit comments

Comments
 (0)