Resource.xs

filter: Statementstmt 66.0% 111 / 168 Branchbran 50.8% 57 / 112 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.0% 168 / 280 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/BSD/Resource.pm ↑ Summary
1 /*
2 * Copyright (c) 1995-2015 Jarkko Hietaniemi. All rights reserved.
3 * This program is free software; you can redistribute it and/or
4 * modify it under the same terms as Perl itself.
5 *
6 * Resource.xs
7 *
8 */
9  
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 #define PERL_NO_GET_CONTEXT
14 #include "EXTERN.h"
15 #include "perl.h"
16 #include "XSUB.h"
17 #include "ppport.h"
18 #ifdef WIN32
19 # include <time.h>
20 #else
21 # include <sys/time.h>
22 #endif
23 #ifdef HAS_SELECT
24 # ifdef I_SYS_SELECT
25 # include <sys/select.h>
26 # endif
27 #endif
28 #ifdef __cplusplus
29 }
30 #endif
31  
32 #if defined(__hpux) && !defined(_INCLUDE_XOPEN_SOURCE_EXTENDED)
33 #define _INCLUDE_XOPEN_SOURCE_EXTENDED
34 #endif
35  
36 /* If this fails your vendor has failed you and Perl cannot help. */
37 #include <sys/resource.h>
38  
39 #if defined(__sun__) && defined(__svr4__) && !defined(SOLARIS_NO_PROCFS)
40 # define SOLARIS
41 # define SOLARIS_PROCFS
42 # ifdef I_SYS_RUSAGE
43 # include <sys/rusage.h>
44 /* Some old Solarises have no RUSAGE_* defined in <sys/resource.h>.
45 * There is <sys/rusage.h> which has but this file is very non-standard.
46 * More the fun, the file itself warns will not be there for long. */
47 # define part_of_sec tv_nsec
48 # endif
49 /* Solaris uses timerstruc_t in struct rusage. According to the <sys/time.h>
50 * in old Solarises tv_nsec in the timerstruc_t is nanoseconds (and the name
51 * also supports that theory) BUT getrusage() seems after all to tick
52 * microseconds, not nano. */
53 # define part_in_sec 0.000001
54 #
55 /* Newer Solarises (5.5 onwards) have much better support for rusage-kinda
56 * things via the proc interface. */
57 # define _STRUCTURED_PROC 1
58 # include <sys/procfs.h>
59 # include <fcntl.h>
60  
61 # ifdef PIOCUSAGE
62 # undef SOLARIS_STRUCTURED_PROC
63 # else
64 # define SOLARIS_STRUCTURED_PROC
65 # endif
66  
67 # ifdef SOLARIS_STRUCTURED_PROC
68 # define Struct_psinfo struct psinfo
69 # define Struct_pstatus struct pstatus
70 # else
71 # define Struct_psinfo struct prpsinfo
72 # define Struct_pstatus struct prstatus
73 # endif
74 #endif
75  
76 #ifdef SOLARIS_NO_PROCFS
77 # define SOLARIS
78 # undef SOLARIS_PROCFS
79 # define TRY_GETRUSAGE_SYS_SYSCALL
80 #endif
81  
82 #ifndef part_of_sec
83 #define part_of_sec tv_usec
84 #define part_in_sec 0.000001
85 #endif
86  
87 #define IDM ((double)part_in_sec)
88 #define TV2DS(tv) ((double)tv.tv_sec+(double)tv.part_of_sec*part_in_sec)
89  
90 #ifndef HAS_GETRUSAGE
91 # if defined(RUSAGE_SELF) || defined(SOLARIS)
92 # define HAS_GETRUSAGE
93 # endif
94 #endif
95  
96 #if defined(OS2) && !defined(PRIO_PROCESS)
97 # define PRIO_PROCESS 0 /* This argument is ignored anyway. */
98 #endif
99  
100 #if defined(__hpux) && defined(RLIMIT_NLIMITS)
101 /* there is getrusage() in HPUX but only as an indirect syscall */
102 # define TRY_GETRUSAGE_AS_SYSCALL
103 /* some rlimits exist (but are officially unsupported by HP) */
104 # define RLIMIT_CPU 0
105 # define RLIMIT_FSIZE 1
106 # define RLIMIT_DATA 2
107 # define RLIMIT_STACK 3
108 # define RLIMIT_CORE 4
109 # define RLIMIT_RSS 5
110 # define RLIMIT_NOFILE 6
111 # define RLIMIT_OPEN_MAX RLIMIT_NOFILE
112 # define RLIM_NLIMITS 7
113 # define RLIM_INFINITY 0x7fffffff
114 #endif
115  
116 #ifdef __linux__
117 /* enums without #defines, how wonderful */
118 # ifndef PRIO_PROCESS
119 # define PRIO_PROCESS PRIO_PROCESS
120 # endif
121 # ifndef PRIO_PGRP
122 # define PRIO_PGRP PRIO_PGRP
123 # endif
124 # ifndef PRIO_USER
125 # define PRIO_USER PRIO_USER
126 # endif
127 #endif
128  
129 #if !defined(RLIMIT_OPEN_MAX) && defined(RLIMIT_NOFILE)
130 # define RLIMIT_OPEN_MAX RLIMIT_NOFILE
131 #endif
132  
133 #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OPEN_MAX)
134 # define RLIMIT_NOFILE RLIMIT_OPEN_MAX
135 #endif
136  
137 #if !defined(RLIMIT_OFILE) && defined(RLIMIT_NOFILE)
138 # define RLIMIT_OFILE RLIMIT_NOFILE
139 #endif
140  
141 #if !defined(RLIMIT_VMEM) && defined(RLIMIT_AS)
142 # define RLIMIT_VMEM RLIMIT_AS
143 #else
144 # if !defined(RLIMIT_AS) && defined(RLIMIT_VMEM)
145 # define RLIMIT_AS RLIMIT_VMEM
146 # endif
147 #endif
148  
149 #ifdef TRY_GETRUSAGE_AS_SYSCALL
150 # include <sys/syscall.h>
151 # if defined(SYS_GETRUSAGE)
152 # define getrusage(a, b) syscall(SYS_GETRUSAGE, (a), (b))
153 # define HAS_GETRUSAGE
154 # endif
155 #endif
156  
157 #ifndef Rlim_t
158 # ifdef Quad_t
159 # define Rlim_t Quad_t
160 # else
161 # define Rlim_t unsigned long
162 # endif
163 #endif
164  
165 #if defined(RLIM_INFINITY) /* this is the only one we can count on (?) */
166 #define HAS_GETRLIMIT
167 #define HAS_SETRLIMIT
168 #endif
169  
170 #ifndef PRIO_MAX
171 # define PRIO_MAX 20
172 #endif
173  
174 #ifndef PRIO_MIN
175 # define PRIO_MIN -20
176 #endif
177  
178 #if defined(PRIO_USER)
179 #ifndef HAS_GETPRIORITY
180 #define HAS_GETPRIORITY
181 #endif
182 #ifndef HAS_SETPRIORITY
183 #define HAS_SETPRIORITY
184 #endif
185 #endif
186  
187 #ifndef HAS_GETPRIORITY
188 #define _getpriority(a,b) not_here("getpriority")
189 #endif
190  
191 #ifndef HAS_GETRLIMIT
192 #define _getrlimit(a,b) not_here("getrlimit")
193 #endif
194  
195 #ifndef HAS_GETRUSAGE
196 #define _getrusage(a,b) not_here("getrusage")
197 #endif
198  
199 #ifndef HAS_SETPRIORITY
200 #define _setpriority(a,b,c) not_here("setpriority")
201 #endif
202  
203 #ifndef HAS_SETRLIMIT
204 #define _setrlimit(a,b) not_here("setrlimit")
205 #endif
206  
207 static int
208 0 not_here(s)
209 char *s;
210 {
211 0 croak("BSD::Resource::%s not implemented on this architecture", s);
212 return -1;
213 }
214  
215 static double
216 41 constant(name, arg)
217 char *name;
218 int arg;
219 {
220 41 errno = 0;
221 41 switch (*name) {
222 16 case 'E':
223 16 ▶ if (strEQ(name, "EINVAL"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "EINVAL")) 8 8 16
224 #ifdef EINVAL
225 8 return EINVAL;
226 #else
227 goto not_there;
228 #endif
229 8 ▶ if (strEQ(name, "ENOENT"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "ENOENT")) 0 8 8
230 #ifdef ENOENT
231 0 return ENOENT;
232 #else
233 goto not_there;
234 #endif
235 8 break;
236 2 case 'P':
237 2 ▶ if (strnEQ(name, "PRIO_", 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ(name, "PRIO_", 5)) { 2 0 2
238 2 ▶ if (strEQ(name, "PRIO_MIN"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "PRIO_MIN")) 0 2 2
239 #if defined(PRIO_MIN) || defined(HAS_PRIO_MIN)
240 0 return PRIO_MIN;
241 #else
242 goto not_there;
243 #endif
244 2 ▶ if (strEQ(name, "PRIO_MAX"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "PRIO_MAX")) 0 2 2
245 #if defined(PRIO_MAX) || defined(HAS_PRIO_MAX)
246 0 return PRIO_MAX;
247 #else
248 goto not_there;
249 #endif
250 2 ▶ if (strEQ(name, "PRIO_USER"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "PRIO_USER")) 0 2 2
251 #if defined(PRIO_USER) || defined(HAS_PRIO_USER)
252 0 return PRIO_USER;
253 #else
254 goto not_there;
255 #endif
256 2 ▶ if (strEQ(name, "PRIO_PGRP"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "PRIO_PGRP")) 0 2 2
257 #if defined(PRIO_PGRP) || defined(HAS_PRIO_PGRP)
258 0 return PRIO_PGRP;
259 #else
260 goto not_there;
261 #endif
262 2 ▶ if (strEQ(name, "PRIO_PROCESS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "PRIO_PROCESS")) 2 0 2
263 #if defined(PRIO_PROCESS) || defined(HAS_PRIO_PROCESS)
264 2 return PRIO_PROCESS;
265 #else
266 goto not_there;
267 #endif
268 }
269 0 goto not_there;
270 4 case 'R':
271 4 ▶ if (strnEQ(name, "RLIM", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ(name, "RLIM", 4)) { 1 3 4
272 1 ▶ if (strEQ(name, "RLIMIT_AIO_MEM"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_AIO_MEM")) 0 1 1
273 #if defined(RLIMIT_AIO_MEM) || defined(HAS_RLIMIT_AIO_MEM)
274 return RLIMIT_AIO_MEM;
275 #else
276 0 goto not_there;
277 #endif
278 1 ▶ if (strEQ(name, "RLIMIT_AIO_OPS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_AIO_OPS")) 0 1 1
279 #if defined(RLIMIT_AIO_OPS) || defined(HAS_RLIMIT_AIO_OPS)
280 return RLIMIT_AIO_OPS;
281 #else
282 0 goto not_there;
283 #endif
284 1 ▶ if (strEQ(name, "RLIMIT_AS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_AS")) 0 1 1
285 #if defined(RLIMIT_AS) || defined(HAS_RLIMIT_AS)
286 0 return RLIMIT_AS;
287 #else
288 goto not_there;
289 #endif
290 1 ▶ if (strEQ(name, "RLIMIT_CORE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_CORE")) 0 1 1
291 #if defined(RLIMIT_CORE) || defined(HAS_RLIMIT_CORE)
292 0 return RLIMIT_CORE;
293 #else
294 goto not_there;
295 #endif
296 1 ▶ if (strEQ(name, "RLIMIT_CPU"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_CPU")) 0 1 1
297 #if defined(RLIMIT_CPU) || defined(HAS_RLIMIT_CPU)
298 0 return RLIMIT_CPU;
299 #else
300 goto not_there;
301 #endif
302 1 ▶ if (strEQ(name, "RLIMIT_DATA"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_DATA")) 0 1 1
303 #if defined(RLIMIT_DATA) || defined(HAS_RLIMIT_DATA)
304 0 return RLIMIT_DATA;
305 #else
306 goto not_there;
307 #endif
308 1 ▶ if (strEQ(name, "RLIMIT_FSIZE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_FSIZE")) 0 1 1
309 #if defined(RLIMIT_FSIZE) || defined(HAS_RLIMIT_FSIZE)
310 0 return RLIMIT_FSIZE;
311 #else
312 goto not_there;
313 #endif
314 1 ▶ if (strEQ(name, "RLIMIT_LOCKS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_LOCKS")) 0 1 1
315 #if defined(RLIMIT_LOCKS) || defined(HAS_RLIMIT_LOCKS)
316 0 return RLIMIT_LOCKS;
317 #else
318 goto not_there;
319 #endif
320 1 ▶ if (strEQ(name, "RLIMIT_MEMLOCK"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_MEMLOCK")) 0 1 1
321 #if defined(RLIMIT_MEMLOCK) || defined(HAS_RLIMIT_MEMLOCK)
322 0 return RLIMIT_MEMLOCK;
323 #else
324 goto not_there;
325 #endif
326 1 ▶ if (strEQ(name, "RLIMIT_MSGQUEUE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_MSGQUEUE")) 0 1 1
327 #if defined(RLIMIT_MSGQUEUE) || defined(HAS_RLIMIT_MSGQUEUE)
328 0 return RLIMIT_MSGQUEUE;
329 #else
330 goto not_there;
331 #endif
332 1 ▶ if (strEQ(name, "RLIMIT_NICE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_NICE")) 0 1 1
333 #if defined(RLIMIT_NICE) || defined(HAS_RLIMIT_NICE)
334 0 return RLIMIT_NICE;
335 #else
336 goto not_there;
337 #endif
338 1 ▶ if (strEQ(name, "RLIMIT_NOFILE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_NOFILE")) 0 1 1
339 #if defined(RLIMIT_NOFILE) || defined(HAS_RLIMIT_NOFILE)
340 0 return RLIMIT_NOFILE;
341 #else
342 goto not_there;
343 #endif
344 1 ▶ if (strEQ(name, "RLIMIT_NPROC"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_NPROC")) 0 1 1
345 #if defined(RLIMIT_NPROC) || defined(HAS_RLIMIT_NPROC)
346 0 return RLIMIT_NPROC;
347 #else
348 goto not_there;
349 #endif
350 1 ▶ if (strEQ(name, "RLIMIT_NPTS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_NPTS")) 0 1 1
351 #if defined(RLIMIT_NPTS) || defined(HAS_RLIMIT_NPTS)
352 return RLIMIT_NPTS;
353 #else
354 0 goto not_there;
355 #endif
356 1 ▶ if (strEQ(name, "RLIMIT_OFILE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_OFILE")) 0 1 1
357 #if defined(RLIMIT_OFILE) || defined(HAS_RLIMIT_OFILE)
358 0 return RLIMIT_OFILE;
359 #else
360 goto not_there;
361 #endif
362 1 ▶ if (strEQ(name, "RLIMIT_OPEN_MAX"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_OPEN_MAX")) 0 1 1
363 #if defined(RLIMIT_OPEN_MAX) || defined(HAS_RLIMIT_OPEN_MAX)
364 0 return RLIMIT_OPEN_MAX;
365 #else
366 goto not_there;
367 #endif
368 1 ▶ if (strEQ(name, "RLIMIT_PTHREAD"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_PTHREAD")) 0 1 1
369 #if defined(RLIMIT_PTHREAD) || defined(HAS_RLIMIT_PTHREAD)
370 return RLIMIT_PTHREAD;
371 #else
372 0 goto not_there;
373 #endif
374 1 ▶ if (strEQ(name, "RLIMIT_RSS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_RSS")) 0 1 1
375 #if defined(RLIMIT_RSS) || defined(HAS_RLIMIT_RSS)
376 0 return RLIMIT_RSS;
377 #else
378 goto not_there;
379 #endif
380 1 ▶ if (strEQ(name, "RLIMIT_RTPRIO"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_RTPRIO")) 0 1 1
381 #if defined(RLIMIT_RTPRIO) || defined(HAS_RLIMIT_RTPRIO)
382 0 return RLIMIT_RTPRIO;
383 #else
384 goto not_there;
385 #endif
386 1 ▶ if (strEQ(name, "RLIMIT_RTTIME"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_RTTIME")) 0 1 1
387 #if defined(RLIMIT_RTTIME) || defined(HAS_RLIMIT_RTTIME)
388 0 return RLIMIT_RTTIME;
389 #else
390 goto not_there;
391 #endif
392 1 ▶ if (strEQ(name, "RLIMIT_SBSIZE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_SBSIZE")) 0 1 1
393 #if defined(RLIMIT_SBSIZE) || defined(HAS_RLIMIT_SBSIZE)
394 return RLIMIT_SBSIZE;
395 #else
396 0 goto not_there;
397 #endif
398 1 ▶ if (strEQ(name, "RLIMIT_SIGPENDING"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_SIGPENDING")) 0 1 1
399 #if defined(RLIMIT_SIGPENDING) || defined(HAS_RLIMIT_SIGPENDING)
400 0 return RLIMIT_SIGPENDING;
401 #else
402 goto not_there;
403 #endif
404 1 ▶ if (strEQ(name, "RLIMIT_STACK"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_STACK")) 0 1 1
405 #if defined(RLIMIT_STACK) || defined(HAS_RLIMIT_STACK)
406 0 return RLIMIT_STACK;
407 #else
408 goto not_there;
409 #endif
410 1 ▶ if (strEQ(name, "RLIMIT_SWAP"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_SWAP")) 0 1 1
411 #if defined(RLIMIT_SWAP) || defined(HAS_RLIMIT_SWAP)
412 return RLIMIT_SWAP;
413 #else
414 0 goto not_there;
415 #endif
416 1 ▶ if (strEQ(name, "RLIMIT_TCACHE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_TCACHE")) 0 1 1
417 #if defined(RLIMIT_TCACHE) || defined(HAS_RLIMIT_TCACHE)
418 return RLIMIT_TCACHE;
419 #else
420 0 goto not_there;
421 #endif
422 1 ▶ if (strEQ(name, "RLIMIT_VMEM"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIMIT_VMEM")) 0 1 1
423 #if defined(RLIMIT_VMEM) || defined(HAS_RLIMIT_VMEM)
424 0 return RLIMIT_VMEM;
425 #else
426 goto not_there;
427 #endif
428 1 ▶ if (strEQ(name, "RLIM_INFINITY"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIM_INFINITY")) 1 0 1
429 #if defined(RLIM_INFINITY) || defined(HAS_RLIM_INFINITY)
430 1 return -1.0; /* trust me */
431 #else
432 goto not_there;
433 #endif
434 0 ▶ if (strEQ(name, "RLIM_NLIMITS"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIM_NLIMITS")) 0 0 0
435 #if defined(RLIM_NLIMITS) || defined(HAS_RLIM_NLIMITS)
436 0 return RLIM_NLIMITS;
437 #else
438 goto not_there;
439 #endif
440 0 ▶ if (strEQ(name, "RLIM_SAVED_CUR"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIM_SAVED_CUR")) 0 0 0
441 #if defined(RLIM_SAVED_CUR) || defined(HAS_RLIM_SAVED_CUR)
442 0 return RLIM_SAVED_CUR;
443 #else
444 goto not_there;
445 #endif
446 0 ▶ if (strEQ(name, "RLIM_SAVED_MAX"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RLIM_SAVED_MAX")) 0 0 0
447 #if defined(RLIM_SAVED_MAX) || defined(HAS_RLIM_SAVED_MAX)
448 0 return RLIM_SAVED_MAX;
449 #else
450 goto not_there;
451 #endif
452 0 break;
453 }
454 3 ▶ if (strnEQ(name, "RUSAGE_", 7)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ(name, "RUSAGE_", 7)) { 3 0 3
455 3 ▶ if (strEQ(name, "RUSAGE_BOTH"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RUSAGE_BOTH")) 0 3 3
456 #if defined(RUSAGE_BOTH) || defined(HAS_RUSAGE_BOTH)
457 return RUSAGE_BOTH;
458 #else
459 0 goto not_there;
460 #endif
461 3 ▶ if (strEQ(name, "RUSAGE_CHILDREN"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RUSAGE_CHILDREN")) 1 2 3
462 #if defined(RUSAGE_CHILDREN) || defined(HAS_RUSAGE_CHILDREN)
463 1 return RUSAGE_CHILDREN;
464 #else
465 goto not_there;
466 #endif
467 2 ▶ if (strEQ(name, "RUSAGE_SELF"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RUSAGE_SELF")) 2 0 2
468 #if defined(RUSAGE_SELF) || defined(HAS_RUSAGE_SELF)
469 2 return RUSAGE_SELF;
470 #else
471 goto not_there;
472 #endif
473 0 ▶ if (strEQ(name, "RUSAGE_THREAD"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "RUSAGE_THREAD")) 0 0 0
474 #if defined(RUSAGE_THREAD) || defined(HAS_RUSAGE_THREAD)
475 0 return RUSAGE_THREAD;
476 #else
477 goto not_there;
478 #endif
479 0 break;
480 }
481 }
482  
483 27 errno = EINVAL;
484 27 return 0;
485  
486 0 not_there:
487 0 errno = ENOENT;
488 0 return 0;
489 }
490  
491 #define HV_STORE_RES(h, l) (void)hv_store(h, #l, sizeof(#l)-1, newSViv(l), 0)
492  
493 MODULE = BSD::Resource PACKAGE = BSD::Resource
494  
495 PROTOTYPES: enable
496  
497 double
498 constant(name,arg)
499 char * name
500 int arg
501  
502 void
503 _getpriority(which = PRIO_PROCESS, who = 0)
504 int which
505 int who
506 CODE:
507 {
508 int prio;
509  
510 6 ST(0) = sv_newmortal();
511 6 errno = 0; /* getpriority() can successfully return <= 0 */
512 6 prio = getpriority(which, who);
513 6 ▶ if (errno == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == 0) 6 0 6
514 6 sv_setiv(ST(0), prio);
515 else
516 0 ST(0) = &PL_sv_undef;
517 }
518  
519 void
520 _getrlimit(resource)
521 int resource
522 PPCODE:
523 struct rlimit rl;
524 47 ▶ if (getrlimit(resource, &rl) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (getrlimit(resource, &rl) == 0) { 47 0 47
525 47 ▶ EXTEND(sp, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(sp, 2); 0 47 47
526 47 ▶ PUSHs(sv_2mortal(newSVnv((double)(rl.rlim_cur == RLIM_INFINITY ? -1.0 : rl.rlim_cur))));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sv_2mortal(newSVnv((double)(rl.rlim_cur == RLIM_INFINITY ? -1.0 : rl.rlim_cur)))); 29 18 47
527 47 ▶ PUSHs(sv_2mortal(newSVnv((double)(rl.rlim_max == RLIM_INFINITY ? -1.0 : rl.rlim_max))));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sv_2mortal(newSVnv((double)(rl.rlim_max == RLIM_INFINITY ? -1.0 : rl.rlim_max)))); 27 20 47
528 }
529  
530 void
531 _getrusage(who = RUSAGE_SELF)
532 int who
533 PPCODE:
534 {
535 struct rusage ru;
536 #ifdef SOLARIS_PROCFS
537 Struct_psinfo psi;
538 Struct_pstatus pst;
539 struct prusage pru;
540 pid_t pid = getpid();
541 int res, fd;
542 char psib[40], pstb[40], prub[40];
543 ru.ru_utime.tv_sec = 0;
544 ru.ru_utime.tv_usec = 0;
545 ru.ru_stime.tv_sec = 0;
546 ru.ru_stime.tv_usec = 0;
547 ru.ru_maxrss = 0;
548 ru.ru_ixrss = 0;
549 ru.ru_idrss = 0;
550 ru.ru_isrss = 0;
551 ru.ru_minflt = 0;
552 ru.ru_majflt = 0;
553 ru.ru_nswap = 0;
554 ru.ru_inblock = 0;
555 ru.ru_oublock = 0;
556 ru.ru_msgsnd = 0;
557 ru.ru_msgrcv = 0;
558 ru.ru_nsignals = 0;
559 ru.ru_nvcsw = 0;
560 ru.ru_nivcsw = 0;
561 # ifndef SOLARIS_STRUCTURED_PROC
562 /* The time fields come okay from getrusage() but would be bad
563 * from PIOCUSAGE. Argh. */
564 res = getrusage(who, &ru);
565 if (res)
566 goto failed;
567 # endif
568 /* With 64-bit pids "/proc/18446744073709551616/psinfo" takes 34 bytes. */
569 sprintf(psib, "/proc/%d", pid);
570 sprintf(pstb, "/proc/%d", pid);
571 sprintf(prub, "/proc/%d", pid);
572 # ifdef SOLARIS_STRUCTURED_PROC
573 res = strlen(psib);
574 sprintf(psib + res, "/psinfo");
575 sprintf(pstb + res, "/status");
576 sprintf(prub + res, "/usage" );
577 # endif
578 fd = open(psib, O_RDONLY);
579 if (fd >= 0) {
580 # ifdef SOLARIS_STRUCTURED_PROC
581 res = read(fd, &psi, sizeof(psi));
582 if (res == sizeof(psi))
583 ru.ru_maxrss = psi.pr_rssize * 1024;
584 else
585 goto failed;
586 # else
587 res = ioctl(fd, PIOCPSINFO, &psi);
588 if (res != -1)
589 ru.ru_maxrss = psi.pr_byrssize;
590 else
591 goto failed;
592 # endif
593 close(fd);
594 } else
595 goto failed;
596 fd = open(pstb, O_RDONLY);
597 if (fd >= 0) {
598 # ifdef SOLARIS_STRUCTURED_PROC
599 res = read(fd, &pst, sizeof(pst));
600 res = res == sizeof(pst) ? 1 : 0;
601 # else
602 res = ioctl(fd, PIOCUSAGE, &pst);
603 res = res == -1 ? 0 : 1;
604 # endif
605 if (res) {
606 # ifdef SOLARIS_STRUCTURED_PROC
607 /* Structured proc seems to have okay values in struct psinfo but
608 * zero values from the earlier getrusage() so get the better ones. */
609 if (who == RUSAGE_SELF) {
610 ru.ru_utime.tv_sec = pst.pr_utime.tv_sec;
611 ru.ru_utime.tv_usec = pst.pr_utime.tv_nsec / 1000;
612 ru.ru_stime.tv_sec = pst.pr_stime.tv_sec;
613 ru.ru_stime.tv_usec = pst.pr_stime.tv_nsec / 1000;
614 } else if (who == RUSAGE_CHILDREN) {
615 ru.ru_utime.tv_sec = pst.pr_cutime.tv_sec;
616 ru.ru_utime.tv_usec = pst.pr_cutime.tv_nsec / 1000;
617 ru.ru_stime.tv_sec = pst.pr_cstime.tv_sec;
618 ru.ru_stime.tv_usec = pst.pr_cstime.tv_nsec / 1000;
619 }
620 # endif
621 /* Current values, not really integrals. */
622 ru.ru_idrss = pst.pr_brksize;
623 ru.ru_isrss = pst.pr_stksize;
624 } else
625 goto failed;
626 close(fd);
627 } else
628 goto failed;
629 fd = open(prub, O_RDONLY);
630 if (fd >= 0) {
631 # ifdef SOLARIS_STRUCTURED_PROC
632 res = read(fd, &pru, sizeof(pru));
633 res = res == sizeof(pru) ? 1 : 0;
634 # else
635 res = ioctl(fd, PIOCUSAGE, &pru);
636 res = res == -1 ? 0 : 1;
637 # endif
638 if (res) {
639 ru.ru_minflt = pru.pr_minf;
640 ru.ru_majflt = pru.pr_majf;
641 ru.ru_nswap = pru.pr_nswap;
642 ru.ru_inblock = pru.pr_inblk;
643 ru.ru_oublock = pru.pr_oublk;
644 ru.ru_msgsnd = pru.pr_msnd;
645 ru.ru_msgrcv = pru.pr_mrcv;
646 ru.ru_nsignals = pru.pr_sigs;
647 ru.ru_nvcsw = pru.pr_vctx;
648 ru.ru_nivcsw = pru.pr_ictx;
649 } else
650 goto failed;
651 close(fd);
652 } else
653 goto failed;
654 #else
655 7 ▶ if (getrusage(who, &ru))
Branch: 1 branch
BranchTrueFalseTotal
if (getrusage(who, &ru)) 0 7 7
656 0 goto failed;
657 #endif
658 7 ▶ EXTEND(sp, 16);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(sp, 16); 0 7 7
659 7 PUSHs(sv_2mortal(newSVnv(TV2DS(ru.ru_utime))));
660 7 PUSHs(sv_2mortal(newSVnv(TV2DS(ru.ru_stime))));
661 7 PUSHs(sv_2mortal(newSViv(ru.ru_maxrss)));
662 7 PUSHs(sv_2mortal(newSVnv(ru.ru_ixrss)));
663 7 PUSHs(sv_2mortal(newSVnv(ru.ru_idrss)));
664 7 PUSHs(sv_2mortal(newSVnv(ru.ru_isrss)));
665 7 PUSHs(sv_2mortal(newSVnv(ru.ru_minflt)));
666 7 PUSHs(sv_2mortal(newSVnv(ru.ru_majflt)));
667 7 PUSHs(sv_2mortal(newSVnv(ru.ru_nswap)));
668 7 PUSHs(sv_2mortal(newSVnv(ru.ru_inblock)));
669 7 PUSHs(sv_2mortal(newSVnv(ru.ru_oublock)));
670 7 PUSHs(sv_2mortal(newSVnv(ru.ru_msgsnd)));
671 7 PUSHs(sv_2mortal(newSVnv(ru.ru_msgrcv)));
672 7 PUSHs(sv_2mortal(newSVnv(ru.ru_nsignals)));
673 7 PUSHs(sv_2mortal(newSVnv(ru.ru_nvcsw)));
674 7 PUSHs(sv_2mortal(newSVnv(ru.ru_nivcsw)));
675 7 failed:
676 ;
677 }
678  
679 void
680 _setpriority(which = PRIO_PROCESS,who = 0,priority = PRIO_MAX/2)
681 int which
682 int who
683 int priority
684 CODE:
685 {
686 2 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 0 2 2
687 /* if two arguments they are (which, priority),
688 * not (which, who). who defaults to 0. */
689 0 priority = who;
690 0 who = 0;
691 }
692 2 ST(0) = sv_newmortal();
693 4 ST(0) = (setpriority(which, who, priority) == 0) ?
694 2 ▶ &PL_sv_yes : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
&PL_sv_yes : &PL_sv_undef; 2 0 2
695 }
696  
697 void
698 _setrlimit(resource,soft,hard)
699 int resource
700 double soft
701 double hard
702 CODE:
703 {
704 struct rlimit rl;
705  
706 9 ▶ rl.rlim_cur = soft == -1.0 ? RLIM_INFINITY : (Rlim_t) soft;
Branch: 1 branch
BranchTrueFalseTotal
rl.rlim_cur = soft == -1.0 ? RLIM_INFINITY : (Rlim_t) soft; 9 0 9
707 9 ▶ rl.rlim_max = hard == -1.0 ? RLIM_INFINITY : (Rlim_t) hard;
Branch: 1 branch
BranchTrueFalseTotal
rl.rlim_max = hard == -1.0 ? RLIM_INFINITY : (Rlim_t) hard; 9 0 9
708  
709 9 ST(0) = sv_newmortal();
710 9 ▶ ST(0) = (setrlimit(resource, &rl) == 0) ? &PL_sv_yes: &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = (setrlimit(resource, &rl) == 0) ? &PL_sv_yes: &PL_sv_undef; 9 0 9
711 }
712  
713 HV *
714 _get_rlimits()
715 CODE:
716 59 RETVAL = newHV();
717 59 sv_2mortal((SV*)RETVAL);
718 #if defined(RLIMIT_AIO_MEM) || defined(HAS_RLIMIT_AIO_MEM)
719 HV_STORE_RES(RETVAL, RLIMIT_AIO_MEM);
720 #endif
721 #if defined(RLIMIT_AIO_OPS) || defined(HAS_RLIMIT_AIO_OPS)
722 HV_STORE_RES(RETVAL, RLIMIT_AIO_OPS);
723 #endif
724 #if defined(RLIMIT_AS) || defined(HAS_RLIMIT_AS)
725 59 HV_STORE_RES(RETVAL, RLIMIT_AS);
726 #endif
727 #if defined(RLIMIT_CORE) || defined(HAS_RLIMIT_CORE)
728 59 HV_STORE_RES(RETVAL, RLIMIT_CORE);
729 #endif
730 #if defined(RLIMIT_CPU) || defined(HAS_RLIMIT_CPU)
731 59 HV_STORE_RES(RETVAL, RLIMIT_CPU);
732 #endif
733 #if defined(RLIMIT_DATA) || defined(HAS_RLIMIT_DATA)
734 59 HV_STORE_RES(RETVAL, RLIMIT_DATA);
735 #endif
736 #if defined(RLIMIT_FSIZE) || defined(HAS_RLIMIT_FSIZE)
737 59 HV_STORE_RES(RETVAL, RLIMIT_FSIZE);
738 #endif
739 #if defined(RLIMIT_LOCKS) || defined(HAS_RLIMIT_LOCKS)
740 59 HV_STORE_RES(RETVAL, RLIMIT_LOCKS);
741 #endif
742 #if defined(RLIMIT_MEMLOCK) || defined(HAS_RLIMIT_MEMLOCK)
743 59 HV_STORE_RES(RETVAL, RLIMIT_MEMLOCK);
744 #endif
745 #if defined(RLIMIT_MSGQUEUE) || defined(HAS_RLIMIT_MSGQUEUE)
746 59 HV_STORE_RES(RETVAL, RLIMIT_MSGQUEUE);
747 #endif
748 #if defined(RLIMIT_NICE) || defined(HAS_RLIMIT_NICE)
749 59 HV_STORE_RES(RETVAL, RLIMIT_NICE);
750 #endif
751 #if defined(RLIMIT_NOFILE) || defined(HAS_RLIMIT_NOFILE)
752 59 HV_STORE_RES(RETVAL, RLIMIT_NOFILE);
753 #endif
754 #if defined(RLIMIT_NPROC) || defined(HAS_RLIMIT_NPROC)
755 59 HV_STORE_RES(RETVAL, RLIMIT_NPROC);
756 #endif
757 #if defined(RLIMIT_NPTS) || defined(HAS_RLIMIT_NPTS)
758 HV_STORE_RES(RETVAL, RLIMIT_NPTS);
759 #endif
760 #if defined(RLIMIT_OFILE) || defined(HAS_RLIMIT_OFILE)
761 59 HV_STORE_RES(RETVAL, RLIMIT_OFILE);
762 #endif
763 #if defined(RLIMIT_OPEN_MAX) || defined(HAS_RLIMIT_OPEN_MAX)
764 59 HV_STORE_RES(RETVAL, RLIMIT_OPEN_MAX);
765 #endif
766 #if defined(RLIMIT_PTHREAD) || defined(HAS_RLIMIT_PTHREAD)
767 HV_STORE_RES(RETVAL, RLIMIT_PTHREAD);
768 #endif
769 #if defined(RLIMIT_RSS) || defined(HAS_RLIMIT_RSS)
770 59 HV_STORE_RES(RETVAL, RLIMIT_RSS);
771 #endif
772 #if defined(RLIMIT_RTPRIO) || defined(HAS_RLIMIT_RTPRIO)
773 59 HV_STORE_RES(RETVAL, RLIMIT_RTPRIO);
774 #endif
775 #if defined(RLIMIT_RTTIME) || defined(HAS_RLIMIT_RTTIME)
776 59 HV_STORE_RES(RETVAL, RLIMIT_RTTIME);
777 #endif
778 #if defined(RLIMIT_SBSIZE) || defined(HAS_RLIMIT_SBSIZE)
779 HV_STORE_RES(RETVAL, RLIMIT_SBSIZE);
780 #endif
781 #if defined(RLIMIT_SIGPENDING) || defined(HAS_RLIMIT_SIGPENDING)
782 59 HV_STORE_RES(RETVAL, RLIMIT_SIGPENDING);
783 #endif
784 #if defined(RLIMIT_STACK) || defined(HAS_RLIMIT_STACK)
785 59 HV_STORE_RES(RETVAL, RLIMIT_STACK);
786 #endif
787 #if defined(RLIMIT_SWAP) || defined(HAS_RLIMIT_SWAP)
788 HV_STORE_RES(RETVAL, RLIMIT_SWAP);
789 #endif
790 #if defined(RLIMIT_TCACHE) || defined(HAS_RLIMIT_TCACHE)
791 HV_STORE_RES(RETVAL, RLIMIT_TCACHE);
792 #endif
793 #if defined(RLIMIT_VMEM) || defined(HAS_RLIMIT_VMEM)
794 59 HV_STORE_RES(RETVAL, RLIMIT_VMEM);
795 #endif
796 OUTPUT:
797 RETVAL
798  
799 HV *
800 _get_prios()
801 CODE:
802 0 RETVAL = newHV();
803 0 sv_2mortal((SV*)RETVAL);
804 #if defined(PRIO_PROCESS)
805 0 HV_STORE_RES(RETVAL, PRIO_PROCESS);
806 #endif
807 #if defined(PRIO_PGRP)
808 0 HV_STORE_RES(RETVAL, PRIO_PGRP);
809 #endif
810 #if defined(PRIO_THREAD)
811 HV_STORE_RES(RETVAL, PRIO_THREAD);
812 #endif
813 #if defined(PRIO_USER)
814 0 HV_STORE_RES(RETVAL, PRIO_USER);
815 #endif
816 OUTPUT:
817 RETVAL
« blib/lib/BSD/Resource.pm ↑ Summary