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