aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/outcome/spruntime.go
blob: 15974a44ab373e86e6b72d8c15698a9e1f689fd5 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package outcome

import (
	"encoding/gob"

	"hakurei.app/check"
	"hakurei.app/container/std"
	"hakurei.app/fhs"
	"hakurei.app/hst"
	"hakurei.app/internal/acl"
	"hakurei.app/internal/system"
)

const (
	/*
		Path to a user-private user-writable directory that is bound
		to the user login time on the machine. It is automatically
		created the first time a user logs in and removed on the
		user's final logout. If a user logs in twice at the same time,
		both sessions will see the same $XDG_RUNTIME_DIR and the same
		contents. If a user logs in once, then logs out again, and
		logs in again, the directory contents will have been lost in
		between, but applications should not rely on this behavior and
		must be able to deal with stale files. To store
		session-private data in this directory, the user should
		include the value of $XDG_SESSION_ID in the filename. This
		directory shall be used for runtime file system objects such
		as AF_UNIX sockets, FIFOs, PID files and similar. It is
		guaranteed that this directory is local and offers the
		greatest possible file system feature set the operating system
		provides. For further details, see the XDG Base Directory
		Specification[3].  $XDG_RUNTIME_DIR is not set if the current
		user is not the original user of the session.
	*/
	envXDGRuntimeDir = "XDG_RUNTIME_DIR"

	/*
		The session class. This may be used instead of class= on the
		module parameter line, and is usually preferred.
	*/
	envXDGSessionClass = "XDG_SESSION_CLASS"

	/*
		A regular interactive user session. This is the default class
		for sessions for which a TTY or X display is known at session
		registration time.
	*/
	xdgSessionClassUser = "user"

	/*
		The session type. This may be used instead of type= on the
		module parameter line, and is usually preferred.

		One of "unspecified", "tty", "x11", "wayland", "mir", or "web".
	*/
	envXDGSessionType = "XDG_SESSION_TYPE"
)

func init() { gob.Register(new(spRuntimeOp)) }

const (
	sessionTypeUnspec = iota
	sessionTypeTTY
	sessionTypeX11
	sessionTypeWayland
)

// spRuntimeOp sets up XDG_RUNTIME_DIR inside the container.
type spRuntimeOp struct {
	// SessionType determines the value of envXDGSessionType.
	//
	// Populated during toSystem.
	SessionType uintptr
}

func (s *spRuntimeOp) toSystem(state *outcomeStateSys) error {
	if state.Container.Flags&hst.FShareRuntime != 0 {
		runtimeDir, runtimeDirInst := s.commonPaths(state.outcomeState)
		state.sys.Ensure(runtimeDir, 0700)
		state.sys.UpdatePermType(system.User, runtimeDir, acl.Execute)
		state.sys.Ensure(runtimeDirInst, 0700)
		state.sys.UpdatePermType(system.User, runtimeDirInst, acl.Read, acl.Write, acl.Execute)
	}

	if state.et&hst.EWayland != 0 {
		s.SessionType = sessionTypeWayland
	} else if state.et&hst.EX11 != 0 {
		s.SessionType = sessionTypeX11
	} else {
		s.SessionType = sessionTypeTTY
	}

	return nil
}

// xdgRuntimeDirSize is the size of the filesystem mounted on inner XDG_RUNTIME_DIR.
const xdgRuntimeDirSize = 1 << 24

func (s *spRuntimeOp) toContainer(state *outcomeStateParams) error {
	state.runtimeDir = fhs.AbsRunUser.Append(state.mapuid.String())
	state.env[envXDGRuntimeDir] = state.runtimeDir.String()
	state.env[envXDGSessionClass] = xdgSessionClassUser

	switch s.SessionType {
	case sessionTypeUnspec:
		state.env[envXDGSessionType] = "unspecified"
	case sessionTypeTTY:
		state.env[envXDGSessionType] = "tty"
	case sessionTypeX11:
		state.env[envXDGSessionType] = "x11"
	case sessionTypeWayland:
		state.env[envXDGSessionType] = "wayland"

	}

	state.params.Tmpfs(fhs.AbsRunUser, xdgRuntimeDirSize, 0755)
	if state.Container.Flags&hst.FShareRuntime != 0 {
		_, runtimeDirInst := s.commonPaths(state.outcomeState)
		state.params.Bind(runtimeDirInst, state.runtimeDir, std.BindWritable)
	} else {
		state.params.Mkdir(state.runtimeDir, 0700)
	}
	return nil
}

func (s *spRuntimeOp) commonPaths(state *outcomeState) (runtimeDir, runtimeDirInst *check.Absolute) {
	runtimeDir = state.sc.SharePath.Append("runtime")
	runtimeDirInst = runtimeDir.Append(state.identity.String())
	return
}