summaryrefslogtreecommitdiff
path: root/cmd/ioworkload/scenario_flock.go
blob: a54f5d098e9ce81bfe392cb201ddfac04471d6d8 (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
package main

import (
	"fmt"
	"path/filepath"
	"syscall"
)

// flockBasic exercises the flock syscall end-to-end. It opens a temp file,
// acquires an exclusive advisory lock (LOCK_EX), releases it (LOCK_UN), then
// closes the file and removes the temp dir.
//
// flock is unprivileged (FamilyFS) and captured as KindFd at args[0]; ior
// resolves that fd to the underlying file path via the procfd cache, so the
// enter_flock record carries the temp filename. Its return value is
// UNCLASSIFIED (0 on success), so this scenario only locks in the enter path.
func flockBasic() error {
	dir, cleanup, err := makeTempDir("flock-basic")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "flockfile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	// Use raw syscall.Flock so the exact sys_enter_flock tracepoint fires.
	if err := syscall.Flock(fd, syscall.LOCK_EX); err != nil {
		return fmt.Errorf("flock LOCK_EX: %w", err)
	}
	if err := syscall.Flock(fd, syscall.LOCK_UN); err != nil {
		return fmt.Errorf("flock LOCK_UN: %w", err)
	}
	return nil
}