-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
163 lines (140 loc) · 3.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @file main.c
*/
/* Core library */
#define TRACE_MODULE _main_
#include "core_general.h"
#include "core_debug.h"
#include "core_signal.h"
/* Server */
#include "common/application.h"
#include "app_init.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdate-time"
static char *compile_time = __DATE__ " " __TIME__;
#pragma GCC diagnostic pop
static void show_version()
{
printf("NextEPC daemon v%s - %s\n",
PACKAGE_VERSION, compile_time);
}
static void show_help(const char *name)
{
show_version();
printf("\n"
"Usage: %s [arguments]\n"
"\n"
"Arguments:\n"
" -v Show version\n"
" -h Show help\n"
" -d Start as daemon\n"
" -f Set configuration file name\n"
" -l log_file Log file path to be logged to\n"
" -p pid_file PID file path\n"
"\n", name);
}
static int check_signal(int signum)
{
switch (signum)
{
case SIGTERM:
case SIGINT:
{
d_info("%s received",
signum == SIGTERM ? "SIGTERM" : "SIGINT");
return 1;
}
case SIGHUP:
{
d_info("SIGHUP received");
app_logger_restart();
break;
}
case SIGUSR1:
{
break;
}
default:
{
d_error("Unknown signal number = %d\n", signum);
break;
}
}
return 0;
}
void terminate()
{
app_terminate();
core_terminate();
}
int main(int argc, char *argv[])
{
/**************************************************************************
* Starting up process.
*
* Keep the order of starting-up
*/
status_t rv;
char *config_path = NULL;
char *log_path = NULL;
char *pid_path = NULL;
while (1)
{
int opt = getopt (argc, argv, "vhdf:l:p:");
if (opt == -1)
break;
switch (opt)
{
case 'v':
show_version();
return EXIT_SUCCESS;
case 'h':
show_help(argv[0]);
return EXIT_SUCCESS;
case 'd':
{
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);
break;
}
case 'f':
config_path = optarg;
break;
case 'l':
log_path = optarg;
break;
case 'p':
pid_path = optarg;
break;
default:
show_help(argv[0]);
return EXIT_FAILURE;
}
}
show_version();
d_print("\n");
atexit(terminate);
core_initialize();
rv = app_initialize(config_path, log_path, pid_path);
if (rv != CORE_OK)
{
if (rv == CORE_EAGAIN)
return EXIT_SUCCESS;
d_fatal("NextEPC initialization failed. Aborted");
return EXIT_FAILURE;
}
d_print("\n\n");
d_info("NextEPC daemon start");
signal_thread(check_signal);
d_info("NextEPC daemon terminating...");
return EXIT_SUCCESS;
}