-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
globals.c
85 lines (69 loc) · 1.66 KB
/
globals.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ev.h>
#include "globals.h"
#include "log.h"
struct globals_t globals;
void init_globals(struct globals_t* g)
{
memset(g, 0, sizeof(struct globals_t));
#ifndef MINIMALISTIC_BUILD
g->pid_fd = -1;
g->piddir_fd = -1;
#endif
g->loop = EV_DEFAULT;
signal(SIGPIPE, SIG_IGN);
}
#ifndef MINIMALISTIC_BUILD
static void kill_pid_file(struct globals_t* g)
{
if (g->pid_fd >= 0) {
assert(g->pid_file != NULL);
assert(g->pid_base != NULL);
if (-1 == unlinkat(g->piddir_fd, g->pid_base, 0)) {
my_log(LOG_DAEMON | LOG_WARNING, "WARNING: Failed to delete the PID file %s: %s", g->pid_file, strerror(errno));
}
close(g->pid_fd);
close(g->piddir_fd);
}
}
#endif
void free_globals(struct globals_t* g)
{
if (g->loop) {
ev_loop_destroy(g->loop);
}
if (g->sockets) {
for (size_t i=0; i<g->nsockets; ++i) {
if (g->sockets[i] >= 0) {
shutdown(g->sockets[i], SHUT_RDWR);
close(g->sockets[i]);
}
}
free(g->sockets);
}
#ifndef MINIMALISTIC_BUILD
kill_pid_file(g);
if (!g->no_syslog) {
closelog();
}
free(g->pid_file);
free(g->daemon_name);
free(g->chroot_dir);
free(g->pid_base);
#endif
if (g->bind_addresses) {
for (size_t i=0; i<g->nsockets; ++i) {
free(g->bind_addresses[i]);
}
free(g->bind_addresses);
}
free(g->bind_port);
free(g->server_ver);
}