aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/state/multi.go
blob: 16afc0868b835eaee51e7123e6cd09dcc97ee35d (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package state

import (
	"encoding/gob"
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path"
	"strconv"
	"sync"
	"syscall"

	"git.ophivana.moe/security/fortify/fst"
	"git.ophivana.moe/security/fortify/internal/fmsg"
)

// fine-grained locking and access
type multiStore struct {
	base string

	// initialised backends
	backends *sync.Map

	lock sync.RWMutex
}

func (s *multiStore) Do(aid int, f func(c Cursor)) (bool, error) {
	s.lock.RLock()
	defer s.lock.RUnlock()

	// load or initialise new backend
	b := new(multiBackend)
	if v, ok := s.backends.LoadOrStore(aid, b); ok {
		b = v.(*multiBackend)
	} else {
		b.lock.Lock()
		b.path = path.Join(s.base, strconv.Itoa(aid))

		// ensure directory
		if err := os.MkdirAll(b.path, 0700); err != nil && !errors.Is(err, fs.ErrExist) {
			s.backends.CompareAndDelete(aid, b)
			return false, err
		}

		// open locker file
		if l, err := os.OpenFile(b.path+".lock", os.O_RDWR|os.O_CREATE, 0600); err != nil {
			s.backends.CompareAndDelete(aid, b)
			return false, err
		} else {
			b.lockfile = l
		}
		b.lock.Unlock()
	}

	// lock backend
	if err := b.lockFile(); err != nil {
		return false, err
	}

	// expose backend methods without exporting the pointer
	c := new(struct{ *multiBackend })
	c.multiBackend = b
	f(b)
	// disable access to the backend on a best-effort basis
	c.multiBackend = nil

	// unlock backend
	return true, b.unlockFile()
}

func (s *multiStore) List() ([]int, error) {
	var entries []os.DirEntry

	// read base directory to get all aids
	if v, err := os.ReadDir(s.base); err != nil && !errors.Is(err, os.ErrNotExist) {
		return nil, err
	} else {
		entries = v
	}

	aidsBuf := make([]int, 0, len(entries))
	for _, e := range entries {
		// skip non-directories
		if !e.IsDir() {
			fmsg.VPrintf("skipped non-directory entry %q", e.Name())
			continue
		}

		// skip non-numerical names
		if v, err := strconv.Atoi(e.Name()); err != nil {
			fmsg.VPrintf("skipped non-aid entry %q", e.Name())
			continue
		} else {
			if v < 0 || v > 9999 {
				fmsg.VPrintf("skipped out of bounds entry %q", e.Name())
				continue
			}

			aidsBuf = append(aidsBuf, v)
		}
	}

	return append([]int(nil), aidsBuf...), nil
}

func (s *multiStore) Close() error {
	s.lock.Lock()
	defer s.lock.Unlock()

	var errs []error
	s.backends.Range(func(_, value any) bool {
		b := value.(*multiBackend)
		errs = append(errs, b.close())
		return true
	})

	return errors.Join(errs...)
}

type multiBackend struct {
	path string

	// created/opened by prepare
	lockfile *os.File

	lock sync.RWMutex
}

func (b *multiBackend) filename(id *fst.ID) string {
	return path.Join(b.path, id.String())
}

func (b *multiBackend) lockFileAct(lt int) (err error) {
	op := "LockAct"
	switch lt {
	case syscall.LOCK_EX:
		op = "Lock"
	case syscall.LOCK_UN:
		op = "Unlock"
	}

	for {
		err = syscall.Flock(int(b.lockfile.Fd()), lt)
		if !errors.Is(err, syscall.EINTR) {
			break
		}
	}
	if err != nil {
		return &fs.PathError{
			Op:   op,
			Path: b.lockfile.Name(),
			Err:  err,
		}
	}
	return nil
}

func (b *multiBackend) lockFile() error {
	return b.lockFileAct(syscall.LOCK_EX)
}

func (b *multiBackend) unlockFile() error {
	return b.lockFileAct(syscall.LOCK_UN)
}

// reads all launchers in simpleBackend
// file contents are ignored if decode is false
func (b *multiBackend) load(decode bool) (Entries, error) {
	b.lock.RLock()
	defer b.lock.RUnlock()

	// read directory contents, should only contain files named after ids
	var entries []os.DirEntry
	if pl, err := os.ReadDir(b.path); err != nil {
		return nil, err
	} else {
		entries = pl
	}

	// allocate as if every entry is valid
	// since that should be the case assuming no external interference happens
	r := make(Entries, len(entries))

	for _, e := range entries {
		if e.IsDir() {
			return nil, fmt.Errorf("unexpected directory %q in store", e.Name())
		}

		id := new(fst.ID)
		if err := fst.ParseAppID(id, e.Name()); err != nil {
			return nil, err
		}

		// run in a function to better handle file closing
		if err := func() error {
			// open state file for reading
			if f, err := os.Open(path.Join(b.path, e.Name())); err != nil {
				return err
			} else {
				defer func() {
					if f.Close() != nil {
						// unreachable
						panic("foreign state file closed prematurely")
					}
				}()

				s := new(State)
				r[*id] = s

				// append regardless, but only parse if required, used to implement Len
				if decode {
					if err = gob.NewDecoder(f).Decode(s); err != nil {
						return err
					}

					if s.ID != *id {
						return fmt.Errorf("state entry %s has unexpected id %s", id, &s.ID)
					}
				}

				return nil
			}
		}(); err != nil {
			return nil, err
		}
	}

	return r, nil
}

// Save writes process state to filesystem
func (b *multiBackend) Save(state *State) error {
	b.lock.Lock()
	defer b.lock.Unlock()

	if state.Config == nil {
		return errors.New("state does not contain config")
	}

	statePath := b.filename(&state.ID)

	// create and open state data file
	if f, err := os.OpenFile(statePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600); err != nil {
		return err
	} else {
		defer func() {
			if f.Close() != nil {
				// unreachable
				panic("state file closed prematurely")
			}
		}()
		// encode into state file
		return gob.NewEncoder(f).Encode(state)
	}
}

func (b *multiBackend) Destroy(id fst.ID) error {
	b.lock.Lock()
	defer b.lock.Unlock()

	return os.Remove(b.filename(&id))
}

func (b *multiBackend) Load() (Entries, error) {
	return b.load(true)
}

func (b *multiBackend) Len() (int, error) {
	// rn consists of only nil entries but has the correct length
	rn, err := b.load(false)
	return len(rn), err
}

func (b *multiBackend) close() error {
	b.lock.Lock()
	defer b.lock.Unlock()

	err := b.lockfile.Close()
	if err == nil || errors.Is(err, os.ErrInvalid) || errors.Is(err, os.ErrClosed) {
		return nil
	}
	return err
}

// NewMulti returns an instance of the multi-file store.
func NewMulti(runDir string) Store {
	b := new(multiStore)
	b.base = path.Join(runDir, "state")
	b.backends = new(sync.Map)
	return b
}