File: tbench_srv.c

package info (click to toggle)
dbench 1.3-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,296 kB
  • ctags: 221
  • sloc: ansic: 1,161; makefile: 91; awk: 59
file content (113 lines) | stat: -rw-r--r-- 2,231 bytes parent folder | download | duplicates (2)
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
/* 
   dbench version 1
   Copyright (C) Andrew Tridgell 1999
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "dbench.h"

char *tcp_options = TCP_OPTIONS;

static void server(int fd)
{
	char buf[70000];
	unsigned *ibuf = (unsigned *)buf;
	int n;

	signal(SIGPIPE, SIG_IGN);
	
	printf("^"); fflush(stdout);

	while (1) {
		if (read_sock(fd, buf, 4) != 4) break;
		n = ntohl(ibuf[0]);
		if (n+4 >= sizeof(buf)) {
			printf("overflow in server!\n");
			exit(1);
		}
		if (read_sock(fd, buf+4, n) != n) break;
		n = ntohl(ibuf[1]);
		ibuf[0] = htonl(n);
		if (write_sock(fd, buf, n+4) != n+4) break;
	}

	exit(0);
}

static void listener(void)
{
	int sock;

	sock = open_socket_in(SOCK_STREAM, TCP_PORT, INADDR_ANY);

	if (listen(sock, 20) == -1) {
		fprintf(stderr,"listen failed\n");
		exit(1);
	}

	printf("waiting for connections\n");

	signal(SIGCHLD, SIG_IGN);

	while (1) {
		struct sockaddr addr;
		int in_addrlen = sizeof(addr);
		int fd;

		while (waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0) ;

		fd = accept(sock,&addr,&in_addrlen);

		if (fd != -1) {
			if (fork() == 0) server(fd);
			close(fd);
		}
	}
}


static void usage(void)
{
	printf("usage: tbench_srv [OPTIONS]\n"
	       "options:\n"
	       "  -t options       set socket options\n");
	exit(1);
}


static void process_opts(int argc, char **argv)
{
	int c;

	while ((c = getopt(argc, argv, "t:")) != -1) {
		switch (c) {
		case 't':
			tcp_options = optarg;
			break;
		default:
			usage();
		}
	}
}


 int main(int argc, char *argv[])
{
	process_opts(argc, argv);

	listener();
	return 0;
}