1 /**
2  * D header file for POSIX.
3  *
4  * Copyright: Copyright (c) 2013 Lars Tandle Kyllingstad.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Lars Tandle Kyllingstad
7  * Standards: The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008
8  */
9 module core.sys.posix.sys.resource;
10 version (Posix):
11 
12 public import core.sys.posix.sys.time;
13 public import core.sys.posix.sys.types: id_t;
14 import core.sys.posix.config;
15 
16 version (OSX)
17     version = Darwin;
18 else version (iOS)
19     version = Darwin;
20 else version (TVOS)
21     version = Darwin;
22 else version (WatchOS)
23     version = Darwin;
24 
nogc(C)25 nothrow @nogc extern(C):
26 
27 //
28 // XOpen (XSI)
29 //
30 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_resource.h.html
31 /*
32 enum
33 {
34     PRIO_PROCESS,
35     PRIO_PGRP,
36     PRIO_USER,
37 }
38 
39 alias ulong rlim_t;
40 
41 enum
42 {
43     RLIM_INFINITY,
44     RLIM_SAVED_MAX,
45     RLIM_SAVED_CUR,
46 }
47 
48 enum
49 {
50     RUSAGE_SELF,
51     RUSAGE_CHILDREN,
52 }
53 
54 struct rlimit
55 {
56     rlim_t rlim_cur;
57     rlim_t rlim_max;
58 }
59 
60 struct rusage
61 {
62     timeval ru_utime;
63     timeval ru_stime;
64 }
65 
66 enum
67 {
68     RLIMIT_CORE,
69     RLIMIT_CPU,
70     RLIMIT_DATA,
71     RLIMIT_FSIZE,
72     RLIMIT_NOFILE,
73     RLIMIT_STACK,
74     RLIMIT_AS,
75 }
76 
77 int getpriority(int, id_t);
78 int getrlimit(int, rlimit*);
79 int getrusage(int, rusage*);
80 int setpriority(int, id_t, int);
81 int setrlimit(int, const rlimit*);
82 */
83 
84 
85 version (CRuntime_Glibc)
86 {
87     // rusage and some other constants in the Bionic section below really
88     // come from the linux kernel headers, but they're all mixed right now.
89     enum
90     {
91         PRIO_PROCESS = 0,
92         PRIO_PGRP    = 1,
93         PRIO_USER    = 2,
94     }
95 
96     static if (__USE_FILE_OFFSET64)
97          alias ulong rlim_t;
98     else
99          alias c_ulong rlim_t;
100 
101     static if (__USE_FILE_OFFSET64)
102         enum RLIM_INFINITY = 0xffffffffffffffffUL;
103     else
104         enum RLIM_INFINITY = cast(c_ulong)(~0UL);
105 
106     enum RLIM_SAVED_MAX = RLIM_INFINITY;
107     enum RLIM_SAVED_CUR = RLIM_INFINITY;
108 
109     enum
110     {
111         RUSAGE_SELF     =  0,
112         RUSAGE_CHILDREN = -1,
113     }
114 
115     struct rusage
116     {
117         timeval ru_utime;
118         timeval ru_stime;
119         c_long ru_maxrss;
120         c_long ru_ixrss;
121         c_long ru_idrss;
122         c_long ru_isrss;
123         c_long ru_minflt;
124         c_long ru_majflt;
125         c_long ru_nswap;
126         c_long ru_inblock;
127         c_long ru_oublock;
128         c_long ru_msgsnd;
129         c_long ru_msgrcv;
130         c_long ru_nsignals;
131         c_long ru_nvcsw;
132         c_long ru_nivcsw;
133     }
134 
135     enum
136     {
137         RLIMIT_CORE   = 4,
138         RLIMIT_CPU    = 0,
139         RLIMIT_DATA   = 2,
140         RLIMIT_FSIZE  = 1,
141         RLIMIT_NOFILE = 7,
142         RLIMIT_STACK  = 3,
143         RLIMIT_AS     = 9,
144     }
145 }
146 else version (Darwin)
147 {
148     enum
149     {
150         PRIO_PROCESS = 0,
151         PRIO_PGRP    = 1,
152         PRIO_USER    = 2,
153     }
154 
155     alias ulong rlim_t;
156 
157     enum
158     {
159         RLIM_INFINITY  = ((cast(ulong) 1 << 63) - 1),
160         RLIM_SAVED_MAX = RLIM_INFINITY,
161         RLIM_SAVED_CUR = RLIM_INFINITY,
162     }
163 
164     enum
165     {
166         RUSAGE_SELF     =  0,
167         RUSAGE_CHILDREN = -1,
168     }
169 
170     struct rusage
171     {
172         timeval ru_utime;
173         timeval ru_stime;
174         c_long[14] ru_opaque;
175     }
176 
177     enum
178     {
179         RLIMIT_CORE   = 4,
180         RLIMIT_CPU    = 0,
181         RLIMIT_DATA   = 2,
182         RLIMIT_FSIZE  = 1,
183         RLIMIT_NOFILE = 8,
184         RLIMIT_STACK  = 3,
185         RLIMIT_AS     = 5,
186     }
187 }
188 else version (FreeBSD)
189 {
190     enum
191     {
192         PRIO_PROCESS = 0,
193         PRIO_PGRP    = 1,
194         PRIO_USER    = 2,
195     }
196 
197     alias long rlim_t;
198 
199     enum
200     {
201         RLIM_INFINITY   = (cast(rlim_t)((cast(ulong) 1 << 63) - 1)),
202         // FreeBSD explicitly does not define the following:
203         //RLIM_SAVED_MAX,
204         //RLIM_SAVED_CUR,
205     }
206 
207     enum
208     {
209         RUSAGE_SELF     =  0,
210         RUSAGE_CHILDREN = -1,
211     }
212 
213     struct rusage
214     {
215         timeval ru_utime;
216         timeval ru_stime;
217         c_long ru_maxrss;
218         alias ru_ixrss ru_first;
219         c_long ru_ixrss;
220         c_long ru_idrss;
221         c_long ru_isrss;
222         c_long ru_minflt;
223         c_long ru_majflt;
224         c_long ru_nswap;
225         c_long ru_inblock;
226         c_long ru_oublock;
227         c_long ru_msgsnd;
228         c_long ru_msgrcv;
229         c_long ru_nsignals;
230         c_long ru_nvcsw;
231         c_long ru_nivcsw;
232         alias ru_nivcsw ru_last;
233     }
234 
235     enum
236     {
237         RLIMIT_CORE   =  4,
238         RLIMIT_CPU    =  0,
239         RLIMIT_DATA   =  2,
240         RLIMIT_FSIZE  =  1,
241         RLIMIT_NOFILE =  8,
242         RLIMIT_STACK  =  3,
243         RLIMIT_AS     = 10,
244     }
245 }
246 else version (NetBSD)
247 {
248     enum
249     {
250         PRIO_PROCESS = 0,
251         PRIO_PGRP    = 1,
252         PRIO_USER    = 2,
253     }
254 
255     alias long rlim_t;
256 
257     enum
258     {
259         RLIM_INFINITY   = (cast(rlim_t)((cast(ulong) 1 << 63) - 1)),
260         // FreeBSD explicitly does not define the following:
261         //RLIM_SAVED_MAX,
262         //RLIM_SAVED_CUR,
263     }
264 
265     enum
266     {
267         RUSAGE_SELF     =  0,
268         RUSAGE_CHILDREN = -1,
269     }
270 
271     struct rusage
272     {
273         timeval ru_utime;
274         timeval ru_stime;
275         c_long ru_maxrss;
276         alias ru_ixrss ru_first;
277         c_long ru_ixrss;
278         c_long ru_idrss;
279         c_long ru_isrss;
280         c_long ru_minflt;
281         c_long ru_majflt;
282         c_long ru_nswap;
283         c_long ru_inblock;
284         c_long ru_oublock;
285         c_long ru_msgsnd;
286         c_long ru_msgrcv;
287         c_long ru_nsignals;
288         c_long ru_nvcsw;
289         c_long ru_nivcsw;
290         alias ru_nivcsw ru_last;
291     }
292 
293     enum
294     {
295         RLIMIT_CORE   =  4,
296         RLIMIT_CPU    =  0,
297         RLIMIT_DATA   =  2,
298         RLIMIT_FSIZE  =  1,
299         RLIMIT_NOFILE =  8,
300         RLIMIT_STACK  =  3,
301         RLIMIT_AS     = 10,
302     }
303 }
304 else version (DragonFlyBSD)
305 {
306     enum
307     {
308         PRIO_PROCESS = 0,
309         PRIO_PGRP    = 1,
310         PRIO_USER    = 2,
311     }
312 
313     alias long rlim_t;
314 
315     enum
316     {
317         RLIM_INFINITY   = (cast(rlim_t)((cast(ulong) 1 << 63) - 1)),
318         // DragonFlyBSD explicitly does not define the following:
319         //RLIM_SAVED_MAX,
320         //RLIM_SAVED_CUR,
321     }
322 
323     enum
324     {
325         RUSAGE_SELF     =  0,
326         RUSAGE_CHILDREN = -1,
327     }
328 
329     struct rusage
330     {
331         timeval ru_utime;
332         timeval ru_stime;
333         c_long ru_maxrss;
334         alias ru_ixrss ru_first;
335         c_long ru_ixrss;
336         c_long ru_idrss;
337         c_long ru_isrss;
338         c_long ru_minflt;
339         c_long ru_majflt;
340         c_long ru_nswap;
341         c_long ru_inblock;
342         c_long ru_oublock;
343         c_long ru_msgsnd;
344         c_long ru_msgrcv;
345         c_long ru_nsignals;
346         c_long ru_nvcsw;
347         c_long ru_nivcsw;
348         alias ru_nivcsw ru_last;
349     }
350 
351     enum
352     {
353         RLIMIT_CORE   =  4,
354         RLIMIT_CPU    =  0,
355         RLIMIT_DATA   =  2,
356         RLIMIT_FSIZE  =  1,
357         RLIMIT_NOFILE =  8,
358         RLIMIT_STACK  =  3,
359         RLIMIT_AS     = 10,
360     }
361 }
362 else version (Solaris)
363 {
364     enum
365     {
366         PRIO_PROCESS = 0,
367         PRIO_PGRP    = 1,
368         PRIO_USER    = 2,
369     }
370 
371     alias c_ulong rlim_t;
372 
373     enum : c_long
374     {
375         RLIM_INFINITY   = -3,
376         RLIM_SAVED_MAX  = -2,
377         RLIM_SAVED_CUR  = -1,
378     }
379 
380     enum
381     {
382         RUSAGE_SELF     =  0,
383         RUSAGE_CHILDREN = -1,
384     }
385 
386     struct rusage
387     {
388         timeval ru_utime;
389         timeval ru_stime;
390         c_long ru_maxrss;
391         c_long ru_ixrss;
392         c_long ru_idrss;
393         c_long ru_isrss;
394         c_long ru_minflt;
395         c_long ru_majflt;
396         c_long ru_nswap;
397         c_long ru_inblock;
398         c_long ru_oublock;
399         c_long ru_msgsnd;
400         c_long ru_msgrcv;
401         c_long ru_nsignals;
402         c_long ru_nvcsw;
403         c_long ru_nivcsw;
404     }
405 
406     enum
407     {
408         RLIMIT_CORE   = 4,
409         RLIMIT_CPU    = 0,
410         RLIMIT_DATA   = 2,
411         RLIMIT_FSIZE  = 1,
412         RLIMIT_NOFILE = 5,
413         RLIMIT_STACK  = 3,
414         RLIMIT_AS     = 6,
415     }
416 }
417 else version (CRuntime_Bionic)
418 {
419     enum
420     {
421         PRIO_PROCESS = 0,
422         PRIO_PGRP    = 1,
423         PRIO_USER    = 2,
424     }
425 
426     alias c_ulong rlim_t;
427     enum RLIM_INFINITY = cast(c_ulong)(~0UL);
428 
429     enum
430     {
431         RUSAGE_SELF     =  0,
432         RUSAGE_CHILDREN = -1,
433     }
434 
435     struct rusage
436     {
437         timeval ru_utime;
438         timeval ru_stime;
439         c_long ru_maxrss;
440         c_long ru_ixrss;
441         c_long ru_idrss;
442         c_long ru_isrss;
443         c_long ru_minflt;
444         c_long ru_majflt;
445         c_long ru_nswap;
446         c_long ru_inblock;
447         c_long ru_oublock;
448         c_long ru_msgsnd;
449         c_long ru_msgrcv;
450         c_long ru_nsignals;
451         c_long ru_nvcsw;
452         c_long ru_nivcsw;
453     }
454 
455     enum
456     {
457         RLIMIT_CORE   = 4,
458         RLIMIT_CPU    = 0,
459         RLIMIT_DATA   = 2,
460         RLIMIT_FSIZE  = 1,
461         RLIMIT_NOFILE = 7,
462         RLIMIT_STACK  = 3,
463         RLIMIT_AS     = 9,
464     }
465 }
466 else version (CRuntime_Musl)
467 {
468     alias ulong rlim_t;
469 
470     int getrlimit(int, rlimit*);
471     int setrlimit(int, in rlimit*);
472     alias getrlimit getrlimit64;
473     alias setrlimit setrlimit64;
474     enum
475     {
476         RUSAGE_SELF = 0,
477         RUSAGE_CHILDREN = -1,
478         RUSAGE_THREAD = 1
479     }
480     struct rusage
481     {
482         timeval ru_utime;
483         timeval ru_stime;
484         c_long ru_maxrss;
485         c_long ru_ixrss;
486         c_long ru_idrss;
487         c_long ru_isrss;
488         c_long ru_minflt;
489         c_long ru_majflt;
490         c_long ru_nswap;
491         c_long ru_inblock;
492         c_long ru_oublock;
493         c_long ru_msgsnd;
494         c_long ru_msgrcv;
495         c_long ru_nsignals;
496         c_long ru_nvcsw;
497         c_long ru_nivcsw;
498         c_long[16] __reserved;
499     }
500 
501     enum
502     {
503         RLIMIT_CPU    = 0,
504         RLIMIT_FSIZE  = 1,
505         RLIMIT_DATA   = 2,
506         RLIMIT_STACK  = 3,
507         RLIMIT_CORE   = 4,
508         RLIMIT_NOFILE = 7,
509         RLIMIT_AS     = 9,
510     }
511 }
512 else version (CRuntime_UClibc)
513 {
514     enum
515     {
516         PRIO_PROCESS = 0,
517         PRIO_PGRP    = 1,
518         PRIO_USER    = 2,
519     }
520 
521     static if (__USE_FILE_OFFSET64)
522          alias ulong rlim_t;
523     else
524          alias c_ulong rlim_t;
525 
526     static if (__USE_FILE_OFFSET64)
527         enum RLIM_INFINITY = 0xffffffffffffffffUL;
528     else
529         enum RLIM_INFINITY = cast(c_ulong)(~0UL);
530 
531     enum RLIM_SAVED_MAX = RLIM_INFINITY;
532     enum RLIM_SAVED_CUR = RLIM_INFINITY;
533 
534     enum
535     {
536         RUSAGE_SELF     =  0,
537         RUSAGE_CHILDREN = -1,
538     }
539 
540     struct rusage
541     {
542         timeval ru_utime;
543         timeval ru_stime;
544         c_long ru_maxrss;
545         c_long ru_ixrss;
546         c_long ru_idrss;
547         c_long ru_isrss;
548         c_long ru_minflt;
549         c_long ru_majflt;
550         c_long ru_nswap;
551         c_long ru_inblock;
552         c_long ru_oublock;
553         c_long ru_msgsnd;
554         c_long ru_msgrcv;
555         c_long ru_nsignals;
556         c_long ru_nvcsw;
557         c_long ru_nivcsw;
558     }
559 
560     enum
561     {
562         RLIMIT_CORE   = 4,
563         RLIMIT_CPU    = 0,
564         RLIMIT_DATA   = 2,
565         RLIMIT_FSIZE  = 1,
566         RLIMIT_NOFILE = 7,
567         RLIMIT_STACK  = 3,
568         RLIMIT_AS     = 9,
569     }
570 }
571 else static assert (false, "Unsupported platform");
572 
573 struct rlimit
574 {
575     rlim_t rlim_cur;
576     rlim_t rlim_max;
577 }
578 
579 version (CRuntime_Glibc)
580 {
581     int getpriority(int, id_t);
582     int setpriority(int, id_t, int);
583 }
584 else version (FreeBSD)
585 {
586     int getpriority(int, int);
587     int setpriority(int, int, int);
588 }
589 else version (DragonFlyBSD)
590 {
591     int getpriority(int, int);
592     int setpriority(int, int, int);
593 }
594 else version (CRuntime_Bionic)
595 {
596     int getpriority(int, int);
597     int setpriority(int, int, int);
598 }
599 else version (Solaris)
600 {
601     int getpriority(int, id_t);
602     int setpriority(int, id_t, int);
603 }
604 else version (Darwin)
605 {
606     int getpriority(int, id_t);
607     int setpriority(int, id_t, int);
608 }
609 else version (CRuntime_UClibc)
610 {
611     int getpriority(int, id_t);
612     int setpriority(int, id_t, int);
613 }
614 
615 version (CRuntime_Glibc)
616 {
617     static if (__USE_FILE_OFFSET64)
618     {
619         int getrlimit64(int, rlimit*);
620         int setrlimit64(int, in rlimit*);
621         alias getrlimit = getrlimit64;
622         alias setrlimit = setrlimit64;
623     }
624     else
625     {
626         int getrlimit(int, rlimit*);
627         int setrlimit(int, in rlimit*);
628     }
629     int getrusage(int, rusage*);
630 }
631 else version (CRuntime_Bionic)
632 {
633     int getrlimit(int, rlimit*);
634     int getrusage(int, rusage*);
635     int setrlimit(int, in rlimit*);
636 }
637 else version (Darwin)
638 {
639     int getrlimit(int, rlimit*);
640     int getrusage(int, rusage*);
641     int setrlimit(int, in rlimit*);
642 }
643 else version (FreeBSD)
644 {
645     int getrlimit(int, rlimit*);
646     int getrusage(int, rusage*);
647     int setrlimit(int, in rlimit*);
648 }
649 else version (NetBSD)
650 {
651     int getrlimit(int, rlimit*);
652     int getrusage(int, rusage*);
653     int setrlimit(int, in rlimit*);
654 }
655 else version (DragonFlyBSD)
656 {
657     int getrlimit(int, rlimit*);
658     int getrusage(int, rusage*);
659     int setrlimit(int, in rlimit*);
660 }
661 else version (Solaris)
662 {
663     int getrlimit(int, rlimit*);
664     int getrusage(int, rusage*);
665     int setrlimit(int, in rlimit*);
666 }
667 else version (CRuntime_UClibc)
668 {
669     static if (__USE_FILE_OFFSET64)
670     {
671         int getrlimit64(int, rlimit*);
672         int setrlimit64(int, in rlimit*);
673         alias getrlimit = getrlimit64;
674         alias setrlimit = setrlimit64;
675     }
676     else
677     {
678         int getrlimit(int, rlimit*);
679         int setrlimit(int, in rlimit*);
680     }
681     int getrusage(int, rusage*);
682 }
683