open5gs/main.c

163 lines
3.5 KiB
C
Raw Normal View History

2017-02-02 11:34:37 +00:00
/**
* @file main.c
*/
/* Core library */
#define TRACE_MODULE _main_
#include "core_general.h"
2017-02-02 11:34:37 +00:00
#include "core_debug.h"
#include "core_signal.h"
/* Server */
2017-07-11 14:38:52 +00:00
#include "app.h"
2017-02-02 11:34:37 +00:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdate-time"
2017-03-22 07:59:34 +00:00
static char *compile_time = __DATE__ " " __TIME__;
#pragma GCC diagnostic pop
2017-02-02 11:34:37 +00:00
static void show_version()
{
2017-07-10 05:33:46 +00:00
printf("NextEPC daemon v%s - %s\n", PACKAGE_VERSION, compile_time);
2017-02-02 11:34:37 +00:00
}
2017-07-11 05:16:15 +00:00
static void show_help(const char *name)
2017-02-02 11:34:37 +00:00
{
show_version();
printf("\n"
2017-07-11 05:16:15 +00:00
"Usage: %s [arguments]\n"
2017-02-02 11:34:37 +00:00
"\n"
"Arguments:\n"
" -v Show version\n"
" -h Show help\n"
" -d Start as daemon\n"
" -f Set configuration file name\n"
2017-10-29 11:48:17 +00:00
" -l log_file Log file path to be logged to\n"
" -p pid_file PID file path\n"
2017-07-11 05:16:15 +00:00
"\n", name);
2017-02-02 11:34:37 +00:00
}
static int check_signal(int signum)
{
switch (signum)
{
case SIGTERM:
case SIGINT:
{
d_info("%s received",
signum == SIGTERM ? "SIGTERM" : "SIGINT");
return 1;
}
2017-10-24 06:55:28 +00:00
case SIGHUP:
{
d_info("SIGHUP received");
app_logger_restart();
2017-10-24 06:55:28 +00:00
break;
}
2017-03-26 06:57:46 +00:00
case SIGUSR1:
{
break;
}
2017-02-02 11:34:37 +00:00
default:
{
2017-02-27 05:55:35 +00:00
d_error("Unknown signal number = %d\n", signum);
2017-02-02 11:34:37 +00:00
break;
}
}
return 0;
}
2017-08-03 01:18:45 +00:00
void terminate()
{
app_terminate();
core_terminate();
}
2017-02-02 11:34:37 +00:00
int main(int argc, char *argv[])
{
/**************************************************************************
* Starting up process.
*
* Keep the order of starting-up
*/
2017-10-31 12:29:39 +00:00
status_t rv;
2017-02-27 05:55:35 +00:00
char *config_path = NULL;
char *log_path = NULL;
2017-10-29 11:48:17 +00:00
char *pid_path = NULL;
2017-02-02 11:34:37 +00:00
while (1)
{
2017-10-29 11:48:17 +00:00
int opt = getopt (argc, argv, "vhdf:l:p:");
2017-02-02 11:34:37 +00:00
if (opt == -1)
break;
switch (opt)
{
case 'v':
show_version();
return EXIT_SUCCESS;
case 'h':
2017-07-11 05:16:15 +00:00
show_help(argv[0]);
2017-02-02 11:34:37 +00:00
return EXIT_SUCCESS;
case 'd':
2017-02-27 05:55:35 +00:00
{
pid_t pid;
pid = fork();
d_assert(pid >= 0, return EXIT_FAILURE, "fork() failed");
if (pid != 0)
{
/* Parent */
return EXIT_SUCCESS;
}
/* Child */
setsid();
umask(027);
2017-02-02 11:34:37 +00:00
break;
2017-02-27 05:55:35 +00:00
}
2017-02-02 11:34:37 +00:00
case 'f':
config_path = optarg;
break;
case 'l':
log_path = optarg;
break;
2017-10-29 11:48:17 +00:00
case 'p':
pid_path = optarg;
break;
2017-02-02 11:34:37 +00:00
default:
2017-07-11 05:16:15 +00:00
show_help(argv[0]);
2017-02-02 11:34:37 +00:00
return EXIT_FAILURE;
}
}
2017-07-14 05:09:31 +00:00
show_version();
d_print("\n");
2017-08-03 01:18:45 +00:00
atexit(terminate);
core_initialize();
2017-10-29 11:48:17 +00:00
app_log_pid(pid_path);
2017-10-31 12:29:39 +00:00
rv = app_initialize(config_path, log_path);
if (rv != CORE_OK)
2017-02-02 11:34:37 +00:00
{
2017-10-31 12:29:39 +00:00
if (rv == CORE_EAGAIN)
return EXIT_SUCCESS;
2017-07-10 05:33:46 +00:00
d_fatal("NextEPC initialization failed. Aborted");
2017-02-02 11:34:37 +00:00
return EXIT_FAILURE;
}
2017-02-27 06:09:41 +00:00
2017-07-14 05:09:31 +00:00
d_print("\n\n");
2017-07-10 05:33:46 +00:00
d_info("NextEPC daemon start");
2017-02-02 11:34:37 +00:00
signal_thread(check_signal);
2017-07-10 05:33:46 +00:00
d_info("NextEPC daemon terminating...");
2017-02-02 11:34:37 +00:00
return EXIT_SUCCESS;
}