lib/Linux/FD/Pid.xs

filter: Statementstmt 39.0% 25 / 64 Branchbran 23.8% 10 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 33.0% 35 / 106 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Linux/FD/Pid.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <signal.h>
7 #include <linux/wait.h>
8 #include <sys/syscall.h>
9 #include <unistd.h>
10  
11 #define die_sys(format) Perl_croak(aTHX_ format, strerror(errno))
12  
13 10 static SV* S_io_fdopen(pTHX_ int fd, const char* classname, char type) {
14 10 PerlIO* pio = PerlIO_fdopen(fd, "r");
15 10 ▶ GV* gv = newGVgen(classname ? classname : "Linux::FD::Pid");
Branch: 1 branch
BranchTrueFalseTotal
GV* gv = newGVgen(classname ? classname : "Linux::FD::Pid"); 10 0 10
16 10 SV* ret = newRV_inc((SV*)gv);
17 10 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
18 10 ▶ IO* io = GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
IO* io = GvIOn(gv); 10 0 10
IO* io = GvIOn(gv); 0 10 10
IO* io = GvIOn(gv); 0 0 0
IO* io = GvIOn(gv); 10 0 10
IO* io = GvIOn(gv); 0 10 10
19 10 IoTYPE(io) = type;
20 10 IoIFP(io) = pio;
21 10 IoOFP(io) = pio;
22 10 ▶ if (classname) {
Branch: 1 branch
BranchTrueFalseTotal
if (classname) { 10 0 10
23 10 HV* stash = gv_stashpv(classname, FALSE);
24 10 sv_bless(ret, stash);
25 }
26 10 return ret;
27 }
28 #define io_fdopen(fd, classname, type) S_io_fdopen(aTHX_ fd, classname, type)
29  
30 0 static char S_get_flags(pTHX_ int fd) {
31 struct stat info;
32 0 ▶ if (fstat(fd, &info) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fd, &info) != -1) { 0 0 0
33 0 ▶ if (S_ISSOCK(info.st_mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISSOCK(info.st_mode)) 0 0 0
34 0 return 's';
35 0 ▶ else if (S_ISFIFO(info.st_mode))
Branch: 1 branch
BranchTrueFalseTotal
else if (S_ISFIFO(info.st_mode)) 0 0 0
36 0 return '|';
37 }
38  
39 0 int flags = fcntl(fd, F_GETFL);
40 0 ▶ if (flags & O_APPEND)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & O_APPEND) 0 0 0
41 0 return 'a';
42 0 switch (flags & 3) {
43 0 case O_RDONLY:
44 0 return '<';
45 0 case O_WRONLY:
46 0 return '>';
47 0 case O_RDWR:
48 0 return '+';
49 0 default:
50 0 close(fd);
51 0 Perl_croak(aTHX_ "Unknown mode on descriptor");
52 }
53 }
54 #define get_flags(fd) S_get_flags(aTHX_ fd)
55  
56 typedef struct { const char* key; unsigned long value; } map[];
57  
58 #define PIDFD_NONBLOCK O_NONBLOCK
59 static map pid_flags = {
60 { "non-blocking", PIDFD_NONBLOCK },
61 };
62  
63 0 static UV S_get_pid_flag(pTHX_ SV* flag_name) {
64 int i;
65 0 ▶ for (i = 0; i < sizeof pid_flags / sizeof *pid_flags; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof pid_flags / sizeof *pid_flags; ++i) 0 0 0
66 0 ▶ if (strEQ(SvPV_nolen(flag_name), pid_flags[i].key))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(SvPV_nolen(flag_name), pid_flags[i].key)) 0 0 0
67 0 return pid_flags[i].value;
68 0 Perl_croak(aTHX_ "No such flag '%s' known", SvPV_nolen(flag_name));
69 }
70 #define get_pid_flag(name) S_get_pid_flag(aTHX_ name)
71  
72 #define get_fd(self) PerlIO_fileno(IoOFP(sv_2io(SvRV(self))))
73  
74 typedef int signo_t;
75  
76 MODULE = Linux::FD::Pid PACKAGE = Linux::FD::Pid
77  
78 PROTOTYPES: DISABLE
79  
80 TYPEMAP: <<END
81 signo_t T_SIGNO
82 END
83  
84 SV* new(const char* classname, int pid, ...)
85 PREINIT:
86 int pidfd;
87 10 int i, flags = 0;
88 CODE:
89 10 ▶ for (i = 2; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) 0 10 10
90 0 flags |= get_pid_flag(ST(i));
91 10 pidfd = syscall(__NR_pidfd_open, pid, 0);
92 10 ▶ if (pidfd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pidfd < 0) 0 10 10
93 0 die_sys("Couldn't open pidfd: %s");
94 10 RETVAL = io_fdopen(pidfd, classname, '<');
95 OUTPUT:
96 RETVAL
97  
98 void send(SV* file_handle, signo_t signal)
99 PREINIT:
100 int fd, ret;
101 CODE:
102 1 fd = get_fd(file_handle);
103 1 ret = syscall(__NR_pidfd_send_signal, fd, signal, NULL, 0);
104 1 ▶ if (ret < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret < 0) 0 1 1
105 0 die_sys("Couldn't send signal: %s");
106  
107 int wait(SV* file_handle, int flags = WEXITED)
108 PREINIT:
109 int fd, wait_result;
110 siginfo_t info;
111 CODE:
112 0 fd = get_fd(file_handle);
113 0 wait_result = waitid(P_PIDFD, fd, &info, flags);
114 0 ▶ if (wait_result != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (wait_result != 0) { 0 0 0
115 0 ▶ if (errno == EAGAIN)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EAGAIN) 0 0 0
116 0 XSRETURN_UNDEF;
117 else
118 0 die_sys("Can't wait pid: %s");
119 }
120 0 ▶ if (info.si_signo == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (info.si_signo == 0) 0 0 0
121 0 XSRETURN_UNDEF;
122 0 ▶ if (info.si_code == CLD_EXITED)
Branch: 1 branch
BranchTrueFalseTotal
if (info.si_code == CLD_EXITED) 0 0 0
123 0 RETVAL = info.si_status << 8;
124 else
125 0 RETVAL = info.si_status;
126 OUTPUT:
127 RETVAL
128  
129 SV* get_handle(SV* file_handle, int fd)
130 PREINIT:
131 int pidfd, newfd;
132 CODE:
133 1 pidfd = get_fd(file_handle);
134 1 newfd = syscall(__NR_pidfd_getfd, pidfd, fd, 0);
135 1 ▶ if (newfd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (newfd < 0) 1 0 1
136 1 die_sys("Can't get file descriptor: %s");
137 0 RETVAL = io_fdopen(newfd, NULL, get_flags(newfd));
138 OUTPUT:
139 RETVAL
↑ Summary blib/lib/Linux/FD/Pid.pm »