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 (OpenBSD)
305 {
306     enum
307     {
308         PRIO_PROCESS = 0,
309         PRIO_PGRP    = 1,
310         PRIO_USER    = 2,
311     }
312 
313     alias ulong rlim_t;
314 
315     enum
316     {
317         RLIM_INFINITY  = (cast(rlim_t)((cast(ulong) 1 << 63) - 1)),
318         RLIM_SAVED_MAX = RLIM_INFINITY,
319         RLIM_SAVED_CUR = RLIM_INFINITY,
320     }
321 
322     enum
323     {
324         RUSAGE_SELF     =  0,
325         RUSAGE_CHILDREN = -1,
326         RUSAGE_THREAD   =  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         // OpenBSD does not define the following:
360         //RLIMIT_AS,
361     }
362 }
363 else version (DragonFlyBSD)
364 {
365     enum
366     {
367         PRIO_PROCESS = 0,
368         PRIO_PGRP    = 1,
369         PRIO_USER    = 2,
370     }
371 
372     alias long rlim_t;
373 
374     enum
375     {
376         RLIM_INFINITY   = (cast(rlim_t)((cast(ulong) 1 << 63) - 1)),
377         // DragonFlyBSD explicitly does not define the following:
378         //RLIM_SAVED_MAX,
379         //RLIM_SAVED_CUR,
380     }
381 
382     enum
383     {
384         RUSAGE_SELF     =  0,
385         RUSAGE_CHILDREN = -1,
386     }
387 
388     struct rusage
389     {
390         timeval ru_utime;
391         timeval ru_stime;
392         c_long ru_maxrss;
393         alias ru_ixrss ru_first;
394         c_long ru_ixrss;
395         c_long ru_idrss;
396         c_long ru_isrss;
397         c_long ru_minflt;
398         c_long ru_majflt;
399         c_long ru_nswap;
400         c_long ru_inblock;
401         c_long ru_oublock;
402         c_long ru_msgsnd;
403         c_long ru_msgrcv;
404         c_long ru_nsignals;
405         c_long ru_nvcsw;
406         c_long ru_nivcsw;
407         alias ru_nivcsw ru_last;
408     }
409 
410     enum
411     {
412         RLIMIT_CORE   =  4,
413         RLIMIT_CPU    =  0,
414         RLIMIT_DATA   =  2,
415         RLIMIT_FSIZE  =  1,
416         RLIMIT_NOFILE =  8,
417         RLIMIT_STACK  =  3,
418         RLIMIT_AS     = 10,
419     }
420 }
421 else version (Solaris)
422 {
423     enum
424     {
425         PRIO_PROCESS = 0,
426         PRIO_PGRP    = 1,
427         PRIO_USER    = 2,
428     }
429 
430     alias c_ulong rlim_t;
431 
432     enum : c_long
433     {
434         RLIM_INFINITY   = -3,
435         RLIM_SAVED_MAX  = -2,
436         RLIM_SAVED_CUR  = -1,
437     }
438 
439     enum
440     {
441         RUSAGE_SELF     =  0,
442         RUSAGE_CHILDREN = -1,
443     }
444 
445     struct rusage
446     {
447         timeval ru_utime;
448         timeval ru_stime;
449         c_long ru_maxrss;
450         c_long ru_ixrss;
451         c_long ru_idrss;
452         c_long ru_isrss;
453         c_long ru_minflt;
454         c_long ru_majflt;
455         c_long ru_nswap;
456         c_long ru_inblock;
457         c_long ru_oublock;
458         c_long ru_msgsnd;
459         c_long ru_msgrcv;
460         c_long ru_nsignals;
461         c_long ru_nvcsw;
462         c_long ru_nivcsw;
463     }
464 
465     enum
466     {
467         RLIMIT_CORE   = 4,
468         RLIMIT_CPU    = 0,
469         RLIMIT_DATA   = 2,
470         RLIMIT_FSIZE  = 1,
471         RLIMIT_NOFILE = 5,
472         RLIMIT_STACK  = 3,
473         RLIMIT_AS     = 6,
474     }
475 }
476 else version (CRuntime_Bionic)
477 {
478     enum
479     {
480         PRIO_PROCESS = 0,
481         PRIO_PGRP    = 1,
482         PRIO_USER    = 2,
483     }
484 
485     alias c_ulong rlim_t;
486     enum RLIM_INFINITY = cast(c_ulong)(~0UL);
487 
488     enum
489     {
490         RUSAGE_SELF     =  0,
491         RUSAGE_CHILDREN = -1,
492     }
493 
494     struct rusage
495     {
496         timeval ru_utime;
497         timeval ru_stime;
498         c_long ru_maxrss;
499         c_long ru_ixrss;
500         c_long ru_idrss;
501         c_long ru_isrss;
502         c_long ru_minflt;
503         c_long ru_majflt;
504         c_long ru_nswap;
505         c_long ru_inblock;
506         c_long ru_oublock;
507         c_long ru_msgsnd;
508         c_long ru_msgrcv;
509         c_long ru_nsignals;
510         c_long ru_nvcsw;
511         c_long ru_nivcsw;
512     }
513 
514     enum
515     {
516         RLIMIT_CORE   = 4,
517         RLIMIT_CPU    = 0,
518         RLIMIT_DATA   = 2,
519         RLIMIT_FSIZE  = 1,
520         RLIMIT_NOFILE = 7,
521         RLIMIT_STACK  = 3,
522         RLIMIT_AS     = 9,
523     }
524 }
525 else version (CRuntime_Musl)
526 {
527     alias ulong rlim_t;
528     enum RLIM_INFINITY = cast(c_ulong)(~0UL);
529 
530     int getrlimit(int, rlimit*);
531     int setrlimit(int, in rlimit*);
532     alias getrlimit getrlimit64;
533     alias setrlimit setrlimit64;
534     enum
535     {
536         RUSAGE_SELF = 0,
537         RUSAGE_CHILDREN = -1,
538         RUSAGE_THREAD = 1
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         c_long[16] __reserved;
559     }
560 
561     enum
562     {
563         RLIMIT_CPU    = 0,
564         RLIMIT_FSIZE  = 1,
565         RLIMIT_DATA   = 2,
566         RLIMIT_STACK  = 3,
567         RLIMIT_CORE   = 4,
568         RLIMIT_NOFILE = 7,
569         RLIMIT_AS     = 9,
570     }
571 }
572 else version (CRuntime_UClibc)
573 {
574     enum
575     {
576         PRIO_PROCESS = 0,
577         PRIO_PGRP    = 1,
578         PRIO_USER    = 2,
579     }
580 
581     static if (__USE_FILE_OFFSET64)
582          alias ulong rlim_t;
583     else
584          alias c_ulong rlim_t;
585 
586     static if (__USE_FILE_OFFSET64)
587         enum RLIM_INFINITY = 0xffffffffffffffffUL;
588     else
589         enum RLIM_INFINITY = cast(c_ulong)(~0UL);
590 
591     enum RLIM_SAVED_MAX = RLIM_INFINITY;
592     enum RLIM_SAVED_CUR = RLIM_INFINITY;
593 
594     enum
595     {
596         RUSAGE_SELF     =  0,
597         RUSAGE_CHILDREN = -1,
598     }
599 
600     struct rusage
601     {
602         timeval ru_utime;
603         timeval ru_stime;
604         c_long ru_maxrss;
605         c_long ru_ixrss;
606         c_long ru_idrss;
607         c_long ru_isrss;
608         c_long ru_minflt;
609         c_long ru_majflt;
610         c_long ru_nswap;
611         c_long ru_inblock;
612         c_long ru_oublock;
613         c_long ru_msgsnd;
614         c_long ru_msgrcv;
615         c_long ru_nsignals;
616         c_long ru_nvcsw;
617         c_long ru_nivcsw;
618     }
619 
620     enum
621     {
622         RLIMIT_CORE   = 4,
623         RLIMIT_CPU    = 0,
624         RLIMIT_DATA   = 2,
625         RLIMIT_FSIZE  = 1,
626         RLIMIT_NOFILE = 7,
627         RLIMIT_STACK  = 3,
628         RLIMIT_AS     = 9,
629     }
630 }
631 else static assert (false, "Unsupported platform");
632 
633 struct rlimit
634 {
635     rlim_t rlim_cur;
636     rlim_t rlim_max;
637 }
638 
639 version (CRuntime_Glibc)
640 {
641     int getpriority(int, id_t);
642     int setpriority(int, id_t, int);
643 }
644 else version (FreeBSD)
645 {
646     int getpriority(int, int);
647     int setpriority(int, int, int);
648 }
649 else version (DragonFlyBSD)
650 {
651     int getpriority(int, int);
652     int setpriority(int, int, int);
653 }
654 else version (CRuntime_Bionic)
655 {
656     int getpriority(int, int);
657     int setpriority(int, int, int);
658 }
659 else version (Solaris)
660 {
661     int getpriority(int, id_t);
662     int setpriority(int, id_t, int);
663 }
664 else version (Darwin)
665 {
666     int getpriority(int, id_t);
667     int setpriority(int, id_t, int);
668 }
669 else version (CRuntime_UClibc)
670 {
671     int getpriority(int, id_t);
672     int setpriority(int, id_t, int);
673 }
674 
675 version (CRuntime_Glibc)
676 {
677     static if (__USE_FILE_OFFSET64)
678     {
679         int getrlimit64(int, rlimit*);
680         int setrlimit64(int, in rlimit*);
681         alias getrlimit = getrlimit64;
682         alias setrlimit = setrlimit64;
683     }
684     else
685     {
686         int getrlimit(int, rlimit*);
687         int setrlimit(int, in rlimit*);
688     }
689     int getrusage(int, rusage*);
690 }
691 else version (CRuntime_Bionic)
692 {
693     int getrlimit(int, rlimit*);
694     int getrusage(int, rusage*);
695     int setrlimit(int, in rlimit*);
696 }
697 else version (Darwin)
698 {
699     int getrlimit(int, rlimit*);
700     int getrusage(int, rusage*);
701     int setrlimit(int, in rlimit*);
702 }
703 else version (FreeBSD)
704 {
705     int getrlimit(int, rlimit*);
706     int getrusage(int, rusage*);
707     int setrlimit(int, in rlimit*);
708 }
709 else version (NetBSD)
710 {
711     int getrlimit(int, rlimit*);
712     int getrusage(int, rusage*);
713     int setrlimit(int, in rlimit*);
714 }
715 else version (OpenBSD)
716 {
717     int getrlimit(int, rlimit*);
718     int getrusage(int, rusage*);
719     int setrlimit(int, in rlimit*);
720 }
721 else version (DragonFlyBSD)
722 {
723     int getrlimit(int, rlimit*);
724     int getrusage(int, rusage*);
725     int setrlimit(int, in rlimit*);
726 }
727 else version (Solaris)
728 {
729     int getrlimit(int, rlimit*);
730     int getrusage(int, rusage*);
731     int setrlimit(int, in rlimit*);
732 }
733 else version (CRuntime_UClibc)
734 {
735     static if (__USE_FILE_OFFSET64)
736     {
737         int getrlimit64(int, rlimit*);
738         int setrlimit64(int, in rlimit*);
739         alias getrlimit = getrlimit64;
740         alias setrlimit = setrlimit64;
741     }
742     else
743     {
744         int getrlimit(int, rlimit*);
745         int setrlimit(int, in rlimit*);
746     }
747     int getrusage(int, rusage*);
748 }
749