Author: trasz
Date: Mon Apr 16 17:30:33 2018
New Revision: 332628
URL: https://svnweb.freebsd.org/changeset/base/332628

Log:
  MFC r325313:
  
  Make syscall_timing(1) default to a temporary file when run without -p.

Modified:
  stable/11/tools/tools/syscall_timing/syscall_timing.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/tools/tools/syscall_timing/syscall_timing.c
==============================================================================
--- stable/11/tools/tools/syscall_timing/syscall_timing.c       Mon Apr 16 
17:29:50 2018        (r332627)
+++ stable/11/tools/tools/syscall_timing/syscall_timing.c       Mon Apr 16 
17:30:33 2018        (r332628)
@@ -39,6 +39,7 @@
 
 #include <assert.h>
 #include <err.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <limits.h>
@@ -792,15 +793,17 @@ main(int argc, char *argv[])
        struct timespec ts_res;
        const struct test *the_test;
        const char *path;
+       char *tmp_dir, *tmp_path;
        long long ll;
        char *endp;
-       int ch, error, i, j, k;
+       int ch, fd, error, i, j, k, rv;
        uintmax_t iterations, loops;
 
        alarm_timeout = 1;
        iterations = 0;
        loops = 10;
        path = NULL;
+       tmp_path = NULL;
        while ((ch = getopt(argc, argv, "i:l:p:s:")) != -1) {
                switch (ch) {
                case 'i':
@@ -859,7 +862,15 @@ main(int argc, char *argv[])
                if (the_test == NULL)
                        usage();
                if ((the_test->t_flags & FLAG_PATH) && (path == NULL)) {
-                       errx(-1, "%s requires -p", the_test->t_name);
+                       tmp_dir = strdup("/tmp/syscall_timing.XXXXXXXX");
+                       if (tmp_dir == NULL)
+                               err(1, "strdup");
+                       tmp_dir = mkdtemp(tmp_dir);
+                       if (tmp_dir == NULL)
+                               err(1, "mkdtemp");
+                       rv = asprintf(&tmp_path, "%s/testfile", tmp_dir);
+                       if (rv <= 0)
+                               err(1, "asprintf");
                }
        }
 
@@ -878,6 +889,19 @@ main(int argc, char *argv[])
                                the_test = &tests[i];
                }
 
+               if (tmp_path != NULL) {
+                       fd = open(tmp_path, O_WRONLY | O_CREAT, 0700);
+                       if (fd < 0)
+                               err(1, "cannot open %s", tmp_path);
+                       error = ftruncate(fd, 1000000);
+                       if (error != 0)
+                               err(1, "ftruncate");
+                       error = close(fd);
+                       if (error != 0)
+                               err(1, "close");
+                       path = tmp_path;
+               }
+
                /*
                 * Run one warmup, then do the real thing (loops) times.
                 */
@@ -903,5 +927,15 @@ main(int argc, char *argv[])
                        printf("0.%09ju\n", (uintmax_t)nsecsperit);
                }
        }
+
+       if (tmp_path != NULL) {
+               error = unlink(tmp_path);
+               if (error != 0 && errno != ENOENT)
+                       warn("cannot unlink %s", tmp_path);
+               error = rmdir(tmp_dir);
+               if (error != 0)
+                       warn("cannot rmdir %s", tmp_dir);
+       }
+
        return (0);
 }
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to