File: epc.c

package info (click to toggle)
nextepc 0.3.10+nods-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 23,304 kB
  • sloc: ansic: 164,982; yacc: 2,767; python: 2,567; makefile: 2,408; lex: 1,732; xml: 795; php: 486; sh: 349; sql: 337; perl: 75; awk: 35; cpp: 6
file content (287 lines) | stat: -rw-r--r-- 8,214 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#define TRACE_MODULE _epc_main

#include "core_general.h"
#include "core_debug.h"
#include "core_semaphore.h"

#include "common/context.h"
#include "common/application.h"

#include "app_init.h"

static semaphore_id pcrf_sem1 = 0;
static semaphore_id pcrf_sem2 = 0;

static semaphore_id pgw_sem1 = 0;
static semaphore_id pgw_sem2 = 0;

static semaphore_id sgw_sem1 = 0;
static semaphore_id sgw_sem2 = 0;

static semaphore_id hss_sem1 = 0;
static semaphore_id hss_sem2 = 0;

status_t app_initialize(
        const char *config_path, const char *log_path, const char *pid_path)
{
    pid_t pid;
    status_t rv;
    int app = 0;

    rv = app_log_pid(pid_path, "epc");
    if (rv != CORE_OK) return rv;

    rv = app_will_initialize(config_path, log_path);
    if (rv != CORE_OK) return rv;

    app = context_self()->logger.trace.app;
    if (app)
    {
        d_trace_level(&_epc_main, app);
    }


    /************************* PCRF Process **********************/
    semaphore_create(&pcrf_sem1, 0); /* copied to PCRF/PGW/SGW/HSS process */
    semaphore_create(&pcrf_sem2, 0); /* copied to PCRF/PGW/SGW/HSS process */

    if (context_self()->parameter.no_pcrf == 0)
    {
        pid = fork();
        d_assert(pid >= 0, _exit(EXIT_FAILURE), "fork() failed");

        if (pid == 0)
        {
            d_trace(1, "PCRF try to initialize\n");
            rv = pcrf_initialize();
            d_assert(rv == CORE_OK,, "Failed to initialize PCRF");
            d_trace(1, "PCRF initialize...done\n");

            if (pcrf_sem1) semaphore_post(pcrf_sem1);
            if (pcrf_sem2) semaphore_wait(pcrf_sem2);

            if (rv == CORE_OK)
            {
                d_trace(1, "PCRF try to terminate\n");
                pcrf_terminate();
                d_trace(1, "PCRF terminate...done\n");
            }

            if (pcrf_sem1) semaphore_post(pcrf_sem1);

            /* allocated from parent process */
            if (pcrf_sem1) semaphore_delete(pcrf_sem1);
            if (pcrf_sem2) semaphore_delete(pcrf_sem2);

            app_did_terminate();

            core_terminate();

            _exit(EXIT_SUCCESS);
        }

        if (pcrf_sem1) semaphore_wait(pcrf_sem1);
    }


    /************************* PGW Process **********************/

    semaphore_create(&pgw_sem1, 0); /* copied to PGW/SGW/HSS process */
    semaphore_create(&pgw_sem2, 0); /* copied to PGW/SGW/HSS process */

    if (context_self()->parameter.no_pgw == 0)
    {
        pid = fork();
        d_assert(pid >= 0, _exit(EXIT_FAILURE), "fork() failed");

        if (pid == 0)
        {
            /* allocated from parent process */
            if (pcrf_sem1) semaphore_delete(pcrf_sem1);
            if (pcrf_sem2) semaphore_delete(pcrf_sem2);

            d_trace(1, "PGW try to initialize\n");
            rv = pgw_initialize();
            d_assert(rv == CORE_OK,, "Failed to initialize PGW");
            d_trace(1, "PGW initialize...done\n");

            if (pgw_sem1) semaphore_post(pgw_sem1);
            if (pgw_sem2) semaphore_wait(pgw_sem2);

            if (rv == CORE_OK)
            {
                d_trace(1, "PGW try to terminate\n");
                pgw_terminate();
                d_trace(1, "PGW terminate...done\n");
            }

            if (pgw_sem1) semaphore_post(pgw_sem1);

            /* allocated from parent process */
            if (pgw_sem1) semaphore_delete(pgw_sem1);
            if (pgw_sem2) semaphore_delete(pgw_sem2);

            app_did_terminate();

            core_terminate();

            _exit(EXIT_SUCCESS);
        }

        if (pgw_sem1) semaphore_wait(pgw_sem1);
    }


    /************************* SGW Process **********************/

    semaphore_create(&sgw_sem1, 0); /* copied to SGW/HSS process */
    semaphore_create(&sgw_sem2, 0); /* copied to SGW/HSS process */

    if (context_self()->parameter.no_sgw == 0)
    {
        pid = fork();
        d_assert(pid >= 0, _exit(EXIT_FAILURE), "fork() failed");

        if (pid == 0)
        {
            /* allocated from parent process */
            if (pcrf_sem1) semaphore_delete(pcrf_sem1);
            if (pcrf_sem2) semaphore_delete(pcrf_sem2);
            if (pgw_sem1) semaphore_delete(pgw_sem1);
            if (pgw_sem2) semaphore_delete(pgw_sem2);

            d_trace(1, "SGW try to initialize\n");
            rv = sgw_initialize();
            d_assert(rv == CORE_OK,, "Failed to initialize SGW");
            d_trace(1, "SGW initialize...done\n");

            if (sgw_sem1) semaphore_post(sgw_sem1);
            if (sgw_sem2) semaphore_wait(sgw_sem2);

            if (rv == CORE_OK)
            {
                d_trace(1, "SGW try to terminate\n");
                sgw_terminate();
                d_trace(1, "SGW terminate...done\n");
            }

            if (sgw_sem1) semaphore_post(sgw_sem1);

            /* allocated from parent process */
            if (sgw_sem1) semaphore_delete(sgw_sem1);
            if (sgw_sem2) semaphore_delete(sgw_sem2);

            app_did_terminate();

            core_terminate();

            _exit(EXIT_SUCCESS);
        }

        if (sgw_sem1) semaphore_wait(sgw_sem1);
    }


    /************************* HSS Process **********************/

    semaphore_create(&hss_sem1, 0); /* copied to HSS process */
    semaphore_create(&hss_sem2, 0); /* copied to HSS process */

    if (context_self()->parameter.no_hss == 0)
    {
        pid = fork();
        d_assert(pid >= 0, _exit(EXIT_FAILURE), "fork() failed");

        if (pid == 0)
        {
            /* allocated from parent process */
            if (pcrf_sem1) semaphore_delete(pcrf_sem1);
            if (pcrf_sem2) semaphore_delete(pcrf_sem2);
            if (pgw_sem1) semaphore_delete(pgw_sem1);
            if (pgw_sem2) semaphore_delete(pgw_sem2);
            if (sgw_sem1) semaphore_delete(sgw_sem1);
            if (sgw_sem2) semaphore_delete(sgw_sem2);

            d_trace(1, "HSS try to initialize\n");
            rv = hss_initialize();
            d_assert(rv == CORE_OK,, "Failed to initialize HSS");
            d_trace(1, "HSS initialize...done\n");

            if (hss_sem1) semaphore_post(hss_sem1);
            if (hss_sem2) semaphore_wait(hss_sem2);

            if (rv == CORE_OK)
            {
                d_trace(1, "HSS try to terminate\n");
                hss_terminate();
                d_trace(1, "HSS terminate...done\n");
            }

            if (hss_sem1) semaphore_post(hss_sem1);

            if (hss_sem1) semaphore_delete(hss_sem1);
            if (hss_sem2) semaphore_delete(hss_sem2);

            app_did_terminate();

            core_terminate();

            _exit(EXIT_SUCCESS);
        }

        if (hss_sem1) semaphore_wait(hss_sem1);
    }

    rv = app_did_initialize();
    if (rv != CORE_OK) return rv;

    d_trace(1, "MME try to initialize\n");
    rv = mme_initialize();
    d_assert(rv == CORE_OK, return rv, "Failed to initialize MME");
    d_trace(1, "MME initialize...done\n");

    return CORE_OK;;
}

void app_terminate(void)
{
    app_will_terminate();

    d_trace(1, "MME try to terminate\n");
    mme_terminate();
    d_trace(1, "MME terminate...done\n");

    if (context_self()->parameter.no_hss == 0)
    {
        if (hss_sem2) semaphore_post(hss_sem2);
        if (hss_sem1) semaphore_wait(hss_sem1);
    }
    if (hss_sem1) semaphore_delete(hss_sem1);
    if (hss_sem2) semaphore_delete(hss_sem2);

    if (context_self()->parameter.no_sgw == 0)
    {
        if (sgw_sem2) semaphore_post(sgw_sem2);
        if (sgw_sem1) semaphore_wait(sgw_sem1);
    }
    if (sgw_sem1) semaphore_delete(sgw_sem1);
    if (sgw_sem2) semaphore_delete(sgw_sem2);

    if (context_self()->parameter.no_pgw == 0)
    {
        if (pgw_sem2) semaphore_post(pgw_sem2);
        if (pgw_sem1) semaphore_wait(pgw_sem1);
    }
    if (pgw_sem1) semaphore_delete(pgw_sem1);
    if (pgw_sem2) semaphore_delete(pgw_sem2);

    if (context_self()->parameter.no_pcrf == 0)
    {
        if (pcrf_sem2) semaphore_post(pcrf_sem2);
        if (pcrf_sem1) semaphore_wait(pcrf_sem1);
    }
    if (pcrf_sem1) semaphore_delete(pcrf_sem1);
    if (pcrf_sem2) semaphore_delete(pcrf_sem2);

    app_did_terminate();
}