File: WaitStat.pm

package info (click to toggle)
libproc-waitstat-perl 1.00-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 48 kB
  • ctags: 11
  • sloc: perl: 109; makefile: 56
file content (178 lines) | stat: -rw-r--r-- 3,954 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# $Id: WaitStat.pm,v 1.3 1999-10-21 12:39:43-04 roderick Exp $
#
# Copyright (c) 1997 Roderick Schertler.  All rights reserved.  This
# program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

=head1 NAME

Proc::WaitStat - Interpret and act on wait() status values

=head1 SYNOPSIS

    $description = waitstat $?;
    exit waitstat_reuse $?;
    waitstat_die $?, 'program-name';
    close_die COMMAND, 'program-name';

=head1 DESCRIPTION

This module contains functions for interpreting and acting on wait
status values.

Nothing is exported by default.

=over

=cut

package Proc::WaitStat;

use 5.003_98;	# piped close errno resetting
use strict;
use vars	qw($VERSION @ISA @EXPORT_OK);

use Carp	qw(croak);
use Exporter	  ();
use IPC::Signal	qw(sig_name);
use POSIX	qw(:sys_wait_h);

$VERSION	= '1.00';
@ISA		= qw(Exporter);
@EXPORT_OK	= qw(waitstat waitstat_reuse waitstat_die close_die);

=item B<waitstat> I<wait-status>

Returns a string representation of wait() status value I<wait-status>.
Values returned are like C<"0"> and C<"64"> and C<"killed (SIGHUP)">.

This function is prototyped to take a single scalar argument.

=cut

sub waitstat ($) {
    my $status = shift;

    if (WIFEXITED $status) {
	WEXITSTATUS $status
    }
    elsif (WIFSIGNALED $status) {
	# XXX WCOREDUMP
	'killed (SIG' . sig_name(WTERMSIG $status) . ')'
    }
    elsif (WIFSTOPPED $status) {
	'stopped (SIG' . sig_name(WSTOPSIG $status) . ')'
    }
    # XXX WIFCONTINUED
    else {
	"invalid wait status $status"
    }
}

=item B<waitstat_reuse> I<wait-status>

Turn I<wait-status> into a value which can be passed to B<exit>, converted
in the same manner the shell uses.  If I<wait-status> indicates a normal
exit, return the exit value.  If I<wait-status> instead indicates death by
signal, return 128 plus the signal number.

This function is prototyped to take a single scalar argument.

=cut

sub waitstat_reuse ($) {
    my $status = shift;

    if (WIFEXITED $status) {
	WEXITSTATUS $status
    }
    elsif (WIFSIGNALED $status) {
	128 + WTERMSIG $status
    }
    elsif (WIFSTOPPED $status) {
	128 + WSTOPSIG $status
    }
    else {
	croak "Invalid wait status $status";
    }
}

=item B<waitstat_die> I<wait-status> I<program-name>

die() if I<wait-status> is non-zero (mentioning I<program-name> as the
source of the error).

This function is prototyped to take two scalar arguments.

=cut

sub waitstat_die ($$) {
    my ($status, $program) = @_;
    croak "Non-zero exit (" . waitstat($status) .
	    ") from $program"
	if $status;
}

=item B<close_die> I<filehandle> I<name>

Close I<filehandle>, if that fails die() with an appropriate message
which refers to I<name>.  This handles failed closings of both programs
and files properly.

This function is prototyped to take a filehandle (actually, a glob ref)
and a scalar.

=cut

sub close_die (*$) {
    my ($fh, $name) = @_;

    unless (ref $fh || ref \$fh eq 'GLOB') {
	require Symbol;
	$fh = Symbol::qualify_to_ref($fh, caller);
    }

    unless (close $fh) {
	croak "Error closing $name: ",
		$!+0 ? "$!" : 'non-zero exit (' . waitstat($?) . ')';
    }
}

1

__END__

=back

=head1 EXAMPLES

    close SENDMAIL;
    exit if $? == 0;
    log "sendmail failure: ", waitstat $?;
    exit EX_TEMPFAIL;

    $pid == waitpid $pid, 0 or croak "Failed to reap $pid: $!";
    exit waitstat_reuse $?;

    $output = `some-program -with args`;
    waitstat_die $?, 'some-program';
    print "Output from some-process:\n", $output;

    open PROGRAM, '| post-processor' or die "Can't fork: $!";
    while (<IN>) {
    	print PROGRAM pre_process $_
	    or die "Error writing to post-processor: $!";
    }
    # This handles both flush failures at close time and a non-zero exit
    # from the subprocess.
    close_die PROGRAM, 'post-processor';

=head1 AUTHOR

Roderick Schertler <F<roderick@argon.org>>

=head1 SEE ALSO

perl(1), IPC::Signal(3pm).

=cut