summaryrefslogtreecommitdiffstats
path: root/server/nastd.c
blob: cbe06eb4139a522ff7dcf0e179c5ffb6f148cdd9 (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "conf.h"
#include "config.h"
#include "nastdio.h"
#include "nastipc.h"
#include "cdb.h"
#include "fqm.h"
#include "log.h"
#include "memdb.h"
#include "mysqldb.h"
#include "periodic.h"

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>

char *progname;
time_t start_time;

static int
make_u_csock()
{
	struct sockaddr_un sunix;
	int sock;

	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock == -1) {
		log_err("Couldn't create unix socket: %s.", strerror(errno));
		return -1;
	}

	memset(&sunix, 0, sizeof(sunix));
	snprintf(sunix.sun_path, sizeof(sunix.sun_path), config.nast_sock);
	sunix.sun_family = AF_UNIX;
	(void)unlink(sunix.sun_path);

	if (bind(sock, (struct sockaddr *)&sunix, sizeof(sunix)) == -1) {
		log_err("Couldn't bind to unix socket: %s.", strerror(errno));
		return -1;
	}
	(void)listen(sock, 10);

	return sock;
}

static int
make_i_csock()
{
	struct sockaddr_in sinet;
	int sock;

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		log_err("Couldn't create inet socket: %S.", strerror(errno));
		return -1;
	}

	memset(&sinet, 0, sizeof(sinet));
	sinet.sin_family = AF_INET;
	sinet.sin_addr.s_addr = INADDR_ANY;
	sinet.sin_port = htons(config.tcp_port);
	if (bind(sock, (struct sockaddr *)&sinet, sizeof(sinet)) == -1) {
		log_err("Couldn't bind to inet socket: %s.", strerror(errno));
		return -1;
	}
	(void)listen(sock, 10);

	return sock;
}

static int
do_client_connect(int sock)
{
	struct sockaddr_un saremote;
	socklen_t addrlen;
	int s;

	addrlen = sizeof(saremote);
	s = accept(sock, (struct sockaddr *)&saremote, &addrlen);
	if (s == -1) {
		log_err("Couldn't accept new connection: %s.",
			strerror(errno));
		return -1;
	}
	io_new(s);

	return 0;
}

static int
init_daemon()
{
	sigset_t sigmask;

	log_open();
	if (cdb_new())
		return -1;
	mysqldb_new();
	if (memdb_new())
		return -1;

	/*
	 * Turn off SIGPIPE. The calls to write() will return fine with
	 * it off, and this means we don't have to do any signal mojo.
	 */
	(void)sigemptyset(&sigmask);
	(void)sigaddset(&sigmask, SIGPIPE);
	(void)sigprocmask(SIG_BLOCK, &sigmask, NULL);

	/* Now we daemonise. */
	switch (fork()) {
	case 0:
		setsid();
		close(STDIN_FILENO);
		close(STDOUT_FILENO);
		break;
	case -1:
		log_err("Couldn't fork into daemon: %s.", strerror(errno));
		return -1;
	default:
		exit(0);
	}

	return 0;
}

void
usage()
{
	fprintf(stderr, "Usage: %s [options]\n"
			"\t-d directory\tSpecify directory for nast files.\n",
			progname);
	exit(1);
}

int
main(int argc, char *argv[])
{
	char ch;
	int u_csock, i_csock;

	progname = strrchr(argv[0], '/');
	if (!progname)
		progname = argv[0];
	else
		progname++;

	/* Initialise configuration values from file. */
	config_setdefaults();
	while ((ch = getopt(argc, argv, "d:")) != -1) {
		switch (ch) {
		case 'd':
			config.nast_dir = optarg;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	if (config_init())
		return 1;

	if (init_daemon()) {
		log_err("Couldn't initialise nastd. Exiting.");
		return 1;
	}

	u_csock = make_u_csock();
	if (u_csock == -1)
		return 1;

	i_csock = make_i_csock();
	if (i_csock == -1)
		return 1;

	/* Create the FQM threads. */
	(void)fqm_new(10);

	/*
	 * Creat a thread that runs periodically.
	 */
	if (periodic_new() == -1) {
		log_err("Couldn't start periodic thread. Exiting.");
		return 1;
	}

	start_time = time(NULL);

	/*
	 * Main control loop. Sit on the socket and wait for a client to
	 * connect. As soon as it does, make another thread to handle it.
	 */
	for (;;) {
		fd_set read_fds;
		int rc;

		FD_ZERO(&read_fds);
		FD_SET(i_csock, &read_fds);
		FD_SET(u_csock, &read_fds);

		rc = select(10, &read_fds, NULL, NULL, NULL);
		if (rc == -1) {
			log_err("Couldn't select on sockets: %s.",
				strerror(errno));
			sleep(30);
		}

		if (FD_ISSET(i_csock, &read_fds))
			(void)do_client_connect(i_csock);

		if (FD_ISSET(u_csock, &read_fds))
			(void)do_client_connect(u_csock);
	}
	return 0;
}