aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/app.go
blob: 3260deab3a82a9171e2dab1bbc568871ff46cdb5 (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
package app

import (
	"sync"

	"git.ophivana.moe/security/fortify/cmd/fshim/ipc/shim"
	"git.ophivana.moe/security/fortify/internal/linux"
)

type App interface {
	// ID returns a copy of App's unique ID.
	ID() ID
	// Start sets up the system and starts the App.
	Start() error
	// Wait waits for App's process to exit and reverts system setup.
	Wait() (int, error)
	// WaitErr returns error returned by the underlying wait syscall.
	WaitErr() error

	Seal(config *Config) error
	String() string
}

type app struct {
	// application unique identifier
	id *ID
	// operating system interface
	os linux.System
	// shim process manager
	shim *shim.Shim
	// child process related information
	seal *appSeal
	// error returned waiting for process
	waitErr error

	lock sync.RWMutex
}

func (a *app) ID() ID {
	return *a.id
}

func (a *app) String() string {
	if a == nil {
		return "(invalid fortified app)"
	}

	a.lock.RLock()
	defer a.lock.RUnlock()

	if a.shim != nil {
		return a.shim.String()
	}

	if a.seal != nil {
		return "(sealed fortified app as uid " + a.seal.sys.user.us + ")"
	}

	return "(unsealed fortified app)"
}

func (a *app) WaitErr() error {
	return a.waitErr
}

func New(os linux.System) (App, error) {
	a := new(app)
	a.id = new(ID)
	a.os = os
	return a, newAppID(a.id)
}