Resource.xs

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