blib/lib/Sys/Prctl.pm

filter: Statementstmt 67.1% 45 / 67 Branchbran 23.6% 9 / 38 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 52.1% 73 / 140 CC 9 SCAR 12.6
CC
9
Cov
50%
prctl
6.6
prctl_name
4.2
new
2.1
CRAP 3.5
↑ Summary
1 package Sys::Prctl;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '1.02';
6  
7 # TODO: FreeBSD support "libc.call('setproctitle', 'hippy\0');"
8  
9 =head1 NAME
10  
11 Sys::Prctl - Give access to prctl system call from Perl
12  
13 =head1 DESCRIPTION
14  
15 This is simple module that wraps the prctl system call. Currently only the
16 PR_SET_NAME and PR_GET_NAME are implemented.
17  
18 This can be use to change the process name as reported by "ps -A" and be
19 killable will killall.
20  
21 =head1 SYNOPSIS
22
23 use Sys::Prctl(prctl_name);
24
25 #
26 # Use with functions
27 #
28  
29 # Process name is now "My long process name"
30 my $oldname = prctl_name();
31 prctl_name("My long process name");
32  
33 #
34 # Use as an object
35 #
36
37 my $process = new Sys::Prctl();
38  
39 # Process name is now "Short name"
40 my $oldname = $process->name();
41 $process->name('Short name');
42  
43 #
44 # Real world use
45 #
46  
47 # instead of "perl helloworld.pl"
48 $0 = "helloworld"
49 prctl_name("helloworld");
50  
51 print "Hello World\n";
52 sleep 100;
53  
54 # Process can now be killed with "killall helloworld"
55  
56 =head1 METHODS
57  
58 =over
59  
60 =cut
61  
62 2 ▶use POSIX qw(uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 ▶use base "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 2 our @EXPORT_OK = qw(prctl_name prctl);
68 2 our %EXPORT_TAGS = ();
69  
70 #
71 # Detect what os we are running and set the correct SYS_* entries
72 #
73  
74 # Defined in linux/sched.h
75 2 our $TASK_COMM_LEN = 16;
76  
77 2 our $SYS_prctl;
78 2 our $SYS_PR_SET_NAME = 15;
79 2 our $SYS_PR_GET_NAME = 16;
80  
81 2 ▶if($^O eq 'linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux") 2 0 2
82 2 my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
83
84 # if we're running on an x86_64 kernel, but a 32-bit process,
85 # we need to use the i386 syscall numbers.
86 2 ▶ if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($machine eq "x86_64" and $Sys::Prctl::Config{"ptrsize"} == 4) 0 2 2
Condition33%1/3
$machine eq "x86_64" and $Sys::Prctl::Config{"ptrsize"} == 4
!ll&&!rl&&r
020
MC/DC0%0/2
$machine eq "x86_64" and $Sys::Prctl::Config{"ptrsize"} == 4
$machine eq "x86_64" $Sys::Prctl::Config{"ptrsize"} == 4
Truth table33%1/3
A and B
A= $machine eq "x86_64"B= $Sys::Prctl::Config{"ptrsize"} == 4
ABresult
0X0
100
111
87 0 $machine = "i386";
88 }
89  
90 2 ▶ if ($machine =~ /^i[3456]86$/) {
Branch: 12 branches
BranchTrueFalseTotal
if ($machine =~ /^i[3456]86$/) { } 0 2 2
elsif ($machine =~ /^blackfin|cris|frv|h8300|m32r|m68k|microblaze|mn10300|sh|s390|parisc$/) { } 0 2 2
elsif ($machine eq 'x86_64') { } 2 0 2
elsif ($machine eq 'sparc64') { } 0 0 0
elsif ($machine eq 'ppc') { } 0 0 0
elsif ($machine eq 'ia64') { } 0 0 0
elsif ($machine eq 'alpha') { } 0 0 0
elsif ($machine eq 'arm') { } 0 0 0
elsif ($machine eq 'avr32') { } 0 0 0
elsif ($machine eq 'mips') { } 0 0 0
elsif ($machine eq 'mips64') { } 0 0 0
elsif ($machine eq 'xtensa') { } 0 0 0
91 0 $SYS_prctl = 172;
92
93 } elsif ($machine =~ /^blackfin|cris|frv|h8300|m32r|m68k|microblaze|mn10300|sh|s390|parisc$/) {
94 0 $SYS_prctl = 172;
95
96 } elsif ($machine eq "x86_64") {
97 2 $SYS_prctl = 157;
98
99 } elsif ($machine eq "sparc64") {
100 0 $SYS_prctl = 147;
101
102 } elsif ($machine eq "ppc") {
103 0 $SYS_prctl = 171;
104  
105 } elsif ($machine eq "ia64") {
106 0 $SYS_prctl = 1170;
107  
108 } elsif ($machine eq "alpha") {
109 0 $SYS_prctl = 348;
110
111 } elsif ($machine eq "arm") {
112 0 $SYS_prctl = 0x900000 + 172;
113
114 } elsif ($machine eq "avr32") {
115 0 $SYS_prctl = 148;
116
117 } elsif ($machine eq "mips") { # 32bit
118 0 $SYS_prctl = 4000 + 192;
119
120 } elsif ($machine eq "mips64") { # 64bit
121 0 $SYS_prctl = 5000 + 153;
122
123 } elsif ($machine eq "xtensa") {
124 0 $SYS_prctl = 130;
125  
126 } else {
127 0 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph _h2ph_pre.ph
128 sys/syscall.ph>};
129 0 my $rv = eval { require 'syscall.ph'; 1 } ## no critic
130 0 ▶ or eval { require 'sys/syscall.ph'; 1 }; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
my $rv = eval { do { require "syscall.ph"; 1 } } or eval { do { require "sys/syscall.ph"; '???' } } 0 0 0
131 0 ▶ $SYS_prctl = eval { &SYS_prctl; }
Branch: 1 branch
BranchTrueFalseTotal
$Sys::Prctl::SYS_prctl = eval { do { &SYS_prctl } } or die "Could not find prctl for this system" 0 0 0
132 or die "Could not find prctl for this system";
133 }
134 }
135  
136 =item new()
137  
138 Creates a new Sys::Prctl object.
139  
140 =cut
141  
142 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
143 1 my ($class, %opts) = @_;
144  
145 1 my %self = (
146
147 );
148
149 1 ▶ return bless \%self, (ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
150 }
151  
152 =item name([$string])
153  
154 Set or get the process name.
155  
156 =cut
157  
158 3 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
159 3 my ($self, $str) = @_;
160 3 return prctl_name($str);
161 }
162  
163 =item prctl_name([$string])
164  
165 Set or get the process name.
166  
167 $string can only be 15 chars long on Linux.
168  
169 Returns undef on error.
170  
171 =cut
172  
173 7 ▶sub prctl_name {
Subroutine prctl_name: called CC=4 SCAR=14.3
Pod documented
174 7 my ($str) = @_;
175
176 7 ▶ if(defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 3 4 7
177 3 my $rv = prctl($SYS_PR_SET_NAME, $str);
178 3 ▶ if($rv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 0) { } 3 0 3
179 3 return 1;
180 } else {
181 0 return;
182 }
183  
184 } else {
185 4 $str = "\x00" x ($TASK_COMM_LEN + 1); # allocate $str
186 4 my $ptr = unpack( 'L', pack( 'P', $str ) );
187 4 my $rv = prctl($SYS_PR_GET_NAME, $ptr);
188 4 ▶ if($rv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 0) { } 0 4 4
189 0 return substr($str, 0, index($str, "\x00"));
190 } else {
191 4 return;
192 }
193 }
194 }
195  
196 =item prctl($option, $arg2, $arg3, $arg4, $arg5)
197  
198 Direct wrapper for prctl call
199  
200 =cut
201  
202 7 ▶sub prctl {
Subroutine prctl: called CC=5 SCAR=18.9
Pod documented
203 7 my ($option, $arg2, $arg3, $arg4, $arg5) = @_;
204 7 ▶ syscall($SYS_prctl, $option,
Condition50%4/8
$arg2 || 0
l!l
70
$arg3 || 0
l!l
07
$arg4 || 0
l!l
07
$arg5 || 0
l!l
07
MC/DC0%0/1
$arg2 || 0
$arg2
MC/DC0%0/1
$arg3 || 0
$arg3
MC/DC0%0/1
$arg4 || 0
$arg4
MC/DC0%0/1
$arg5 || 0
$arg5
Truth table50%1/2
A
A= $arg2
Aresult
11
00
Truth table50%1/2
A
A= $arg3
Aresult
11
00
Truth table50%1/2
A
A= $arg4
Aresult
11
00
Truth table50%1/2
A
A= $arg5
Aresult
11
00
205 ($arg2 or 0), ($arg3 or 0), ($arg4 or 0), ($arg5 or 0));
206 }
207  
208 =back
209  
210 =head1 NOTES
211  
212 Currently only 32bit Linux has been tested. So test reports and patches are
213 wellcome.
214  
215 =head1 AUTHOR
216  
217 Troels Liebe Bentsen <tlb@rapanden.dk>
218  
219 =head1 COPYRIGHT
220  
221 Copyright(C) 2005-2007 Troels Liebe Bentsen
222  
223 This library is free software; you can redistribute it and/or modify
224 it under the same terms as Perl itself.
225  
226 =cut
227  
228 2 1;
↑ Summary