summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 9710be9d35a91b56e351e4ac4adbe341750d6dd0 (plain)
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
#include <signal.h>
#include <assert.h>
#include <libdaemon/dlog.h>

#include "main.h"
#include "exec.h"
#include "modemman.h"

#define CHANNELS 1

oop_source* event_source = NULL;

static void *oop_exit_cb(oop_source *source, int sig, void *user) {
    daemon_log(LOG_ERR, "Recieved signal %s", sig == SIGINT ? "SIGINT" : (sig == SIGTERM ? "SIGTERM" : "UNKNWON"));
    return OOP_HALT;
}

int main_loop(void) {
    int r = -1;
    oop_source_sys *sys = NULL;

    if (!(sys = oop_sys_new())) {
        daemon_log(LOG_ERR, "Failed to create system source");
        goto finish;
    }

    event_source = oop_sys_source(sys);
    assert(event_source);

    if (child_process_init() < 0)
        goto finish;
    
    if (modem_manager_init(CHANNELS) < 0)
        goto finish;

    event_source->on_signal(event_source, SIGINT, oop_exit_cb, NULL);
    event_source->on_signal(event_source, SIGTERM, oop_exit_cb, NULL);
    signal(SIGPIPE, SIG_IGN);
    
    if (oop_sys_run(sys) == OOP_ERROR) {
        daemon_log(LOG_ERR, "oop_sys_new() returned OOP_ERROR");
        goto finish;
    }

    r = 0;

finish:
    event_source->cancel_signal(event_source, SIGTERM, oop_exit_cb, NULL);
    event_source->cancel_signal(event_source, SIGINT, oop_exit_cb, NULL);
    
    modem_manager_done();
    child_process_done();

    if (sys) {
        event_source = NULL;
        oop_sys_delete(sys);
    }
    
    return r;
}

int main(int argc, char*argv[]) {
    return main_loop() < 0 ? 1 : 0;
}