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