-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.c
141 lines (124 loc) · 3.71 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ev.h>
#include "globals.h"
#include "cmdline.h"
#include "daemon.h"
#include "eventloop.h"
#include "log.h"
#include "pidfile.h"
#include "utils.h"
static void cleanup(void)
{
free_globals(&globals);
}
static void create_socket(struct globals_t* g)
{
size_t good = 0;
const int on = 1;
int res;
uint16_t port;
union {
struct sockaddr sa;
struct sockaddr_in sa_in;
struct sockaddr_in6 sa_in6;
} sin;
g->sockets = calloc(g->nsockets, sizeof(int));
if (!g->sockets) {
perror("calloc");
exit(EXIT_FAILURE);
}
port = (uint16_t)atoi(g->bind_port);
for (size_t i=0; i<g->nsockets; ++i) {
memset(&sin, 0, sizeof(sin));
if (inet_pton(AF_INET, g->bind_addresses[i], &sin.sa_in.sin_addr) == 1) {
sin.sa_in.sin_family = AF_INET;
sin.sa_in.sin_port = htons(port);
}
else if (inet_pton(AF_INET6, g->bind_addresses[i], &sin.sa_in6.sin6_addr) == 1) {
sin.sa_in6.sin6_family = AF_INET6;
sin.sa_in6.sin6_port = htons(port);
}
else {
g->sockets[i] = -1;
fprintf(stderr, "ERROR: '%s' is not a valid address\n", g->bind_addresses[i]);
free(g->bind_addresses[i]);
g->bind_addresses[i] = NULL;
continue;
}
#if defined(__linux__) && defined(SOCK_NONBLOCK)
g->sockets[i] = socket(sin.sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
g->sockets[i] = socket(sin.sa.sa_family, SOCK_STREAM, 0);
#endif
if (-1 == g->sockets[i]) {
fprintf(stderr, "ERROR: Failed to create socket: %s\n", strerror(errno));
continue;
}
if (-1 == setsockopt(g->sockets[i], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int))) {
fprintf(stderr, "WARNING: setsockopt(SO_REUSEADDR) failed: %s\n", strerror(errno));
}
if (-1 == setsockopt(g->sockets[i], SOL_IP, IP_FREEBIND, &on, sizeof(int))) {
fprintf(stderr, "WARNING: setsockopt(IP_FREEBIND) failed: %s\n", strerror(errno));
}
#if !defined(__linux__) || !defined(SOCK_NONBLOCK)
if (-1 == make_nonblocking(g->sockets[i])) {
fprintf(stderr, "ERROR: Failed to make the socket non-blocking: %s\n", strerror(errno));
close(g->sockets[i]);
g->sockets[i] = -1;
continue;
}
#endif
res = bind(g->sockets[i], &sin.sa, sizeof(sin));
if (-1 == res) {
fprintf(stderr, "ERROR: failed to bind(): %s\n", strerror(errno));
close(g->sockets[i]);
g->sockets[i] = -1;
continue;
}
res = listen(g->sockets[i], 1024);
if (-1 == res) {
fprintf(stderr, "ERROR: failed to listen(): %s\n", strerror(errno));
close(g->sockets[i]);
g->sockets[i] = -1;
continue;
}
++good;
}
if (!good) {
fprintf(stderr, "ERROR: No listening sockets available\n");
exit(EXIT_FAILURE);
}
}
int main(int argc, char** argv)
{
init_globals(&globals);
atexit(cleanup);
parse_options(argc, argv, &globals);
#ifndef MINIMALISTIC_BUILD
if (!globals.no_syslog) {
openlog(
globals.daemon_name,
(
LOG_PID
| LOG_CONS
#ifdef LOG_PERROR
| LOG_PERROR
#endif
),
LOG_DAEMON
);
}
check_pid_file(&globals);
#endif
create_socket(&globals);
become_daemon(&globals);
return main_loop(&globals);
}