-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
119 lines (106 loc) · 3.05 KB
/
process.go
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
package service
import (
"fmt"
"math/rand"
"os"
"reflect"
"syscall"
"time"
"unsafe"
pid "github.com/multiverse-os/service/pid"
signal "github.com/multiverse-os/service/signal"
)
// [ General process utilities ]////////////////////////////////////////////////
func SeedRandom() {
rand.Seed(time.Now().UnixNano())
}
func isProcessRunning(pid int) bool {
if process, err := os.FindProcess(pid); err == nil {
return false
} else {
err = process.Signal(syscall.Signal(0))
return (err != nil)
}
}
//func SetUserId(procAttr *syscall.SysProcAttr, uid uint32, gid uint32) {
// procAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid, NoSetGroups: true}
//}
// TODO: May want to add a weight concept so that certain functions will be
// esnured to be ran before others
type Process struct {
Pid int
PidFile *pid.File
User string
UID int
GID int
TempDirectory string
UserCacheDirectory string
Env map[string]string
EnvVars []string
Executable string
WorkingDirectory string
StartedAt time.Time
Signals signal.Handler
shutdown []func()
Credential *syscall.Credential
LogDirectory string
LogFile string
Initialized bool
Chroot string
Args []string
Umask int
abspath string
logFile *os.File
nullFile *os.File
rpipe, wpipe *os.File
EnvVar
//EnvVars map[string]string
}
// TODO
// This started at isn't the actual started at time, its the parsed at
// time.
func ParseProcess() *Process {
if executable, err := os.Executable(); err != nil {
panic(fmt.Errorf("failed to parse process:", err))
} else {
return &Process{
Pid: os.Getpid(),
UID: os.Getuid(),
GID: os.Getgid(),
Executable: executable,
StartedAt: time.Now(),
}
}
}
func (p *Process) ShutdownCount() int { return len(p.shutdown) }
func (p *Process) Shutdown() {
for _, function := range p.shutdown {
function()
}
}
func (p *Process) AppendToShutdown(exitFunction func()) {
p.shutdown = append(p.shutdown, exitFunction)
}
// [ Method for process ]///////////////////////////////////////////////////////
// NOTE: Returns the close function so that it can be called easily added to a
// defer. This is important because since we are doing OS based locks on the
// pidfile we may need to unlock the file
func (p *Process) WritePid(path string) *pid.File {
fmt.Println("writing pid:", p.Pid)
if pidFile, err := pid.Write(path); err != nil {
panic(fmt.Errorf("failed to write pid: ", err))
} else {
p.PidFile = pidFile
return p.PidFile
}
}
func (p *Process) CleanPid() error { return p.PidFile.Clean() }
// NOTE: Set process name, as in the name seen in `ps`
func (p *Process) SetName(name string) {
argv0str := (*reflect.StringHeader)(unsafe.Pointer(&os.Args[0]))
argv0 := (*[1 << 30]byte)(unsafe.Pointer(argv0str.Data))[:argv0str.Len]
n := copy(argv0, name)
if n < len(argv0) {
argv0[n] = 0
}
}