Skip to content

Latest commit

 

History

History
331 lines (309 loc) · 9.84 KB

main.c

File metadata and controls

331 lines (309 loc) · 9.84 KB
 
Feb 17, 2014
Feb 17, 2014
1
/*
Feb 24, 2015
Feb 24, 2015
2
3
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
Feb 17, 2014
Feb 17, 2014
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
*/
#include <syslog.h>
#include <glib-unix.h>
#include "mms_engine.h"
#include "mms_ofono_log.h"
#include "mms_lib_log.h"
#include "mms_lib_util.h"
May 12, 2014
May 12, 2014
23
#include "mms_settings.h"
Feb 17, 2014
Feb 17, 2014
24
Apr 17, 2014
Apr 17, 2014
25
26
27
#define RET_OK (0)
#define RET_ERR (1)
Feb 17, 2014
Feb 17, 2014
28
29
30
/* Options configurable from the command line */
typedef struct mms_app_options {
GBusType bus_type;
May 12, 2014
May 12, 2014
31
int flags;
Feb 18, 2014
Feb 18, 2014
32
char* dir;
Apr 30, 2014
Apr 30, 2014
33
char* user_agent;
May 30, 2014
May 30, 2014
34
char* uaprof;
Feb 17, 2014
Feb 17, 2014
35
MMSConfig config;
May 12, 2014
May 12, 2014
36
MMSSettingsSimData settings;
Feb 17, 2014
Feb 17, 2014
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
} MMSAppOptions;
/* All known log modules */
static MMSLogModule* mms_app_log_modules[] = {
&mms_log_default,
#define MMS_LIB_LOG_MODULE(m) &(m),
MMS_LIB_LOG_MODULES(MMS_LIB_LOG_MODULE)
MMS_OFONO_LOG_MODULES(MMS_LIB_LOG_MODULE)
#undef MMS_LIB_LOG_MODULE
};
/* Signal handler */
static
gboolean
mms_app_signal(
gpointer arg)
{
Feb 24, 2015
Feb 24, 2015
54
MMSEngine* engine = arg;
Feb 17, 2014
Feb 17, 2014
55
MMS_INFO("Caught signal, shutting down...");
Feb 24, 2015
Feb 24, 2015
56
57
mms_engine_stop(engine);
return TRUE;
Feb 17, 2014
Feb 17, 2014
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
}
/* D-Bus event handlers */
static
void
mms_app_bus_acquired(
GDBusConnection* bus,
const gchar* name,
gpointer arg)
{
MMSEngine* engine = arg;
GError* error = NULL;
MMS_DEBUG("Bus acquired, starting...");
if (!mms_engine_register(engine, bus, &error)) {
MMS_ERR("Could not start: %s", MMS_ERRMSG(error));
g_error_free(error);
mms_engine_stop(engine);
}
}
static
void
mms_app_name_acquired(
GDBusConnection* bus,
const gchar* name,
gpointer arg)
{
MMS_DEBUG("Acquired service name '%s'", name);
}
static
void
mms_app_name_lost(
GDBusConnection* bus,
const gchar* name,
gpointer arg)
{
MMSEngine* engine = arg;
MMS_ERR("'%s' service already running or access denied", name);
mms_engine_stop(engine);
}
/* Option parsing callbacks */
static
gboolean
mms_app_option_loglevel(
const gchar* name,
const gchar* value,
gpointer data,
GError** error)
{
return mms_log_parse_option(value, mms_app_log_modules,
G_N_ELEMENTS(mms_app_log_modules), error);
}
static
gboolean
mms_app_option_logtype(
const gchar* name,
const gchar* value,
gpointer data,
GError** error)
{
if (mms_log_set_type(value, MMS_APP_LOG_PREFIX)) {
return TRUE;
} else {
if (error) {
*error = g_error_new(G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid log type \'%s\'", value);
}
return FALSE;
}
}
static
gboolean
mms_app_option_verbose(
const gchar* name,
const gchar* value,
gpointer data,
GError** error)
{
mms_log_default.level = MMS_LOGLEVEL_VERBOSE;
return TRUE;
}
Apr 17, 2014
Apr 17, 2014
144
145
146
147
148
/**
* Parses command line and sets up application options. Returns TRUE if
* we should go ahead and run the application, FALSE if we should exit
* immediately.
*/
Feb 17, 2014
Feb 17, 2014
149
150
151
152
153
static
gboolean
mms_app_parse_options(
MMSAppOptions* opt,
int argc,
Apr 17, 2014
Apr 17, 2014
154
155
char* argv[],
int* result)
Feb 17, 2014
Feb 17, 2014
156
157
158
159
{
gboolean ok;
GError* error = NULL;
gboolean session_bus = FALSE;
Jul 16, 2014
Jul 16, 2014
160
#ifdef MMS_VERSION_STRING
Apr 17, 2014
Apr 17, 2014
161
162
gboolean print_version = FALSE;
#endif
May 15, 2014
May 15, 2014
163
gboolean log_modules = FALSE;
May 12, 2014
May 12, 2014
164
165
166
gboolean keep_running = FALSE;
gint size_limit_kb = -1;
gdouble megapixels = -1;
Feb 17, 2014
Feb 17, 2014
167
168
169
170
171
172
173
174
175
char* root_dir_help = g_strdup_printf(
"Root directory for MMS files [%s]",
opt->config.root_dir);
char* retry_secs_help = g_strdup_printf(
"Retry period in seconds [%d]",
opt->config.retry_secs);
char* idle_secs_help = g_strdup_printf(
"Inactivity timeout in seconds [%d]",
opt->config.idle_secs);
May 15, 2014
May 15, 2014
176
char* description = mms_log_description(NULL, 0);
Feb 17, 2014
Feb 17, 2014
177
178
179
180
181
182
GOptionContext* options;
GOptionEntry entries[] = {
{ "session", 0, 0, G_OPTION_ARG_NONE, &session_bus,
"Use session bus (default is system)", NULL },
{ "root-dir", 'd', 0, G_OPTION_ARG_FILENAME,
Feb 18, 2014
Feb 18, 2014
183
&opt->dir, root_dir_help, "DIR" },
Feb 17, 2014
Feb 17, 2014
184
185
186
187
{ "retry-secs", 'r', 0, G_OPTION_ARG_INT,
&opt->config.retry_secs, retry_secs_help, "SEC" },
{ "idle-secs", 'i', 0, G_OPTION_ARG_INT,
&opt->config.idle_secs, idle_secs_help, "SEC" },
Feb 27, 2014
Feb 27, 2014
188
{ "size-limit", 's', 0, G_OPTION_ARG_INT,
May 12, 2014
May 12, 2014
189
&size_limit_kb, "Maximum size for outgoing messages", "KB" },
Mar 2, 2014
Mar 2, 2014
190
{ "pix-limit", 'p', 0, G_OPTION_ARG_DOUBLE,
May 12, 2014
May 12, 2014
191
&megapixels, "Maximum pixel count for outgoing images", "MPIX" },
Apr 30, 2014
Apr 30, 2014
192
{ "user-agent", 'u', 0, G_OPTION_ARG_STRING,
May 30, 2014
May 30, 2014
193
194
195
&opt->user_agent, "The value of the User-Agent header", "STRING" },
{ "x-wap-profile", 'x', 0, G_OPTION_ARG_STRING,
&opt->uaprof, "User agent profile", "URL" },
May 12, 2014
May 12, 2014
196
{ "keep-running", 'k', 0, G_OPTION_ARG_NONE, &keep_running,
Feb 17, 2014
Feb 17, 2014
197
198
199
200
201
202
203
204
205
206
207
"Keep running after everything is done", NULL },
{ "keep-temp-files", 't', 0, G_OPTION_ARG_NONE,
&opt->config.keep_temp_files,
"Don't delete temporary files", NULL },
{ "attic", 'a', 0, G_OPTION_ARG_NONE,
&opt->config.attic_enabled,
"Store unrecognized push messages in the attic", NULL },
{ "verbose", 'v', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
mms_app_option_verbose, "Be verbose (equivalent to -l=verbose)",
NULL },
{ "log-output", 'o', 0, G_OPTION_ARG_CALLBACK, mms_app_option_logtype,
Mar 2, 2014
Mar 2, 2014
208
"Log output (stdout|syslog|glib) [stdout]", "TYPE" },
Feb 17, 2014
Feb 17, 2014
209
210
{ "log-level", 'l', 0, G_OPTION_ARG_CALLBACK, mms_app_option_loglevel,
"Set log level (repeatable)", "[MODULE:]LEVEL" },
May 15, 2014
May 15, 2014
211
212
{ "log-modules", 0, 0, G_OPTION_ARG_NONE, &log_modules,
"List available log modules", NULL },
Jul 16, 2014
Jul 16, 2014
213
#ifdef MMS_VERSION_STRING
Apr 17, 2014
Apr 17, 2014
214
215
216
{ "version", 0, 0, G_OPTION_ARG_NONE, &print_version,
"Print program version and exit", NULL },
#endif
Feb 17, 2014
Feb 17, 2014
217
218
219
220
221
222
223
224
225
226
227
228
229
{ NULL }
};
options = g_option_context_new("- part of Jolla MMS system");
g_option_context_add_main_entries(options, entries, NULL);
g_option_context_set_description(options, description);
ok = g_option_context_parse(options, &argc, &argv, &error);
g_option_context_free(options);
g_free(root_dir_help);
g_free(retry_secs_help);
g_free(idle_secs_help);
g_free(description);
May 15, 2014
May 15, 2014
230
231
232
233
234
235
236
237
238
239
240
241
if (!ok) {
fprintf(stderr, "%s\n", MMS_ERRMSG(error));
g_error_free(error);
*result = RET_ERR;
return FALSE;
} else if (log_modules) {
unsigned int i;
for (i=0; i<G_N_ELEMENTS(mms_app_log_modules); i++) {
printf("%s\n", mms_app_log_modules[i]->name);
}
*result = RET_OK;
return FALSE;
Jul 16, 2014
Jul 16, 2014
242
#ifdef MMS_VERSION_STRING
May 15, 2014
May 15, 2014
243
} else if (print_version) {
Jul 16, 2014
Jul 16, 2014
244
printf("MMS engine %s\n", MMS_VERSION_STRING);
Apr 17, 2014
Apr 17, 2014
245
246
247
*result = RET_OK;
return FALSE;
#endif
May 15, 2014
May 15, 2014
248
} else {
Jul 16, 2014
Jul 16, 2014
249
250
#ifdef MMS_VERSION_STRING
MMS_INFO("Version %s starting", MMS_VERSION_STRING);
May 12, 2014
May 12, 2014
251
#else
Feb 17, 2014
Feb 17, 2014
252
MMS_INFO("Starting");
May 12, 2014
May 12, 2014
253
#endif
Mar 2, 2014
Mar 2, 2014
254
if (size_limit_kb >= 0) {
May 12, 2014
May 12, 2014
255
256
opt->settings.size_limit = size_limit_kb * 1024;
opt->flags |= MMS_ENGINE_FLAG_OVERRIDE_SIZE_LIMIT;
Mar 2, 2014
Mar 2, 2014
257
258
}
if (megapixels >= 0) {
May 12, 2014
May 12, 2014
259
260
261
262
263
264
opt->settings.max_pixels = (int)(megapixels*1000)*1000;
opt->flags |= MMS_ENGINE_FLAG_OVERRIDE_MAX_PIXELS;
}
if (opt->user_agent) {
opt->settings.user_agent = opt->user_agent;
opt->flags |= MMS_ENGINE_FLAG_OVERRIDE_USER_AGENT;
Mar 2, 2014
Mar 2, 2014
265
}
May 30, 2014
May 30, 2014
266
267
268
269
if (opt->uaprof) {
opt->settings.uaprof = opt->uaprof;
opt->flags |= MMS_ENGINE_FLAG_OVERRIDE_UAPROF;
}
Feb 18, 2014
Feb 18, 2014
270
if (opt->dir) opt->config.root_dir = opt->dir;
May 12, 2014
May 12, 2014
271
if (keep_running) opt->flags |= MMS_ENGINE_FLAG_KEEP_RUNNING;
Feb 17, 2014
Feb 17, 2014
272
273
274
275
276
277
278
if (session_bus) {
MMS_DEBUG("Attaching to session bus");
opt->bus_type = G_BUS_TYPE_SESSION;
} else {
MMS_DEBUG("Attaching to system bus");
opt->bus_type = G_BUS_TYPE_SYSTEM;
}
Apr 17, 2014
Apr 17, 2014
279
*result = RET_OK;
Feb 17, 2014
Feb 17, 2014
280
281
282
283
284
285
return TRUE;
}
}
int main(int argc, char* argv[])
{
Apr 17, 2014
Apr 17, 2014
286
int result = RET_ERR;
Feb 17, 2014
Feb 17, 2014
287
MMSAppOptions opt = {0};
Feb 27, 2014
Feb 27, 2014
288
mms_lib_init(argv[0]);
Oct 27, 2015
Oct 27, 2015
289
gofono_log.name = "mms-ofono";
Feb 17, 2014
Feb 17, 2014
290
291
mms_log_default.name = MMS_APP_LOG_PREFIX;
mms_lib_default_config(&opt.config);
May 12, 2014
May 12, 2014
292
mms_settings_sim_data_default(&opt.settings);
Apr 17, 2014
Apr 17, 2014
293
if (mms_app_parse_options(&opt, argc, argv, &result)) {
Feb 17, 2014
Feb 17, 2014
294
295
296
MMSEngine* engine;
/* Create engine instance. This may fail */
May 12, 2014
May 12, 2014
297
engine = mms_engine_new(&opt.config, &opt.settings, opt.flags,
Feb 17, 2014
Feb 17, 2014
298
299
300
301
302
mms_app_log_modules, G_N_ELEMENTS(mms_app_log_modules));
if (engine) {
/* Setup main loop */
GMainLoop* loop = g_main_loop_new(NULL, FALSE);
Feb 24, 2015
Feb 24, 2015
303
304
guint sigtrm = g_unix_signal_add(SIGTERM, mms_app_signal, engine);
guint sigint = g_unix_signal_add(SIGINT, mms_app_signal, engine);
Feb 17, 2014
Feb 17, 2014
305
306
/* Acquire name, don't allow replacement */
Feb 24, 2015
Feb 24, 2015
307
guint name_id = g_bus_own_name(opt.bus_type, MMS_ENGINE_SERVICE,
Feb 17, 2014
Feb 17, 2014
308
309
310
311
312
313
314
G_BUS_NAME_OWNER_FLAGS_REPLACE, mms_app_bus_acquired,
mms_app_name_acquired, mms_app_name_lost, engine, NULL);
/* Run the main loop */
mms_engine_run(engine, loop);
/* Cleanup and exit */
Feb 24, 2015
Feb 24, 2015
315
316
if (sigtrm) g_source_remove(sigtrm);
if (sigint) g_source_remove(sigint);
Feb 17, 2014
Feb 17, 2014
317
318
319
320
321
322
323
324
325
g_bus_unown_name(name_id);
g_main_loop_unref(loop);
mms_engine_unref(engine);
}
MMS_INFO("Exiting");
}
if (mms_log_func == mms_log_syslog) {
closelog();
}
Feb 18, 2014
Feb 18, 2014
326
g_free(opt.dir);
Apr 30, 2014
Apr 30, 2014
327
g_free(opt.user_agent);
May 30, 2014
May 30, 2014
328
g_free(opt.uaprof);
Feb 27, 2014
Feb 27, 2014
329
mms_lib_deinit();
Feb 17, 2014
Feb 17, 2014
330
331
return result;
}