dont assume that all smb loadfiles will always use /clients/client* so dont force...
[sahlberg/dbench.git] / tbench_srv.c
1 /* 
2    dbench version 2
3    Copyright (C) Andrew Tridgell 1999
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "dbench.h"
20
21 struct options options = {
22         .tcp_options = TCP_OPTIONS
23 };
24
25 static void server(int fd)
26 {
27         char buf[70000];
28         unsigned *ibuf = (unsigned *)buf;
29         uint32_t n;
30
31         signal(SIGPIPE, SIG_IGN);
32         
33         printf("^"); fflush(stdout);
34
35         while (1) {
36                 if (read_sock(fd, buf, 4) != 4) break;
37                 n = ntohl(ibuf[0]);
38                 if (n+4 >= sizeof(buf)) {
39                         printf("overflow in server!\n");
40                         exit(1);
41                 }
42                 if (read_sock(fd, buf+4, n) != (int)n) break;
43                 n = ntohl(ibuf[1]);
44                 ibuf[0] = htonl(n);
45                 if (write_sock(fd, buf, n+4) != (int)(n+4)) break;
46         }
47
48         exit(0);
49 }
50
51 static void listener(void)
52 {
53         int sock;
54
55         sock = open_socket_in(SOCK_STREAM, TCP_PORT);
56
57         if (listen(sock, 20) == -1) {
58                 fprintf(stderr,"listen failed\n");
59                 exit(1);
60         }
61
62         printf("waiting for connections\n");
63
64         signal(SIGCHLD, SIG_IGN);
65
66         while (1) {
67                 struct sockaddr addr;
68                 socklen_t in_addrlen = sizeof(addr);
69                 int fd;
70
71                 while (waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0) ;
72
73                 fd = accept(sock, &addr, &in_addrlen);
74
75                 if (fd != -1) {
76                         if (fork() == 0) server(fd);
77                         close(fd);
78                 }
79         }
80 }
81
82
83 static void usage(void)
84 {
85         printf("usage: tbench_srv [OPTIONS]\n"
86                "options:\n"
87                "  -t options       set socket options\n");
88         exit(1);
89 }
90
91
92 static void process_opts(int argc, char **argv)
93 {
94         int c;
95
96         while ((c = getopt(argc, argv, "t:")) != -1) {
97                 switch (c) {
98                 case 't':
99                         options.tcp_options = optarg;
100                         break;
101                 default:
102                         usage();
103                 }
104         }
105 }
106
107
108  int main(int argc, char *argv[])
109 {
110         process_opts(argc, argv);
111
112         listener();
113         return 0;
114 }