mirror of
https://github.com/taigrr/systemctl.git
synced 2026-04-02 10:38:48 -07:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d38136c0dc | |||
|
|
14c9f0f70d | ||
| 5f1537f8bc | |||
|
|
d38d347cc6 | ||
| 14a2ca2acd | |||
| 451a949ace | |||
| 21fce7918e | |||
| 54f4f7a235 | |||
| fa15432121 | |||
|
7bd5bef0cb
|
|||
| a82f845b84 | |||
| c9e7f79f8c | |||
| 0075dc6b4d | |||
|
5da4924315
|
|||
|
33828cf7b9
|
|||
|
e44344ee7d
|
|||
|
c8050d2258
|
|||
|
ae23e6ecb9
|
|||
|
c9dec8a0b7
|
|||
|
f10c42cec1
|
|||
|
f1979375cd
|
|||
|
4e38d03629
|
|||
|
701893ebbd
|
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: taigrr # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||||
|
patreon: # Replace with a single Patreon username
|
||||||
|
open_collective: # Replace with a single Open Collective username
|
||||||
|
ko_fi: # Replace with a single Ko-fi username
|
||||||
|
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||||
|
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||||
|
liberapay: # Replace with a single Liberapay username
|
||||||
|
issuehunt: # Replace with a single IssueHunt username
|
||||||
|
otechie: # Replace with a single Otechie username
|
||||||
|
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||||
11
README.md
11
README.md
@@ -5,7 +5,6 @@ This library aims at providing idiomatic `systemctl` bindings for go developers,
|
|||||||
This tool tries to take guesswork out of arbitrarily shelling out to `systemctl` by providing a structured, thoroughly-tested wrapper for the `systemctl` functions most-likely to be used in a system program.
|
This tool tries to take guesswork out of arbitrarily shelling out to `systemctl` by providing a structured, thoroughly-tested wrapper for the `systemctl` functions most-likely to be used in a system program.
|
||||||
|
|
||||||
If your system isn't running (or targeting another system running) `systemctl`, this library will be of little use to you.
|
If your system isn't running (or targeting another system running) `systemctl`, this library will be of little use to you.
|
||||||
In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
|
|
||||||
|
|
||||||
## What is systemctl
|
## What is systemctl
|
||||||
|
|
||||||
@@ -18,6 +17,7 @@ In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
|
|||||||
- [x] `systemctl daemon-reload`
|
- [x] `systemctl daemon-reload`
|
||||||
- [x] `systemctl disable`
|
- [x] `systemctl disable`
|
||||||
- [x] `systemctl enable`
|
- [x] `systemctl enable`
|
||||||
|
- [x] `systemctl reenable`
|
||||||
- [x] `systemctl is-active`
|
- [x] `systemctl is-active`
|
||||||
- [x] `systemctl is-enabled`
|
- [x] `systemctl is-enabled`
|
||||||
- [x] `systemctl is-failed`
|
- [x] `systemctl is-failed`
|
||||||
@@ -32,7 +32,7 @@ In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
|
|||||||
## Helper functionality
|
## Helper functionality
|
||||||
|
|
||||||
- [x] Get start time of a service (`ExecMainStartTimestamp`) as a `Time` type
|
- [x] Get start time of a service (`ExecMainStartTimestamp`) as a `Time` type
|
||||||
- [x] Get current memory in bytes (`MemoryCurrent`) an an int
|
- [x] Get current memory in bytes (`MemoryCurrent`) as an int
|
||||||
- [x] Get the PID of the main process (`MainPID`) as an int
|
- [x] Get the PID of the main process (`MainPID`) as an int
|
||||||
- [x] Get the restart count of a unit (`NRestarts`) as an int
|
- [x] Get the restart count of a unit (`NRestarts`) as an int
|
||||||
|
|
||||||
@@ -65,10 +65,9 @@ func main() {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
// Equivalent to `systemctl enable nginx` with a 10 second timeout
|
// Equivalent to `systemctl enable nginx` with a 10 second timeout
|
||||||
opts := Options{
|
opts := systemctl.Options{ UserMode: false }
|
||||||
usermode: false,
|
unit := "nginx"
|
||||||
}
|
err := systemctl.Enable(ctx, unit, opts)
|
||||||
err := Enable(ctx, unit, opts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to enable unit %s: %v", "nginx", err)
|
log.Fatalf("unable to enable unit %s: %v", "nginx", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ var (
|
|||||||
// Masked units can only be unmasked, but something else was attempted
|
// Masked units can only be unmasked, but something else was attempted
|
||||||
// Unmask the unit before enabling or disabling it
|
// Unmask the unit before enabling or disabling it
|
||||||
ErrMasked = errors.New("unit masked")
|
ErrMasked = errors.New("unit masked")
|
||||||
// If this error occurs, the library isn't entirely useful, as it causes a panic
|
|
||||||
// Make sure systemctl is in the PATH before calling again
|
// Make sure systemctl is in the PATH before calling again
|
||||||
ErrNotInstalled = errors.New("systemctl not in $PATH")
|
ErrNotInstalled = errors.New("systemctl not in $PATH")
|
||||||
// A unit was expected to be running but was found inactive
|
// A unit was expected to be running but was found inactive
|
||||||
@@ -36,5 +35,5 @@ var (
|
|||||||
|
|
||||||
// Something in the stderr output contains the word `Failed`, but it is not a known case
|
// Something in the stderr output contains the word `Failed`, but it is not a known case
|
||||||
// This is a catch-all, and if it's ever seen in the wild, please submit a PR
|
// This is a catch-all, and if it's ever seen in the wild, please submit a PR
|
||||||
ErrUnspecified = errors.New("Unknown error, please submit an issue at github.com/taigrr/systemctl")
|
ErrUnspecified = errors.New("unknown error, please submit an issue at github.com/taigrr/systemctl")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -25,7 +26,7 @@ func TestErrorFuncs(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
/* Run these tests only as an unpriviledged user */
|
/* Run these tests only as an unpriviledged user */
|
||||||
|
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
|
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", nil, Options{UserMode: true}, true},
|
{"syncthing", nil, Options{UserMode: true}, true},
|
||||||
@@ -53,7 +54,6 @@ func TestErrorFuncs(t *testing.T) {
|
|||||||
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
||||||
fName = strings.TrimPrefix(fName, "github.com/taigrr/")
|
fName = strings.TrimPrefix(fName, "github.com/taigrr/")
|
||||||
t.Run(fmt.Sprintf("Errorcheck %s", fName), func(t *testing.T) {
|
t.Run(fmt.Sprintf("Errorcheck %s", fName), func(t *testing.T) {
|
||||||
|
|
||||||
for _, tc := range errCases {
|
for _, tc := range errCases {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
@@ -64,7 +64,7 @@ func TestErrorFuncs(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := f(ctx, tc.unit, tc.opts)
|
err := f(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,5 +1,3 @@
|
|||||||
module github.com/taigrr/systemctl
|
module github.com/taigrr/systemctl
|
||||||
|
|
||||||
go 1.16
|
go 1.26
|
||||||
|
|
||||||
require golang.org/x/tools v0.1.1 // indirect
|
|
||||||
|
|||||||
33
go.sum
33
go.sum
@@ -1,33 +0,0 @@
|
|||||||
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs=
|
|
||||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
|
|
||||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
|
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
|
|
||||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
|
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
|
|
||||||
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
|
|||||||
125
helpers.go
125
helpers.go
@@ -2,7 +2,10 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/taigrr/systemctl/properties"
|
"github.com/taigrr/systemctl/properties"
|
||||||
@@ -13,13 +16,12 @@ const dateFormat = "Mon 2006-01-02 15:04:05 MST"
|
|||||||
// Get start time of a service (`systemctl show [unit] --property ExecMainStartTimestamp`) as a `Time` type
|
// Get start time of a service (`systemctl show [unit] --property ExecMainStartTimestamp`) as a `Time` type
|
||||||
func GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, error) {
|
func GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, error) {
|
||||||
value, err := Show(ctx, unit, properties.ExecMainStartTimestamp, opts)
|
value, err := Show(ctx, unit, properties.ExecMainStartTimestamp, opts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Unix(0, 0), err
|
return time.Time{}, err
|
||||||
}
|
}
|
||||||
// ExecMainStartTimestamp returns an empty string if the unit is not running
|
// ExecMainStartTimestamp returns an empty string if the unit is not running
|
||||||
if value == "" {
|
if value == "" {
|
||||||
return time.Unix(0, 0), ErrUnitNotActive
|
return time.Time{}, ErrUnitNotActive
|
||||||
}
|
}
|
||||||
return time.Parse(dateFormat, value)
|
return time.Parse(dateFormat, value)
|
||||||
}
|
}
|
||||||
@@ -33,7 +35,7 @@ func GetNumRestarts(ctx context.Context, unit string, opts Options) (int, error)
|
|||||||
return strconv.Atoi(value)
|
return strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current memory in bytes (`systemctl show [unit] --property MemoryCurrent`) an an int
|
// Get current memory in bytes (`systemctl show [unit] --property MemoryCurrent`) as an int
|
||||||
func GetMemoryUsage(ctx context.Context, unit string, opts Options) (int, error) {
|
func GetMemoryUsage(ctx context.Context, unit string, opts Options) (int, error) {
|
||||||
value, err := Show(ctx, unit, properties.MemoryCurrent, opts)
|
value, err := Show(ctx, unit, properties.MemoryCurrent, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -53,3 +55,118 @@ func GetPID(ctx context.Context, unit string, opts Options) (int, error) {
|
|||||||
}
|
}
|
||||||
return strconv.Atoi(value)
|
return strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSocketsForServiceUnit returns the socket units associated with a given service unit.
|
||||||
|
func GetSocketsForServiceUnit(ctx context.Context, unit string, opts Options) ([]string, error) {
|
||||||
|
args := []string{"list-sockets", "--all", "--no-legend", "--no-pager"}
|
||||||
|
if opts.UserMode {
|
||||||
|
args = append(args, "--user")
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
lines := strings.Split(stdout, "\n")
|
||||||
|
sockets := []string{}
|
||||||
|
for _, line := range lines {
|
||||||
|
fields := strings.Fields(line)
|
||||||
|
if len(fields) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
socketUnit := fields[1]
|
||||||
|
serviceUnit := fields[2]
|
||||||
|
if serviceUnit == unit+".service" {
|
||||||
|
sockets = append(sockets, socketUnit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sockets, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUnits returns a list of all loaded units and their states.
|
||||||
|
func GetUnits(ctx context.Context, opts Options) ([]Unit, error) {
|
||||||
|
args := []string{"list-units", "--all", "--no-legend", "--full", "--no-pager"}
|
||||||
|
if opts.UserMode {
|
||||||
|
args = append(args, "--user")
|
||||||
|
}
|
||||||
|
stdout, stderr, _, err := execute(ctx, args)
|
||||||
|
if err != nil {
|
||||||
|
return []Unit{}, errors.Join(err, filterErr(stderr))
|
||||||
|
}
|
||||||
|
lines := strings.Split(stdout, "\n")
|
||||||
|
units := []Unit{}
|
||||||
|
for _, line := range lines {
|
||||||
|
entry := strings.Fields(line)
|
||||||
|
if len(entry) < 4 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
unit := Unit{
|
||||||
|
Name: entry[0],
|
||||||
|
Load: entry[1],
|
||||||
|
Active: entry[2],
|
||||||
|
Sub: entry[3],
|
||||||
|
Description: strings.Join(entry[4:], " "),
|
||||||
|
}
|
||||||
|
units = append(units, unit)
|
||||||
|
}
|
||||||
|
return units, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMaskedUnits returns a list of all masked unit names.
|
||||||
|
func GetMaskedUnits(ctx context.Context, opts Options) ([]string, error) {
|
||||||
|
args := []string{"list-unit-files", "--state=masked"}
|
||||||
|
if opts.UserMode {
|
||||||
|
args = append(args, "--user")
|
||||||
|
}
|
||||||
|
stdout, stderr, _, err := execute(ctx, args)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, errors.Join(err, filterErr(stderr))
|
||||||
|
}
|
||||||
|
lines := strings.Split(stdout, "\n")
|
||||||
|
units := []string{}
|
||||||
|
for _, line := range lines {
|
||||||
|
if !strings.Contains(line, "masked") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry := strings.Split(line, " ")
|
||||||
|
if len(entry) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if entry[1] == "masked" {
|
||||||
|
unit := entry[0]
|
||||||
|
uName := strings.Split(unit, ".")
|
||||||
|
unit = uName[0]
|
||||||
|
units = append(units, unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return units, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSystemd checks if systemd is the current init system by reading /proc/1/comm.
|
||||||
|
func IsSystemd() (bool, error) {
|
||||||
|
b, err := os.ReadFile("/proc/1/comm")
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(b)) == "systemd", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMasked checks if a unit is masked.
|
||||||
|
func IsMasked(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
units, err := GetMaskedUnits(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for _, u := range units {
|
||||||
|
if u == unit {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsRunning checks if a unit's sub-state is "running".
|
||||||
|
// See https://unix.stackexchange.com/a/396633 for details.
|
||||||
|
func IsRunning(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
status, err := Show(ctx, unit, properties.SubState, opts)
|
||||||
|
return status == "running", err
|
||||||
|
}
|
||||||
|
|||||||
186
helpers_test.go
186
helpers_test.go
@@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -27,7 +28,7 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
|
{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
|
{"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
|
||||||
@@ -58,13 +59,13 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := GetStartTime(ctx, tc.unit, tc.opts)
|
_, err := GetStartTime(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Prove start time changes after a restart
|
// Prove start time changes after a restart
|
||||||
t.Run(fmt.Sprintf("prove start time changes"), func(t *testing.T) {
|
t.Run("prove start time changes", func(t *testing.T) {
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -90,18 +91,19 @@ func TestGetStartTime(t *testing.T) {
|
|||||||
t.Errorf("Expected start diff to be positive, but got: %d", int(diff))
|
t.Errorf("Expected start diff to be positive, but got: %d", int(diff))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetNumRestarts(t *testing.T) {
|
func TestGetNumRestarts(t *testing.T) {
|
||||||
testCases := []struct {
|
type testCase struct {
|
||||||
unit string
|
unit string
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}
|
||||||
|
testCases := []testCase{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
|
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, true},
|
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", ErrValueNotSet, Options{UserMode: true}, true},
|
{"syncthing", ErrValueNotSet, Options{UserMode: true}, true},
|
||||||
@@ -118,23 +120,25 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
{"nginx", nil, Options{UserMode: false}, false},
|
{"nginx", nil, Options{UserMode: false}, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
func(tc testCase) {
|
||||||
t.Parallel()
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
t.Parallel()
|
||||||
t.Skip("skipping user test while running as superuser")
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
t.Skip("skipping user test while running as superuser")
|
||||||
t.Skip("skipping superuser test while running as user")
|
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
||||||
}
|
t.Skip("skipping superuser test while running as user")
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
}
|
||||||
defer cancel()
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
_, err := GetNumRestarts(ctx, tc.unit, tc.opts)
|
defer cancel()
|
||||||
if err != tc.err {
|
_, err := GetNumRestarts(ctx, tc.unit, tc.opts)
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
if !errors.Is(err, tc.err) {
|
||||||
}
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
})
|
}
|
||||||
|
})
|
||||||
|
}(tc)
|
||||||
}
|
}
|
||||||
// Prove restart count increases by one after a restart
|
// Prove restart count increases by one after a restart
|
||||||
t.Run(fmt.Sprintf("prove restart count increases by one after a restart"), func(t *testing.T) {
|
t.Run("prove restart count increases by one after a restart", func(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping in short mode")
|
t.Skip("skipping in short mode")
|
||||||
}
|
}
|
||||||
@@ -154,9 +158,9 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
syscall.Kill(pid, syscall.SIGKILL)
|
syscall.Kill(pid, syscall.SIGKILL)
|
||||||
for {
|
for {
|
||||||
running, err := IsActive(ctx, "nginx", Options{UserMode: false})
|
running, errIsActive := IsActive(ctx, "nginx", Options{UserMode: false})
|
||||||
if err != nil {
|
if errIsActive != nil {
|
||||||
t.Errorf("error asserting nginx is up: %v", err)
|
t.Errorf("error asserting nginx is up: %v", errIsActive)
|
||||||
break
|
break
|
||||||
} else if running {
|
} else if running {
|
||||||
break
|
break
|
||||||
@@ -170,19 +174,19 @@ func TestGetNumRestarts(t *testing.T) {
|
|||||||
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
|
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMemoryUsage(t *testing.T) {
|
func TestGetMemoryUsage(t *testing.T) {
|
||||||
testCases := []struct {
|
type testCase struct {
|
||||||
unit string
|
unit string
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}
|
||||||
|
testCases := []testCase{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
|
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, true},
|
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", ErrValueNotSet, Options{UserMode: true}, true},
|
{"syncthing", ErrValueNotSet, Options{UserMode: true}, true},
|
||||||
@@ -199,23 +203,25 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
{"nginx", nil, Options{UserMode: false}, false},
|
{"nginx", nil, Options{UserMode: false}, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
func(tc testCase) {
|
||||||
t.Parallel()
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
t.Parallel()
|
||||||
t.Skip("skipping user test while running as superuser")
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
t.Skip("skipping user test while running as superuser")
|
||||||
t.Skip("skipping superuser test while running as user")
|
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
||||||
}
|
t.Skip("skipping superuser test while running as user")
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
}
|
||||||
defer cancel()
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
_, err := GetMemoryUsage(ctx, tc.unit, tc.opts)
|
defer cancel()
|
||||||
if err != tc.err {
|
_, err := GetMemoryUsage(ctx, tc.unit, tc.opts)
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
if !errors.Is(err, tc.err) {
|
||||||
}
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
})
|
}
|
||||||
|
})
|
||||||
|
}(tc)
|
||||||
}
|
}
|
||||||
// Prove memory usage values change across services
|
// Prove memory usage values change across services
|
||||||
t.Run(fmt.Sprintf("prove memory usage values change across services"), func(t *testing.T) {
|
t.Run("prove memory usage values change across services", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false})
|
bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false})
|
||||||
@@ -230,18 +236,68 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes)
|
t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetUnits(t *testing.T) {
|
||||||
|
type testCase struct {
|
||||||
|
err error
|
||||||
|
runAsUser bool
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
testCases := []testCase{{
|
||||||
|
// Run these tests only as a user
|
||||||
|
runAsUser: true,
|
||||||
|
opts: Options{UserMode: true},
|
||||||
|
err: nil,
|
||||||
|
}}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(fmt.Sprintf("as %s", userString), func(t *testing.T) {
|
||||||
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
|
t.Skip("skipping user test while running as superuser")
|
||||||
|
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
||||||
|
t.Skip("skipping superuser test while running as user")
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
units, err := GetUnits(ctx, tc.opts)
|
||||||
|
if !errors.Is(err, tc.err) {
|
||||||
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
|
}
|
||||||
|
if len(units) == 0 {
|
||||||
|
t.Errorf("Expected at least one unit, but got none")
|
||||||
|
}
|
||||||
|
unit := units[0]
|
||||||
|
if unit.Name == "" {
|
||||||
|
t.Errorf("Expected unit name to be non-empty, but got empty")
|
||||||
|
}
|
||||||
|
if unit.Load == "" {
|
||||||
|
t.Errorf("Expected unit load state to be non-empty, but got empty")
|
||||||
|
}
|
||||||
|
if unit.Active == "" {
|
||||||
|
t.Errorf("Expected unit active state to be non-empty, but got empty")
|
||||||
|
}
|
||||||
|
if unit.Sub == "" {
|
||||||
|
t.Errorf("Expected unit sub state to be non-empty, but got empty")
|
||||||
|
}
|
||||||
|
if unit.Description == "" {
|
||||||
|
t.Errorf("Expected unit description to be non-empty, but got empty")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetPID(t *testing.T) {
|
func TestGetPID(t *testing.T) {
|
||||||
testCases := []struct {
|
type testCase struct {
|
||||||
unit string
|
unit string
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
testCases := []testCase{
|
||||||
// Run these tests only as a user
|
// Run these tests only as a user
|
||||||
|
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", nil, Options{UserMode: false}, true},
|
{"nonexistant", nil, Options{UserMode: false}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", nil, Options{UserMode: true}, true},
|
{"syncthing", nil, Options{UserMode: true}, true},
|
||||||
@@ -258,22 +314,24 @@ func TestGetPID(t *testing.T) {
|
|||||||
{"nginx", nil, Options{UserMode: false}, false},
|
{"nginx", nil, Options{UserMode: false}, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
func(tc testCase) {
|
||||||
t.Parallel()
|
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
t.Parallel()
|
||||||
t.Skip("skipping user test while running as superuser")
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
t.Skip("skipping user test while running as superuser")
|
||||||
t.Skip("skipping superuser test while running as user")
|
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
||||||
}
|
t.Skip("skipping superuser test while running as user")
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
}
|
||||||
defer cancel()
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
_, err := GetPID(ctx, tc.unit, tc.opts)
|
defer cancel()
|
||||||
if err != tc.err {
|
_, err := GetPID(ctx, tc.unit, tc.opts)
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
if !errors.Is(err, tc.err) {
|
||||||
}
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
})
|
}
|
||||||
|
})
|
||||||
|
}(tc)
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("prove pid changes"), func(t *testing.T) {
|
t.Run("prove pid changes", func(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping in short mode")
|
t.Skip("skipping in short mode")
|
||||||
}
|
}
|
||||||
@@ -296,7 +354,5 @@ func TestGetPID(t *testing.T) {
|
|||||||
if pid == secondPid {
|
if pid == secondPid {
|
||||||
t.Errorf("Expected pid != secondPid, but both were: %d", pid)
|
t.Errorf("Expected pid != secondPid, but both were: %d", pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,246 +3,330 @@ package properties
|
|||||||
type Property string
|
type Property string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ActiveEnterTimestamp Property = "ActiveEnterTimestamp"
|
Accept Property = "Accept"
|
||||||
ActiveEnterTimestampMonotonic Property = "ActiveEnterTimestampMonotonic"
|
ActiveEnterTimestamp Property = "ActiveEnterTimestamp"
|
||||||
ActiveExitTimestampMonotonic Property = "ActiveExitTimestampMonotonic"
|
ActiveEnterTimestampMonotonic Property = "ActiveEnterTimestampMonotonic"
|
||||||
ActiveState Property = "ActiveState"
|
ActiveExitTimestampMonotonic Property = "ActiveExitTimestampMonotonic"
|
||||||
After Property = "After"
|
ActiveState Property = "ActiveState"
|
||||||
AllowIsolate Property = "AllowIsolate"
|
After Property = "After"
|
||||||
AssertResult Property = "AssertResult"
|
AllowIsolate Property = "AllowIsolate"
|
||||||
AssertTimestamp Property = "AssertTimestamp"
|
AssertResult Property = "AssertResult"
|
||||||
AssertTimestampMonotonic Property = "AssertTimestampMonotonic"
|
AssertTimestamp Property = "AssertTimestamp"
|
||||||
Before Property = "Before"
|
AssertTimestampMonotonic Property = "AssertTimestampMonotonic"
|
||||||
BlockIOAccounting Property = "BlockIOAccounting"
|
Backlog Property = "Backlog"
|
||||||
BlockIOWeight Property = "BlockIOWeight"
|
Before Property = "Before"
|
||||||
CPUAccounting Property = "CPUAccounting"
|
BindIPv6Only Property = "BindIPv6Only"
|
||||||
CPUAffinityFromNUMA Property = "CPUAffinityFromNUMA"
|
BindLogSockets Property = "BindLogSockets"
|
||||||
CPUQuotaPerSecUSec Property = "CPUQuotaPerSecUSec"
|
BlockIOAccounting Property = "BlockIOAccounting"
|
||||||
CPUQuotaPeriodUSec Property = "CPUQuotaPeriodUSec"
|
BlockIOWeight Property = "BlockIOWeight"
|
||||||
CPUSchedulingPolicy Property = "CPUSchedulingPolicy"
|
Broadcast Property = "Broadcast"
|
||||||
CPUSchedulingPriority Property = "CPUSchedulingPriority"
|
CPUAccounting Property = "CPUAccounting"
|
||||||
CPUSchedulingResetOnFork Property = "CPUSchedulingResetOnFork"
|
CPUAffinityFromNUMA Property = "CPUAffinityFromNUMA"
|
||||||
CPUShares Property = "CPUShares"
|
CPUQuotaPerSecUSec Property = "CPUQuotaPerSecUSec"
|
||||||
CPUUsageNSec Property = "CPUUsageNSec"
|
CPUQuotaPeriodUSec Property = "CPUQuotaPeriodUSec"
|
||||||
CPUWeight Property = "CPUWeight"
|
CPUSchedulingPolicy Property = "CPUSchedulingPolicy"
|
||||||
CacheDirectoryMode Property = "CacheDirectoryMode"
|
CPUSchedulingPriority Property = "CPUSchedulingPriority"
|
||||||
CanFreeze Property = "CanFreeze"
|
CPUSchedulingResetOnFork Property = "CPUSchedulingResetOnFork"
|
||||||
CanIsolate Property = "CanIsolate"
|
CPUShares Property = "CPUShares"
|
||||||
CanReload Property = "CanReload"
|
CPUUsageNSec Property = "CPUUsageNSec"
|
||||||
CanStart Property = "CanStart"
|
CPUWeight Property = "CPUWeight"
|
||||||
CanStop Property = "CanStop"
|
CacheDirectoryMode Property = "CacheDirectoryMode"
|
||||||
CapabilityBoundingSet Property = "CapabilityBoundingSet"
|
CanFreeze Property = "CanFreeze"
|
||||||
CleanResult Property = "CleanResult"
|
CanIsolate Property = "CanIsolate"
|
||||||
CollectMode Property = "CollectMode"
|
CanLiveMount Property = "CanLiveMount"
|
||||||
ConditionResult Property = "ConditionResult"
|
CanReload Property = "CanReload"
|
||||||
ConditionTimestamp Property = "ConditionTimestamp"
|
CanStart Property = "CanStart"
|
||||||
ConditionTimestampMonotonic Property = "ConditionTimestampMonotonic"
|
CanStop Property = "CanStop"
|
||||||
ConfigurationDirectoryMode Property = "ConfigurationDirectoryMode"
|
CapabilityBoundingSet Property = "CapabilityBoundingSet"
|
||||||
Conflicts Property = "Conflicts"
|
CleanResult Property = "CleanResult"
|
||||||
ControlGroup Property = "ControlGroup"
|
CollectMode Property = "CollectMode"
|
||||||
ControlPID Property = "ControlPID"
|
ConditionResult Property = "ConditionResult"
|
||||||
CoredumpFilter Property = "CoredumpFilter"
|
ConditionTimestamp Property = "ConditionTimestamp"
|
||||||
DefaultDependencies Property = "DefaultDependencies"
|
ConditionTimestampMonotonic Property = "ConditionTimestampMonotonic"
|
||||||
DefaultMemoryLow Property = "DefaultMemoryLow"
|
ConfigurationDirectoryMode Property = "ConfigurationDirectoryMode"
|
||||||
DefaultMemoryMin Property = "DefaultMemoryMin"
|
Conflicts Property = "Conflicts"
|
||||||
Delegate Property = "Delegate"
|
ControlGroup Property = "ControlGroup"
|
||||||
Description Property = "Description"
|
ControlGroupId Property = "ControlGroupId"
|
||||||
DevicePolicy Property = "DevicePolicy"
|
ControlPID Property = "ControlPID"
|
||||||
DynamicUser Property = "DynamicUser"
|
CoredumpFilter Property = "CoredumpFilter"
|
||||||
EffectiveCPUs Property = "EffectiveCPUs"
|
CoredumpReceive Property = "CoredumpReceive"
|
||||||
EffectiveMemoryNodes Property = "EffectiveMemoryNodes"
|
DebugInvocation Property = "DebugInvocation"
|
||||||
ExecMainCode Property = "ExecMainCode"
|
DefaultDependencies Property = "DefaultDependencies"
|
||||||
ExecMainExitTimestampMonotonic Property = "ExecMainExitTimestampMonotonic"
|
DefaultMemoryLow Property = "DefaultMemoryLow"
|
||||||
ExecMainPID Property = "ExecMainPID"
|
DefaultMemoryMin Property = "DefaultMemoryMin"
|
||||||
ExecMainStartTimestamp Property = "ExecMainStartTimestamp"
|
DefaultStartupMemoryLow Property = "DefaultStartupMemoryLow"
|
||||||
ExecMainStartTimestampMonotonic Property = "ExecMainStartTimestampMonotonic"
|
DeferAcceptUSec Property = "DeferAcceptUSec"
|
||||||
ExecMainStatus Property = "ExecMainStatus"
|
Delegate Property = "Delegate"
|
||||||
ExecReload Property = "ExecReload"
|
Description Property = "Description"
|
||||||
ExecReloadEx Property = "ExecReloadEx"
|
DevicePolicy Property = "DevicePolicy"
|
||||||
ExecStart Property = "ExecStart"
|
DirectoryMode Property = "DirectoryMode"
|
||||||
ExecStartEx Property = "ExecStartEx"
|
DynamicUser Property = "DynamicUser"
|
||||||
FailureAction Property = "FailureAction"
|
EffectiveCPUs Property = "EffectiveCPUs"
|
||||||
FileDescriptorStoreMax Property = "FileDescriptorStoreMax"
|
EffectiveMemoryHigh Property = "EffectiveMemoryHigh"
|
||||||
FinalKillSignal Property = "FinalKillSignal"
|
EffectiveMemoryMax Property = "EffectiveMemoryMax"
|
||||||
FragmentPath Property = "FragmentPath"
|
EffectiveMemoryNodes Property = "EffectiveMemoryNodes"
|
||||||
FreezerState Property = "FreezerState"
|
EffectiveTasksMax Property = "EffectiveTasksMax"
|
||||||
GID Property = "GID"
|
ExecMainCode Property = "ExecMainCode"
|
||||||
GuessMainPID Property = "GuessMainPID"
|
ExecMainExitTimestampMonotonic Property = "ExecMainExitTimestampMonotonic"
|
||||||
IOAccounting Property = "IOAccounting"
|
ExecMainPID Property = "ExecMainPID"
|
||||||
IOReadBytes Property = "IOReadBytes"
|
ExecMainStartTimestamp Property = "ExecMainStartTimestamp"
|
||||||
IOReadOperations Property = "IOReadOperations"
|
ExecMainStartTimestampMonotonic Property = "ExecMainStartTimestampMonotonic"
|
||||||
IOSchedulingClass Property = "IOSchedulingClass"
|
ExecMainStatus Property = "ExecMainStatus"
|
||||||
IOSchedulingPriority Property = "IOSchedulingPriority"
|
ExecReload Property = "ExecReload"
|
||||||
IOWeight Property = "IOWeight"
|
ExecReloadEx Property = "ExecReloadEx"
|
||||||
IOWriteBytes Property = "IOWriteBytes"
|
ExecStart Property = "ExecStart"
|
||||||
IOWriteOperations Property = "IOWriteOperations"
|
ExecStartEx Property = "ExecStartEx"
|
||||||
IPAccounting Property = "IPAccounting"
|
ExtensionImagePolicy Property = "ExtensionImagePolicy"
|
||||||
IPEgressBytes Property = "IPEgressBytes"
|
FailureAction Property = "FailureAction"
|
||||||
IPEgressPackets Property = "IPEgressPackets"
|
FileDescriptorName Property = "FileDescriptorName"
|
||||||
IPIngressBytes Property = "IPIngressBytes"
|
FileDescriptorStoreMax Property = "FileDescriptorStoreMax"
|
||||||
IPIngressPackets Property = "IPIngressPackets"
|
FinalKillSignal Property = "FinalKillSignal"
|
||||||
Id Property = "Id"
|
FlushPending Property = "FlushPending"
|
||||||
IgnoreOnIsolate Property = "IgnoreOnIsolate"
|
FragmentPath Property = "FragmentPath"
|
||||||
IgnoreSIGPIPE Property = "IgnoreSIGPIPE"
|
FreeBind Property = "FreeBind"
|
||||||
InactiveEnterTimestampMonotonic Property = "InactiveEnterTimestampMonotonic"
|
FreezerState Property = "FreezerState"
|
||||||
InactiveExitTimestamp Property = "InactiveExitTimestamp"
|
GID Property = "GID"
|
||||||
InactiveExitTimestampMonotonic Property = "InactiveExitTimestampMonotonic"
|
GuessMainPID Property = "GuessMainPID"
|
||||||
InvocationID Property = "InvocationID"
|
IOAccounting Property = "IOAccounting"
|
||||||
JobRunningTimeoutUSec Property = "JobRunningTimeoutUSec"
|
IOReadBytes Property = "IOReadBytes"
|
||||||
JobTimeoutAction Property = "JobTimeoutAction"
|
IOReadOperations Property = "IOReadOperations"
|
||||||
JobTimeoutUSec Property = "JobTimeoutUSec"
|
IOSchedulingClass Property = "IOSchedulingClass"
|
||||||
KeyringMode Property = "KeyringMode"
|
IOSchedulingPriority Property = "IOSchedulingPriority"
|
||||||
KillMode Property = "KillMode"
|
IOWeight Property = "IOWeight"
|
||||||
KillSignal Property = "KillSignal"
|
IOWriteBytes Property = "IOWriteBytes"
|
||||||
LimitAS Property = "LimitAS"
|
IOWriteOperations Property = "IOWriteOperations"
|
||||||
LimitASSoft Property = "LimitASSoft"
|
IPAccounting Property = "IPAccounting"
|
||||||
LimitCORE Property = "LimitCORE"
|
IPEgressBytes Property = "IPEgressBytes"
|
||||||
LimitCORESoft Property = "LimitCORESoft"
|
IPEgressPackets Property = "IPEgressPackets"
|
||||||
LimitCPU Property = "LimitCPU"
|
IPIngressBytes Property = "IPIngressBytes"
|
||||||
LimitCPUSoft Property = "LimitCPUSoft"
|
IPIngressPackets Property = "IPIngressPackets"
|
||||||
LimitDATA Property = "LimitDATA"
|
IPTOS Property = "IPTOS"
|
||||||
LimitDATASoft Property = "LimitDATASoft"
|
IPTTL Property = "IPTTL"
|
||||||
LimitFSIZE Property = "LimitFSIZE"
|
Id Property = "Id"
|
||||||
LimitFSIZESoft Property = "LimitFSIZESoft"
|
IgnoreOnIsolate Property = "IgnoreOnIsolate"
|
||||||
LimitLOCKS Property = "LimitLOCKS"
|
IgnoreSIGPIPE Property = "IgnoreSIGPIPE"
|
||||||
LimitLOCKSSoft Property = "LimitLOCKSSoft"
|
InactiveEnterTimestampMonotonic Property = "InactiveEnterTimestampMonotonic"
|
||||||
LimitMEMLOCK Property = "LimitMEMLOCK"
|
InactiveExitTimestamp Property = "InactiveExitTimestamp"
|
||||||
LimitMEMLOCKSoft Property = "LimitMEMLOCKSoft"
|
InactiveExitTimestampMonotonic Property = "InactiveExitTimestampMonotonic"
|
||||||
LimitMSGQUEUE Property = "LimitMSGQUEUE"
|
InvocationID Property = "InvocationID"
|
||||||
LimitMSGQUEUESoft Property = "LimitMSGQUEUESoft"
|
JobRunningTimeoutUSec Property = "JobRunningTimeoutUSec"
|
||||||
LimitNICE Property = "LimitNICE"
|
JobTimeoutAction Property = "JobTimeoutAction"
|
||||||
LimitNICESoft Property = "LimitNICESoft"
|
JobTimeoutUSec Property = "JobTimeoutUSec"
|
||||||
LimitNOFILE Property = "LimitNOFILE"
|
KeepAlive Property = "KeepAlive"
|
||||||
LimitNOFILESoft Property = "LimitNOFILESoft"
|
KeepAliveIntervalUSec Property = "KeepAliveIntervalUSec"
|
||||||
LimitNPROC Property = "LimitNPROC"
|
KeepAliveProbes Property = "KeepAliveProbes"
|
||||||
LimitNPROCSoft Property = "LimitNPROCSoft"
|
KeepAliveTimeUSec Property = "KeepAliveTimeUSec"
|
||||||
LimitRSS Property = "LimitRSS"
|
KeyringMode Property = "KeyringMode"
|
||||||
LimitRSSSoft Property = "LimitRSSSoft"
|
KillMode Property = "KillMode"
|
||||||
LimitRTPRIO Property = "LimitRTPRIO"
|
KillSignal Property = "KillSignal"
|
||||||
LimitRTPRIOSoft Property = "LimitRTPRIOSoft"
|
LimitAS Property = "LimitAS"
|
||||||
LimitRTTIME Property = "LimitRTTIME"
|
LimitASSoft Property = "LimitASSoft"
|
||||||
LimitRTTIMESoft Property = "LimitRTTIMESoft"
|
LimitCORE Property = "LimitCORE"
|
||||||
LimitSIGPENDING Property = "LimitSIGPENDING"
|
LimitCORESoft Property = "LimitCORESoft"
|
||||||
LimitSIGPENDINGSoft Property = "LimitSIGPENDINGSoft"
|
LimitCPU Property = "LimitCPU"
|
||||||
LimitSTACK Property = "LimitSTACK"
|
LimitCPUSoft Property = "LimitCPUSoft"
|
||||||
LimitSTACKSoft Property = "LimitSTACKSoft"
|
LimitDATA Property = "LimitDATA"
|
||||||
LoadState Property = "LoadState"
|
LimitDATASoft Property = "LimitDATASoft"
|
||||||
LockPersonality Property = "LockPersonality"
|
LimitFSIZE Property = "LimitFSIZE"
|
||||||
LogLevelMax Property = "LogLevelMax"
|
LimitFSIZESoft Property = "LimitFSIZESoft"
|
||||||
LogRateLimitBurst Property = "LogRateLimitBurst"
|
LimitLOCKS Property = "LimitLOCKS"
|
||||||
LogRateLimitIntervalUSec Property = "LogRateLimitIntervalUSec"
|
LimitLOCKSSoft Property = "LimitLOCKSSoft"
|
||||||
LogsDirectoryMode Property = "LogsDirectoryMode"
|
LimitMEMLOCK Property = "LimitMEMLOCK"
|
||||||
MainPID Property = "MainPID"
|
LimitMEMLOCKSoft Property = "LimitMEMLOCKSoft"
|
||||||
ManagedOOMMemoryPressure Property = "ManagedOOMMemoryPressure"
|
LimitMSGQUEUE Property = "LimitMSGQUEUE"
|
||||||
ManagedOOMMemoryPressureLimit Property = "ManagedOOMMemoryPressureLimit"
|
LimitMSGQUEUESoft Property = "LimitMSGQUEUESoft"
|
||||||
ManagedOOMPreference Property = "ManagedOOMPreference"
|
LimitNICE Property = "LimitNICE"
|
||||||
ManagedOOMSwap Property = "ManagedOOMSwap"
|
LimitNICESoft Property = "LimitNICESoft"
|
||||||
MemoryAccounting Property = "MemoryAccounting"
|
LimitNOFILE Property = "LimitNOFILE"
|
||||||
MemoryCurrent Property = "MemoryCurrent"
|
LimitNOFILESoft Property = "LimitNOFILESoft"
|
||||||
MemoryDenyWriteExecute Property = "MemoryDenyWriteExecute"
|
LimitNPROC Property = "LimitNPROC"
|
||||||
MemoryHigh Property = "MemoryHigh"
|
LimitNPROCSoft Property = "LimitNPROCSoft"
|
||||||
MemoryLimit Property = "MemoryLimit"
|
LimitRSS Property = "LimitRSS"
|
||||||
MemoryLow Property = "MemoryLow"
|
LimitRSSSoft Property = "LimitRSSSoft"
|
||||||
MemoryMax Property = "MemoryMax"
|
LimitRTPRIO Property = "LimitRTPRIO"
|
||||||
MemoryMin Property = "MemoryMin"
|
LimitRTPRIOSoft Property = "LimitRTPRIOSoft"
|
||||||
MemorySwapMax Property = "MemorySwapMax"
|
LimitRTTIME Property = "LimitRTTIME"
|
||||||
MountAPIVFS Property = "MountAPIVFS"
|
LimitRTTIMESoft Property = "LimitRTTIMESoft"
|
||||||
NFileDescriptorStore Property = "NFileDescriptorStore"
|
LimitSIGPENDING Property = "LimitSIGPENDING"
|
||||||
NRestarts Property = "NRestarts"
|
LimitSIGPENDINGSoft Property = "LimitSIGPENDINGSoft"
|
||||||
NUMAPolicy Property = "NUMAPolicy"
|
LimitSTACK Property = "LimitSTACK"
|
||||||
Names Property = "Names"
|
LimitSTACKSoft Property = "LimitSTACKSoft"
|
||||||
NeedDaemonReload Property = "NeedDaemonReload"
|
Listen Property = "Listen"
|
||||||
Nice Property = "Nice"
|
LoadState Property = "LoadState"
|
||||||
NoNewPrivileges Property = "NoNewPrivileges"
|
LockPersonality Property = "LockPersonality"
|
||||||
NonBlocking Property = "NonBlocking"
|
LogLevelMax Property = "LogLevelMax"
|
||||||
NotifyAccess Property = "NotifyAccess"
|
LogRateLimitBurst Property = "LogRateLimitBurst"
|
||||||
OOMPolicy Property = "OOMPolicy"
|
LogRateLimitIntervalUSec Property = "LogRateLimitIntervalUSec"
|
||||||
OOMScoreAdjust Property = "OOMScoreAdjust"
|
LogsDirectoryMode Property = "LogsDirectoryMode"
|
||||||
OnFailureJobMode Property = "OnFailureJobMode"
|
MainPID Property = "MainPID"
|
||||||
PIDFile Property = "PIDFile"
|
ManagedOOMMemoryPressure Property = "ManagedOOMMemoryPressure"
|
||||||
Perpetual Property = "Perpetual"
|
ManagedOOMMemoryPressureDurationUSec Property = "ManagedOOMMemoryPressureDurationUSec"
|
||||||
PrivateDevices Property = "PrivateDevices"
|
ManagedOOMMemoryPressureLimit Property = "ManagedOOMMemoryPressureLimit"
|
||||||
PrivateIPC Property = "PrivateIPC"
|
ManagedOOMPreference Property = "ManagedOOMPreference"
|
||||||
PrivateMounts Property = "PrivateMounts"
|
ManagedOOMSwap Property = "ManagedOOMSwap"
|
||||||
PrivateNetwork Property = "PrivateNetwork"
|
Mark Property = "Mark"
|
||||||
PrivateTmp Property = "PrivateTmp"
|
MaxConnections Property = "MaxConnections"
|
||||||
PrivateUsers Property = "PrivateUsers"
|
MaxConnectionsPerSource Property = "MaxConnectionsPerSource"
|
||||||
ProcSubset Property = "ProcSubset"
|
MemoryAccounting Property = "MemoryAccounting"
|
||||||
ProtectClock Property = "ProtectClock"
|
MemoryAvailable Property = "MemoryAvailable"
|
||||||
ProtectControlGroups Property = "ProtectControlGroups"
|
MemoryCurrent Property = "MemoryCurrent"
|
||||||
ProtectHome Property = "ProtectHome"
|
MemoryDenyWriteExecute Property = "MemoryDenyWriteExecute"
|
||||||
ProtectHostname Property = "ProtectHostname"
|
MemoryHigh Property = "MemoryHigh"
|
||||||
ProtectKernelLogs Property = "ProtectKernelLogs"
|
MemoryKSM Property = "MemoryKSM"
|
||||||
ProtectKernelModules Property = "ProtectKernelModules"
|
MemoryLimit Property = "MemoryLimit"
|
||||||
ProtectKernelTunables Property = "ProtectKernelTunables"
|
MemoryLow Property = "MemoryLow"
|
||||||
ProtectProc Property = "ProtectProc"
|
MemoryMax Property = "MemoryMax"
|
||||||
ProtectSystem Property = "ProtectSystem"
|
MemoryMin Property = "MemoryMin"
|
||||||
RefuseManualStart Property = "RefuseManualStart"
|
MemoryPeak Property = "MemoryPeak"
|
||||||
RefuseManualStop Property = "RefuseManualStop"
|
MemoryPressureThresholdUSec Property = "MemoryPressureThresholdUSec"
|
||||||
ReloadResult Property = "ReloadResult"
|
MemoryPressureWatch Property = "MemoryPressureWatch"
|
||||||
RemainAfterExit Property = "RemainAfterExit"
|
MemorySwapCurrent Property = "MemorySwapCurrent"
|
||||||
RemoveIPC Property = "RemoveIPC"
|
MemorySwapMax Property = "MemorySwapMax"
|
||||||
Requires Property = "Requires"
|
MemorySwapPeak Property = "MemorySwapPeak"
|
||||||
Restart Property = "Restart"
|
MemoryZSwapCurrent Property = "MemoryZSwapCurrent"
|
||||||
RestartKillSignal Property = "RestartKillSignal"
|
MemoryZSwapMax Property = "MemoryZSwapMax"
|
||||||
RestartUSec Property = "RestartUSec"
|
MemoryZSwapWriteback Property = "MemoryZSwapWriteback"
|
||||||
RestrictNamespaces Property = "RestrictNamespaces"
|
MessageQueueMaxMessages Property = "MessageQueueMaxMessages"
|
||||||
RestrictRealtime Property = "RestrictRealtime"
|
MessageQueueMessageSize Property = "MessageQueueMessageSize"
|
||||||
RestrictSUIDSGID Property = "RestrictSUIDSGID"
|
MountAPIVFS Property = "MountAPIVFS"
|
||||||
Result Property = "Result"
|
MountImagePolicy Property = "MountImagePolicy"
|
||||||
RootDirectoryStartOnly Property = "RootDirectoryStartOnly"
|
NAccepted Property = "NAccepted"
|
||||||
RuntimeDirectoryMode Property = "RuntimeDirectoryMode"
|
NConnections Property = "NConnections"
|
||||||
RuntimeDirectoryPreserve Property = "RuntimeDirectoryPreserve"
|
NFileDescriptorStore Property = "NFileDescriptorStore"
|
||||||
RuntimeMaxUSec Property = "RuntimeMaxUSec"
|
NRefused Property = "NRefused"
|
||||||
SameProcessGroup Property = "SameProcessGroup"
|
NRestarts Property = "NRestarts"
|
||||||
SecureBits Property = "SecureBits"
|
NUMAPolicy Property = "NUMAPolicy"
|
||||||
SendSIGHUP Property = "SendSIGHUP"
|
Names Property = "Names"
|
||||||
SendSIGKILL Property = "SendSIGKILL"
|
NeedDaemonReload Property = "NeedDaemonReload"
|
||||||
Slice Property = "Slice"
|
Nice Property = "Nice"
|
||||||
StandardError Property = "StandardError"
|
NoDelay Property = "NoDelay"
|
||||||
StandardInput Property = "StandardInput"
|
NoNewPrivileges Property = "NoNewPrivileges"
|
||||||
StandardOutput Property = "StandardOutput"
|
NonBlocking Property = "NonBlocking"
|
||||||
StartLimitAction Property = "StartLimitAction"
|
NotifyAccess Property = "NotifyAccess"
|
||||||
StartLimitBurst Property = "StartLimitBurst"
|
OOMPolicy Property = "OOMPolicy"
|
||||||
StartLimitIntervalUSec Property = "StartLimitIntervalUSec"
|
OOMScoreAdjust Property = "OOMScoreAdjust"
|
||||||
StartupBlockIOWeight Property = "StartupBlockIOWeight"
|
OnFailureJobMode Property = "OnFailureJobMode"
|
||||||
StartupCPUShares Property = "StartupCPUShares"
|
OnSuccessJobMode Property = "OnSuccessJobMode"
|
||||||
StartupCPUWeight Property = "StartupCPUWeight"
|
PIDFile Property = "PIDFile"
|
||||||
StartupIOWeight Property = "StartupIOWeight"
|
PassCredentials Property = "PassCredentials"
|
||||||
StateChangeTimestamp Property = "StateChangeTimestamp"
|
PassFileDescriptorsToExec Property = "PassFileDescriptorsToExec"
|
||||||
StateChangeTimestampMonotonic Property = "StateChangeTimestampMonotonic"
|
PassPacketInfo Property = "PassPacketInfo"
|
||||||
StateDirectoryMode Property = "StateDirectoryMode"
|
PassSecurity Property = "PassSecurity"
|
||||||
StatusErrno Property = "StatusErrno"
|
Perpetual Property = "Perpetual"
|
||||||
StopWhenUnneeded Property = "StopWhenUnneeded"
|
PipeSize Property = "PipeSize"
|
||||||
SubState Property = "SubState"
|
PollLimitBurst Property = "PollLimitBurst"
|
||||||
SuccessAction Property = "SuccessAction"
|
PollLimitIntervalUSec Property = "PollLimitIntervalUSec"
|
||||||
SyslogFacility Property = "SyslogFacility"
|
Priority Property = "Priority"
|
||||||
SyslogLevel Property = "SyslogLevel"
|
PrivateDevices Property = "PrivateDevices"
|
||||||
SyslogLevelPrefix Property = "SyslogLevelPrefix"
|
PrivateIPC Property = "PrivateIPC"
|
||||||
SyslogPriority Property = "SyslogPriority"
|
PrivateMounts Property = "PrivateMounts"
|
||||||
SystemCallErrorNumber Property = "SystemCallErrorNumber"
|
PrivateNetwork Property = "PrivateNetwork"
|
||||||
TTYReset Property = "TTYReset"
|
PrivatePIDs Property = "PrivatePIDs"
|
||||||
TTYVHangup Property = "TTYVHangup"
|
PrivateTmp Property = "PrivateTmp"
|
||||||
TTYVTDisallocate Property = "TTYVTDisallocate"
|
PrivateTmpEx Property = "PrivateTmpEx"
|
||||||
TasksAccounting Property = "TasksAccounting"
|
PrivateUsers Property = "PrivateUsers"
|
||||||
TasksCurrent Property = "TasksCurrent"
|
PrivateUsersEx Property = "PrivateUsersEx"
|
||||||
TasksMax Property = "TasksMax"
|
ProcSubset Property = "ProcSubset"
|
||||||
TimeoutAbortUSec Property = "TimeoutAbortUSec"
|
ProtectClock Property = "ProtectClock"
|
||||||
TimeoutCleanUSec Property = "TimeoutCleanUSec"
|
ProtectControlGroups Property = "ProtectControlGroups"
|
||||||
TimeoutStartFailureMode Property = "TimeoutStartFailureMode"
|
ProtectControlGroupsEx Property = "ProtectControlGroupsEx"
|
||||||
TimeoutStartUSec Property = "TimeoutStartUSec"
|
ProtectHome Property = "ProtectHome"
|
||||||
TimeoutStopFailureMode Property = "TimeoutStopFailureMode"
|
ProtectHostname Property = "ProtectHostname"
|
||||||
TimeoutStopUSec Property = "TimeoutStopUSec"
|
ProtectKernelLogs Property = "ProtectKernelLogs"
|
||||||
TimerSlackNSec Property = "TimerSlackNSec"
|
ProtectKernelModules Property = "ProtectKernelModules"
|
||||||
Transient Property = "Transient"
|
ProtectKernelTunables Property = "ProtectKernelTunables"
|
||||||
Type Property = "Type"
|
ProtectProc Property = "ProtectProc"
|
||||||
UID Property = "UID"
|
ProtectSystem Property = "ProtectSystem"
|
||||||
UMask Property = "UMask"
|
ReceiveBuffer Property = "ReceiveBuffer"
|
||||||
UnitFilePreset Property = "UnitFilePreset"
|
RefuseManualStart Property = "RefuseManualStart"
|
||||||
UnitFileState Property = "UnitFileState"
|
RefuseManualStop Property = "RefuseManualStop"
|
||||||
UtmpMode Property = "UtmpMode"
|
ReloadResult Property = "ReloadResult"
|
||||||
WantedBy Property = "WantedBy"
|
RemainAfterExit Property = "RemainAfterExit"
|
||||||
WatchdogSignal Property = "WatchdogSignal"
|
RemoveIPC Property = "RemoveIPC"
|
||||||
WatchdogTimestampMonotonic Property = "WatchdogTimestampMonotonic"
|
RemoveOnStop Property = "RemoveOnStop"
|
||||||
WatchdogUSec Property = "WatchdogUSec"
|
RequiredBy Property = "RequiredBy"
|
||||||
|
Requires Property = "Requires"
|
||||||
|
RequiresMountsFor Property = "RequiresMountsFor"
|
||||||
|
Restart Property = "Restart"
|
||||||
|
RestartKillSignal Property = "RestartKillSignal"
|
||||||
|
RestartUSec Property = "RestartUSec"
|
||||||
|
RestrictNamespaces Property = "RestrictNamespaces"
|
||||||
|
RestrictRealtime Property = "RestrictRealtime"
|
||||||
|
RestrictSUIDSGID Property = "RestrictSUIDSGID"
|
||||||
|
Result Property = "Result"
|
||||||
|
ReusePort Property = "ReusePort"
|
||||||
|
RootDirectoryStartOnly Property = "RootDirectoryStartOnly"
|
||||||
|
RootEphemeral Property = "RootEphemeral"
|
||||||
|
RootImagePolicy Property = "RootImagePolicy"
|
||||||
|
RuntimeDirectoryMode Property = "RuntimeDirectoryMode"
|
||||||
|
RuntimeDirectoryPreserve Property = "RuntimeDirectoryPreserve"
|
||||||
|
RuntimeMaxUSec Property = "RuntimeMaxUSec"
|
||||||
|
SameProcessGroup Property = "SameProcessGroup"
|
||||||
|
SecureBits Property = "SecureBits"
|
||||||
|
SendBuffer Property = "SendBuffer"
|
||||||
|
SendSIGHUP Property = "SendSIGHUP"
|
||||||
|
SendSIGKILL Property = "SendSIGKILL"
|
||||||
|
SetLoginEnvironment Property = "SetLoginEnvironment"
|
||||||
|
Slice Property = "Slice"
|
||||||
|
SocketMode Property = "SocketMode"
|
||||||
|
SocketProtocol Property = "SocketProtocol"
|
||||||
|
StandardError Property = "StandardError"
|
||||||
|
StandardInput Property = "StandardInput"
|
||||||
|
StandardOutput Property = "StandardOutput"
|
||||||
|
StartLimitAction Property = "StartLimitAction"
|
||||||
|
StartLimitBurst Property = "StartLimitBurst"
|
||||||
|
StartLimitIntervalUSec Property = "StartLimitIntervalUSec"
|
||||||
|
StartupBlockIOWeight Property = "StartupBlockIOWeight"
|
||||||
|
StartupCPUShares Property = "StartupCPUShares"
|
||||||
|
StartupCPUWeight Property = "StartupCPUWeight"
|
||||||
|
StartupIOWeight Property = "StartupIOWeight"
|
||||||
|
StartupMemoryHigh Property = "StartupMemoryHigh"
|
||||||
|
StartupMemoryLow Property = "StartupMemoryLow"
|
||||||
|
StartupMemoryMax Property = "StartupMemoryMax"
|
||||||
|
StartupMemorySwapMax Property = "StartupMemorySwapMax"
|
||||||
|
StartupMemoryZSwapMax Property = "StartupMemoryZSwapMax"
|
||||||
|
StateChangeTimestamp Property = "StateChangeTimestamp"
|
||||||
|
StateChangeTimestampMonotonic Property = "StateChangeTimestampMonotonic"
|
||||||
|
StateDirectoryMode Property = "StateDirectoryMode"
|
||||||
|
StatusErrno Property = "StatusErrno"
|
||||||
|
StopWhenUnneeded Property = "StopWhenUnneeded"
|
||||||
|
SubState Property = "SubState"
|
||||||
|
SuccessAction Property = "SuccessAction"
|
||||||
|
SurviveFinalKillSignal Property = "SurviveFinalKillSignal"
|
||||||
|
SyslogFacility Property = "SyslogFacility"
|
||||||
|
SyslogLevel Property = "SyslogLevel"
|
||||||
|
SyslogLevelPrefix Property = "SyslogLevelPrefix"
|
||||||
|
SyslogPriority Property = "SyslogPriority"
|
||||||
|
SystemCallErrorNumber Property = "SystemCallErrorNumber"
|
||||||
|
TTYReset Property = "TTYReset"
|
||||||
|
TTYVHangup Property = "TTYVHangup"
|
||||||
|
TTYVTDisallocate Property = "TTYVTDisallocate"
|
||||||
|
TasksAccounting Property = "TasksAccounting"
|
||||||
|
TasksCurrent Property = "TasksCurrent"
|
||||||
|
TasksMax Property = "TasksMax"
|
||||||
|
TimeoutAbortUSec Property = "TimeoutAbortUSec"
|
||||||
|
TimeoutCleanUSec Property = "TimeoutCleanUSec"
|
||||||
|
TimeoutStartFailureMode Property = "TimeoutStartFailureMode"
|
||||||
|
TimeoutStartUSec Property = "TimeoutStartUSec"
|
||||||
|
TimeoutStopFailureMode Property = "TimeoutStopFailureMode"
|
||||||
|
TimeoutStopUSec Property = "TimeoutStopUSec"
|
||||||
|
TimeoutUSec Property = "TimeoutUSec"
|
||||||
|
TimerSlackNSec Property = "TimerSlackNSec"
|
||||||
|
Timestamping Property = "Timestamping"
|
||||||
|
Transient Property = "Transient"
|
||||||
|
Transparent Property = "Transparent"
|
||||||
|
TriggerLimitBurst Property = "TriggerLimitBurst"
|
||||||
|
TriggerLimitIntervalUSec Property = "TriggerLimitIntervalUSec"
|
||||||
|
Triggers Property = "Triggers"
|
||||||
|
Type Property = "Type"
|
||||||
|
UID Property = "UID"
|
||||||
|
UMask Property = "UMask"
|
||||||
|
UnitFilePreset Property = "UnitFilePreset"
|
||||||
|
UnitFileState Property = "UnitFileState"
|
||||||
|
UtmpMode Property = "UtmpMode"
|
||||||
|
WantedBy Property = "WantedBy"
|
||||||
|
WatchdogSignal Property = "WatchdogSignal"
|
||||||
|
WatchdogTimestampMonotonic Property = "WatchdogTimestampMonotonic"
|
||||||
|
WatchdogUSec Property = "WatchdogUSec"
|
||||||
|
Writable Property = "Writable"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package properties
|
package properties
|
||||||
|
|
||||||
var Properties = []Property{
|
var Properties = []Property{
|
||||||
|
Accept,
|
||||||
ActiveEnterTimestamp,
|
ActiveEnterTimestamp,
|
||||||
ActiveEnterTimestampMonotonic,
|
ActiveEnterTimestampMonotonic,
|
||||||
ActiveExitTimestampMonotonic,
|
ActiveExitTimestampMonotonic,
|
||||||
@@ -10,9 +11,13 @@ var Properties = []Property{
|
|||||||
AssertResult,
|
AssertResult,
|
||||||
AssertTimestamp,
|
AssertTimestamp,
|
||||||
AssertTimestampMonotonic,
|
AssertTimestampMonotonic,
|
||||||
|
Backlog,
|
||||||
Before,
|
Before,
|
||||||
|
BindIPv6Only,
|
||||||
|
BindLogSockets,
|
||||||
BlockIOAccounting,
|
BlockIOAccounting,
|
||||||
BlockIOWeight,
|
BlockIOWeight,
|
||||||
|
Broadcast,
|
||||||
CPUAccounting,
|
CPUAccounting,
|
||||||
CPUAffinityFromNUMA,
|
CPUAffinityFromNUMA,
|
||||||
CPUQuotaPerSecUSec,
|
CPUQuotaPerSecUSec,
|
||||||
@@ -26,6 +31,7 @@ var Properties = []Property{
|
|||||||
CacheDirectoryMode,
|
CacheDirectoryMode,
|
||||||
CanFreeze,
|
CanFreeze,
|
||||||
CanIsolate,
|
CanIsolate,
|
||||||
|
CanLiveMount,
|
||||||
CanReload,
|
CanReload,
|
||||||
CanStart,
|
CanStart,
|
||||||
CanStop,
|
CanStop,
|
||||||
@@ -38,17 +44,26 @@ var Properties = []Property{
|
|||||||
ConfigurationDirectoryMode,
|
ConfigurationDirectoryMode,
|
||||||
Conflicts,
|
Conflicts,
|
||||||
ControlGroup,
|
ControlGroup,
|
||||||
|
ControlGroupId,
|
||||||
ControlPID,
|
ControlPID,
|
||||||
CoredumpFilter,
|
CoredumpFilter,
|
||||||
|
CoredumpReceive,
|
||||||
|
DebugInvocation,
|
||||||
DefaultDependencies,
|
DefaultDependencies,
|
||||||
DefaultMemoryLow,
|
DefaultMemoryLow,
|
||||||
DefaultMemoryMin,
|
DefaultMemoryMin,
|
||||||
|
DefaultStartupMemoryLow,
|
||||||
|
DeferAcceptUSec,
|
||||||
Delegate,
|
Delegate,
|
||||||
Description,
|
Description,
|
||||||
DevicePolicy,
|
DevicePolicy,
|
||||||
|
DirectoryMode,
|
||||||
DynamicUser,
|
DynamicUser,
|
||||||
EffectiveCPUs,
|
EffectiveCPUs,
|
||||||
|
EffectiveMemoryHigh,
|
||||||
|
EffectiveMemoryMax,
|
||||||
EffectiveMemoryNodes,
|
EffectiveMemoryNodes,
|
||||||
|
EffectiveTasksMax,
|
||||||
ExecMainCode,
|
ExecMainCode,
|
||||||
ExecMainExitTimestampMonotonic,
|
ExecMainExitTimestampMonotonic,
|
||||||
ExecMainPID,
|
ExecMainPID,
|
||||||
@@ -59,10 +74,14 @@ var Properties = []Property{
|
|||||||
ExecReloadEx,
|
ExecReloadEx,
|
||||||
ExecStart,
|
ExecStart,
|
||||||
ExecStartEx,
|
ExecStartEx,
|
||||||
|
ExtensionImagePolicy,
|
||||||
FailureAction,
|
FailureAction,
|
||||||
|
FileDescriptorName,
|
||||||
FileDescriptorStoreMax,
|
FileDescriptorStoreMax,
|
||||||
FinalKillSignal,
|
FinalKillSignal,
|
||||||
|
FlushPending,
|
||||||
FragmentPath,
|
FragmentPath,
|
||||||
|
FreeBind,
|
||||||
FreezerState,
|
FreezerState,
|
||||||
GID,
|
GID,
|
||||||
GuessMainPID,
|
GuessMainPID,
|
||||||
@@ -79,6 +98,8 @@ var Properties = []Property{
|
|||||||
IPEgressPackets,
|
IPEgressPackets,
|
||||||
IPIngressBytes,
|
IPIngressBytes,
|
||||||
IPIngressPackets,
|
IPIngressPackets,
|
||||||
|
IPTOS,
|
||||||
|
IPTTL,
|
||||||
Id,
|
Id,
|
||||||
IgnoreOnIsolate,
|
IgnoreOnIsolate,
|
||||||
IgnoreSIGPIPE,
|
IgnoreSIGPIPE,
|
||||||
@@ -89,6 +110,10 @@ var Properties = []Property{
|
|||||||
JobRunningTimeoutUSec,
|
JobRunningTimeoutUSec,
|
||||||
JobTimeoutAction,
|
JobTimeoutAction,
|
||||||
JobTimeoutUSec,
|
JobTimeoutUSec,
|
||||||
|
KeepAlive,
|
||||||
|
KeepAliveIntervalUSec,
|
||||||
|
KeepAliveProbes,
|
||||||
|
KeepAliveTimeUSec,
|
||||||
KeyringMode,
|
KeyringMode,
|
||||||
KillMode,
|
KillMode,
|
||||||
KillSignal,
|
KillSignal,
|
||||||
@@ -124,6 +149,7 @@ var Properties = []Property{
|
|||||||
LimitSIGPENDINGSoft,
|
LimitSIGPENDINGSoft,
|
||||||
LimitSTACK,
|
LimitSTACK,
|
||||||
LimitSTACKSoft,
|
LimitSTACKSoft,
|
||||||
|
Listen,
|
||||||
LoadState,
|
LoadState,
|
||||||
LockPersonality,
|
LockPersonality,
|
||||||
LogLevelMax,
|
LogLevelMax,
|
||||||
@@ -132,42 +158,76 @@ var Properties = []Property{
|
|||||||
LogsDirectoryMode,
|
LogsDirectoryMode,
|
||||||
MainPID,
|
MainPID,
|
||||||
ManagedOOMMemoryPressure,
|
ManagedOOMMemoryPressure,
|
||||||
|
ManagedOOMMemoryPressureDurationUSec,
|
||||||
ManagedOOMMemoryPressureLimit,
|
ManagedOOMMemoryPressureLimit,
|
||||||
ManagedOOMPreference,
|
ManagedOOMPreference,
|
||||||
ManagedOOMSwap,
|
ManagedOOMSwap,
|
||||||
|
Mark,
|
||||||
|
MaxConnections,
|
||||||
|
MaxConnectionsPerSource,
|
||||||
MemoryAccounting,
|
MemoryAccounting,
|
||||||
|
MemoryAvailable,
|
||||||
MemoryCurrent,
|
MemoryCurrent,
|
||||||
MemoryDenyWriteExecute,
|
MemoryDenyWriteExecute,
|
||||||
MemoryHigh,
|
MemoryHigh,
|
||||||
|
MemoryKSM,
|
||||||
MemoryLimit,
|
MemoryLimit,
|
||||||
MemoryLow,
|
MemoryLow,
|
||||||
MemoryMax,
|
MemoryMax,
|
||||||
MemoryMin,
|
MemoryMin,
|
||||||
|
MemoryPeak,
|
||||||
|
MemoryPressureThresholdUSec,
|
||||||
|
MemoryPressureWatch,
|
||||||
|
MemorySwapCurrent,
|
||||||
MemorySwapMax,
|
MemorySwapMax,
|
||||||
|
MemorySwapPeak,
|
||||||
|
MemoryZSwapCurrent,
|
||||||
|
MemoryZSwapMax,
|
||||||
|
MemoryZSwapWriteback,
|
||||||
|
MessageQueueMaxMessages,
|
||||||
|
MessageQueueMessageSize,
|
||||||
MountAPIVFS,
|
MountAPIVFS,
|
||||||
|
MountImagePolicy,
|
||||||
|
NAccepted,
|
||||||
|
NConnections,
|
||||||
NFileDescriptorStore,
|
NFileDescriptorStore,
|
||||||
|
NRefused,
|
||||||
NRestarts,
|
NRestarts,
|
||||||
NUMAPolicy,
|
NUMAPolicy,
|
||||||
Names,
|
Names,
|
||||||
NeedDaemonReload,
|
NeedDaemonReload,
|
||||||
Nice,
|
Nice,
|
||||||
|
NoDelay,
|
||||||
NoNewPrivileges,
|
NoNewPrivileges,
|
||||||
NonBlocking,
|
NonBlocking,
|
||||||
NotifyAccess,
|
NotifyAccess,
|
||||||
OOMPolicy,
|
OOMPolicy,
|
||||||
OOMScoreAdjust,
|
OOMScoreAdjust,
|
||||||
OnFailureJobMode,
|
OnFailureJobMode,
|
||||||
|
OnSuccessJobMode,
|
||||||
PIDFile,
|
PIDFile,
|
||||||
|
PassCredentials,
|
||||||
|
PassFileDescriptorsToExec,
|
||||||
|
PassPacketInfo,
|
||||||
|
PassSecurity,
|
||||||
Perpetual,
|
Perpetual,
|
||||||
|
PipeSize,
|
||||||
|
PollLimitBurst,
|
||||||
|
PollLimitIntervalUSec,
|
||||||
|
Priority,
|
||||||
PrivateDevices,
|
PrivateDevices,
|
||||||
PrivateIPC,
|
PrivateIPC,
|
||||||
PrivateMounts,
|
PrivateMounts,
|
||||||
PrivateNetwork,
|
PrivateNetwork,
|
||||||
|
PrivatePIDs,
|
||||||
PrivateTmp,
|
PrivateTmp,
|
||||||
|
PrivateTmpEx,
|
||||||
PrivateUsers,
|
PrivateUsers,
|
||||||
|
PrivateUsersEx,
|
||||||
ProcSubset,
|
ProcSubset,
|
||||||
ProtectClock,
|
ProtectClock,
|
||||||
ProtectControlGroups,
|
ProtectControlGroups,
|
||||||
|
ProtectControlGroupsEx,
|
||||||
ProtectHome,
|
ProtectHome,
|
||||||
ProtectHostname,
|
ProtectHostname,
|
||||||
ProtectKernelLogs,
|
ProtectKernelLogs,
|
||||||
@@ -175,12 +235,16 @@ var Properties = []Property{
|
|||||||
ProtectKernelTunables,
|
ProtectKernelTunables,
|
||||||
ProtectProc,
|
ProtectProc,
|
||||||
ProtectSystem,
|
ProtectSystem,
|
||||||
|
ReceiveBuffer,
|
||||||
RefuseManualStart,
|
RefuseManualStart,
|
||||||
RefuseManualStop,
|
RefuseManualStop,
|
||||||
ReloadResult,
|
ReloadResult,
|
||||||
RemainAfterExit,
|
RemainAfterExit,
|
||||||
RemoveIPC,
|
RemoveIPC,
|
||||||
|
RemoveOnStop,
|
||||||
|
RequiredBy,
|
||||||
Requires,
|
Requires,
|
||||||
|
RequiresMountsFor,
|
||||||
Restart,
|
Restart,
|
||||||
RestartKillSignal,
|
RestartKillSignal,
|
||||||
RestartUSec,
|
RestartUSec,
|
||||||
@@ -188,15 +252,22 @@ var Properties = []Property{
|
|||||||
RestrictRealtime,
|
RestrictRealtime,
|
||||||
RestrictSUIDSGID,
|
RestrictSUIDSGID,
|
||||||
Result,
|
Result,
|
||||||
|
ReusePort,
|
||||||
RootDirectoryStartOnly,
|
RootDirectoryStartOnly,
|
||||||
|
RootEphemeral,
|
||||||
|
RootImagePolicy,
|
||||||
RuntimeDirectoryMode,
|
RuntimeDirectoryMode,
|
||||||
RuntimeDirectoryPreserve,
|
RuntimeDirectoryPreserve,
|
||||||
RuntimeMaxUSec,
|
RuntimeMaxUSec,
|
||||||
SameProcessGroup,
|
SameProcessGroup,
|
||||||
SecureBits,
|
SecureBits,
|
||||||
|
SendBuffer,
|
||||||
SendSIGHUP,
|
SendSIGHUP,
|
||||||
SendSIGKILL,
|
SendSIGKILL,
|
||||||
|
SetLoginEnvironment,
|
||||||
Slice,
|
Slice,
|
||||||
|
SocketMode,
|
||||||
|
SocketProtocol,
|
||||||
StandardError,
|
StandardError,
|
||||||
StandardInput,
|
StandardInput,
|
||||||
StandardOutput,
|
StandardOutput,
|
||||||
@@ -207,6 +278,11 @@ var Properties = []Property{
|
|||||||
StartupCPUShares,
|
StartupCPUShares,
|
||||||
StartupCPUWeight,
|
StartupCPUWeight,
|
||||||
StartupIOWeight,
|
StartupIOWeight,
|
||||||
|
StartupMemoryHigh,
|
||||||
|
StartupMemoryLow,
|
||||||
|
StartupMemoryMax,
|
||||||
|
StartupMemorySwapMax,
|
||||||
|
StartupMemoryZSwapMax,
|
||||||
StateChangeTimestamp,
|
StateChangeTimestamp,
|
||||||
StateChangeTimestampMonotonic,
|
StateChangeTimestampMonotonic,
|
||||||
StateDirectoryMode,
|
StateDirectoryMode,
|
||||||
@@ -214,6 +290,7 @@ var Properties = []Property{
|
|||||||
StopWhenUnneeded,
|
StopWhenUnneeded,
|
||||||
SubState,
|
SubState,
|
||||||
SuccessAction,
|
SuccessAction,
|
||||||
|
SurviveFinalKillSignal,
|
||||||
SyslogFacility,
|
SyslogFacility,
|
||||||
SyslogLevel,
|
SyslogLevel,
|
||||||
SyslogLevelPrefix,
|
SyslogLevelPrefix,
|
||||||
@@ -231,8 +308,14 @@ var Properties = []Property{
|
|||||||
TimeoutStartUSec,
|
TimeoutStartUSec,
|
||||||
TimeoutStopFailureMode,
|
TimeoutStopFailureMode,
|
||||||
TimeoutStopUSec,
|
TimeoutStopUSec,
|
||||||
|
TimeoutUSec,
|
||||||
TimerSlackNSec,
|
TimerSlackNSec,
|
||||||
|
Timestamping,
|
||||||
Transient,
|
Transient,
|
||||||
|
Transparent,
|
||||||
|
TriggerLimitBurst,
|
||||||
|
TriggerLimitIntervalUSec,
|
||||||
|
Triggers,
|
||||||
Type,
|
Type,
|
||||||
UID,
|
UID,
|
||||||
UMask,
|
UMask,
|
||||||
@@ -243,4 +326,5 @@ var Properties = []Property{
|
|||||||
WatchdogSignal,
|
WatchdogSignal,
|
||||||
WatchdogTimestampMonotonic,
|
WatchdogTimestampMonotonic,
|
||||||
WatchdogUSec,
|
WatchdogUSec,
|
||||||
|
Writable,
|
||||||
}
|
}
|
||||||
|
|||||||
37
structs.go
37
structs.go
@@ -1,5 +1,42 @@
|
|||||||
package systemctl
|
package systemctl
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
UserMode bool
|
UserMode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Unit struct {
|
||||||
|
Name string
|
||||||
|
Load string
|
||||||
|
Active string
|
||||||
|
Sub string
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnitTypes contains all valid systemd unit type suffixes.
|
||||||
|
var UnitTypes = []string{
|
||||||
|
"automount",
|
||||||
|
"device",
|
||||||
|
"mount",
|
||||||
|
"path",
|
||||||
|
"scope",
|
||||||
|
"service",
|
||||||
|
"slice",
|
||||||
|
"snapshot",
|
||||||
|
"socket",
|
||||||
|
"swap",
|
||||||
|
"target",
|
||||||
|
"timer",
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasValidUnitSuffix checks whether the given unit name ends with a valid
|
||||||
|
// systemd unit type suffix (e.g. ".service", ".timer").
|
||||||
|
func HasValidUnitSuffix(unit string) bool {
|
||||||
|
for _, t := range UnitTypes {
|
||||||
|
if strings.HasSuffix(unit, "."+t) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
160
systemctl.go
160
systemctl.go
@@ -2,8 +2,6 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/taigrr/systemctl/properties"
|
"github.com/taigrr/systemctl/properties"
|
||||||
)
|
)
|
||||||
@@ -15,12 +13,16 @@ import (
|
|||||||
// reloaded, all sockets systemd listens on behalf of user configuration will
|
// reloaded, all sockets systemd listens on behalf of user configuration will
|
||||||
// stay accessible.
|
// stay accessible.
|
||||||
func DaemonReload(ctx context.Context, opts Options) error {
|
func DaemonReload(ctx context.Context, opts Options) error {
|
||||||
var args = []string{"daemon-reload", "--system"}
|
return daemonReload(ctx, opts)
|
||||||
if opts.UserMode {
|
}
|
||||||
args[1] = "--user"
|
|
||||||
}
|
// Reenables one or more units.
|
||||||
_, _, _, err := execute(ctx, args)
|
//
|
||||||
return err
|
// This removes all symlinks to the unit files backing the specified units from
|
||||||
|
// the unit configuration directory, then recreates the symlink to the unit again,
|
||||||
|
// atomically. Can be used to change the symlink target.
|
||||||
|
func Reenable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return reenable(ctx, unit, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disables one or more units.
|
// Disables one or more units.
|
||||||
@@ -29,12 +31,7 @@ func DaemonReload(ctx context.Context, opts Options) error {
|
|||||||
// the unit configuration directory, and hence undoes any changes made by
|
// the unit configuration directory, and hence undoes any changes made by
|
||||||
// enable or link.
|
// enable or link.
|
||||||
func Disable(ctx context.Context, unit string, opts Options) error {
|
func Disable(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"disable", "--system", unit}
|
return disable(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable one or more units or unit instances.
|
// Enable one or more units or unit instances.
|
||||||
@@ -44,12 +41,7 @@ func Disable(ctx context.Context, unit string, opts Options) error {
|
|||||||
// manager configuration is reloaded (in a way equivalent to daemon-reload),
|
// manager configuration is reloaded (in a way equivalent to daemon-reload),
|
||||||
// in order to ensure the changes are taken into account immediately.
|
// in order to ensure the changes are taken into account immediately.
|
||||||
func Enable(ctx context.Context, unit string, opts Options) error {
|
func Enable(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"enable", "--system", unit}
|
return enable(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether any of the specified units are active (i.e. running).
|
// Check whether any of the specified units are active (i.e. running).
|
||||||
@@ -57,24 +49,8 @@ func Enable(ctx context.Context, unit string, opts Options) error {
|
|||||||
// Returns true if the unit is active, false if inactive or failed.
|
// Returns true if the unit is active, false if inactive or failed.
|
||||||
// Also returns false in an error case.
|
// Also returns false in an error case.
|
||||||
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
var args = []string{"is-active", "--system", unit}
|
result, err := isActive(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
return result, err
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
stdout, _, _, err := execute(ctx, args)
|
|
||||||
stdout = strings.TrimSuffix(stdout, "\n")
|
|
||||||
switch stdout {
|
|
||||||
case "inactive":
|
|
||||||
return false, nil
|
|
||||||
case "active":
|
|
||||||
return true, nil
|
|
||||||
case "failed":
|
|
||||||
return false, nil
|
|
||||||
case "activating":
|
|
||||||
return false, nil
|
|
||||||
default:
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks whether any of the specified unit files are enabled (as with enable).
|
// Checks whether any of the specified unit files are enabled (as with enable).
|
||||||
@@ -87,59 +63,14 @@ func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
|||||||
// See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
|
// See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
|
||||||
// for more information
|
// for more information
|
||||||
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
var args = []string{"is-enabled", "--system", unit}
|
result, err := isEnabled(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
return result, err
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
stdout, _, _, err := execute(ctx, args)
|
|
||||||
stdout = strings.TrimSuffix(stdout, "\n")
|
|
||||||
switch stdout {
|
|
||||||
case "enabled":
|
|
||||||
return true, nil
|
|
||||||
case "enabled-runtime":
|
|
||||||
return true, nil
|
|
||||||
case "linked":
|
|
||||||
return false, ErrLinked
|
|
||||||
case "linked-runtime":
|
|
||||||
return false, ErrLinked
|
|
||||||
case "alias":
|
|
||||||
return true, nil
|
|
||||||
case "masked":
|
|
||||||
return false, ErrMasked
|
|
||||||
case "masked-runtime":
|
|
||||||
return false, ErrMasked
|
|
||||||
case "static":
|
|
||||||
return true, nil
|
|
||||||
case "indirect":
|
|
||||||
return true, nil
|
|
||||||
case "disabled":
|
|
||||||
return false, nil
|
|
||||||
case "generated":
|
|
||||||
return true, nil
|
|
||||||
case "transient":
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return false, ErrUnspecified
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether any of the specified units are in a "failed" state.
|
// Check whether any of the specified units are in a "failed" state.
|
||||||
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
var args = []string{"is-failed", "--system", unit}
|
result, err := isFailed(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
return result, err
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
stdout, _, _, err := execute(ctx, args)
|
|
||||||
if matched, _ := regexp.MatchString(`inactive`, stdout); matched {
|
|
||||||
return false, nil
|
|
||||||
} else if matched, _ := regexp.MatchString(`active`, stdout); matched {
|
|
||||||
return false, nil
|
|
||||||
} else if matched, _ := regexp.MatchString(`failed`, stdout); matched {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mask one or more units, as specified on the command line. This will link
|
// Mask one or more units, as specified on the command line. This will link
|
||||||
@@ -149,70 +80,40 @@ func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
|||||||
// continue masking anyway. Calling Mask on a non-existing masked unit does not
|
// continue masking anyway. Calling Mask on a non-existing masked unit does not
|
||||||
// return an error. Similarly, see Unmask.
|
// return an error. Similarly, see Unmask.
|
||||||
func Mask(ctx context.Context, unit string, opts Options) error {
|
func Mask(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"mask", "--system", unit}
|
return mask(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop and then start one or more units specified on the command line.
|
// Stop and then start one or more units specified on the command line.
|
||||||
// If the units are not running yet, they will be started.
|
// If the units are not running yet, they will be started.
|
||||||
func Restart(ctx context.Context, unit string, opts Options) error {
|
func Restart(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"restart", "--system", unit}
|
return restart(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show a selected property of a unit. Accepted properties are predefined in the
|
// Show a selected property of a unit. Accepted properties are predefined in the
|
||||||
// properties subpackage to guarantee properties are valid and assist code-completion.
|
// properties subpackage to guarantee properties are valid and assist code-completion.
|
||||||
func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
|
func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
|
||||||
var args = []string{"show", "--system", unit, "--property", string(property)}
|
str, err := show(ctx, unit, property, opts)
|
||||||
if opts.UserMode {
|
return str, err
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
stdout, _, _, err := execute(ctx, args)
|
|
||||||
stdout = strings.TrimPrefix(stdout, string(property)+"=")
|
|
||||||
stdout = strings.TrimSuffix(stdout, "\n")
|
|
||||||
return stdout, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start (activate) a given unit
|
// Start (activate) a given unit
|
||||||
func Start(ctx context.Context, unit string, opts Options) error {
|
func Start(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"start", "--system", unit}
|
return start(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get back the status string which would be returned by running
|
// Get back the status string which would be returned by running
|
||||||
// `systemctl status [unit]`.
|
// `systemctl status [unit]`.
|
||||||
//
|
//
|
||||||
// Generally, it makes more sense to programatically retrieve the properties
|
// Generally, it makes more sense to programmatically retrieve the properties
|
||||||
// using Show, but this command is provided for the sake of completeness
|
// using Show, but this command is provided for the sake of completeness
|
||||||
func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
||||||
var args = []string{"status", "--system", unit}
|
stat, err := status(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
return stat, err
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
stdout, _, _, err := execute(ctx, args)
|
|
||||||
return stdout, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop (deactivate) a given unit
|
// Stop (deactivate) a given unit
|
||||||
func Stop(ctx context.Context, unit string, opts Options) error {
|
func Stop(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"stop", "--system", unit}
|
return stop(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmask one or more unit files, as specified on the command line.
|
// Unmask one or more unit files, as specified on the command line.
|
||||||
@@ -223,10 +124,5 @@ func Stop(ctx context.Context, unit string, opts Options) error {
|
|||||||
// If the unit doesn't exist but it's masked anyway, no error will be
|
// If the unit doesn't exist but it's masked anyway, no error will be
|
||||||
// returned. Gross, I know. Take it up with Poettering.
|
// returned. Gross, I know. Take it up with Poettering.
|
||||||
func Unmask(ctx context.Context, unit string, opts Options) error {
|
func Unmask(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"unmask", "--system", unit}
|
return unmask(ctx, unit, opts)
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
65
systemctl_darwin.go
Normal file
65
systemctl_darwin.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package systemctl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/taigrr/systemctl/properties"
|
||||||
|
)
|
||||||
|
|
||||||
|
func daemonReload(ctx context.Context, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func reenable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func disable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func enable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mask(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func restart(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func start(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func status(ctx context.Context, unit string, opts Options) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmask(ctx context.Context, unit string, opts Options) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
250
systemctl_linux.go
Normal file
250
systemctl_linux.go
Normal file
@@ -0,0 +1,250 @@
|
|||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package systemctl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/taigrr/systemctl/properties"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reload systemd manager configuration.
|
||||||
|
//
|
||||||
|
// This will rerun all generators (see systemd. generator(7)), reload all unit
|
||||||
|
// files, and recreate the entire dependency tree. While the daemon is being
|
||||||
|
// reloaded, all sockets systemd listens on behalf of user configuration will
|
||||||
|
// stay accessible.
|
||||||
|
func daemonReload(ctx context.Context, opts Options) error {
|
||||||
|
args := []string{"daemon-reload", "--system"}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reenables one or more units.
|
||||||
|
//
|
||||||
|
// This removes all symlinks to the unit files backing the specified units from
|
||||||
|
// the unit configuration directory, then recreates the symlink to the unit again,
|
||||||
|
// atomically. Can be used to change the symlink target.
|
||||||
|
func reenable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"reenable", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disables one or more units.
|
||||||
|
//
|
||||||
|
// This removes all symlinks to the unit files backing the specified units from
|
||||||
|
// the unit configuration directory, and hence undoes any changes made by
|
||||||
|
// enable or link.
|
||||||
|
func disable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"disable", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable one or more units or unit instances.
|
||||||
|
//
|
||||||
|
// This will create a set of symlinks, as encoded in the [Install] sections of
|
||||||
|
// the indicated unit files. After the symlinks have been created, the system
|
||||||
|
// manager configuration is reloaded (in a way equivalent to daemon-reload),
|
||||||
|
// in order to ensure the changes are taken into account immediately.
|
||||||
|
func enable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"enable", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether any of the specified units are active (i.e. running).
|
||||||
|
//
|
||||||
|
// Returns true if the unit is active, false if inactive or failed.
|
||||||
|
// Also returns false in an error case.
|
||||||
|
func isActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
args := []string{"is-active", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
stdout = strings.TrimSuffix(stdout, "\n")
|
||||||
|
switch stdout {
|
||||||
|
case "inactive":
|
||||||
|
return false, nil
|
||||||
|
case "active":
|
||||||
|
return true, nil
|
||||||
|
case "failed":
|
||||||
|
return false, nil
|
||||||
|
case "activating":
|
||||||
|
return false, nil
|
||||||
|
default:
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks whether any of the specified unit files are enabled (as with enable).
|
||||||
|
//
|
||||||
|
// Returns true if the unit is enabled, aliased, static, indirect, generated
|
||||||
|
// or transient.
|
||||||
|
//
|
||||||
|
// Returns false if disabled. Also returns an error if linked, masked, or bad.
|
||||||
|
//
|
||||||
|
// See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
|
||||||
|
// for more information
|
||||||
|
func isEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
args := []string{"is-enabled", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
stdout = strings.TrimSuffix(stdout, "\n")
|
||||||
|
switch stdout {
|
||||||
|
case "enabled":
|
||||||
|
return true, nil
|
||||||
|
case "enabled-runtime":
|
||||||
|
return true, nil
|
||||||
|
case "linked":
|
||||||
|
return false, ErrLinked
|
||||||
|
case "linked-runtime":
|
||||||
|
return false, ErrLinked
|
||||||
|
case "alias":
|
||||||
|
return true, nil
|
||||||
|
case "masked":
|
||||||
|
return false, ErrMasked
|
||||||
|
case "masked-runtime":
|
||||||
|
return false, ErrMasked
|
||||||
|
case "static":
|
||||||
|
return true, nil
|
||||||
|
case "indirect":
|
||||||
|
return true, nil
|
||||||
|
case "disabled":
|
||||||
|
return false, nil
|
||||||
|
case "generated":
|
||||||
|
return true, nil
|
||||||
|
case "transient":
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return false, ErrUnspecified
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether any of the specified units are in a "failed" state.
|
||||||
|
func isFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
args := []string{"is-failed", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
stdout = strings.TrimSuffix(stdout, "\n")
|
||||||
|
switch stdout {
|
||||||
|
case "inactive":
|
||||||
|
return false, nil
|
||||||
|
case "active":
|
||||||
|
return false, nil
|
||||||
|
case "failed":
|
||||||
|
return true, nil
|
||||||
|
default:
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mask one or more units, as specified on the command line. This will link
|
||||||
|
// these unit files to /dev/null, making it impossible to start them.
|
||||||
|
//
|
||||||
|
// Notably, Mask may return ErrDoesNotExist if a unit doesn't exist, but it will
|
||||||
|
// continue masking anyway. Calling Mask on a non-existing masked unit does not
|
||||||
|
// return an error. Similarly, see Unmask.
|
||||||
|
func mask(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"mask", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop and then start one or more units specified on the command line.
|
||||||
|
// If the units are not running yet, they will be started.
|
||||||
|
func restart(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"restart", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show a selected property of a unit. Accepted properties are predefined in the
|
||||||
|
// properties subpackage to guarantee properties are valid and assist code-completion.
|
||||||
|
func show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
|
||||||
|
args := []string{"show", "--system", unit, "--property", string(property)}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
stdout = strings.TrimPrefix(stdout, string(property)+"=")
|
||||||
|
stdout = strings.TrimSuffix(stdout, "\n")
|
||||||
|
return stdout, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start (activate) a given unit
|
||||||
|
func start(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"start", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get back the status string which would be returned by running
|
||||||
|
// `systemctl status [unit]`.
|
||||||
|
//
|
||||||
|
// Generally, it makes more sense to programmatically retrieve the properties
|
||||||
|
// using Show, but this command is provided for the sake of completeness
|
||||||
|
func status(ctx context.Context, unit string, opts Options) (string, error) {
|
||||||
|
args := []string{"status", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
return stdout, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop (deactivate) a given unit
|
||||||
|
func stop(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"stop", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmask one or more unit files, as specified on the command line.
|
||||||
|
// This will undo the effect of Mask.
|
||||||
|
//
|
||||||
|
// In line with systemd, Unmask will return ErrDoesNotExist if the unit
|
||||||
|
// doesn't exist, but only if it's not already masked.
|
||||||
|
// If the unit doesn't exist but it's masked anyway, no error will be
|
||||||
|
// returned. Gross, I know. Take it up with Poettering.
|
||||||
|
func unmask(ctx context.Context, unit string, opts Options) error {
|
||||||
|
args := []string{"unmask", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
@@ -38,9 +39,9 @@ func TestMain(m *testing.M) {
|
|||||||
}
|
}
|
||||||
os.Exit(retCode)
|
os.Exit(retCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDaemonReload(t *testing.T) {
|
func TestDaemonReload(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
unit string
|
|
||||||
err error
|
err error
|
||||||
opts Options
|
opts Options
|
||||||
runAsUser bool
|
runAsUser bool
|
||||||
@@ -48,22 +49,26 @@ func TestDaemonReload(t *testing.T) {
|
|||||||
/* Run these tests only as a user */
|
/* Run these tests only as a user */
|
||||||
|
|
||||||
// fail to reload system daemon as user
|
// fail to reload system daemon as user
|
||||||
{"", ErrInsufficientPermissions, Options{UserMode: false}, true},
|
{ErrInsufficientPermissions, Options{UserMode: false}, true},
|
||||||
// reload user's scope daemon
|
// reload user's scope daemon
|
||||||
{"", nil, Options{UserMode: true}, true},
|
{nil, Options{UserMode: true}, true},
|
||||||
/* End user tests*/
|
/* End user tests*/
|
||||||
|
|
||||||
/* Run these tests only as a superuser */
|
/* Run these tests only as a superuser */
|
||||||
|
|
||||||
// succeed to reload daemon
|
// succeed to reload daemon
|
||||||
{"", nil, Options{UserMode: false}, false},
|
{nil, Options{UserMode: false}, false},
|
||||||
// fail to connect to user bus as system
|
// fail to connect to user bus as system
|
||||||
{"", ErrBusFailure, Options{UserMode: true}, false},
|
{ErrBusFailure, Options{UserMode: true}, false},
|
||||||
|
|
||||||
/* End superuser tests*/
|
/* End superuser tests*/
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
|
mode := "user"
|
||||||
|
if tc.opts.UserMode == false {
|
||||||
|
mode = "system"
|
||||||
|
}
|
||||||
|
t.Run(fmt.Sprintf("DaemonReload as %s, %s mode", userString, mode), func(t *testing.T) {
|
||||||
if (userString == "root" || userString == "system") && tc.runAsUser {
|
if (userString == "root" || userString == "system") && tc.runAsUser {
|
||||||
t.Skip("skipping user test while running as superuser")
|
t.Skip("skipping user test while running as superuser")
|
||||||
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
|
||||||
@@ -72,79 +77,98 @@ func TestDaemonReload(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := DaemonReload(ctx, tc.opts)
|
err := DaemonReload(ctx, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDisable(t *testing.T) {
|
func TestDisable(t *testing.T) {
|
||||||
t.Run(fmt.Sprintf(""), func(t *testing.T) {
|
if userString != "root" && userString != "system" {
|
||||||
if userString != "root" && userString != "system" {
|
t.Skip("skipping superuser test while running as user")
|
||||||
t.Skip("skipping superuser test while running as user")
|
}
|
||||||
}
|
unit := "nginx"
|
||||||
unit := "nginx"
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
defer cancel()
|
||||||
defer cancel()
|
err := Mask(ctx, unit, Options{UserMode: false})
|
||||||
err := Mask(ctx, unit, Options{UserMode: false})
|
if err != nil {
|
||||||
if err != nil {
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
t.Errorf("Unable to mask %s", unit)
|
||||||
t.Errorf("Unable to mask %s", unit)
|
}
|
||||||
}
|
err = Disable(ctx, unit, Options{UserMode: false})
|
||||||
err = Disable(ctx, unit, Options{UserMode: false})
|
if !errors.Is(err, ErrMasked) {
|
||||||
if err != ErrMasked {
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
}
|
||||||
}
|
err = Unmask(ctx, unit, Options{UserMode: false})
|
||||||
err = Unmask(ctx, unit, Options{UserMode: false})
|
if err != nil {
|
||||||
if err != nil {
|
t.Errorf("Unable to unmask %s", unit)
|
||||||
t.Errorf("Unable to unmask %s", unit)
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReenable(t *testing.T) {
|
||||||
|
if userString != "root" && userString != "system" {
|
||||||
|
t.Skip("skipping superuser test while running as user")
|
||||||
|
}
|
||||||
|
unit := "nginx"
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
err := Mask(ctx, unit, Options{UserMode: false})
|
||||||
|
if err != nil {
|
||||||
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
|
t.Errorf("Unable to mask %s", unit)
|
||||||
|
}
|
||||||
|
err = Reenable(ctx, unit, Options{UserMode: false})
|
||||||
|
if !errors.Is(err, ErrMasked) {
|
||||||
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
|
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
||||||
|
}
|
||||||
|
err = Unmask(ctx, unit, Options{UserMode: false})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unable to unmask %s", unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnable(t *testing.T) {
|
func TestEnable(t *testing.T) {
|
||||||
|
if userString != "root" && userString != "system" {
|
||||||
t.Run(fmt.Sprintf(""), func(t *testing.T) {
|
t.Skip("skipping superuser test while running as user")
|
||||||
if userString != "root" && userString != "system" {
|
}
|
||||||
t.Skip("skipping superuser test while running as user")
|
unit := "nginx"
|
||||||
}
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
unit := "nginx"
|
defer cancel()
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
err := Mask(ctx, unit, Options{UserMode: false})
|
||||||
defer cancel()
|
if err != nil {
|
||||||
err := Mask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
if err != nil {
|
t.Errorf("Unable to mask %s", unit)
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
}
|
||||||
t.Errorf("Unable to mask %s", unit)
|
err = Enable(ctx, unit, Options{UserMode: false})
|
||||||
}
|
if !errors.Is(err, ErrMasked) {
|
||||||
err = Enable(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
if err != ErrMasked {
|
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
}
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
|
err = Unmask(ctx, unit, Options{UserMode: false})
|
||||||
}
|
if err != nil {
|
||||||
err = Unmask(ctx, unit, Options{UserMode: false})
|
t.Errorf("Unable to unmask %s", unit)
|
||||||
if err != nil {
|
}
|
||||||
t.Errorf("Unable to unmask %s", unit)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEnable() {
|
func ExampleEnable() {
|
||||||
unit := "syncthing"
|
unit := "syncthing"
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err := Enable(ctx, unit, Options{UserMode: true})
|
err := Enable(ctx, unit, Options{UserMode: true})
|
||||||
switch err {
|
switch {
|
||||||
case ErrMasked:
|
case errors.Is(err, ErrMasked):
|
||||||
fmt.Printf("%s is masked, unmask it before enabling\n", unit)
|
fmt.Printf("%s is masked, unmask it before enabling\n", unit)
|
||||||
case ErrDoesNotExist:
|
case errors.Is(err, ErrDoesNotExist):
|
||||||
fmt.Printf("%s does not exist\n", unit)
|
fmt.Printf("%s does not exist\n", unit)
|
||||||
case ErrInsufficientPermissions:
|
case errors.Is(err, ErrInsufficientPermissions):
|
||||||
fmt.Printf("permission to enable %s denied\n", unit)
|
fmt.Printf("permission to enable %s denied\n", unit)
|
||||||
case ErrBusFailure:
|
case errors.Is(err, ErrBusFailure):
|
||||||
fmt.Printf("Cannot communicate with the bus\n")
|
fmt.Printf("Cannot communicate with the bus\n")
|
||||||
case nil:
|
case err == nil:
|
||||||
fmt.Printf("%s enabled successfully\n", unit)
|
fmt.Printf("%s enabled successfully\n", unit)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("Error: %v", err)
|
fmt.Printf("Error: %v", err)
|
||||||
@@ -153,7 +177,7 @@ func ExampleEnable() {
|
|||||||
|
|
||||||
func TestIsActive(t *testing.T) {
|
func TestIsActive(t *testing.T) {
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
t.Run(fmt.Sprintf("check active"), func(t *testing.T) {
|
t.Run("check active", func(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping in short mode")
|
t.Skip("skipping in short mode")
|
||||||
}
|
}
|
||||||
@@ -169,10 +193,10 @@ func TestIsActive(t *testing.T) {
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
isActive, err := IsActive(ctx, unit, Options{UserMode: false})
|
isActive, err := IsActive(ctx, unit, Options{UserMode: false})
|
||||||
if !isActive {
|
if !isActive {
|
||||||
t.Errorf("IsActive didn't return true for %s", unit)
|
t.Errorf("IsActive didn't return true for %s: %v", unit, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
|
t.Run("check masked", func(t *testing.T) {
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
t.Skip("skipping superuser test while running as user")
|
t.Skip("skipping superuser test while running as user")
|
||||||
}
|
}
|
||||||
@@ -188,7 +212,7 @@ func TestIsActive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
|
t.Run("check masked", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := IsActive(ctx, "nonexistant", Options{UserMode: false})
|
_, err := IsActive(ctx, "nonexistant", Options{UserMode: false})
|
||||||
@@ -196,7 +220,6 @@ func TestIsActive(t *testing.T) {
|
|||||||
t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist)
|
t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsEnabled(t *testing.T) {
|
func TestIsEnabled(t *testing.T) {
|
||||||
@@ -206,7 +229,7 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
userMode = true
|
userMode = true
|
||||||
unit = "syncthing"
|
unit = "syncthing"
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("check enabled"), func(t *testing.T) {
|
t.Run("check enabled", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Enable(ctx, unit, Options{UserMode: userMode})
|
err := Enable(ctx, unit, Options{UserMode: userMode})
|
||||||
@@ -215,10 +238,10 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
isEnabled, err := IsEnabled(ctx, unit, Options{UserMode: userMode})
|
isEnabled, err := IsEnabled(ctx, unit, Options{UserMode: userMode})
|
||||||
if !isEnabled {
|
if !isEnabled {
|
||||||
t.Errorf("IsEnabled didn't return true for %s", unit)
|
t.Errorf("IsEnabled didn't return true for %s: %v", unit, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check disabled"), func(t *testing.T) {
|
t.Run("check disabled", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Disable(ctx, unit, Options{UserMode: userMode})
|
err := Disable(ctx, unit, Options{UserMode: userMode})
|
||||||
@@ -234,7 +257,7 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Enable(ctx, unit, Options{UserMode: false})
|
Enable(ctx, unit, Options{UserMode: false})
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("check masked"), func(t *testing.T) {
|
t.Run("check masked", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, unit, Options{UserMode: userMode})
|
err := Mask(ctx, unit, Options{UserMode: userMode})
|
||||||
@@ -251,7 +274,6 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
Unmask(ctx, unit, Options{UserMode: userMode})
|
Unmask(ctx, unit, Options{UserMode: userMode})
|
||||||
Enable(ctx, unit, Options{UserMode: userMode})
|
Enable(ctx, unit, Options{UserMode: userMode})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMask(t *testing.T) {
|
func TestMask(t *testing.T) {
|
||||||
@@ -263,7 +285,7 @@ func TestMask(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
/* Run these tests only as an unpriviledged user */
|
/* Run these tests only as an unpriviledged user */
|
||||||
|
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
|
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", nil, Options{UserMode: true}, true},
|
{"syncthing", nil, Options{UserMode: true}, true},
|
||||||
@@ -296,13 +318,13 @@ func TestMask(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, tc.unit, tc.opts)
|
err := Mask(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
Unmask(ctx, tc.unit, tc.opts)
|
Unmask(ctx, tc.unit, tc.opts)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("test double masking existing"), func(t *testing.T) {
|
t.Run("test double masking existing", func(t *testing.T) {
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
userMode := false
|
userMode := false
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
@@ -321,19 +343,16 @@ func TestMask(t *testing.T) {
|
|||||||
t.Errorf("error on second masking is %v, but should have been %v", err, nil)
|
t.Errorf("error on second masking is %v, but should have been %v", err, nil)
|
||||||
}
|
}
|
||||||
Unmask(ctx, unit, opts)
|
Unmask(ctx, unit, opts)
|
||||||
|
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("test double masking nonexisting"), func(t *testing.T) {
|
t.Run("test double masking nonexisting", func(t *testing.T) {
|
||||||
unit := "nonexistant"
|
unit := "nonexistant"
|
||||||
userMode := false
|
userMode := userString != "root" && userString != "system"
|
||||||
if userString != "root" && userString != "system" {
|
|
||||||
userMode = true
|
|
||||||
}
|
|
||||||
opts := Options{UserMode: userMode}
|
opts := Options{UserMode: userMode}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, unit, opts)
|
err := Mask(ctx, unit, opts)
|
||||||
if err != ErrDoesNotExist {
|
if !errors.Is(err, ErrDoesNotExist) {
|
||||||
t.Errorf("error on initial masking is %v, but should have been %v", err, ErrDoesNotExist)
|
t.Errorf("error on initial masking is %v, but should have been %v", err, ErrDoesNotExist)
|
||||||
}
|
}
|
||||||
err = Mask(ctx, unit, opts)
|
err = Mask(ctx, unit, opts)
|
||||||
@@ -342,7 +361,6 @@ func TestMask(t *testing.T) {
|
|||||||
}
|
}
|
||||||
Unmask(ctx, unit, opts)
|
Unmask(ctx, unit, opts)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRestart(t *testing.T) {
|
func TestRestart(t *testing.T) {
|
||||||
@@ -366,9 +384,9 @@ func TestRestart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
syscall.Kill(pid, syscall.SIGKILL)
|
syscall.Kill(pid, syscall.SIGKILL)
|
||||||
for {
|
for {
|
||||||
running, err := IsActive(ctx, unit, opts)
|
running, errIsActive := IsActive(ctx, unit, opts)
|
||||||
if err != nil {
|
if errIsActive != nil {
|
||||||
t.Errorf("error asserting %s is up: %v", unit, err)
|
t.Errorf("error asserting %s is up: %v", unit, errIsActive)
|
||||||
break
|
break
|
||||||
} else if running {
|
} else if running {
|
||||||
break
|
break
|
||||||
@@ -381,7 +399,6 @@ func TestRestart(t *testing.T) {
|
|||||||
if restarts+1 != secondRestarts {
|
if restarts+1 != secondRestarts {
|
||||||
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
|
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runs through all defined Properties in parallel and checks for error cases
|
// Runs through all defined Properties in parallel and checks for error cases
|
||||||
@@ -394,15 +411,17 @@ func TestShow(t *testing.T) {
|
|||||||
UserMode: false,
|
UserMode: false,
|
||||||
}
|
}
|
||||||
for _, x := range properties.Properties {
|
for _, x := range properties.Properties {
|
||||||
t.Run(fmt.Sprintf("show property %s", string(x)), func(t *testing.T) {
|
func(x properties.Property) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
t.Run(fmt.Sprintf("show property %s", string(x)), func(t *testing.T) {
|
||||||
defer cancel()
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
t.Parallel()
|
defer cancel()
|
||||||
_, err := Show(ctx, unit, x, opts)
|
t.Parallel()
|
||||||
if err != nil {
|
_, err := Show(ctx, unit, x, opts)
|
||||||
t.Errorf("error is %v, but should have been %v", err, nil)
|
if err != nil {
|
||||||
}
|
t.Errorf("error is %v, but should have been %v", err, nil)
|
||||||
})
|
}
|
||||||
|
})
|
||||||
|
}(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -439,7 +458,6 @@ func TestStart(t *testing.T) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStatus(t *testing.T) {
|
func TestStatus(t *testing.T) {
|
||||||
@@ -452,7 +470,6 @@ func TestStatus(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error: %v", err)
|
t.Errorf("error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStop(t *testing.T) {
|
func TestStop(t *testing.T) {
|
||||||
@@ -488,7 +505,6 @@ func TestStop(t *testing.T) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmask(t *testing.T) {
|
func TestUnmask(t *testing.T) {
|
||||||
@@ -500,7 +516,7 @@ func TestUnmask(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
/* Run these tests only as an unpriviledged user */
|
/* Run these tests only as an unpriviledged user */
|
||||||
|
|
||||||
//try nonexistant unit in user mode as user
|
// try nonexistant unit in user mode as user
|
||||||
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
|
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
|
||||||
// try existing unit in user mode as user
|
// try existing unit in user mode as user
|
||||||
{"syncthing", nil, Options{UserMode: true}, true},
|
{"syncthing", nil, Options{UserMode: true}, true},
|
||||||
@@ -533,13 +549,13 @@ func TestUnmask(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Mask(ctx, tc.unit, tc.opts)
|
err := Mask(ctx, tc.unit, tc.opts)
|
||||||
if err != tc.err {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
t.Errorf("error is %v, but should have been %v", err, tc.err)
|
||||||
}
|
}
|
||||||
Unmask(ctx, tc.unit, tc.opts)
|
Unmask(ctx, tc.unit, tc.opts)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
t.Run(fmt.Sprintf("test double unmasking existing"), func(t *testing.T) {
|
t.Run("test double unmasking existing", func(t *testing.T) {
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
userMode := false
|
userMode := false
|
||||||
if userString != "root" && userString != "system" {
|
if userString != "root" && userString != "system" {
|
||||||
@@ -558,14 +574,11 @@ func TestUnmask(t *testing.T) {
|
|||||||
t.Errorf("error on second unmasking is %v, but should have been %v", err, nil)
|
t.Errorf("error on second unmasking is %v, but should have been %v", err, nil)
|
||||||
}
|
}
|
||||||
Unmask(ctx, unit, opts)
|
Unmask(ctx, unit, opts)
|
||||||
|
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("test double unmasking nonexisting"), func(t *testing.T) {
|
t.Run("test double unmasking nonexisting", func(t *testing.T) {
|
||||||
unit := "nonexistant"
|
unit := "nonexistant"
|
||||||
userMode := false
|
userMode := userString != "root" && userString != "system"
|
||||||
if userString != "root" && userString != "system" {
|
|
||||||
userMode = true
|
|
||||||
}
|
|
||||||
opts := Options{UserMode: userMode}
|
opts := Options{UserMode: userMode}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -575,9 +588,8 @@ func TestUnmask(t *testing.T) {
|
|||||||
t.Errorf("error on initial unmasking is %v, but should have been %v", err, nil)
|
t.Errorf("error on initial unmasking is %v, but should have been %v", err, nil)
|
||||||
}
|
}
|
||||||
err = Unmask(ctx, unit, opts)
|
err = Unmask(ctx, unit, opts)
|
||||||
if err != ErrDoesNotExist {
|
if !errors.Is(err, ErrDoesNotExist) {
|
||||||
t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist)
|
t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
64
util.go
64
util.go
@@ -3,20 +3,19 @@ package systemctl
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var systemctl string
|
var systemctl string
|
||||||
|
|
||||||
|
// killed is the exit code returned when a process is terminated by SIGINT.
|
||||||
const killed = 130
|
const killed = 130
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
path, err := exec.LookPath("systemctl")
|
path, _ := exec.LookPath("systemctl")
|
||||||
if err != nil {
|
|
||||||
panic(ErrNotInstalled)
|
|
||||||
}
|
|
||||||
systemctl = path
|
systemctl = path
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +29,9 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
|||||||
warnings string
|
warnings string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if systemctl == "" {
|
||||||
|
return "", "", 1, ErrNotInstalled
|
||||||
|
}
|
||||||
cmd := exec.CommandContext(ctx, systemctl, args...)
|
cmd := exec.CommandContext(ctx, systemctl, args...)
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
@@ -38,6 +40,10 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
|||||||
warnings = stderr.String()
|
warnings = stderr.String()
|
||||||
code = cmd.ProcessState.ExitCode()
|
code = cmd.ProcessState.ExitCode()
|
||||||
|
|
||||||
|
if code == killed {
|
||||||
|
return output, warnings, code, ErrExecTimeout
|
||||||
|
}
|
||||||
|
|
||||||
customErr := filterErr(warnings)
|
customErr := filterErr(warnings)
|
||||||
if customErr != nil {
|
if customErr != nil {
|
||||||
err = customErr
|
err = customErr
|
||||||
@@ -50,32 +56,26 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func filterErr(stderr string) error {
|
func filterErr(stderr string) error {
|
||||||
if matched, _ := regexp.MatchString(`does not exist`, stderr); matched {
|
switch {
|
||||||
return ErrDoesNotExist
|
case strings.Contains(stderr, `does not exist`):
|
||||||
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `not found.`):
|
||||||
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `not loaded.`):
|
||||||
|
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `No such file or directory`):
|
||||||
|
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `Interactive authentication required`):
|
||||||
|
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `Access denied`):
|
||||||
|
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `DBUS_SESSION_BUS_ADDRESS`):
|
||||||
|
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `is masked`):
|
||||||
|
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
case strings.Contains(stderr, `Failed`):
|
||||||
|
return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr))
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if matched, _ := regexp.MatchString(`not found.`, stderr); matched {
|
|
||||||
return ErrDoesNotExist
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`not loaded.`, stderr); matched {
|
|
||||||
return ErrUnitNotLoaded
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`No such file or directory`, stderr); matched {
|
|
||||||
return ErrDoesNotExist
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`Interactive authentication required`, stderr); matched {
|
|
||||||
return ErrInsufficientPermissions
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`Access denied`, stderr); matched {
|
|
||||||
return ErrInsufficientPermissions
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`DBUS_SESSION_BUS_ADDRESS`, stderr); matched {
|
|
||||||
return ErrBusFailure
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`is masked`, stderr); matched {
|
|
||||||
return ErrMasked
|
|
||||||
}
|
|
||||||
if matched, _ := regexp.MatchString(`Failed`, stderr); matched {
|
|
||||||
return ErrUnspecified
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user