Add systemd resource budgets with TOML profiles
This commit is contained in:
commit
be8749a384
1 changed files with 2591 additions and 0 deletions
52
internal/budget/launch.go
Normal file
52
internal/budget/launch.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package budget
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func systemctl(args ...string) error {
|
||||
cmd := exec.Command("systemctl", append([]string{"--user"}, args...)...)
|
||||
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func runArgs(c config, name, id string, command []string) ([]string, error) {
|
||||
if len(command) > 0 && command[0] == "--" {
|
||||
command = command[1:]
|
||||
}
|
||||
if len(command) == 0 {
|
||||
return nil, fmt.Errorf("a command is required")
|
||||
}
|
||||
values, err := c.assignments(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
slice := "app.slice"
|
||||
if name == "search" {
|
||||
slice = "search.slice"
|
||||
}
|
||||
args := []string{"systemd-run", "--user", "--scope", "--collect", "--quiet", "--unit=work-" + id, "--slice=" + slice}
|
||||
for _, value := range values {
|
||||
args = append(args, "--property="+value)
|
||||
}
|
||||
return append(append(args, "--"), command...), nil
|
||||
}
|
||||
|
||||
func launch(c config, name string, command []string) error {
|
||||
id, err := randomID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args, err := runArgs(c, name, id, command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path, err := exec.LookPath(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return syscall.Exec(path, args, os.Environ())
|
||||
}
|
||||
Loading…
Reference in a new issue