[dbench @ cvs-1:mbp-20010730222405-m2ccq4k1m91qrrcl]
[tridge/dbench.git] / child.c
1 /* 
2    dbench version 1
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 2 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, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* This file links against either fileio.c to do operations against a
21    local filesystem (making dbench), or sockio.c to issue SMB-like
22    command packets over a socket (making tbench).
23
24    So, the pattern of operations and the control structure is the same
25    for both benchmarks, but the operations performed are different.
26 */
27
28 #include "dbench.h"
29
30 int line_count=0;
31
32 char *client_filename = "client.txt";
33
34
35 static int sigsegv(int sig)
36 {
37         char line[200];
38         printf("segv at line %d\n", line_count);
39         sprintf(line, "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
40                 getpid(), getpid());
41         system(line);
42         exit(1);
43 }
44
45
46 FILE * open_client_dump(void)
47 {
48         FILE            *f;
49
50         if ((f = fopen(client_filename, "rt")) != NULL)
51                 return f;
52
53         fprintf(stderr,
54                 "dbench: error opening %s: %s", client_filename,
55                 strerror(errno));
56
57         return NULL;
58 }
59
60 void child_run(int client)
61 {
62         int i;
63         char fname[128];
64         char line[1024];
65         char cname[20];
66         FILE *f;
67         char *params[20];
68
69         signal(SIGSEGV, sigsegv);
70
71         sprintf(cname,"CLIENT%d", client);
72
73         f = open_client_dump();
74
75         if (!f) {
76                 perror("client.txt");
77                 return;
78         }
79
80         while (fgets(line, sizeof(line)-1, f)) {
81                 line_count++;
82
83                 line[strlen(line)-1] = 0;
84
85                 all_string_sub(line,"CLIENT1", cname);
86                 all_string_sub(line,"\\", "/");
87                 all_string_sub(line," /", " ");
88                 
89                 for (i=0;i<20;i++) params[i] = "";
90
91                 /* parse the command parameters */
92                 params[0] = strtok(line," ");
93                 i = 0;
94                 while (params[i]) params[++i] = strtok(NULL," ");
95
96                 params[i] = "";
97
98                 if (i < 2) continue;
99
100                 if (strcmp(params[1],"REQUEST") == 0) {
101                         if (!strcmp(params[0],"SMBopenX")) {
102                                 strcpy(fname, params[5]);
103                         } else if (!strcmp(params[0],"SMBclose")) {
104                                 nb_close(atoi(params[3]));
105                         } else if (!strcmp(params[0],"SMBmkdir")) {
106                                 nb_mkdir(params[3]);
107                         } else if (!strcmp(params[0],"CREATE")) {
108                                 nb_create(params[3], atoi(params[5]));
109                         } else if (!strcmp(params[0],"SMBrmdir")) {
110                                 nb_rmdir(params[3]);
111                         } else if (!strcmp(params[0],"SMBunlink")) {
112                                 strcpy(fname, params[3]);
113                         } else if (!strcmp(params[0],"SMBmv")) {
114                                 nb_rename(params[3], params[5]);
115                         } else if (!strcmp(params[0],"SMBgetatr")) {
116                                 strcpy(fname, params[3]);
117                         } else if (!strcmp(params[0],"SMBwrite")) {
118                                 nb_write(atoi(params[3]), 
119                                          atoi(params[5]), atoi(params[7]));
120                         } else if (!strcmp(params[0],"SMBwritebraw")) {
121                                 nb_write(atoi(params[3]), 
122                                          atoi(params[7]), atoi(params[5]));
123                         } else if (!strcmp(params[0],"SMBreadbraw")) {
124                                 nb_read(atoi(params[3]), 
125                                          atoi(params[7]), atoi(params[5]));
126                         } else if (!strcmp(params[0],"SMBread")) {
127                                 nb_read(atoi(params[3]), 
128                                          atoi(params[5]), atoi(params[7]));
129                         }
130                 } else {
131                         if (!strcmp(params[0],"SMBopenX")) {
132                                 if (!strncmp(params[2], "ERR", 3)) continue;
133                                 nb_open(fname, atoi(params[3]), atoi(params[5]));
134                         } else if (!strcmp(params[0],"SMBgetatr")) {
135                                 if (!strncmp(params[2], "ERR", 3)) continue;
136                                 nb_stat(fname, atoi(params[3]));
137                         } else if (!strcmp(params[0],"SMBunlink")) {
138                                 if (!strncmp(params[2], "ERR", 3)) continue;
139                                 nb_unlink(fname);
140                         }
141                 }
142         }
143         fclose(f);
144
145         sprintf(fname,"CLIENTS/CLIENT%d", client);
146         rmdir(fname);
147         rmdir("CLIENTS");
148
149         printf("+");
150 }