Fix string overflow due to wrong size calculation
[Samba/gebeck_regimport.git] / source3 / torture / nbio.c
blob2e79584d23f7bfa297dce6109d8251674157798c
1 #define NBDEBUG 0
3 /*
4 Unix SMB/CIFS implementation.
5 SMB torture tester
6 Copyright (C) Andrew Tridgell 1997-1998
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.
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.
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.
23 #define NO_SYSLOG
25 #include "includes.h"
27 #define MAX_FILES 1000
29 static char buf[70000];
30 extern int line_count;
31 extern int nbio_id;
32 static int nprocs;
34 static struct {
35 int fd;
36 int handle;
37 } ftable[MAX_FILES];
39 static struct {
40 double bytes_in, bytes_out;
41 int line;
42 int done;
43 } *children;
45 double nbio_total(void)
47 int i;
48 double total = 0;
49 for (i=0;i<nprocs;i++) {
50 total += children[i].bytes_out + children[i].bytes_in;
52 return total;
55 void nb_alarm(int ignore)
57 int i;
58 int lines=0, num_clients=0;
59 if (nbio_id != -1) return;
61 for (i=0;i<nprocs;i++) {
62 lines += children[i].line;
63 if (!children[i].done) num_clients++;
66 printf("%4d %8d %.2f MB/sec\r", num_clients, lines/nprocs, 1.0e-6 * nbio_total() / end_timer());
68 signal(SIGALRM, nb_alarm);
69 alarm(1);
72 void nbio_shmem(int n)
74 nprocs = n;
75 children = shm_setup(sizeof(*children) * nprocs);
76 if (!children) {
77 printf("Failed to setup shared memory!\n");
78 exit(1);
82 #if 0
83 static int ne_find_handle(int handle)
85 int i;
86 children[nbio_id].line = line_count;
87 for (i=0;i<MAX_FILES;i++) {
88 if (ftable[i].handle == handle) return i;
90 return -1;
92 #endif
94 static int find_handle(int handle)
96 int i;
97 children[nbio_id].line = line_count;
98 for (i=0;i<MAX_FILES;i++) {
99 if (ftable[i].handle == handle) return i;
101 printf("(%d) ERROR: handle %d was not found\n",
102 line_count, handle);
103 exit(1);
105 return -1; /* Not reached */
109 static struct cli_state *c;
111 static void sigsegv(int sig)
113 char line[200];
114 printf("segv at line %d\n", line_count);
115 slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d",
116 (int)getpid(), (int)getpid());
117 system(line);
118 exit(1);
121 void nb_setup(struct cli_state *cli)
123 signal(SIGSEGV, sigsegv);
124 c = cli;
125 start_timer();
126 children[nbio_id].done = 0;
130 void nb_unlink(const char *fname)
132 if (!cli_unlink(c, fname)) {
133 #if NBDEBUG
134 printf("(%d) unlink %s failed (%s)\n",
135 line_count, fname, cli_errstr(c));
136 #endif
141 void nb_createx(const char *fname,
142 unsigned create_options, unsigned create_disposition, int handle)
144 int fd, i;
145 uint32 desired_access;
147 if (create_options & FILE_DIRECTORY_FILE) {
148 desired_access = FILE_READ_DATA;
149 } else {
150 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
153 fd = cli_nt_create_full(c, fname, 0,
154 desired_access,
155 0x0,
156 FILE_SHARE_READ|FILE_SHARE_WRITE,
157 create_disposition,
158 create_options, 0);
159 if (fd == -1 && handle != -1) {
160 printf("ERROR: cli_nt_create_full failed for %s - %s\n",
161 fname, cli_errstr(c));
162 exit(1);
164 if (fd != -1 && handle == -1) {
165 printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
166 exit(1);
168 if (fd == -1) return;
170 for (i=0;i<MAX_FILES;i++) {
171 if (ftable[i].handle == 0) break;
173 if (i == MAX_FILES) {
174 printf("(%d) file table full for %s\n", line_count,
175 fname);
176 exit(1);
178 ftable[i].handle = handle;
179 ftable[i].fd = fd;
182 void nb_writex(int handle, int offset, int size, int ret_size)
184 int i;
186 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
188 i = find_handle(handle);
189 if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
190 printf("(%d) ERROR: write failed on handle %d, fd %d \
191 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
192 exit(1);
195 children[nbio_id].bytes_out += ret_size;
198 void nb_readx(int handle, int offset, int size, int ret_size)
200 int i, ret;
202 i = find_handle(handle);
203 if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
204 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
205 line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
206 exit(1);
208 children[nbio_id].bytes_in += ret_size;
211 void nb_close(int handle)
213 int i;
214 i = find_handle(handle);
215 if (!cli_close(c, ftable[i].fd)) {
216 printf("(%d) close failed on handle %d\n", line_count, handle);
217 exit(1);
219 ftable[i].handle = 0;
222 void nb_rmdir(const char *fname)
224 if (!cli_rmdir(c, fname)) {
225 printf("ERROR: rmdir %s failed (%s)\n",
226 fname, cli_errstr(c));
227 exit(1);
231 void nb_rename(const char *old, const char *new)
233 if (!cli_rename(c, old, new)) {
234 printf("ERROR: rename %s %s failed (%s)\n",
235 old, new, cli_errstr(c));
236 exit(1);
241 void nb_qpathinfo(const char *fname)
243 cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
246 void nb_qfileinfo(int fnum)
248 int i;
249 i = find_handle(fnum);
250 cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
253 void nb_qfsinfo(int level)
255 int bsize, total, avail;
256 /* this is not the right call - we need cli_qfsinfo() */
257 cli_dskattr(c, &bsize, &total, &avail);
260 static void find_fn(file_info *finfo, const char *name, void *state)
262 /* noop */
265 void nb_findfirst(const char *mask)
267 cli_list(c, mask, 0, find_fn, NULL);
270 void nb_flush(int fnum)
272 int i;
273 i = find_handle(fnum);
274 /* hmmm, we don't have cli_flush() yet */
277 static int total_deleted;
279 static void delete_fn(file_info *finfo, const char *name, void *state)
281 char *s, *n;
282 if (finfo->name[0] == '.') return;
284 n = strdup(name);
285 n[strlen(n)-1] = 0;
286 asprintf(&s, "%s%s", n, finfo->name);
287 if (finfo->mode & aDIR) {
288 char *s2;
289 asprintf(&s2, "%s\\*", s);
290 cli_list(c, s2, aDIR, delete_fn, NULL);
291 nb_rmdir(s);
292 } else {
293 total_deleted++;
294 nb_unlink(s);
296 free(s);
297 free(n);
300 void nb_deltree(const char *dname)
302 char *mask;
303 asprintf(&mask, "%s\\*", dname);
305 total_deleted = 0;
306 cli_list(c, mask, aDIR, delete_fn, NULL);
307 free(mask);
308 cli_rmdir(c, dname);
310 if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
314 void nb_cleanup(void)
316 cli_rmdir(c, "clients");
317 children[nbio_id].done = 1;