File: server.c

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (257 lines) | stat: -rw-r--r-- 4,879 bytes parent folder | download | duplicates (3)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* server.c - Utilities for fake servers used by the nmh test suite
 *
 * This code is Copyright (c) 2014, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <signal.h>

#include "server.h"

static const char *PIDFN = NULL;

static void killpidfile(void);
static void handleterm(int);

#ifndef EPROTOTYPE
#define EPROTOTYPE 0
#endif

static int
try_bind(int socket, const struct sockaddr *address, socklen_t len)
{
	int i, status;
	for (i = 0; i < 5; i++) {
		if ((status = bind(socket, address, len)) == 0) {
			return 0;
		}
		sleep(1);
	}

	return status;
}

int
serve(const char *pidfn, const char *port)
{
	struct addrinfo hints, *res;
	int rc, l, conn, on;
	FILE *pid;
	pid_t child;
	fd_set readfd;
	struct stat st;
	struct timeval tv;

	PIDFN = pidfn;

	/*
	 * If there is a pid file already around, kill the previously running
	 * fakesmtp process.  Hopefully this will reduce the race conditions
	 * that crop up when running the test suite.
	 */

	if (stat(pidfn, &st) == 0) {
		long oldpid;

		if (!(pid = fopen(pidfn, "r"))) {
			fprintf(stderr, "Cannot open %s (%s), continuing ...\n",
				pidfn, strerror(errno));
		} else {
			rc = fscanf(pid, "%ld", &oldpid);
			fclose(pid);

			if (rc != 1) {
				fprintf(stderr, "Unable to parse pid in %s,"
					" continuing ...\n",
					pidfn);
			} else {
				kill((pid_t) oldpid, SIGTERM);
			}
		}

		unlink(pidfn);
	}

	memset(&hints, 0, sizeof(hints));

	hints.ai_family = PF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	rc = getaddrinfo("127.0.0.1", port, &hints, &res);

	if (rc) {
		fprintf(stderr, "Unable to resolve localhost/%s: %s\n",
			port, gai_strerror(rc));
		exit(1);
	}

	l = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

	if (l == -1) {
		fprintf(stderr, "Unable to create listening socket: %s\n",
			strerror(errno));
		exit(1);
	}

	on = 1;

	if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
		fprintf(stderr, "Unable to set SO_REUSEADDR: %s\n",
			strerror(errno));
		exit(1);
	}

	if (try_bind(l, res->ai_addr, res->ai_addrlen) == -1) {
		fprintf(stderr, "Unable to bind socket: %s\n", strerror(errno));
		exit(1);
	}

	freeaddrinfo(res);

	if (listen(l, 1) == -1) {
		fprintf(stderr, "Unable to listen on socket: %s\n",
			strerror(errno));
		exit(1);
	}

	/*
	 * Now we fork() and print out the process ID of our child
	 * for scripts to use.  Once we do that, then exit.
	 */

	child = fork();

	switch (child) {
	case -1:
		fprintf(stderr, "Unable to fork child: %s\n", strerror(errno));
		exit(1);
		break;
	case 0:
		/*
		 * Close stdin & stdout, otherwise people can
		 * think we're still doing stuff.  For now leave stderr
		 * open.
		 */
		fclose(stdin);
		fclose(stdout);
		break;
	default:
		/* XXX why?  it's never used... */
		printf("%ld\n", (long) child);
		exit(0);
	}

	/*
	 * Now that our socket & files are set up, wait 30 seconds for
	 * a connection.  If there isn't one, then exit.
	 */

	if (!(pid = fopen(pidfn, "w"))) {
		fprintf(stderr, "Cannot open %s: %s\n",
			pidfn, strerror(errno));
		exit(1);
	}

	fprintf(pid, "%ld\n", (long) getpid());
	fclose(pid);

	signal(SIGTERM, handleterm);
	atexit(killpidfile);

	FD_ZERO(&readfd);
	FD_SET(l, &readfd);
	tv.tv_sec = 30;
	tv.tv_usec = 0;

	rc = select(l + 1, &readfd, NULL, NULL, &tv);

	if (rc < 0) {
		fprintf(stderr, "select() failed: %s\n", strerror(errno));
		exit(1);
	}

	/*
	 * I think if we get a timeout, we should just exit quietly.
	 */

	if (rc == 0) {
		exit(1);
	}

	/*
	 * Alright, got a connection!  Accept it.
	 */

	if ((conn = accept(l, NULL, NULL)) == -1) {
		fprintf(stderr, "Unable to accept connection: %s\n",
			strerror(errno));
		exit(1);
	}

	close(l);

    return conn;
}

/*
 * Write a line (adding \r\n) to the client on the other end
 */
void
putcrlf(int socket, char *data)
{
	struct iovec iov[2];

	iov[0].iov_base = data;
	iov[0].iov_len = strlen(data);
	iov[1].iov_base = "\r\n";
	iov[1].iov_len = 2;

	/* ECONNRESET just means the client already closed its end */
	/* MacOS X can also return EPROTOTYPE (!) here sometimes */
	/* XXX is it useful to log errors here at all? */
	if (writev(socket, iov, 2) < 0 && errno != ECONNRESET &&
	    errno != EPROTOTYPE) {
	    perror ("server writev");
	}
}

/*
 * Handle a SIGTERM
 */

static void
handleterm(int signal)
{
	(void) signal;

	killpidfile();
	fflush(NULL);
	_exit(1);
}

/*
 * Get rid of our pid file
 */

static void
killpidfile(void)
{
	if (PIDFN != NULL) {
		unlink(PIDFN);
	}
}