summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 53f8e80a6693262f53a37e60b1c9a6d22b5405ec (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
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
#include <signal.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <sys/types.h>

#include <libdaemon/dlog.h>
#include <libdaemon/dfork.h>
#include <libdaemon/dpid.h>

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

oop_source* event_source = NULL;
struct gengetopt_args_info args;
const char *appname = NULL;

uid_t target_uid = 0;
gid_t target_gid = 0;

#define DEFAULT_MSNTABLE "../conf/msntab"
#define IVAM_USER "ivam"
#define IVAM_GROUP "ivam"

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;
}

static void *oop_reload_cb(oop_source *source, int sig, void *user) {
    daemon_log(LOG_ERR, "Reloading MSN table.");
    msntab_flush();

    if (msntab_load(args.msntab_arg ? args.msntab_arg : DEFAULT_MSNTABLE) < 0) {
        daemon_log(LOG_ERR, "Ignoring all calls.");
        msntab_flush();
    }

    return OOP_CONTINUE;
}

static void *oop_dump_cb(oop_source *source, int sig, void *user) {
    msntab_dump();
    return OOP_CONTINUE;
}


static int change_uid_gid(void) {
    if (args.no_drop_root_flag)
        return 0;
        
    if (initgroups(IVAM_USER, target_gid) != 0) {
        daemon_log(LOG_ERR, "Failed to change group list: %s", strerror(errno));
        return -1;
    }
    
    if (setgid(target_gid) != 0) {
        daemon_log(LOG_ERR, "Failed to change GID: %s", strerror(errno));
        return -1;
    }
    
    if (setuid(target_uid) != 0) {
        daemon_log(LOG_ERR, "Failed to change UID: %s", strerror(errno));
        return -1;
    }

    daemon_log(LOG_INFO, "Successfully dropped root privileges.");
    
    return 0;
}

static int get_target_uid_gid(void) {
    struct passwd *pw;
    struct group * gr;

    if (args.no_drop_root_flag) {
        target_uid = getuid();
        target_gid = getgid();
        return 0;
    }

    if (!(pw = getpwnam(IVAM_USER))) {
        daemon_log(LOG_ERR, "Failed to find user '%s'.", IVAM_USER);
        return -1;
    }

    if (!(gr = getgrnam(IVAM_GROUP))) {
        daemon_log(LOG_ERR, "Failed to find group '%s'.", IVAM_GROUP);
        return -1;
    }

    target_uid = pw->pw_uid;
    target_gid = gr->gr_gid;
    
    daemon_log(LOG_INFO, "Found user '%s' (UID %lu) and group '%s' (GID %lu).", IVAM_USER, (unsigned long) target_uid, IVAM_GROUP, (unsigned long) target_gid);

    return 0;
}

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

    daemon_log(LOG_INFO, "Starting up.");

    if (get_target_uid_gid() < 0)
        goto finish;
    
    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 (modem_manager_init(args.channels_arg) < 0)
        goto finish;

    if (change_uid_gid() < 0)
        goto finish;

    if (child_process_init() < 0)
        goto finish;
    
    if (msntab_load(args.msntab_arg ? args.msntab_arg : DEFAULT_MSNTABLE) < 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);
    event_source->on_signal(event_source, SIGHUP, oop_reload_cb, NULL);
    event_source->on_signal(event_source, SIGUSR1, oop_dump_cb, NULL);
    signal(SIGPIPE, SIG_IGN);

    if (!args.no_daemon_flag) {
        daemon_retval_send(0);
        retval_sent = 1;
    }
    
    daemon_log(LOG_INFO, "Start up complete.");
    
    if (oop_sys_run(sys) == OOP_ERROR) {
        daemon_log(LOG_ERR, "oop_sys_new() returned OOP_ERROR");
        goto finish;
    }

    r = 0;
    
finish:
    
    daemon_log(LOG_INFO, "Shutting down.");

    if (!retval_sent && !args.no_daemon_flag)
        daemon_retval_send(1);
    
    event_source->cancel_signal(event_source, SIGTERM, oop_exit_cb, NULL);
    event_source->cancel_signal(event_source, SIGINT, oop_exit_cb, NULL);
    event_source->cancel_signal(event_source, SIGHUP, oop_reload_cb, NULL);
    event_source->cancel_signal(event_source, SIGUSR1, oop_dump_cb, NULL);

    msntab_flush();
    
    modem_manager_done();
    child_process_done();

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

    daemon_log(LOG_INFO, "Shut down complete.");
    
    return r;
}

int main(int argc, char*argv[]) {
    pid_t pid;
    int ret;

    appname = daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);

    cmdline_parser(argc, argv, &args);

    if (args.no_syslog_flag)
        daemon_log_use = DAEMON_LOG_STDERR;
    
    if (args.kill_flag) {
        int ret;
        
        if ((ret = daemon_pid_file_kill_wait(SIGINT, 5)) < 0)
            daemon_log(LOG_WARNING, "Failed to kill daemon.");

        return ret  < 0 ? 1 : 0;
    }

    if (args.check_flag) {
        if ((pid = daemon_pid_file_is_running()) >= 0)
            daemon_log(LOG_INFO, "Daemon running on PID %u.", pid);
        else
            daemon_log(LOG_INFO, "Daemon not running.");

        return 1;
    }

    if ((pid = daemon_pid_file_is_running()) >= 0) {
        daemon_log(LOG_ERR, "Daemon already running on PID %u.", pid);
        return 1;
    }

    if (!args.no_daemon_flag) {
        daemon_retval_init();

        if ((pid = daemon_fork()) < 0) {
            daemon_retval_done();
            return 1;
        } else if (pid) {
            int ret;
            
            if ((ret = daemon_retval_wait(20)) < 0) {
                daemon_log(LOG_ERR, "Failed to recieve return value from daemon process.");
                return 255;
            }

            if (ret)
                daemon_log(LOG_ERR, "Daemon returned %i as return value.", ret);
            return ret;
        }
    }

    if (daemon_pid_file_create() < 0) {
        daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno));
        
        if (!args.no_daemon_flag)
            daemon_retval_send(1);
        
        return 1;
    }

    ret = main_loop() < 0 ? 1 : 0;

    if (daemon_pid_file_remove() < 0)
        daemon_log(LOG_WARNING, "Failed to remove PID file (%s).", strerror(errno));

    return ret;
}