Removed version number from file header.
[jra/samba/.git] / source3 / torture / nbio.c
1 #define NBDEBUG 0
2
3 /* 
4    Unix SMB/CIFS implementation.
5    SMB torture tester
6    Copyright (C) Andrew Tridgell 1997-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define NO_SYSLOG
24
25 #include "includes.h"
26
27 #define MAX_FILES 1000
28
29 static char buf[70000];
30 extern int line_count;
31
32 static struct {
33         int fd;
34         int handle;
35 } ftable[MAX_FILES];
36
37 static struct cli_state *c;
38
39 static void sigsegv(int sig)
40 {
41         char line[200];
42         printf("segv at line %d\n", line_count);
43         slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
44                 (int)getpid(), (int)getpid());
45         system(line);
46         exit(1);
47 }
48
49 void nb_setup(struct cli_state *cli)
50 {
51         signal(SIGSEGV, sigsegv);
52         c = cli;
53 }
54
55
56 void nb_unlink(char *fname)
57 {
58         strupper(fname);
59
60         if (!cli_unlink(c, fname)) {
61 #if NBDEBUG
62                 printf("(%d) unlink %s failed (%s)\n", 
63                        line_count, fname, cli_errstr(c));
64 #endif
65         }
66 }
67
68 void nb_open(char *fname, int handle, int size)
69 {
70         int fd, i;
71         int flags = O_RDWR|O_CREAT;
72         size_t st_size;
73         static int count;
74
75         strupper(fname);
76
77         if (size == 0) flags |= O_TRUNC;
78
79         fd = cli_open(c, fname, flags, DENY_NONE);
80         if (fd == -1) {
81 #if NBDEBUG
82                 printf("(%d) open %s failed for handle %d (%s)\n", 
83                        line_count, fname, handle, cli_errstr(c));
84 #endif
85                 return;
86         }
87         cli_getattrE(c, fd, NULL, &st_size, NULL, NULL, NULL);
88         if (size > st_size) {
89 #if NBDEBUG
90                 printf("(%d) needs expanding %s to %d from %d\n", 
91                        line_count, fname, size, (int)st_size);
92 #endif
93         } else if (size < st_size) {
94 #if NBDEBUG
95                 printf("(%d) needs truncating %s to %d from %d\n", 
96                        line_count, fname, size, (int)st_size);
97 #endif
98         }
99         for (i=0;i<MAX_FILES;i++) {
100                 if (ftable[i].handle == 0) break;
101         }
102         if (i == MAX_FILES) {
103                 printf("file table full for %s\n", fname);
104                 exit(1);
105         }
106         ftable[i].handle = handle;
107         ftable[i].fd = fd;
108         if (count++ % 100 == 0) {
109                 printf(".");
110         }
111 }
112
113 void nb_write(int handle, int size, int offset)
114 {
115         int i;
116
117         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
118
119         for (i=0;i<MAX_FILES;i++) {
120                 if (ftable[i].handle == handle) break;
121         }
122         if (i == MAX_FILES) {
123 #if NBDEBUG
124                 printf("(%d) nb_write: handle %d was not open size=%d ofs=%d\n", 
125                        line_count, handle, size, offset);
126 #endif
127                 return;
128         }
129         if (cli_smbwrite(c, ftable[i].fd, buf, offset, size) != size) {
130                 printf("(%d) write failed on handle %d, fd %d \
131 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
132                 if (errno == ENOSPC) {
133                         printf("Halting.\n");
134                         fflush(stdout);
135                         fflush(stderr);
136                         exit(3);
137                 }
138         }
139 }
140
141 void nb_read(int handle, int size, int offset)
142 {
143         int i, ret;
144
145         for (i=0;i<MAX_FILES;i++) {
146                 if (ftable[i].handle == handle) break;
147         }
148         if (i == MAX_FILES) {
149                 printf("(%d) nb_read: handle %d was not open size=%d ofs=%d\n", 
150                        line_count, handle, size, offset);
151                 return;
152         }
153         if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != size) {
154 #if NBDEBUG
155                 printf("(%d) read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
156                         line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
157 #endif
158         }
159 }
160
161 void nb_close(int handle)
162 {
163         int i;
164         for (i=0;i<MAX_FILES;i++) {
165                 if (ftable[i].handle == handle) break;
166         }
167         if (i == MAX_FILES) {
168                 printf("(%d) nb_close: handle %d was not open\n", 
169                        line_count, handle);
170                 return;
171         }
172         cli_close(c, ftable[i].fd);
173         ftable[i].handle = 0;
174 }
175
176 void nb_mkdir(char *fname)
177 {
178         strupper(fname);
179
180         if (!cli_mkdir(c, fname)) {
181 #if NBDEBUG
182                 printf("mkdir %s failed (%s)\n", 
183                        fname, cli_errstr(c));
184 #endif
185         }
186 }
187
188 void nb_rmdir(char *fname)
189 {
190         strupper(fname);
191
192         if (!cli_rmdir(c, fname)) {
193 #if NBDEBUG
194                 printf("rmdir %s failed (%s)\n", 
195                        fname, cli_errstr(c));
196 #endif
197         }
198 }
199
200 void nb_rename(char *old, char *new)
201 {
202         strupper(old);
203         strupper(new);
204
205         if (!cli_rename(c, old, new)) {
206 #if NBDEBUG
207                 printf("rename %s %s failed (%s)\n", 
208                        old, new, cli_errstr(c));
209 #endif
210         }
211 }
212
213
214 void nb_stat(char *fname, int size)
215 {
216         size_t st_size;
217
218         strupper(fname);
219
220         if (!cli_getatr(c, fname, NULL, &st_size, NULL)) {
221 #if NBDEBUG
222                 printf("(%d) nb_stat: %s size=%d %s\n", 
223                        line_count, fname, size, cli_errstr(c));
224 #endif
225                 return;
226         }
227         if (st_size != size) {
228 #if NBDEBUG
229                 printf("(%d) nb_stat: %s wrong size %d %d\n", 
230                        line_count, fname, (int)st_size, size);
231 #endif
232         }
233 }
234
235 void nb_create(char *fname, int size)
236 {
237         nb_open(fname, 5000, size);
238         nb_close(5000);
239 }