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
|
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <syslog.h>
#include <unistd.h>
#include "config.h"
pid_t fork_proc() {
pid_t pid = fork();
if (pid) {
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) {
pid_t pid = fork_proc();
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
signal(SIGCHLD, child_handler);
signal(SIGHUP, hangup_handler);
pid = fork_proc();
umask(0);
if (change_dir) {
chdir((!path) ? "/" : path);
}
for (int i = sysconf(_SC_OPEN_MAX); i >= 0; close(i--));
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);
if (*cfg != NULL) {
syslog(LOG_INFO, "Successfully read %s.", config_file);
} else {
syslog(LOG_ERR, "Error reading %s.", config_file);
}
return (*cfg != NULL);
}
int init_socket(config *cfg) {
union sock_addr {
struct sockaddr sa;
struct sockaddr_un sa_un;
struct sockaddr_in sa_in;
} sock_addr;
const int sock_type = (!strcasecmp(cfg->sock_type, "unix")) ? AF_UNIX : AF_INET;
int fd = socket(sock_type, SOCK_SEQPACKET, 0);
TODO
if (sock_type == AF_UNIX) {
}
return fd;
}
int main_loop(config *cfg, int listen_socket) {
int done = 0;
for (; !done;) {
break;
}
return 0;
}
void cleanup(config *cfg, int listen_socket) {
if (listen_socket > 0) {
close(listen_socket);
if (!strcasecmp(cfg->sock_type, "unix")) {
unlink(cfg->sock);
}
}
if (cfg) {
cleanup_config(cfg);
}
}
int main(int argc, char **argv) {
config *cfg;
char *config_file = "test.conf";
int config_read = 0;
int listen_socket = -1;
int exit_status = EXIT_FAILURE;
init_daemon(1, "/");
syslog(LOG_NOTICE, "pullreqd started.");
config_read = init_config(config_file, &cfg);
if (config_read) {
listen_socket = init_socket(cfg);
}
if (config_read && listen_socket >= 0) {
exit_status = main_loop(cfg, listen_socket);
}
cleanup(cfg, listen_socket);
syslog(LOG_NOTICE, "pullreqd stopped.");
closelog();
return exit_status;
}
|