summaryrefslogtreecommitdiffstats
path: root/src/daemon/main.c
blob: 94e59fdda2c3cb56a774648e88f165e846eab7fe (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
/*
 * Usurpation – server daemon main logic
 *
 * Copyright (C) 2019 Gediminas Jakutis
 * Copyright (C) 2019 Paulius Ratkevičius
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; version 2.1
 * of the License.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "settings.h"
#include "net.h"
#include "purple.h"

static struct _state {
	int nd;
} _progstate;

void cleanup(void);

/* the logic is a placeholder right now */
int main(int argc, char **argv)
{
	extern const char * const version;
	struct timespec t = {3600, 0}; /* one hour */

	printf("Usurpation daemon version %s starting\n", version);

	atexit(cleanup);
	settings_init();
	_progstate.nd = net_init(setting_port());
	if (purple_init() && setting_verbose()) {
		fprintf(stderr, "libpurple initialization failed\n");
	}

	/* by default and if running by as a system service, the init system
	 * needs to keep control of the process and thus only detach if
	 * requested when ran manually by a user.
	 */
	if (setting_detach()) {
		daemon(0, 0);
	}

	while(!(nanosleep(&t, NULL))) {
		/* noop */

	}

	return 0;
}

void cleanup(void)
{
	purple_close();
	net_close(_progstate.nd);
	settings_cleanup();
}