42
49
if (!defined $pid) {
43
50
BAIL_OUT ("can't fork: $!");
44
51
} elsif ($pid == 0) {
46
exec ($KRENEW, '-K', 30, '-p', 'pid')
52
open (STDERR, '>', "$TMP/krenew-errors")
53
or BAIL_OUT ("can't create $TMP/krenew-errors: $!");
54
exec ($KRENEW, '-K', 30, '-p', "$TMP/pid")
47
55
or BAIL_OUT ("can't run $KRENEW: $!");
50
while (not -f 'pid' and $tries < 10) {
58
while (not -f "$TMP/pid" and $tries < 10) {
51
59
select (undef, undef, undef, 0.1);
55
my $daemon = contents ('pid');
63
my $daemon = contents ("$TMP/pid");
56
64
is ($pid, $daemon, 'The right PID is written');
58
66
ok (0, 'The right PID is written');
60
68
ok (kill (0, $pid), ' and krenew is still running');
69
unlink "$TMP/krb5cc_test";
62
70
kill (14, $pid) or warn "Can't kill $pid: $!\n";
63
71
is (waitpid ($pid, 0), $pid, ' and it dies after failure to renew');
64
72
is (($? >> 8), 1, ' with non-zero exit status');
73
if (open (ERRORS, '<', "$TMP/krenew-errors")) {
74
like (scalar (<ERRORS>), qr/^krenew: error reading ticket cache: /,
75
' and the correct error message');
77
ok (0, ' and the correct error message');
79
unlink "$TMP/krenew-errors";
80
ok (!-f "$TMP/pid", ' and the PID file was removed');
66
81
kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m');
68
83
# Try again with the -b flag.
69
84
my ($out, $err, $status)
70
= command ($KRENEW, '-bK', 30, '-p', "$cwd/pid");
85
= command ($KRENEW, '-bK', 30, '-p', "$TMP/pid");
71
86
is ($status, 0, 'Backgrounding krenew works');
72
87
is ($err, '', ' with no error output');
73
88
is ($out, '', ' and -q was added implicitly');
75
while (not -f 'pid' and $tries < 10) {
90
while (not -f "$TMP/pid" and $tries < 10) {
76
91
select (undef, undef, undef, 0.1);
79
$pid = contents ('pid');
94
$pid = contents ("$TMP/pid");
80
95
ok (kill (0, $pid), ' and the PID file is correct');
81
96
kill (15, $pid) or warn "Can't kill $pid: $!\n";
82
97
select (undef, undef, undef, 0.5);
83
98
ok (!kill (0, $pid), ' and it dies after SIGTERM');
99
ok (!-f "$TMP/pid", ' and the PID file was removed');
86
101
# Now try with -i. In this case, krenew should keep running even if the
87
102
# ticket cache disappears and be able to start refreshing it again when it
89
($out, $err, $status) = command ($KRENEW, '-biK', 30, '-p', "$cwd/pid");
104
($out, $err, $status) = command ($KRENEW, '-biK', 30, '-p', "$TMP/pid");
90
105
is ($status, 0, 'Backgrounding krenew works');
91
106
is ($err, '', ' with no error output');
92
107
is ($out, '', ' and -q was added implicitly');
94
while (not -f 'pid' and $tries < 10) {
109
while (not -f "$TMP/pid" and $tries < 10) {
95
110
select (undef, undef, undef, 0.1);
98
$pid = contents ('pid');
113
$pid = contents ("$TMP/pid");
99
114
ok (kill (0, $pid), ' and the PID file is correct');
100
unlink 'krb5cc_test';
115
unlink "$TMP/krb5cc_test";
101
116
kill (14, $pid) or warn "Can't kill $pid: $!\n";
102
117
select (undef, undef, undef, 0.5);
103
118
ok (kill (0, $pid), ' and it keeps running after failure to renew');
104
119
kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m');
105
my $time = (stat 'krb5cc_test')[9];
120
my $time = (stat "$TMP/krb5cc_test")[9];
106
121
while (time == $time) {
107
122
select (undef, undef, undef, 0.1);
109
is ($time, (stat 'krb5cc_test')[9], 'Cache has not been touched');
124
is ($time, (stat "$TMP/krb5cc_test")[9], 'Cache has not been touched');
110
125
kill (14, $pid) or warn "Can't kill $pid: $!\n";
112
while ($time >= (stat 'krb5cc_test')[9] && $tries < 10) {
127
while ($time >= (stat "$TMP/krb5cc_test")[9] && $tries < 10) {
113
128
select (undef, undef, undef, 0.5);
116
ok ($time < (stat 'krb5cc_test')[9], ' and is updated after SIGALRM');
131
ok ($time < (stat "$TMP/krb5cc_test")[9], ' and is updated after SIGALRM');
117
132
kill (15, $pid) or warn "Can't kill $pid: $!\n";
118
133
select (undef, undef, undef, 0.5);
119
134
ok (!kill (0, $pid), ' and it dies after SIGTERM');
135
ok (!-f "$TMP/pid", ' and the PID file was removed');
137
# Check that krenew keeps running if the ticket cache directory is not
141
BAIL_OUT ("can't fork: $!");
142
} elsif ($pid == 0) {
143
open (STDERR, '>', "$TMP/krenew-errors")
144
or BAIL_OUT ("can't create $TMP/krenew-errors: $!");
145
exec ($KRENEW, '-K', 30, '-p', "$TMP/pid")
146
or BAIL_OUT ("can't run $KRENEW: $!");
149
while (not -f "$TMP/pid" and $tries < 10) {
150
select (undef, undef, undef, 0.1);
153
$pid = contents ("$TMP/pid");
154
ok (kill (0, $pid), 'krenew -K 30 started');
155
chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
156
kill (14, $pid) or warn "Can't kill $pid: $!\n";
157
select (undef, undef, undef, 0.5);
158
ok (kill (0, $pid), ' and it keeps running on a non-writeable cache');
159
chmod 0755, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
160
if (open (ERRORS, '<', "$TMP/krenew-errors")) {
161
like (scalar (<ERRORS>), qr/^krenew: error reinitializing cache: /,
162
' and the correct error message');
164
ok (0, ' and the correct error message');
166
unlink "$TMP/krenew-errors";
167
kill (15, $pid) or warn "Can't kill $pid: $!\n";
168
is (waitpid ($pid, 0), $pid, ' and it dies on SIGTERM');
169
ok (!-f "$TMP/pid", ' and the PID file was removed');
171
# If we do that again with -x, krenew should exit.
172
($out, $err, $status) = command ($KRENEW, '-xbK', 30, '-p', "$TMP/pid");
173
is ($status, 0, 'krenew -xb works');
174
is ($err, '', ' with no error output');
175
is ($out, '', ' and no regular output');
176
$pid = contents ("$TMP/pid");
177
ok (kill (0, $pid), 'krenew -xb started');
178
chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
179
kill (14, $pid) or warn "Can't kill $pid: $!\n";
180
select (undef, undef, undef, 0.5);
181
ok (!kill (0, $pid), ' and it exits on a non-writeable cache');
182
chmod 0755, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
122
185
# Now, run a command in the background.
123
186
unlink 'child-out';
124
187
($out, $err, $status)
125
= command ($KRENEW, '-bK', 30, '-p', "$cwd/pid", '-c', "$cwd/child-pid",
126
'--', "$ENV{SOURCE}/data/command", "$cwd/child-out");
127
is ($status, 0, 'Backgrounding krenew works');
128
is ($err, '', ' with no error output');
129
is ($out, '', ' and output was redirected properly');
131
while (not -f 'child-pid' and $tries < 10) {
132
select (undef, undef, undef, 0.1);
135
$pid = contents ('pid');
136
ok (kill (0, $pid), 'krenew is running');
137
$child = contents ('child-pid');
138
ok (kill (0, $child), 'The child process is running');
140
while (not -S 'child-out' and $tries < 10) {
141
select (undef, undef, undef, 0.1);
144
kill (1, $pid) or warn "Cannot send HUP to $pid: $!\n";
145
select (undef, undef, undef, 0.1);
146
kill (15, $pid) or warn "Cannot send TERM to $pid: $!\n";
147
select (undef, undef, undef, 0.1);
148
ok (!kill (0, $pid), 'krenew is no longer running');
149
ok (!kill (0, $child), 'The child process is no longer running');
150
open (OUT, '<', 'child-out') or BAIL_OUT ("cannot open child-out: $!");
153
is ($child, $daemon, 'Child PID is correct');
155
is ($dir, "/\n", 'Child working directory is /');
157
like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
158
is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was recorded');
159
is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
160
ok (eof OUT, 'No more child output written');
162
ok (! -f $cache, 'New child cache removed');
163
unlink 'krb5cc_test', 'pid', 'child-pid', 'child-out';
188
= command ($KRENEW, '-bK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
189
'--', "$ENV{SOURCE}/data/command", "$TMP/child-out");
190
is ($status, 0, 'Backgrounding krenew works');
191
is ($err, '', ' with no error output');
192
is ($out, '', ' and output was redirected properly');
194
while (not -f "$TMP/child-pid" and $tries < 10) {
195
select (undef, undef, undef, 0.1);
198
$pid = contents ("$TMP/pid");
199
ok (kill (0, $pid), 'krenew is running');
200
$child = contents ("$TMP/child-pid");
201
ok (kill (0, $child), 'The child process is running');
203
while (not -S "$TMP/child-out" and $tries < 10) {
204
select (undef, undef, undef, 0.1);
207
kill (1, $pid) or warn "Cannot send HUP to parent $pid: $!\n";
208
select (undef, undef, undef, 0.1);
209
kill (2, $pid) or warn "Cannot send INT to parent $pid: $!\n";
210
select (undef, undef, undef, 0.1);
211
kill (15, $pid) or warn "Cannot send TERM to parent $pid: $!\n";
212
select (undef, undef, undef, 0.2);
213
ok (!kill (0, $pid), 'krenew is no longer running');
214
ok (!kill (0, $child), 'The child process is no longer running');
215
open (OUT, '<', "$TMP/child-out")
216
or BAIL_OUT ("cannot open $TMP/child-out: $!");
217
is (scalar (<OUT>), "$child\n", 'Child PID is correct');
218
is (scalar (<OUT>), "/\n", 'Child working directory is /');
219
my $cache = scalar <OUT>;
220
like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
221
is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was recorded');
222
is (scalar (<OUT>), "got SIGINT\n", 'SIGINT was recorded');
223
is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
224
ok (eof OUT, 'No more child output written');
226
ok (! -f $cache, 'New child cache removed');
227
ok (!-f "$TMP/pid", ' and the PID file was removed');
228
ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
229
unlink "$TMP/child-out";
231
# One more time to test propagation of QUIT signals.
232
($out, $err, $status)
233
= command ($KRENEW, '-bK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
234
'--', "$ENV{SOURCE}/data/command", "$TMP/child-out");
235
is ($status, 0, 'Backgrounding krenew works');
236
is ($err, '', ' with no error output');
237
is ($out, '', ' and output was redirected properly');
239
while (not -f "$TMP/child-pid" and $tries < 10) {
240
select (undef, undef, undef, 0.1);
243
$pid = contents ("$TMP/pid");
244
ok (kill (0, $pid), 'krenew is running');
245
$child = contents ("$TMP/child-pid");
246
ok (kill (0, $child), 'The child process is running');
248
while (not -S "$TMP/child-out" and $tries < 10) {
249
select (undef, undef, undef, 0.1);
252
kill (3, $pid) or warn "Cannot send QUIT to parent $pid: $!\n";
253
select (undef, undef, undef, 0.2);
254
ok (!kill (0, $pid), 'krenew is no longer running');
255
ok (!kill (0, $child), 'The child process is no longer running');
256
open (OUT, '<', "$TMP/child-out")
257
or BAIL_OUT ("cannot open $TMP/child-out: $!");
258
is (scalar (<OUT>), "$child\n", 'Child PID is correct');
259
is (scalar (<OUT>), "/\n", 'Child working directory is /');
260
$cache = scalar <OUT>;
261
like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
262
is (scalar (<OUT>), "got SIGQUIT\n", 'SIGQUIT was recorded');
263
ok (eof OUT, 'No more child output written');
266
ok (! -f $cache, 'New child cache removed');
267
ok (!-f "$TMP/pid", ' and the PID file was removed');
268
ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
269
unlink "$TMP/child-out";
271
# Normally, if we are running a command and krenew has to exit because it
272
# can't renew the ticket cache any more, it should exit and leave the command
274
($out, $err, $status)
275
= command ($KRENEW, '-bK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
276
'--', "$ENV{SOURCE}/data/command", "$TMP/child-out");
277
is ($status, 0, 'Backgrounding krenew works');
278
is ($err, '', ' with no error output');
279
is ($out, '', ' and output was redirected properly');
281
while (not -f "$TMP/child-pid" and $tries < 10) {
282
select (undef, undef, undef, 0.1);
285
$pid = contents ("$TMP/pid");
286
ok (kill (0, $pid), 'krenew is running');
287
$child = contents ("$TMP/child-pid");
288
ok (kill (0, $child), 'The child process is running');
290
while (not -S "$TMP/child-out" and $tries < 10) {
291
select (undef, undef, undef, 0.1);
294
select (undef, undef, undef, 0.2);
295
open (OUT, '<', "$TMP/child-out")
296
or BAIL_OUT ("cannot open $TMP/child-out: $!");
297
is (scalar (<OUT>), "$child\n", 'Child PID is correct');
298
is (scalar (<OUT>), "/\n", 'Child working directory is /');
299
$cache = scalar <OUT>;
301
like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
303
kill (14, $pid) or warn "Can't kill $pid: $!\n";
304
select (undef, undef, undef, 0.2);
305
ok (!kill (0, $pid), 'krenew dies after failure to renew');
306
ok (kill (0, $child), ' and the child process is still running');
307
kill (15, $child) or warn "Can't kill child $child: $!\n";
308
select (undef, undef, undef, 0.2);
309
ok (!kill (0, $child), 'The child process is no longer running');
310
is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
311
ok (eof OUT, 'No more child output written');
313
ok (!-f "$TMP/pid", ' and the PID file was removed');
314
ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
315
unlink "$TMP/child-out";
317
# If run with -s, krenew should instead kill the child process with HUP on
318
# failure to renew the ticket.
319
($out, $err, $status)
320
= command ($KRENEW, '-bsK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
321
'--', "$ENV{SOURCE}/data/command", "$TMP/child-out");
322
is ($status, 0, 'Backgrounding krenew -s works');
323
is ($err, '', ' with no error output');
324
is ($out, '', ' and output was redirected properly');
326
while (not -f "$TMP/child-pid" and $tries < 10) {
327
select (undef, undef, undef, 0.1);
330
$pid = contents ("$TMP/pid");
331
ok (kill (0, $pid), 'krenew is running');
332
$child = contents ("$TMP/child-pid");
333
ok (kill (0, $child), 'The child process is running');
335
while (not -S "$TMP/child-out" and $tries < 10) {
336
select (undef, undef, undef, 0.1);
339
select (undef, undef, undef, 0.2);
340
open (OUT, '<', "$TMP/child-out")
341
or BAIL_OUT ("cannot open $TMP/child-out: $!");
342
is (scalar (<OUT>), "$child\n", 'Child PID is correct');
343
is (scalar (<OUT>), "/\n", 'Child working directory is /');
344
$cache = scalar <OUT>;
346
like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
348
kill (14, $pid) or warn "Can't kill $pid: $!\n";
349
select (undef, undef, undef, 0.2);
350
ok (!kill (0, $pid), 'krenew dies after failure to renew');
351
ok (kill (0, $child), ' and the child process is still running');
352
kill (15, $child) or warn "Can't kill child $child: $!\n";
353
select (undef, undef, undef, 0.2);
354
ok (!kill (0, $child), 'The child process is no longer running');
355
is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was recorded');
356
is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
357
ok (eof OUT, 'No more child output written');
359
ok (!-f "$TMP/pid", ' and the PID file was removed');
360
ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
361
unlink "$TMP/child-out";
364
unlink "$TMP/krb5cc_test", "$TMP/child-out";