-
Notifications
You must be signed in to change notification settings - Fork 7
/
BBBikeProcUtil.pm
76 lines (65 loc) · 1.25 KB
/
BBBikeProcUtil.pm
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
# -*- perl -*-
#
# Author: Slaven Rezic
#
# Copyright (C) 2013 Slaven Rezic. All rights reserved.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: [email protected]
# WWW: http://www.rezic.de/eserte/
#
package BBBikeProcUtil;
use strict;
use vars qw($VERSION);
$VERSION = '0.02';
use Exporter 'import';
use vars qw(@EXPORT_OK);
@EXPORT_OK = qw(double_fork double_forked_exec);
sub double_fork (&) {
my($cb) = @_;
my $pid = fork;
if (!defined $pid) {
die "Fork failed: $!";
}
if ($pid == 0) {
my $pid2 = fork;
if (!defined $pid2) {
_hard_die("Inner fork failed: $!");
}
if ($pid2 == 0) {
eval {
$cb->();
};
if ($@) {
_hard_die("Failure while running callback: $@");
} else {
_hard_exit(0);
}
}
_hard_exit(0);
}
waitpid $pid, 0;
}
sub double_forked_exec (@) {
my(@cmd_and_args) = @_;
double_fork {
{ exec @cmd_and_args }
_hard_die("@cmd_and_args failed: $!");
};
}
sub _hard_die {
my $msg = shift;
warn $msg;
_hard_exit(1);
}
sub _hard_exit {
my $code = shift;
if ($^O ne 'MSWin32' && eval { require POSIX; 1 }) {
POSIX::_exit($code);
} else {
CORE::exit($code);
}
}
1;
__END__