File: systemd.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (85 lines) | stat: -rw-r--r-- 1,489 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//go:build systemd && cgo

package config

import (
	"os"
	"path/filepath"
	"strings"
	"sync"

	"github.com/containers/common/pkg/cgroupv2"
	"github.com/containers/storage/pkg/unshare"
)

var (
	systemdOnce  sync.Once
	usesSystemd  bool
	journaldOnce sync.Once
	usesJournald bool
)

const (
	// DefaultLogDriver is the default type of log files
	DefaultLogDriver = "journald"
)

func defaultCgroupManager() string {
	if !useSystemd() {
		return CgroupfsCgroupsManager
	}
	enabled, err := cgroupv2.Enabled()
	if err == nil && !enabled && unshare.IsRootless() {
		return CgroupfsCgroupsManager
	}

	return SystemdCgroupsManager
}

func defaultEventsLogger() string {
	if useJournald() {
		return "journald"
	}
	return "file"
}

func defaultLogDriver() string {
	if useJournald() {
		return "journald"
	}
	return "k8s-file"
}

func useSystemd() bool {
	systemdOnce.Do(func() {
		dat, err := os.ReadFile("/proc/1/comm")
		if err == nil {
			val := strings.TrimSuffix(string(dat), "\n")
			usesSystemd = (val == "systemd")
		}
	})
	return usesSystemd
}

func useJournald() bool {
	journaldOnce.Do(func() {
		if !useSystemd() {
			return
		}
		for _, root := range []string{"/run/log/journal", "/var/log/journal"} {
			dirs, err := os.ReadDir(root)
			if err != nil {
				continue
			}
			for _, d := range dirs {
				if d.IsDir() {
					if _, err := os.ReadDir(filepath.Join(root, d.Name())); err == nil {
						usesJournald = true
						return
					}
				}
			}
		}
	})
	return usesJournald
}