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