commit c3131375632ce0ac3e5d961bee303951722c14dd
parent 9eaff6c065b9c33859f11bce84e10897352dde9a
Author: kyle <kyle@0x30.net>
Date: Fri, 25 Nov 2016 22:14:27 -0700
src: change CITRUN_PROCFILE to CITRUN_PROCDIR
Diffstat:
11 files changed, 72 insertions(+), 55 deletions(-)
diff --git a/src/rt.c b/src/rt.c
@@ -16,9 +16,11 @@
#include <sys/mman.h> /* mmap */
#include <sys/stat.h> /* S_IRUSR, S_IWUSR, mkdir */
+#include <assert.h>
#include <err.h>
#include <errno.h> /* EEXIST */
#include <fcntl.h> /* O_CREAT */
+#include <limits.h> /* PATH_MAX */
#include <stdlib.h> /* get{env,progname} */
#include <string.h> /* str{l,n}cpy */
#include <unistd.h> /* lseek get{cwd,pid,ppid,pgrp} */
@@ -68,36 +70,38 @@ shm_extend(size_t requested_bytes)
static void
shm_create()
{
- char *procfile;
- char memfile_path[23];
- char *template = "/tmp/citrun/XXXXXXXXXX";
- char *process_dir = "/tmp/citrun";
+ char *procdir;
+ char procfile[PATH_MAX];
struct citrun_header *header;
- if ((procfile = getenv("CITRUN_PROCFILE")) != NULL) {
- if ((shm_fd = open(procfile, O_RDWR | O_CREAT,
- S_IRUSR | S_IWUSR)) == -1)
- err(1, "open");
- } else {
- /* Existing directory is OK. */
- if (mkdir(process_dir, S_IRWXU) && errno != EEXIST)
- err(1, "mkdir '%s'", process_dir);
+ /* User of this env var must give trailing slash */
+ if ((procdir = getenv("CITRUN_PROCDIR")) == NULL)
+ procdir = "/tmp/citrun/";
- strlcpy(memfile_path, template, sizeof(memfile_path));
+ if (mkdir(procdir, S_IRWXU) && errno != EEXIST)
+ err(1, "mkdir '%s'", procdir);
- if ((shm_fd = mkstemp(memfile_path)) == -1)
- err(1, "mkstemp");
- }
+ strlcpy(procfile, procdir, PATH_MAX);
+ strlcat(procfile, getprogname(), PATH_MAX);
+ strlcat(procfile, "_XXXXXXXXXX", PATH_MAX);
+
+ if ((shm_fd = mkstemp(procfile)) < 0)
+ err(1, "mkstemp");
/* Add header. */
+ assert(sizeof(struct citrun_header) < getpagesize());
header = shm_extend(sizeof(struct citrun_header));
+ /* Purposefully not null terminated. */
strncpy(header->magic, "ctrn", sizeof(header->magic));
+
header->major = citrun_major;
header->minor = citrun_minor;
header->pids[0] = getpid();
header->pids[1] = getppid();
header->pids[2] = getpgrp();
+
+ /* getprogname() should never fail. */
strlcpy(header->progname, getprogname(), sizeof(header->progname));
if (getcwd(header->cwd, sizeof(header->cwd)) == NULL)
diff --git a/t/rt_exectotals.t b/t/rt_exectotals.t
@@ -3,8 +3,8 @@
#
use strict;
use warnings;
-use Test::More tests => 50;
-use Time::HiRes qw( usleep );
+use Test::More tests => 26;
+use Time::HiRes qw( time usleep );
use t::program;
use t::shm;
use t::tmpdir;
@@ -18,12 +18,19 @@ if ($child_pid == 0) {
exec ("$tmp_dir/program", "45") or die $!;
}
-# Give the runtime time to set up.
-sleep 1;
-my $shm = t::shm->new($tmp_dir);
+# Give the forked child time to set up, but no longer than 1.0 seconds.
+my $start = time;
+my @procfiles;
+do {
+ @procfiles = glob("$ENV{CITRUN_PROCDIR}/program_*");
+} while (scalar @procfiles == 0 && (time - $start) < 1.0);
+
+is scalar @procfiles, 1, "is one file in procdir";
+
+my $shm = t::shm->new($procfiles[0]);
my $last_total = 0;
-for (0..49) {
+for (0..24) {
usleep 100 * 1000;
my $total = 0;
@@ -32,7 +39,7 @@ for (0..49) {
$total += $_ for (@$execs);
}
- cmp_ok $total, '>', $last_total, "new total > old total";
+ cmp_ok $total, '>', $last_total, "is total line count increasing";
$last_total = $total;
}
diff --git a/t/rt_header.t b/t/rt_header.t
@@ -3,7 +3,7 @@
#
use strict;
use warnings;
-use Test::More tests => 12;
+use Test::More tests => 13;
use t::program;
use t::shm;
use t::tmpdir;
@@ -14,18 +14,21 @@ t::program->new($tmp_dir);
my $ret = system("cd $tmp_dir && program 1");
is $ret >> 8, 0, "is program exit code 0";
-my $shm = t::shm->new($tmp_dir);
-is $shm->{magic}, "ctrn", "is file magic correct";
-is $shm->{major}, 0, "is major correct";
-is $shm->{minor}, 0, "is minor correct";
+my @procfiles = glob("$ENV{CITRUN_PROCDIR}/program_*");
+is scalar @procfiles, 1, "is one file in procdir";
+
+my $shm = t::shm->new($procfiles[0]);
+is $shm->{magic}, "ctrn", "is file magic correct";
+is $shm->{major}, 0, "is major correct";
+is $shm->{minor}, 0, "is minor correct";
my ($pid, $ppid, $pgrp) = @{ $shm->{pids} };
-cmp_ok $pid, '<', 100 * 1000, "pid is less than max pid";
-cmp_ok $pid, '>', 1, "pid is greater than min pid";
-cmp_ok $ppid, '<', 100 * 1000, "ppid is less than max pid";
-cmp_ok $ppid, '>', 1, "ppid is greater than min pid";
-cmp_ok $pgrp, '<', 100 * 1000, "pgrp is less than max pid";
-cmp_ok $pgrp, '>', 1, "pgrp is greater than min pid";
+cmp_ok $pid, '<', 100 * 1000, "is pid < max pid";
+cmp_ok $pid, '>', 1, "is pid > min pid";
+cmp_ok $ppid, '<', 100 * 1000, "is ppid < max pid";
+cmp_ok $ppid, '>', 1, "is ppid > min pid";
+cmp_ok $pgrp, '<', 100 * 1000, "is pgrp < max pid";
+cmp_ok $pgrp, '>', 1, "is pgrp > min pid";
is $shm->{progname}, "program", 'is test program name correct';
is $shm->{cwd}, $tmp_dir, 'is working directory believable';
diff --git a/t/rt_size.t b/t/rt_size.t
@@ -4,7 +4,7 @@
use strict;
use warnings;
use POSIX;
-use Test::More tests => 2;
+use Test::More tests => 3;
use t::program;
use t::shm;
use t::tmpdir;
@@ -13,9 +13,12 @@ my $tmp_dir = t::tmpdir->new();
t::program->new($tmp_dir);
my $ret = system("$tmp_dir/program 1");
-is $ret >> 8, 0, "is test program exit code 0";
+is $ret >> 8, 0, "is test program exit code 0";
-my $procfile = t::shm->new($tmp_dir);
+my @procfiles = glob("$ENV{CITRUN_PROCDIR}/program_*");
+is scalar @procfiles, 1, "is one file in procdir";
+
+my $procfile = t::shm->new($procfiles[0]);
my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE);
-is($procfile->{size}, $pagesize * 4, "is memory file 4 pages long");
+is($procfile->{size}, $pagesize * 4, "is memory file 4 pages long");
diff --git a/t/rt_translunit.t b/t/rt_translunit.t
@@ -3,7 +3,7 @@
#
use strict;
use warnings;
-use Test::More tests => 7;
+use Test::More tests => 8;
use t::program;
use t::shm;
use t::tmpdir;
@@ -14,7 +14,10 @@ t::program->new($tmp_dir);
my $ret = system("$tmp_dir/program 10");
is $ret >> 8, 0, "is program exit code 0";
-my $shm = t::shm->new($tmp_dir);
+my @procfiles = glob("$ENV{CITRUN_PROCDIR}/program_*");
+is scalar @procfiles, 1, "is one file in procdir";
+
+my $shm = t::shm->new($procfiles[0]);
my ($tu1, $tu2, $tu3) = @{ $shm->{translation_units} };
is $tu1->{size}, 9, "is translation unit 1 9 lines";
diff --git a/t/shm.pm b/t/shm.pm
@@ -6,15 +6,15 @@ use POSIX;
my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE);
sub new {
- my ($class, $tmpdir) = @_;
+ my ($class, $procfile) = @_;
my $self = {};
bless($self, $class);
- open(my $fh, "<:mmap", "$tmpdir/procfile.shm") or die $!;
+ open(my $fh, "<:mmap", $procfile) or die $!;
$self->{fh} = $fh;
- $self->{size} = (stat "$tmpdir/procfile.shm")[7];
+ $self->{size} = (stat $procfile)[7];
( $self->{magic},
$self->{major}, $self->{minor},
diff --git a/t/tmpdir.pm b/t/tmpdir.pm
@@ -5,7 +5,7 @@ use File::Temp qw( tempdir );
sub new {
my $tmp_dir = tempdir( CLEANUP => 1 );
- $ENV{CITRUN_PROCFILE} = "$tmp_dir/procfile.shm";
+ $ENV{CITRUN_PROCDIR} = "$tmp_dir/procdir/";
return $tmp_dir;
}
diff --git a/t/wrap_cmake.sh b/t/wrap_cmake.sh
@@ -45,6 +45,5 @@ EOF
strip_millis check.out
ok "is citrun-check output identical" diff -u check.good check.out
-export CITRUN_PROCFILE="procfile.shm"
-ok "does compiled program run" program
-ok "is runtime shared memory file created" test -f procfile.shm
+CITRUN_PROCDIR="procdir/" ok "does compiled program run" program
+ok "is runtime shared memory file created" test -f procdir/program_*
diff --git a/t/wrap_jam.sh b/t/wrap_jam.sh
@@ -41,6 +41,5 @@ EOF
strip_millis check.out
ok "is citrun-check output identical" diff -u check.good check.out
-export CITRUN_PROCFILE="procfile.shm"
-ok "does compiled program run" program
-ok "is runtime shared memory file created" test -f procfile.shm
+CITRUN_PROCDIR="procdir/" ok "does compiled program run" program
+ok "is runtime shared memory file created" test -f procdir/program_*
diff --git a/t/wrap_make.sh b/t/wrap_make.sh
@@ -43,5 +43,5 @@ strip_millis check.out
ok "is citrun-check output identical" diff -u check.good check.out
export CITRUN_PROCFILE="procfile.shm"
-ok "does compiled program run" program
-ok "is runtime shared memory file created" test -f procfile.shm
+CITRUN_PROCDIR="procdir/" ok "does compiled program run" program
+ok "is runtime shared memory file created" test -f procdir/program_*
diff --git a/t/wrap_ninja.sh b/t/wrap_ninja.sh
@@ -49,6 +49,5 @@ EOF
strip_millis check.out
ok "is citrun-check output identical" diff -u check.good check.out
-export CITRUN_PROCFILE="procfile.shm"
-ok "does compiled program run" program
-ok "is runtime shared memory file created" test -f procfile.shm
+CITRUN_PROCDIR="procdir/" ok "does compiled program run" program
+ok "is runtime shared memory file created" test -f procdir/program_*