Coverage Report

Created: 2023-09-25 07:12

/src/open5gs/lib/app/ogs-init.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
3
 *
4
 * This file is part of Open5GS.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
#include "ogs-app.h"
21
22
int __ogs_app_domain;
23
24
int ogs_app_initialize(
25
        const char *version, const char *default_config,
26
        const char *const argv[])
27
0
{
28
0
    int rv, opt;
29
0
    ogs_getopt_t options;
30
0
    struct {
31
0
        char *config_file;
32
0
        char *log_file;
33
0
        char *log_level;
34
0
        char *domain_mask;
35
0
    } optarg;
36
37
0
    ogs_core_initialize();
38
0
    ogs_app_setup_log();
39
40
0
    ogs_app_context_init();
41
0
    ogs_app()->version = version;
42
43
    /**************************************************************************
44
     * Stage 1 : Command Line Options
45
     */
46
0
    memset(&optarg, 0, sizeof(optarg));
47
48
0
    ogs_getopt_init(&options, (char**)argv);
49
0
    while ((opt = ogs_getopt(&options, "c:l:e:m:")) != -1) {
50
0
        switch (opt) {
51
0
        case 'c':
52
0
            optarg.config_file = options.optarg;
53
0
            break;
54
0
        case 'l':
55
0
            optarg.log_file = options.optarg;
56
0
            break;
57
0
        case 'e':
58
0
            optarg.log_level = options.optarg;
59
0
            break;
60
0
        case 'm':
61
0
            optarg.domain_mask = options.optarg;
62
0
            break;
63
0
        case '?':
64
0
        default:
65
0
            ogs_assert_if_reached();
66
0
            return OGS_ERROR;
67
0
        }
68
0
    }
69
70
    /**************************************************************************
71
     * Stage 2 : Load Configuration File
72
     */
73
0
    if (optarg.config_file)
74
0
        ogs_app()->file = optarg.config_file;
75
0
    else
76
0
        ogs_app()->file = default_config;
77
78
0
    rv = ogs_app_config_read();
79
0
    if (rv != OGS_OK) return rv;
80
81
0
    rv = ogs_app_context_parse_config();
82
0
    if (rv != OGS_OK) return rv;
83
84
    /**************************************************************************
85
     * Stage 3 : Initialize Default Memory Pool
86
     */
87
0
    ogs_pkbuf_default_create(&ogs_app()->pool.defconfig);
88
89
    /**************************************************************************
90
     * Stage 4 : Setup LOG Module
91
     */
92
0
    if (optarg.log_file)
93
0
        ogs_app()->logger.file = optarg.log_file;
94
95
0
    if (ogs_app()->logger.file) {
96
0
        if (ogs_log_add_file(ogs_app()->logger.file) == NULL) {
97
0
            ogs_fatal("cannot open log file : %s", 
98
0
                    ogs_app()->logger.file);
99
0
            return OGS_ERROR;
100
0
        }
101
0
    }
102
103
0
    if (optarg.domain_mask)
104
0
        ogs_app()->logger.domain = optarg.domain_mask;
105
106
0
    if (optarg.log_level) 
107
0
        ogs_app()->logger.level = optarg.log_level;
108
109
0
    rv = ogs_log_config_domain(
110
0
            ogs_app()->logger.domain, ogs_app()->logger.level);
111
0
    if (rv != OGS_OK) return rv;
112
113
    /**************************************************************************
114
     * Stage 5 : Setup Database Module
115
     */
116
0
    if (ogs_env_get("DB_URI"))
117
0
        ogs_app()->db_uri = ogs_env_get("DB_URI");
118
119
    /**************************************************************************
120
     * Stage 6 : Print Banner
121
     */
122
0
    if (ogs_app()->version) {
123
0
        ogs_log_print(OGS_LOG_INFO,
124
0
                "Open5GS daemon %s\n\n", ogs_app()->version);
125
126
0
        ogs_info("Configuration: '%s'", ogs_app()->file);
127
128
0
        if (ogs_app()->logger.file) {
129
0
            ogs_info("File Logging: '%s'", ogs_app()->logger.file);
130
131
0
            if (ogs_app()->logger.level)
132
0
                ogs_info("LOG-LEVEL: '%s'", ogs_app()->logger.level);
133
134
0
            if (ogs_app()->logger.domain)
135
0
                ogs_info("LOG-DOMAIN: '%s'", ogs_app()->logger.domain);
136
0
        }
137
0
    }
138
139
    /**************************************************************************
140
     * Stage 7 : Queue, Timer and Poll
141
     */
142
0
    ogs_app()->queue = ogs_queue_create(ogs_app()->pool.event);
143
0
    ogs_assert(ogs_app()->queue);
144
0
    ogs_app()->timer_mgr = ogs_timer_mgr_create(ogs_app()->pool.timer);
145
0
    ogs_assert(ogs_app()->timer_mgr);
146
0
    ogs_app()->pollset = ogs_pollset_create(ogs_app()->pool.socket);
147
0
    ogs_assert(ogs_app()->pollset);
148
149
0
    return rv;
150
0
}
151
152
void ogs_app_terminate(void)
153
0
{
154
0
    ogs_app_context_final();
155
156
0
    ogs_pkbuf_default_destroy();
157
158
0
    ogs_core_terminate();
159
0
}
160
161
int ogs_app_config_read(void)
162
0
{
163
0
    FILE *file;
164
0
    yaml_parser_t parser;
165
0
    yaml_document_t *document = NULL;
166
167
0
    ogs_assert(ogs_app()->file);
168
169
0
    file = fopen(ogs_app()->file, "rb");
170
0
    if (!file) {
171
0
        ogs_fatal("cannot open file `%s`", ogs_app()->file);
172
0
        return OGS_ERROR;
173
0
    }
174
175
0
    ogs_assert(yaml_parser_initialize(&parser));
176
0
    yaml_parser_set_input_file(&parser, file);
177
178
0
    document = calloc(1, sizeof(yaml_document_t));
179
0
    if (!yaml_parser_load(&parser, document)) {
180
0
        ogs_fatal("Failed to parse configuration file '%s'", ogs_app()->file);
181
0
        switch (parser.error) {
182
0
        case YAML_MEMORY_ERROR:
183
0
            ogs_error("Memory error: Not enough memory for parsing");
184
0
            break;
185
0
        case YAML_READER_ERROR:
186
0
            if (parser.problem_value != -1)
187
0
                ogs_error("Reader error - %s: #%X at %zd", parser.problem,
188
0
                    parser.problem_value, parser.problem_offset);
189
0
            else
190
0
                ogs_error("Reader error - %s at %zd", parser.problem,
191
0
                    parser.problem_offset);
192
0
            break;
193
0
        case YAML_SCANNER_ERROR:
194
0
            if (parser.context)
195
0
                ogs_error("Scanner error - %s at line %zu, column %zu"
196
0
                        "%s at line %zu, column %zu", parser.context,
197
0
                        parser.context_mark.line+1,
198
0
                        parser.context_mark.column+1,
199
0
                        parser.problem, parser.problem_mark.line+1,
200
0
                        parser.problem_mark.column+1);
201
0
            else
202
0
                ogs_error("Scanner error - %s at line %zu, column %zu",
203
0
                        parser.problem, parser.problem_mark.line+1,
204
0
                        parser.problem_mark.column+1);
205
0
            break;
206
0
        case YAML_PARSER_ERROR:
207
0
            if (parser.context)
208
0
                ogs_error("Parser error - %s at line %zu, column %zu"
209
0
                        "%s at line %zu, column %zu", parser.context,
210
0
                        parser.context_mark.line+1,
211
0
                        parser.context_mark.column+1,
212
0
                        parser.problem, parser.problem_mark.line+1,
213
0
                        parser.problem_mark.column+1);
214
0
            else
215
0
                ogs_error("Parser error - %s at line %zu, column %zu",
216
0
                        parser.problem, parser.problem_mark.line+1,
217
0
                        parser.problem_mark.column+1);
218
0
            break;
219
0
        default:
220
            /* Couldn't happen. */
221
0
            ogs_assert_if_reached();
222
0
            break;
223
0
        }
224
225
0
        free(document);
226
0
        yaml_parser_delete(&parser);
227
0
        ogs_assert(!fclose(file));
228
0
        return OGS_ERROR;
229
0
    }
230
231
0
    ogs_app()->document = document;
232
233
0
    yaml_parser_delete(&parser);
234
0
    ogs_assert(!fclose(file));
235
236
0
    return OGS_OK;
237
0
}
238
239
void ogs_app_setup_log(void)
240
0
{
241
0
    ogs_log_install_domain(&__ogs_app_domain, "app", ogs_core()->log.level);
242
0
}