From 1371108e64c07c369a17b7298203c609dbc37e4a Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Sun, 13 May 2012 16:03:13 +0200 Subject: [PATCH] Fixed with test: Test bug #35820: sem breaks if $HOME is not writable --- src/parallel | 23 ++++++++++++++++++--- testsuite/tests-to-run/parallel-local150.sh | 8 ++++++- testsuite/wanted-results/parallel-local150 | 4 ++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/parallel b/src/parallel index c2078892..c6e97bf9 100755 --- a/src/parallel +++ b/src/parallel @@ -5000,9 +5000,9 @@ sub new { $id=~s/([^-_a-z0-9])/unpack("H*",$1)/ige; # Convert non-word chars to hex $id="id-".$id; # To distinguish it from a process id my $parallel_dir = $ENV{'HOME'}."/.parallel"; - -d $parallel_dir or mkdir $parallel_dir; + -d $parallel_dir or mkdir_or_die($parallel_dir); my $parallel_locks = $parallel_dir."/semaphores"; - -d $parallel_locks or mkdir $parallel_locks; + -d $parallel_locks or mkdir_or_die($parallel_locks); my $lockdir = "$parallel_locks/$id"; my $lockfile = $lockdir.".lock"; if($count < 1) { ::die_bug("semaphore-count: $count"); } @@ -5082,7 +5082,7 @@ sub atomic_link_if_count_less_than { $self->lock(); ::debug($self->nlinks()."<".$self->{'count'}); if($self->nlinks() < $self->{'count'}) { - -d $self->{'lockdir'} || mkdir $self->{'lockdir'}; + -d $self->{'lockdir'} or mkdir_or_die($self->{'lockdir'}); if(not -e $self->{'idfile'}) { open (A, ">", $self->{'idfile'}) or ::die_bug("write_idfile: $self->{'idfile'}"); @@ -5137,6 +5137,23 @@ sub unlock { ::debug("unlocked\n"); } +sub mkdir_or_die { + # If dir is not writable: die + my $dir = shift; + my @dir_parts = split(m:/:,$dir); + my ($ddir,$part); + while(defined ($part = shift @dir_parts)) { + $part eq "" and next; + $ddir .= "/".$part; + -d $ddir and next; + mkdir $ddir; + } + if(not -w $dir) { + print STDERR "$Global::progname: Cannot write to $dir: $!\n"; + exit(255); + } +} + # Keep perl -w happy $::opt_x = $Semaphore::timeout = $Semaphore::wait = $::opt_shebang = 0; diff --git a/testsuite/tests-to-run/parallel-local150.sh b/testsuite/tests-to-run/parallel-local150.sh index a5abb911..5c030ac6 100644 --- a/testsuite/tests-to-run/parallel-local150.sh +++ b/testsuite/tests-to-run/parallel-local150.sh @@ -1,7 +1,7 @@ #!/bin/bash echo '### Test fix #32191' -seq 1 150 | parallel -j9 --retries 2 -S localhost,: "/bin/non-existant 2>/dev/null" +seq 1 150 | nice nice parallel -j9 --retries 2 -S localhost,: "/bin/non-existant 2>/dev/null" echo '### Test --tagstring' parallel -j1 -X -v --tagstring a{}b echo ::: 3 4 @@ -9,3 +9,9 @@ parallel -j1 -k -v --tagstring a{}b echo ::: 3 4 parallel -j1 -k -v --tagstring a{}b echo job{#} ::: 3 4 parallel -j1 -k -v --tagstring ajob{#}b echo job{#} ::: 3 4 +echo '### Test bug #35820: sem breaks if $HOME is not writable' +echo 'Workaround: use another writable dir' +rm -rf /tmp/.parallel +HOME=/tmp sem echo OK +HOME=/tmp sem --wait +HOME=/usr/this/should/fail stdout sem echo should fail diff --git a/testsuite/wanted-results/parallel-local150 b/testsuite/wanted-results/parallel-local150 index 887a3df1..cc1f63d0 100644 --- a/testsuite/wanted-results/parallel-local150 +++ b/testsuite/wanted-results/parallel-local150 @@ -14,3 +14,7 @@ echo job1 ajob1b job1 echo job2 ajob2b job2 +### Test bug #35820: sem breaks if $HOME is not writable +Workaround: use another writable dir +OK +parallel: Cannot write to /usr/this/should/fail/.parallel: No such file or directory