Skip to content

Commit

Permalink
build: utils: detect fork(2) support / enable/disable daemon mode
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Dec 16, 2016
1 parent 5aa836a commit e4aa1f6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ if(FLB_VALGRIND OR FLB_HAVE_VALGRIND)
FLB_DEFINITION(FLB_HAVE_VALGRIND)
endif()

# fork(2) support
check_c_source_compiles("
#include <unistd.h>
int main() {
fork();
return 0;
}" FLB_HAVE_FORK)

if(FLB_HAVE_FORK)
FLB_DEFINITION(FLB_HAVE_FORK)
endif()

# mtrace support
if(FLB_MTRACE)
check_c_source_compiles("
Expand Down
5 changes: 5 additions & 0 deletions include/fluent-bit/flb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef FLB_UTILS_H
#define FLB_UTILS_H

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_config.h>

struct flb_split_entry {
Expand All @@ -32,7 +33,11 @@ void flb_utils_error(int err);
void flb_utils_error_c(const char *msg);
void flb_utils_warn_c(const char *msg);
void flb_message(int type, char *file, int line, const char *fmt, ...);

#ifdef FLB_HAVE_FORK
int flb_utils_set_daemon();
#endif

void flb_utils_print_setup(struct flb_config *config);

struct mk_list *flb_utils_split(char *line, int separator);
Expand Down
4 changes: 3 additions & 1 deletion src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <msgpack.h>
Expand Down Expand Up @@ -112,6 +112,7 @@ void flb_utils_warn_c(const char *msg)
ANSI_BOLD ANSI_YELLOW, ANSI_RESET, msg);
}

#ifdef FLB_HAVE_FORK
/* Run current process in background mode */
int flb_utils_set_daemon(struct flb_config *config)
{
Expand Down Expand Up @@ -145,6 +146,7 @@ int flb_utils_set_daemon(struct flb_config *config)

return 0;
}
#endif

void flb_utils_print_setup(struct flb_config *config)
{
Expand Down
8 changes: 8 additions & 0 deletions src/fluent-bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ static void flb_help(int rc, struct flb_config *config)
printf(" -B --buf_workers=N\tnumber of workers for buffering\n");
#endif
printf(" -c --config=FILE\tspecify an optional configuration file\n");
#ifdef FLB_HAVE_FORK
printf(" -d, --daemon\t\trun Fluent Bit in background mode\n");
#endif
printf(" -f, --flush=SECONDS\tflush timeout in seconds (default: %i)\n",
FLB_CONFIG_FLUSH_SECS);
printf(" -i, --input=INPUT\tset an input\n");
Expand Down Expand Up @@ -333,7 +335,9 @@ int main(int argc, char **argv)
{ "buf_path", required_argument, NULL, 'b' },
{ "buf_workers", required_argument, NULL, 'B' },
{ "config", required_argument, NULL, 'c' },
#ifdef FLB_HAVE_FORK
{ "daemon", no_argument , NULL, 'd' },
#endif
{ "flush", required_argument, NULL, 'f' },
{ "http", no_argument , NULL, 'H' },
{ "logfile", required_argument, NULL, 'l' },
Expand Down Expand Up @@ -387,9 +391,11 @@ int main(int argc, char **argv)
case 'c':
cfg_file = flb_strdup(optarg);
break;
#ifdef FLB_HAVE_FORK
case 'd':
config->daemon = FLB_TRUE;
break;
#endif
case 'e':
if (!flb_plugin_proxy_create(optarg, 0, config)) {
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -501,10 +507,12 @@ int main(int argc, char **argv)
flb_utils_print_setup(config);
}

#ifdef FLB_HAVE_FORK
/* Run in background/daemon mode */
if (config->daemon == FLB_TRUE) {
flb_utils_set_daemon(config);
}
#endif

flb_engine_start(config);
return 0;
Expand Down

0 comments on commit e4aa1f6

Please sign in to comment.