summaryrefslogtreecommitdiff
path: root/pullreqd.c
blob: 1c76241a032361fa976d040f95e69d15afc7548e (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
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
#include "config.h"

pid_t fork_proc() {
	/* Fork off the parent. */
	pid_t pid = fork();

	/* The process id is non zero, so it must be the parent process. */
	if (pid) {
		/* Exit with EXIT_FAILURE if the parent's pid is
		 * negative, otherwise exit with EXIT_SUCCESS.
		 */
		exit((pid < 0) ? EXIT_FAILURE : EXIT_SUCCESS);
	}
	return pid;
}

void child_handler(int sig_num) {
	signal(SIGCHLD, child_handler);
}

void hangup_handler(int sig_num) {
	signal(SIGHUP, hangup_handler);
}

void init_daemon(int change_dir, char *path) {
	/* Fork the parent. */
	pid_t pid = fork_proc();

	/* Exit if the child process isn't the session leader. */
	if (setsid() < 0) {
		exit(EXIT_FAILURE);
	}

	/* Catch, ignore, and/or handle signals. */
	signal(SIGCHLD, child_handler);
	signal(SIGHUP, hangup_handler);

	/* Fork again. */
	pid = fork_proc();

	/* Set new file permissions. */
	umask(0);

	/* If change_dir is true, change the working directory to path, or
	 * the root directory if path isn't set.
	 */
	if (change_dir) {
		chdir((!path) ? "/" : path);
	}

	/* Close any open file descriptors. */
	for (int i = sysconf(_SC_OPEN_MAX); i >= 0; close(i--));

	/* Open the logfile. */
	openlog("pullreqd", LOG_PID, LOG_DAEMON);
}

int init_config(char *config_file, config **cfg) {
	syslog(LOG_INFO, "Reading config file %s.", config_file);

	*cfg = parse_config(config_file);

	/* Did the config file parser succeed? */
	if (*cfg != NULL) {
		syslog(LOG_INFO, "Successfully read %s.", config_file);
	} else {
		syslog(LOG_ERROR, "Error reading %s.", config_file);
	}

	return (*cfg != NULL);
}

int main(int argc, char **argv) {
	config *cfg;
	char *config_file = "test.conf";
	int done = 0;
	int config_read = 0;

	init_daemon(1, "/");
	syslog(LOG_NOTICE, "pullreqd started.");

	/* Read the config file. */
	config_read = init_config(config_file, &cfg);
	/* Immediatly exit, if we failed to read the config file. */
	done = !config_read;

	for (; !done;) {
		break;
	}

	syslog(LOG_NOTICE, "pullreqd stopped.");

	closelog();

	return EXIT_SUCCESS;
}