This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlimit.lisp
65 lines (55 loc) · 2.18 KB
/
rlimit.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(in-package :net.mwatters.libc-misc)
(defcfun (get-rlimit "getrlimit")
:int
(resource rlimit-resource-type)
(rlimit (:pointer (:struct rlimit))))
(defcfun (set-rlimit "setrlimit")
:int
(resource rlimit-resource-type)
(rlimit (:pointer (:struct rlimit))))
(defun get-resource-limit (which)
"Return the current resource limit value for WHICH, an
RLIMIT-RESOURCE-TYPE keyword \(e.g., :FILE-DESCRIPTORS\). Returns as
two values the current soft and hard limit values \(either integers or
:INFINITY\)."
(with-foreign-object (rlimit '(:struct rlimit))
(check-errno (get-rlimit which rlimit)
"failed to getrlimit\(\)")
(cffi:with-foreign-slots ((soft hard) rlimit (:struct rlimit))
(values (if (= +rlimit-infinity+ soft)
:infinity
soft)
(if (= +rlimit-infinity+ hard)
:infinity
hard)))))
(defun set-resource-limit (which value)
"Attempt to set the soft resource limit denoted by WHICH to the
given value. If VALUE is :INFINITY or greater than the current hard
limit, the value is clamped to the current hard limit value. Returns
as values the new soft and hard limits."
(multiple-value-bind (cur-soft cur-hard)
(get-resource-limit which)
(declare (ignore cur-soft))
(with-foreign-object (rlimit '(:struct rlimit))
(cffi:with-foreign-slots ((soft hard) rlimit (:struct rlimit))
(unless (eq :infinity cur-hard)
(when (or (eq :infinity value)
(> value cur-hard))
#+nil (warn "clamping requested value ~A for ~A to maximum ~A"
value which cur-hard)
(setq value cur-hard)))
;;
(setq
hard (if (eq :infinity cur-hard)
+rlimit-infinity+
cur-hard)
soft (if (eq :infinity value)
+rlimit-infinity+
value))
(check-errno (set-rlimit which rlimit)
"failed to setrlimit\(\)")
#+nil (warn "set resource limit value for ~A to ~A" which soft)
(values soft hard)))))
(defun (setf get-resource-limit) (value which)
(set-resource-limit which value)
value)