-
Notifications
You must be signed in to change notification settings - Fork 233
/
rlimit.go
66 lines (57 loc) · 1.32 KB
/
rlimit.go
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
66
// This file is part of the program "NoiseTorch-ng".
// Please see the LICENSE file for copyright information.
package main
import (
"log"
"os"
"strconv"
"strings"
"syscall"
"unsafe"
"github.com/noisetorch/pulseaudio"
)
const rlimitRTTime = 15
func getPulsePid() (int, error) {
pulsepidfile, err := pulseaudio.RuntimePath("pid")
if err != nil {
return 0, err
}
pidbuf, err := os.ReadFile(pulsepidfile)
if err != nil {
return 0, err
}
pid, err := strconv.Atoi(strings.TrimSpace(string(pidbuf)))
if err != nil {
return 0, err
}
return pid, nil
}
func getRlimit(pid int) (syscall.Rlimit, error) {
var res syscall.Rlimit
err := pRlimit(pid, rlimitRTTime, nil, &res)
return res, err
}
func setRlimit(pid int, new *syscall.Rlimit) error {
var junk syscall.Rlimit
err := pRlimit(pid, rlimitRTTime, new, &junk)
return err
}
func removeRlimit(pid int) {
const MaxUint = ^uint64(0)
new := syscall.Rlimit{Cur: MaxUint, Max: MaxUint}
err := setRlimit(pid, &new)
if err != nil {
log.Printf("Couldn't set rlimit with caps\n")
}
}
func pRlimit(pid int, limit uintptr, new *syscall.Rlimit, old *syscall.Rlimit) error {
_, _, errno := syscall.RawSyscall6(syscall.SYS_PRLIMIT64,
uintptr(pid),
limit,
uintptr(unsafe.Pointer(new)),
uintptr(unsafe.Pointer(old)), 0, 0)
if errno != 0 {
return errno
}
return nil
}