2019-10-27 08:41:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of Open5GS.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-02-02 11:34:37 +00:00
|
|
|
*/
|
|
|
|
|
2019-09-13 12:07:47 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2017-02-02 11:34:37 +00:00
|
|
|
|
2019-09-13 12:07:47 +00:00
|
|
|
#include "ogs-app.h"
|
2019-10-27 08:41:14 +00:00
|
|
|
#include "version.h"
|
2017-02-02 11:34:37 +00:00
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
static void show_version(void)
|
2017-02-02 11:34:37 +00:00
|
|
|
{
|
2020-11-27 02:44:37 +00:00
|
|
|
printf("Open5GS %s\n\n", OPEN5GS_VERSION);
|
2017-02-02 11:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 12:07:47 +00:00
|
|
|
static void show_help(const char *name)
|
2017-02-02 11:34:37 +00:00
|
|
|
{
|
2019-08-25 11:15:20 +00:00
|
|
|
printf("Usage: %s [options]\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -c filename : set configuration file\n"
|
|
|
|
" -l filename : set logging file\n"
|
|
|
|
" -e level : set global log-level (default:info)\n"
|
|
|
|
" -m domain : set log-domain (e.g. mme:sgw:gtp)\n"
|
|
|
|
" -d : print lots of debugging information\n"
|
|
|
|
" -t : print tracing information for developer\n"
|
|
|
|
" -D : start as a daemon\n"
|
|
|
|
" -v : show version number and exit\n"
|
|
|
|
" -h : show this message and exit\n"
|
|
|
|
"\n", name);
|
2017-02-02 11:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int check_signal(int signum)
|
|
|
|
{
|
2019-08-25 11:15:20 +00:00
|
|
|
switch (signum) {
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
ogs_info("%s received",
|
|
|
|
signum == SIGTERM ? "SIGTERM" : "SIGINT");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
case SIGHUP:
|
|
|
|
ogs_info("SIGHUP received");
|
2019-09-13 12:07:47 +00:00
|
|
|
ogs_log_cycle();
|
|
|
|
|
2019-08-25 11:15:20 +00:00
|
|
|
break;
|
2021-07-12 10:52:28 +00:00
|
|
|
case SIGWINCH:
|
|
|
|
ogs_info("Signal-NUM[%d] received (%s)",
|
|
|
|
signum, ogs_signal_description_get(signum));
|
|
|
|
break;
|
2022-01-03 23:29:18 +00:00
|
|
|
case SIGUSR1:
|
2022-01-23 04:49:03 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
|
|
|
|
0, "", "core",
|
|
|
|
(unsigned long)talloc_total_size(__ogs_talloc_core),
|
|
|
|
(unsigned long)talloc_total_blocks(__ogs_talloc_core),
|
|
|
|
(int)talloc_reference_count(__ogs_talloc_core),
|
|
|
|
__ogs_talloc_core);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGUSR2:
|
2022-01-03 23:29:18 +00:00
|
|
|
talloc_report_full(__ogs_talloc_core, stderr);
|
|
|
|
break;
|
|
|
|
|
2019-08-25 11:15:20 +00:00
|
|
|
default:
|
|
|
|
ogs_error("Signal-NUM[%d] received (%s)",
|
|
|
|
signum, ogs_signal_description_get(signum));
|
|
|
|
break;
|
2017-02-02 11:34:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
static void terminate(void)
|
2017-08-03 01:18:45 +00:00
|
|
|
{
|
|
|
|
app_terminate();
|
2019-09-13 12:07:47 +00:00
|
|
|
|
|
|
|
ogs_app_terminate();
|
2017-08-03 01:18:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
int main(int argc, const char *const argv[])
|
2017-02-02 11:34:37 +00:00
|
|
|
{
|
|
|
|
/**************************************************************************
|
|
|
|
* Starting up process.
|
|
|
|
*
|
|
|
|
* Keep the order of starting-up
|
|
|
|
*/
|
2019-08-25 11:15:20 +00:00
|
|
|
int rv, i, opt;
|
|
|
|
ogs_getopt_t options;
|
|
|
|
struct {
|
|
|
|
char *config_file;
|
|
|
|
char *log_file;
|
|
|
|
char *log_level;
|
|
|
|
char *domain_mask;
|
|
|
|
|
|
|
|
bool enable_debug;
|
|
|
|
bool enable_trace;
|
|
|
|
} optarg;
|
2019-10-27 08:41:14 +00:00
|
|
|
const char *argv_out[argc];
|
2019-08-25 11:15:20 +00:00
|
|
|
|
|
|
|
memset(&optarg, 0, sizeof(optarg));
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
ogs_getopt_init(&options, (char**)argv);
|
2019-08-25 11:15:20 +00:00
|
|
|
while ((opt = ogs_getopt(&options, "vhDc:l:e:m:dt")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'v':
|
2019-04-27 14:54:30 +00:00
|
|
|
show_version();
|
2019-08-25 11:15:20 +00:00
|
|
|
return OGS_OK;
|
|
|
|
case 'h':
|
2019-04-27 14:54:30 +00:00
|
|
|
show_help(argv[0]);
|
2019-08-25 11:15:20 +00:00
|
|
|
return OGS_OK;
|
|
|
|
case 'D':
|
|
|
|
#if !defined(_WIN32)
|
2019-04-27 14:54:30 +00:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork();
|
2017-02-27 05:55:35 +00:00
|
|
|
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_assert(pid >= 0);
|
2017-02-27 05:55:35 +00:00
|
|
|
|
2019-04-27 14:54:30 +00:00
|
|
|
if (pid != 0)
|
|
|
|
{
|
|
|
|
/* Parent */
|
|
|
|
return EXIT_SUCCESS;
|
2017-02-27 05:55:35 +00:00
|
|
|
}
|
2019-04-27 14:54:30 +00:00
|
|
|
/* Child */
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
umask(027);
|
|
|
|
}
|
2019-08-25 11:15:20 +00:00
|
|
|
#else
|
|
|
|
printf("%s: Not Support in WINDOWS", argv[0]);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
optarg.config_file = options.optarg;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
optarg.log_file = options.optarg;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
optarg.log_level = options.optarg;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
optarg.domain_mask = options.optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
optarg.enable_debug = true;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
optarg.enable_trace = true;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
|
2019-04-27 14:54:30 +00:00
|
|
|
show_help(argv[0]);
|
2019-08-25 11:15:20 +00:00
|
|
|
return OGS_ERROR;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: should not be reached\n", OGS_FUNC);
|
|
|
|
return OGS_ERROR;
|
2017-02-02 11:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
if (optarg.enable_debug) optarg.log_level = (char*)"debug";
|
|
|
|
if (optarg.enable_trace) optarg.log_level = (char*)"trace";
|
2019-08-25 11:15:20 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
argv_out[i++] = argv[0];
|
|
|
|
|
|
|
|
if (optarg.config_file) {
|
|
|
|
argv_out[i++] = "-c";
|
|
|
|
argv_out[i++] = optarg.config_file;
|
|
|
|
}
|
|
|
|
if (optarg.log_file) {
|
|
|
|
argv_out[i++] = "-l";
|
|
|
|
argv_out[i++] = optarg.log_file;
|
|
|
|
}
|
|
|
|
if (optarg.log_level) {
|
|
|
|
argv_out[i++] = "-e";
|
|
|
|
argv_out[i++] = optarg.log_level;
|
|
|
|
}
|
|
|
|
if (optarg.domain_mask) {
|
|
|
|
argv_out[i++] = "-m";
|
|
|
|
argv_out[i++] = optarg.domain_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
argv_out[i] = NULL;
|
|
|
|
|
2019-09-13 12:07:47 +00:00
|
|
|
ogs_signal_init();
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_setup_signal_thread();
|
2019-09-13 12:07:47 +00:00
|
|
|
|
2020-11-27 02:44:37 +00:00
|
|
|
rv = ogs_app_initialize(OPEN5GS_VERSION, DEFAULT_CONFIG_FILENAME, argv_out);
|
2019-10-27 08:41:14 +00:00
|
|
|
if (rv != OGS_OK) {
|
|
|
|
if (rv == OGS_RETRY)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
ogs_fatal("Open5GS initialization failed. Aborted");
|
|
|
|
return OGS_ERROR;
|
|
|
|
}
|
2019-09-13 12:07:47 +00:00
|
|
|
|
2019-08-25 11:15:20 +00:00
|
|
|
rv = app_initialize(argv_out);
|
|
|
|
if (rv != OGS_OK) {
|
2019-04-27 14:54:30 +00:00
|
|
|
if (rv == OGS_RETRY)
|
2017-10-31 12:29:39 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
ogs_fatal("Open5GS initialization failed. Aborted");
|
2019-08-25 11:15:20 +00:00
|
|
|
return OGS_ERROR;
|
2017-02-02 11:34:37 +00:00
|
|
|
}
|
2019-09-13 12:07:47 +00:00
|
|
|
|
2019-08-25 11:15:20 +00:00
|
|
|
atexit(terminate);
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_signal_thread(check_signal);
|
2017-02-02 11:34:37 +00:00
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
ogs_info("Open5GS daemon terminating...");
|
2017-02-02 11:34:37 +00:00
|
|
|
|
2019-08-25 11:15:20 +00:00
|
|
|
return OGS_OK;
|
2017-02-02 11:34:37 +00:00
|
|
|
}
|
2019-08-25 11:15:20 +00:00
|
|
|
|