s4-dns: dlz_bind9: Fix ipv6 updates
[samba.git] / source3 / torture / nbio.c
index 6c51db3cf3020f93104d836a0f6a74d501338d5a..ba4fa9559872f15eea31c1087c7791ee8835bed5 100644 (file)
@@ -7,7 +7,7 @@
    
    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
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    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.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#define NO_SYSLOG
-
 #include "includes.h"
+#include "torture/proto.h"
+#include "../libcli/security/security.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
 
 #define MAX_FILES 1000
 
@@ -30,13 +31,14 @@ static char buf[70000];
 extern int line_count;
 extern int nbio_id;
 static int nprocs;
+static struct timeval nb_start;
 
 static struct {
        int fd;
        int handle;
 } ftable[MAX_FILES];
 
-static struct {
+static struct children {
        double bytes_in, bytes_out;
        int line;
        int done;
@@ -52,7 +54,7 @@ double nbio_total(void)
        return total;
 }
 
-void nb_alarm(void)
+void nb_alarm(int ignore)
 {
        int i;
        int lines=0, num_clients=0;
@@ -63,7 +65,9 @@ void nb_alarm(void)
                if (!children[i].done) num_clients++;
        }
 
-       printf("%4d  %8d  %.2f MB/sec\r", num_clients, lines/nprocs, 1.0e-6 * nbio_total() / end_timer());
+       printf("%4d  %8d  %.2f MB/sec\r",
+              num_clients, lines/nprocs,
+              1.0e-6 * nbio_total() / timeval_elapsed(&nb_start));
 
        signal(SIGALRM, nb_alarm);
        alarm(1);       
@@ -72,13 +76,14 @@ void nb_alarm(void)
 void nbio_shmem(int n)
 {
        nprocs = n;
-       children = shm_setup(sizeof(*children) * nprocs);
+       children = (struct children *)anonymous_shared_allocate(sizeof(*children) * nprocs);
        if (!children) {
                printf("Failed to setup shared memory!\n");
                exit(1);
        }
 }
 
+#if 0
 static int ne_find_handle(int handle)
 {
        int i;
@@ -88,6 +93,7 @@ static int ne_find_handle(int handle)
        }
        return -1;
 }
+#endif
 
 static int find_handle(int handle)
 {
@@ -112,7 +118,9 @@ static void sigsegv(int sig)
        printf("segv at line %d\n", line_count);
        slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
                (int)getpid(), (int)getpid());
-       system(line);
+       if (system(line) == -1) {
+               printf("system() failed\n");
+       }
        exit(1);
 }
 
@@ -120,17 +128,20 @@ void nb_setup(struct cli_state *cli)
 {
        signal(SIGSEGV, sigsegv);
        c = cli;
-       start_timer();
+       nb_start = timeval_current();
        children[nbio_id].done = 0;
 }
 
 
 void nb_unlink(const char *fname)
 {
-       if (!cli_unlink(c, fname)) {
+       NTSTATUS status;
+
+       status = cli_unlink(c, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
 #if NBDEBUG
                printf("(%d) unlink %s failed (%s)\n", 
-                      line_count, fname, cli_errstr(c));
+                      line_count, fname, nt_errstr(status));
 #endif
        }
 }
@@ -139,7 +150,9 @@ void nb_unlink(const char *fname)
 void nb_createx(const char *fname, 
                unsigned create_options, unsigned create_disposition, int handle)
 {
-       int fd, i;
+       uint16_t fd = (uint16_t)-1;
+       int i;
+       NTSTATUS status;
        uint32 desired_access;
 
        if (create_options & FILE_DIRECTORY_FILE) {
@@ -148,22 +161,22 @@ void nb_createx(const char *fname,
                desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
        }
 
-       fd = cli_nt_create_full(c, fname, 0, 
+       status = cli_ntcreate(c, fname, 0, 
                                desired_access,
                                0x0,
                                FILE_SHARE_READ|FILE_SHARE_WRITE, 
                                create_disposition, 
-                               create_options, 0);
-       if (fd == -1 && handle != -1) {
-               printf("ERROR: cli_nt_create_full failed for %s - %s\n",
-                      fname, cli_errstr(c));
+                               create_options, 0, &fd, NULL);
+       if (!NT_STATUS_IS_OK(status) && handle != -1) {
+               printf("ERROR: cli_ntcreate failed for %s - %s\n",
+                      fname, nt_errstr(status));
                exit(1);
        }
-       if (fd != -1 && handle == -1) {
-               printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
+       if (NT_STATUS_IS_OK(status) && handle == -1) {
+               printf("ERROR: cli_ntcreate succeeded for %s\n", fname);
                exit(1);
        }
-       if (fd == -1) return;
+       if (fd == (uint16_t)-1) return;
 
        for (i=0;i<MAX_FILES;i++) {
                if (ftable[i].handle == 0) break;
@@ -180,13 +193,17 @@ void nb_createx(const char *fname,
 void nb_writex(int handle, int offset, int size, int ret_size)
 {
        int i;
+       NTSTATUS status;
 
        if (buf[0] == 0) memset(buf, 1, sizeof(buf));
 
        i = find_handle(handle);
-       if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
-               printf("(%d) ERROR: write failed on handle %d, fd %d \
-errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
+       status = cli_writeall(c, ftable[i].fd, 0, (uint8_t *)buf, offset, size,
+                             NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(%d) ERROR: write failed on handle %d, fd %d "
+                      "error %s\n", line_count, handle, ftable[i].fd,
+                      nt_errstr(status));
                exit(1);
        }
 
@@ -195,12 +212,23 @@ errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
 
 void nb_readx(int handle, int offset, int size, int ret_size)
 {
-       int i, ret;
+       int i;
+       NTSTATUS status;
+       size_t nread;
 
        i = find_handle(handle);
-       if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
-               printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
-                       line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
+       status = cli_read(c, ftable[i].fd, buf, offset, size, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d "
+                      "fd %d nterror %s\n",
+                      line_count, handle, offset, size,
+                      ftable[i].fd, nt_errstr(status));
+               exit(1);
+       } else if (nread != ret_size) {
+               printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d "
+                      "nread=%lu ret_size=%d fd %d\n",
+                      line_count, handle, offset, size, (unsigned long)nread,
+                      ret_size, ftable[i].fd);
                exit(1);
        }
        children[nbio_id].bytes_in += ret_size;
@@ -210,7 +238,7 @@ void nb_close(int handle)
 {
        int i;
        i = find_handle(handle);
-       if (!cli_close(c, ftable[i].fd)) {
+       if (!NT_STATUS_IS_OK(cli_close(c, ftable[i].fd))) {
                printf("(%d) close failed on handle %d\n", line_count, handle);
                exit(1);
        }
@@ -219,18 +247,24 @@ void nb_close(int handle)
 
 void nb_rmdir(const char *fname)
 {
-       if (!cli_rmdir(c, fname)) {
+       NTSTATUS status;
+
+       status = cli_rmdir(c, fname);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("ERROR: rmdir %s failed (%s)\n", 
-                      fname, cli_errstr(c));
+                      fname, nt_errstr(status));
                exit(1);
        }
 }
 
-void nb_rename(const char *old, const char *new)
+void nb_rename(const char *oldname, const char *newname)
 {
-       if (!cli_rename(c, old, new)) {
+       NTSTATUS status;
+
+       status = cli_rename(c, oldname, newname);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("ERROR: rename %s %s failed (%s)\n", 
-                      old, new, cli_errstr(c));
+                      oldname, newname, nt_errstr(status));
                exit(1);
        }
 }
@@ -238,14 +272,15 @@ void nb_rename(const char *old, const char *new)
 
 void nb_qpathinfo(const char *fname)
 {
-       cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
+       cli_qpathinfo1(c, fname, NULL, NULL, NULL, NULL, NULL);
 }
 
 void nb_qfileinfo(int fnum)
 {
        int i;
        i = find_handle(fnum);
-       cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+       cli_qfileinfo_basic(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL,
+                           NULL, NULL);
 }
 
 void nb_qfsinfo(int level)
@@ -255,9 +290,11 @@ void nb_qfsinfo(int level)
        cli_dskattr(c, &bsize, &total, &avail);
 }
 
-static void find_fn(file_info *finfo, const char *name, void *state)
+static NTSTATUS find_fn(const char *mnt, struct file_info *finfo, const char *name,
+                   void *state)
 {
        /* noop */
+       return NT_STATUS_OK;
 }
 
 void nb_findfirst(const char *mask)
@@ -269,23 +306,43 @@ void nb_flush(int fnum)
 {
        int i;
        i = find_handle(fnum);
-       /* hmmm, we don't have cli_flush() yet */
+
+       cli_flush(NULL, c, i);
 }
 
 static int total_deleted;
 
-static void delete_fn(file_info *finfo, const char *name, void *state)
+static NTSTATUS delete_fn(const char *mnt, struct file_info *finfo,
+                     const char *name, void *state)
 {
+       NTSTATUS status;
        char *s, *n;
-       if (finfo->name[0] == '.') return;
+       if (finfo->name[0] == '.') {
+               return NT_STATUS_OK;
+       }
 
-       n = strdup(name);
+       n = SMB_STRDUP(name);
        n[strlen(n)-1] = 0;
-       asprintf(&s, "%s%s", n, finfo->name);
-       if (finfo->mode & aDIR) {
+       if (asprintf(&s, "%s%s", n, finfo->name) == -1) {
+               free(n);
+               printf("asprintf failed\n");
+               return NT_STATUS_NO_MEMORY;
+       }
+       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
                char *s2;
-               asprintf(&s2, "%s\\*", s);
-               cli_list(c, s2, aDIR, delete_fn, NULL);
+               if (asprintf(&s2, "%s\\*", s) == -1) {
+                       printf("asprintf failed\n");
+                       free(s);
+                       free(n);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               status = cli_list(c, s2, FILE_ATTRIBUTE_DIRECTORY, delete_fn, NULL);
+               free(s2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       free(s);
+                       free(n);
+                       return status;
+               }
                nb_rmdir(s);
        } else {
                total_deleted++;
@@ -293,15 +350,19 @@ static void delete_fn(file_info *finfo, const char *name, void *state)
        }
        free(s);
        free(n);
+       return NT_STATUS_OK;
 }
 
 void nb_deltree(const char *dname)
 {
        char *mask;
-       asprintf(&mask, "%s\\*", dname);
+       if (asprintf(&mask, "%s\\*", dname) == -1) {
+               printf("asprintf failed\n");
+               return;
+       }
 
        total_deleted = 0;
-       cli_list(c, mask, aDIR, delete_fn, NULL);
+       cli_list(c, mask, FILE_ATTRIBUTE_DIRECTORY, delete_fn, NULL);
        free(mask);
        cli_rmdir(c, dname);