commit ff93cb6bf624f20b36815841fc732e6ba3bf2000
parent afa7efed5db8e5e0cb26cce8bd032fc582cc9861
Author: Kyle Milz <milz@imac.0x30.net>
Date: Thu, 4 Mar 2021 15:47:18 -0800
lib: delete CITRUN_PROCDIR
Diffstat:
19 files changed, 70 insertions(+), 131 deletions(-)
diff --git a/lib/os.h b/lib/os.h
@@ -24,10 +24,7 @@
void *citrun_extend(int, size_t);
/*
- * Creates a new semi random file name, opens it and returns the descriptor.
- * If the CITRUN_PROCDIR environment variable is set its value is prefixed to
- * the file name otherwise an operating system specific prefix is used.
- *
+ * Creates a new file, opens it and returns the descriptor.
* Returns the descriptor number on success.
*/
int citrun_open_fd();
@@ -39,10 +36,6 @@ int citrun_open_fd();
void citrun_os_info(struct citrun_header *);
/*
- * Checks if the global `citrun_gl.lock` file is in the directory given by
- * CITRUN_PROCDIR if it exists. If CITRUN_PROCDIR does not exist an operating
- * system specific location is used instead.
- *
* If no lock file exists, a viewer is started.
*/
void citrun_start_viewer();
diff --git a/lib/unix.c b/lib/unix.c
@@ -28,7 +28,7 @@
#include "citrun.h" /* struct citrun_header */
#include "os.h"
-#define UNIX_PROCDIR "/tmp/citrun"
+#define UNIX_MMAP_FILE "/tmp/citrun.out"
/*
@@ -76,34 +76,18 @@ citrun_extend(int fd, size_t req_bytes)
}
/*
- * If CITRUN_PROCDIR is not set UNIX_PROCDIR is used as a prefix. Attempt to
- * create the prefix but don't error if it already exists.
- *
- * Create a file name by concatenating the program name with a 10 character (man
- * page suggests this amount) random template and pass that to mkstemp(3).
- *
- * If this program fails the instrumented program will exit nonzero.
+ * If this function fails the instrumented program will exit nonzero.
*/
int
citrun_open_fd()
{
- const char *procdir;
- char procfile[PATH_MAX];
- int fd;
-
- if ((procdir = getenv("CITRUN_PROCDIR")) == NULL)
- procdir = UNIX_PROCDIR;
+ int flags, mode, fd;
- if (mkdir(procdir, S_IRWXU) && errno != EEXIST)
- err(1, "mkdir '%s'", procdir);
+ flags = O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC;
+ mode = S_IRUSR | S_IWUSR;
- strlcpy(procfile, procdir, PATH_MAX);
- strlcat(procfile, "/", PATH_MAX);
- strlcat(procfile, getprogname(), PATH_MAX);
- strlcat(procfile, "_XXXXXXXXXX", PATH_MAX);
-
- if ((fd = mkstemp(procfile)) < 0)
- err(1, "mkstemp");
+ if ((fd = open(UNIX_MMAP_FILE, flags, mode)) < 0)
+ err(1, "open: %s", UNIX_MMAP_FILE);
return fd;
}
@@ -132,36 +116,13 @@ citrun_os_info(struct citrun_header *h)
}
/*
- * Checks for the global citrun_gl lock file in the directory either given by the
- * CITRUN_PROCDIR environment variable value or UNIX_PROCDIR if CITRUN_PROCDIR
- * doesn't exist.
- *
- * If no lock file exists a new process is forked that tries to exec 'citrun_gl'
- * which must be on the path.
- *
+ * Tries to exec 'citrun_gl' which must be on the path.
* The instrumented program exits on failure and returns nothing on success.
*/
void
citrun_start_viewer()
{
- pid_t pid;
- const char *procdir;
- char viewer_file[PATH_MAX];
-
- if ((procdir = getenv("CITRUN_PROCDIR")) == NULL)
- procdir = UNIX_PROCDIR;
-
- strlcpy(viewer_file, procdir, PATH_MAX);
- strlcat(viewer_file, "/", PATH_MAX);
- strlcat(viewer_file, "citrun_gl.lock", PATH_MAX);
-
- if (access(viewer_file, F_OK)) {
- /* If errno was ENOENT then fall through otherwise error. */
- if (errno != ENOENT)
- err(1, "access");
- } else
- /* File already exists, don't create a new viewer. */
- return;
+ pid_t pid;
pid = fork();
if (pid < 0)
diff --git a/t/e2e_intent.t b/t/e2e_intent.t
@@ -68,19 +68,19 @@ is( $e2e->stderr, '', 'is citrun_wrap compile stderr silent' );
is( $? >> 8, 0, 'is citrun_wrap compile exit code 0' );
eq_or_diff( $log, $log_good, 'is citrun_wrap log file identical' );
-$ENV{CITRUN_PROCDIR} = $e2e->workdir;
-$e2e->write( 'citrun_gl.lock', '' );
-
$e2e->run( prog => $e2e->workdir . "/fib", chdir => $e2e->curdir );
-is( $e2e->stderr, '', 'is fib stderr silent' );
+isnt( $e2e->stderr, '', 'is fib stderr not silent' );
is( $? >> 8, 1, 'is fib with no args exit 1' );
+unlink "/tmp/citrun.out";
$e2e->run( prog => $e2e->workdir . "/fib", args => '10', chdir => $e2e->curdir );
is( $e2e->stdout, '55', 'is fib 10 equal to 55' );
-is( $e2e->stderr, '', 'is fib 10 stderr silent' );
+isnt( $e2e->stderr, '', 'is fib 10 stderr not silent' );
is( $? >> 8, 0, 'is fib 10 exit 0' );
+unlink "/tmp/citrun.out";
$e2e->run( prog => $e2e->workdir . "/fib", args => '20', chdir => $e2e->curdir );
is( $e2e->stdout, '6765', 'is fib 20 equal to 6765' );
-is( $e2e->stderr, '', 'is fib 20 stderr silent' );
+isnt( $e2e->stderr, '', 'is fib 20 stderr not silent' );
is( $? >> 8, 0, 'is fib 20 exit 0' );
+unlink "/tmp/citrun.out";
diff --git a/t/e2e_shlib.t b/t/e2e_shlib.t
@@ -77,12 +77,11 @@ is( $wrap->stderr, '', 'is citrun_report stderr silent' );
is( $? >> 8, 0, 'is citrun_report exit code 0' );
$ENV{LD_LIBRARY_PATH} = $wrap->workdir;
-$ENV{CITRUN_PROCDIR} = $wrap->workdir;
-$wrap->write( 'citrun_gl.lock', '' );
$wrap->run( prog => $wrap->workdir . '/main', chdir => $wrap->curdir );
is( $wrap->stdout, 'lib called', 'is instrumented program stdout silent' );
-is( $wrap->stderr, '', 'is instrumented program stderr silent' );
+isnt( $wrap->stderr, '', 'is instrumented program stderr not silent' );
is( $? >> 8, 0, 'is instrumented program exit code 0' );
+unlink "/tmp/citrun.out";
#my $file = get_one_shmfile( $wrap->workdir );
diff --git a/t/lib_badver.t b/t/lib_badver.t
@@ -30,11 +30,10 @@ is( $? >> 8, 0, 'is citrun_wrap exit code 0' );
my $err_good = 'libcitrun 0.0: incompatible version 0.255.
Try cleaning and rebuilding your project.';
-set_procdir( $wrap->workdir );
-#$wrap->write( 'citrun_gl.lock', '' );
-
my $abs_prog_path = File::Spec->catfile( $wrap->workdir, "main" );
+
$wrap->run( prog => $abs_prog_path, chdir => $wrap->curdir );
is( $wrap->stdout, '', 'is node program stdout silent' );
like( $wrap->stderr, qr/$err_good/, 'is node program stderr identical' );
isnt( $? >> 8, 0, 'is node program exit code nonzero' );
+unlink "/tmp/citrun.out";
diff --git a/t/lib_deadcount.t b/t/lib_deadcount.t
@@ -11,12 +11,11 @@ plan tests => 8;
my $dir = setup_projdir();
$dir->run( prog => $dir->workdir . '/program', args => '10', chdir => $dir->curdir );
-is( $dir->stdout, '55', 'is instrumented program stdout correct' );
-is( $dir->stderr, '', 'is instrumented program stderr silent' );
-is( $? >> 8, 0, "is instrumented program exit code 0" );
+is( $dir->stdout, '55', 'inst program stdout correct' );
+isnt( $dir->stderr, '', 'inst program stderr not silent' );
+is( $? >> 8, 0, 'inst program exit code 0' );
-my $shm_file_path = get_one_shmfile( $ENV{CITRUN_PROCDIR} );
-my $shm = t::mem->new( $shm_file_path );
+my $shm = t::mem->new();
my %tus = %{ $shm->{trans_units} };
my ($tu1, $tu2, $tu3) = sort keys %tus;
diff --git a/t/lib_header.t b/t/lib_header.t
@@ -11,11 +11,10 @@ my $dir = setup_projdir();
$dir->run( prog => $dir->workdir . '/program', args => '1', chdir => $dir->curdir );
is( $dir->stdout, '1', 'is instrumented program stdout correct' );
-is( $dir->stderr, '', 'is instrumented program stderr empty' );
+isnt( $dir->stderr, '', 'is instrumented program stderr not empty' );
is( $? >> 8, 0, 'is instrumented program exit code 0' );
-my $shm_file_path = get_one_shmfile( $ENV{CITRUN_PROCDIR} );
-my $shm = t::mem->new( $shm_file_path );
+my $shm = t::mem->new();
is( $shm->{magic}, 'ctrn', 'is file magic correct' );
is( $shm->{major}, 0, 'is major 0' );
diff --git a/t/lib_livecount.t b/t/lib_livecount.t
@@ -17,8 +17,7 @@ if ($child_pid == 0) {
}
usleep 500 * 1000;
-my $shm_path = get_one_shmfile( $ENV{CITRUN_PROCDIR} );
-my $shm = t::mem->new( $shm_path );
+my $shm = t::mem->new();
my %trans_units = %{ $shm->{trans_units} };
diff --git a/t/lib_size.t b/t/lib_size.t
@@ -12,10 +12,9 @@ my $dir = setup_projdir();
$dir->run( prog => $dir->workdir . "/program", args => '1', chdir => $dir->curdir );
is( $dir->stdout, '1', 'is instrumented program stdout correct' );
-is( $dir->stderr, '', 'is instrumented program stderr silent' );
+isnt( $dir->stderr, '', 'is instrumented program stderr not silent' );
is( $? >> 8, 0, 'is instrumented program exit code 0' );
-my $shm_file_path = get_one_shmfile( $ENV{CITRUN_PROCDIR} );
-my $procfile = t::mem->new( $shm_file_path );
+my $procfile = t::mem->new();
is( $procfile->{size}, $t::mem::os_allocsize * 4, 'is file 4 allocation units' );
diff --git a/t/lib_transunit.t b/t/lib_transunit.t
@@ -11,11 +11,10 @@ my $dir = setup_projdir();
$dir->run( prog => $dir->workdir . '/program', args => '1', chdir => $dir->curdir );
is( $dir->stdout, '1', 'is instrumented program stdout correct' );
-is( $dir->stderr, '', 'is instrumented program stderr silent' );
+isnt( $dir->stderr, '', 'is instrumented program stderr not silent' );
is( $? >> 8, 0, "is instrumented program exit code 0" );
-my $shm_file_path = get_one_shmfile( $ENV{CITRUN_PROCDIR} );
-my $shm = t::mem->new( $shm_file_path );
+my $shm = t::mem->new();
my %tus = %{ $shm->{trans_units} };
my ($tu1, $tu2, $tu3) = sort keys %tus;
diff --git a/t/lib_viewer.t b/t/lib_viewer.t
@@ -1,15 +1,14 @@
#
-# Check that the runtime starts the viewer if no 'citrun_gl.lock' file exists.
+# Check that the runtime starts the viewer.
#
# Cases:
-# 1) not having `citrun_gl` on the PATH and citrun_gl.lock missing is ok
-# 2) no lock file and `citrun_gl` on the PATH is ok
-# 3) no viewer is executed when the lock file is present
+# 1) citrun_gl not on the PATH
+# 2) citrun_gl on the PATH
#
use Modern::Perl;
use t::utils; # os_compiler()
-plan tests => 11;
+plan tests => 8;
my $wrap = Test::Cmd->new( prog => 'bin/citrun_wrap', workdir => '' );
@@ -22,33 +21,35 @@ $wrap->run( args => os_compiler() . 'main main.c', chdir => $wrap->curdir );
is( $wrap->stderr, '', 'is wrapped compile stderr silent' );
is( $? >> 8, 0, 'is wrapped compile exit code 0' );
+#
# Case 1.
+#
+
my $inst_prog = Test::Cmd->new( prog => $wrap->workdir . "/main", workdir => '' );
-$ENV{CITRUN_PROCDIR} = $inst_prog->workdir;
my $err_good = 'libcitrun: exec citrun_gl: No such file or directory';
$inst_prog->run( chdir => $inst_prog->curdir );
is( $inst_prog->stdout, '', 'is case 1 stdout silent' );
like( $inst_prog->stderr, qr/$err_good/, 'is case 1 stderr an error' );
is( $? >> 8, 0, 'is case 1 exit code 0' );
+unlink "/tmp/citrun.out";
+#
# Case 2.
+#
+
$inst_prog->write( 'citrun_gl', <<EOF );
#!/bin/sh
echo ran citrun_gl
EOF
+
chmod(0775, $inst_prog->workdir . '/citrun_gl') or die $!;
+$ENV{PATH} .= ".:";
+
$inst_prog->run( chdir => $wrap->curdir );
like( $inst_prog->stdout, qr/ran citrun_gl/, 'is case 2 viewer started' );
is( $inst_prog->stderr, '', 'is case 2 stderr empty' );
is( $? >> 8, 0, 'is case 2 exit code zero' );
-
-# Case 3.
-$inst_prog->write( $ENV{CITRUN_PROCDIR} . '/citrun_gl.lock', '' );
-$inst_prog->run( chdir => $wrap->curdir );
-
-is( $inst_prog->stdout, '', 'is case 3 stdout silent' );
-is( $inst_prog->stderr, '', 'is case 3 stderr silent' );
-is( $? >> 8, 0, 'is case 3 exit code zero' );
+unlink "/tmp/citrun.out";
diff --git a/t/mem.pm b/t/mem.pm
@@ -8,12 +8,12 @@ use if $^O ne 'MSWin32', 't::mem_unix';
use autodie;
sub new {
- my ($class, $procfile) = @_;
+ my ($class) = @_;
my $self = {};
bless($self, $class);
- get_mem( $self, $procfile );
+ get_mem( $self );
# These functions proved by C code at the end of this file.
my $header_size = citrun_header_size();
diff --git a/t/mem_unix.pm b/t/mem_unix.pm
@@ -8,16 +8,19 @@ use autodie;
our $os_allocsize = POSIX::sysconf(POSIX::_SC_PAGESIZE);
sub get_mem {
- my ($self, $procfile) = @_;
+ my ($self) = @_;
- open( FH, "<", $procfile );
+ my $procfile = "/tmp/citrun.out";
+ open( my $fh, "<", $procfile );
$self->{mem} = '';
- mmap( $self->{mem}, 0, PROT_READ, MAP_SHARED, FH ) or die "mmap: $!";
+ mmap( $self->{mem}, 0, PROT_READ, MAP_SHARED, $fh ) or die "mmap: $!";
$self->{size} = length $self->{mem};
- close FH;
+ close $fh;
+ # Consumer of /tmp/citrun.out must remove it.
+ unlink $procfile;
}
1;
diff --git a/t/utils.pm b/t/utils.pm
@@ -1,4 +1,3 @@
-use if $^O eq "MSWin32", 'File::DosGlob' => 'glob';
use Modern::Perl;
use Test::Cmd;
use Test::Differences;
@@ -8,6 +7,9 @@ use autodie;
unified_diff; # For Test::Differences diffs
+# Common message seen while testing lib.
+our $err_gl = 'libcitrun: exec citrun_gl: No such file or directory';
+
sub os_compiler {
if ($^O eq 'MSWin32') {
return 'cl /nologo /Fe';
@@ -26,19 +28,8 @@ sub clean_citrun_log {
return $log;
}
-sub get_one_shmfile {
- my ($dir) = @_;
-
- my @files = glob File::Spec->catfile( $dir, "program_*");
- die "not exactly one procfile found" if (scalar @files != 1);
-
- return $files[0];
-}
-
sub setup_projdir {
-
my $wrap = Test::Cmd->new( prog => 'bin/citrun_wrap', workdir => '' );
- $ENV{CITRUN_PROCDIR} = $wrap->workdir;
$wrap->write( 'main.c', <<EOF);
#include <stdio.h>
@@ -98,8 +89,11 @@ EOF
$wrap->run( args => 'make', chdir => $wrap->curdir );
+ print $wrap->stdout;
is( $wrap->stderr, '', 'is citrun_wrap make stderr empty' );
is( $? >> 8, 0, 'is citrun_wrap make exit code 0' );
return $wrap;
}
+
+1;
diff --git a/t/wrap_cmake.t b/t/wrap_cmake.t
@@ -74,14 +74,11 @@ print $wrap->stdout;
is( $wrap->stderr, '', 'is citrun_wrap make stderr empty');
is( $? >> 8, 0, 'is citrun_wrap make exit code 0');
-$ENV{CITRUN_PROCDIR} = $wrap->workdir;
-$wrap->write( 'citrun_gl.lock', '' );
-
# Check the instrumented program runs.
$wrap->run( prog => $wrap->workdir . "/program", chdir => $wrap->curdir );
is( $wrap->stdout, '', 'is instrumented program stdout empty');
-is( $wrap->stderr, '', 'is instrumented program stderr empty');
+isnt( $wrap->stderr, '', 'is instrumented program stderr not empty');
is( $? >> 8, 0, 'is instrumented program exit code 0');
-
+unlink "/tmp/citrun.out";
#ok "is runtime shared memory file created" test -f procdir/program_*
diff --git a/t/wrap_jam.t b/t/wrap_jam.t
@@ -59,9 +59,8 @@ $citrun_log = clean_citrun_log( $citrun_log );
eq_or_diff( $citrun_log, $log_good, 'is citrun_wrap log file identical', { context => 3 } );
-$ENV{CITRUN_PROCDIR} = $wrap->workdir;
-$wrap->write( 'citrun_gl.lock', '' );
$wrap->run( prog => $wrap->workdir . '/program', chdir => $wrap->curdir );
is( $wrap->stdout, '', 'is instrumented program stdout silent' );
is( $wrap->stderr, '', 'is instrumented program stderr silent' );
is( $? >> 8, 0, 'is instrumented program exit code 0' );
+unlink "/tmp/citrun.out";
diff --git a/t/wrap_make.t b/t/wrap_make.t
@@ -52,9 +52,8 @@ $citrun_log = clean_citrun_log( $citrun_log );
eq_or_diff( $citrun_log, $log_good, 'is citrun_wrap log file identical', { context => 3 } );
-$ENV{CITRUN_PROCDIR} = $wrap->workdir;
-$wrap->write( 'citrun_gl.lock', '' );
$wrap->run( prog => $wrap->workdir . '/program', chdir => $wrap->curdir );
is( $wrap->stdout, '', 'is instrumented program stdout silent' );
-is( $wrap->stderr, '', 'is instrumented program stderr silent' );
+isnt( $wrap->stderr, '', 'is instrumented program stderr not silent' );
is( $? >> 8, 0, 'is instrumented program exit code 0' );
+unlink "/tmp/citrun.out";
diff --git a/t/wrap_ninja.t b/t/wrap_ninja.t
@@ -58,9 +58,8 @@ $citrun_log = clean_citrun_log( $citrun_log );
eq_or_diff( $citrun_log, $log_good, 'is citrun_wrap log file identical', { context => 3 } );
-$ENV{CITRUN_PROCDIR} = $wrap->workdir;
-$wrap->write( 'citrun_gl.lock', '' );
$wrap->run( prog => $wrap->workdir . '/program', chdir => $wrap->curdir );
is( $wrap->stdout, '', 'is instrumented program stdout silent' );
-is( $wrap->stderr, '', 'is instrumented program stderr silent' );
+isnt( $wrap->stderr, '', 'is instrumented program stderr not silent' );
is( $? >> 8, 0, 'is instrumented program exit code 0' );
+unlink "/tmp/citrun.out";
diff --git a/t/wrap_parallel.t b/t/wrap_parallel.t
@@ -54,10 +54,10 @@ my $report_out = $wrap->stdout;
$report_out =~ s/^.*Milliseconds spent rewriting.*\n//gm;
eq_or_diff( $report_out, $report_good, 'is citrun_report stdout identical', { context => 3 } );
-$ENV{CITRUN_PROCDIR} = $wrap->workdir;
for (1..4) {
$wrap->run( prog => $wrap->workdir . "/program$_", chdir => $wrap->curdir );
is( $wrap->stdout, '', "is instrumented program$_ stdout silent" );
- is( $wrap->stderr, '', "is instrumented program$_ stderr silent" );
+ isnt( $wrap->stderr, '', "is instrumented program$_ stderr not silent" );
is( $? >> 8, 0, "is instrumented program$_ exit code 0" );
+ unlink "/tmp/citrun.out";
}