]> eyrie.org Git - kerberos/kstart.git/commitdiff
Fix races in k5start/daemon and krenew/daemon tests
authorRuss Allbery <eagle@eyrie.org>
Tue, 30 Mar 2021 03:13:14 +0000 (20:13 -0700)
committerRuss Allbery <eagle@eyrie.org>
Tue, 30 Mar 2021 17:30:28 +0000 (10:30 -0700)
Several of the tests for backgrounding were missing a delay to
wait for the daemon to start.  Add those in, and clean up the
PID files from previous tests to avoid spurious failures.

k5start and krenew do not write their PID file atomically, so
there was a race condition in the tests where they would detect
the existence of the file and then try to read an empty file.
Avoid this by checking that the file contains a PID using -s.

tests/k5start/daemon-t
tests/krenew/daemon-t

index f7de31de1e9ec34e5431b08802913d6d36f3fd3b..dfc38b779debe08b3b8492da6e07f315e4aaeb70 100755 (executable)
@@ -54,7 +54,7 @@ if (!defined $pid) {
           $principal) or BAIL_OUT ("can't run $K5START: $!");
 }
 my $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -97,7 +97,7 @@ is ($status, 0, 'Backgrounding k5start works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and -q was added implicitly');
 $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -121,6 +121,10 @@ unlink "$TMP/krb5cc_test";
 is ($status, 0, 'Backgrounding k5start with bad keytab works');
 like ($err, qr/^k5start: error getting credentials: /, ' with error output');
 is ($out, '', ' and -q was added implicitly');
+while (not -s "$TMP/pid" and $tries < 100) {
+    select (undef, undef, undef, 0.1);
+    $tries++;
+}
 $pid = contents ("$TMP/pid");
 ok (kill (0, $pid), ' and the PID file is correct');
 kill (15, $pid) or warn "Can't kill $pid: $!\n";
@@ -138,6 +142,10 @@ is ($status, 1, 'Backgrounding k5start -x with bad keytab fails');
 like ($err, qr/^k5start: error getting credentials: /, ' with error output');
 is ($out, '', ' and -q was added implicitly');
 if ($status == 0) {
+    while (not -s "$TMP/pid" and $tries < 100) {
+        select (undef, undef, undef, 0.1);
+        $tries++;
+    }
     $pid = contents ("$TMP/pid");
     kill (15, $pid) or warn "Can't kill $pid: $!\n";
 }
@@ -154,7 +162,7 @@ if (!defined $pid) {
         or BAIL_OUT ("can't run $K5START: $!");
 }
 $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -187,6 +195,11 @@ ok (!-f "$TMP/pid", ' and the PID file was removed');
 is ($status, 0, 'k5start -xb works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and -q was added implicitly');
+$tries = 0;
+while (not -s "$TMP/pid" and $tries < 100) {
+    select (undef, undef, undef, 0.1);
+    $tries++;
+}
 $pid = contents ("$TMP/pid");
 ok (kill (0, $pid), 'k5start -xb started');
 chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
@@ -211,7 +224,7 @@ is ($status, 0, 'Backgrounding k5start works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and output was redirected properly');
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -263,7 +276,7 @@ is ($status, 0, 'Backgrounding k5start works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and output was redirected properly');
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -308,6 +321,10 @@ unlink "$TMP/krb5cc_child", "$TMP/child-out";
 is ($status, 0, 'Backgrounding k5start works');
 like ($err, qr/^k5start: error getting credentials: /, ' with error output');
 is ($out, '', ' and output was redirected properly');
+while (not -s "$TMP/pid" and $tries < 100) {
+    select (undef, undef, undef, 0.1);
+    $tries++;
+}
 $pid = contents ("$TMP/pid");
 ok (kill (0, $pid), 'k5start is running');
 select (undef, undef, undef, 1);
@@ -315,7 +332,7 @@ ok (!-f "$TMP/child-pid", ' child did not start');
 ok (!-f "$TMP/child-out", ' and has no output');
 copy ("$DATA/test.keytab", "$TMP/test.keytab");
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -342,4 +359,5 @@ ok (!-f "$TMP/child-pid", 'Child PID file cleaned up');
 
 # Clean up.
 unlink "$TMP/krb5cc_child", "$TMP/child-out", "$TMP/test.keytab";
+unlink "$TMP/pid", "$TMP/child-pid";
 rmdir $TMP;
index c446b516b6c1b049e1a164f39f0fef511d8d8856..059498a1e46a47b4ba562c36033a6a1589ab8500 100755 (executable)
@@ -56,7 +56,7 @@ if (!defined $pid) {
         or BAIL_OUT ("can't run $KRENEW: $!");
 }
 my $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -88,7 +88,7 @@ is ($status, 0, 'Backgrounding krenew works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and -q was added implicitly');
 $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -107,7 +107,7 @@ is ($status, 0, 'Backgrounding krenew works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and -q was added implicitly');
 $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -147,7 +147,7 @@ if (!defined $pid) {
         or BAIL_OUT ("can't run $KRENEW: $!");
 }
 $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -179,7 +179,7 @@ is ($status, 0, 'krenew -xb works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and no regular output');
 $tries = 0;
-while (not -f "$TMP/pid" and $tries < 100) {
+while (not -s "$TMP/pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -205,7 +205,7 @@ is ($status, 0, 'Backgrounding krenew works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and output was redirected properly');
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -254,7 +254,7 @@ is ($status, 0, 'Backgrounding krenew works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and output was redirected properly');
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -300,7 +300,7 @@ is ($status, 0, 'Backgrounding krenew works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and output was redirected properly');
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }
@@ -344,7 +344,7 @@ is ($status, 0, 'Backgrounding krenew -s works');
 is ($err, '', ' with no error output');
 is ($out, '', ' and output was redirected properly');
 $tries = 0;
-while (not -f "$TMP/child-pid" and $tries < 100) {
+while (not -s "$TMP/child-pid" and $tries < 100) {
     select (undef, undef, undef, 0.1);
     $tries++;
 }