-
Notifications
You must be signed in to change notification settings - Fork 141
/
os.go
74 lines (59 loc) · 1.28 KB
/
os.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
package debos
import (
"fmt"
"os"
"path"
)
const debianPolicyHelper = "/usr/sbin/policy-rc.d"
/*
ServiceHelper is used to manage services.
Currently supports only debian-based family.
*/
type ServiceHelper struct {
Rootdir string
}
type ServicesManager interface {
Allow() error
Deny() error
}
/*
Allow() allows to start/stop services on OS level.
*/
func (s *ServiceHelper) Allow() error {
helperFile := path.Join(s.Rootdir, debianPolicyHelper)
if _, err := os.Stat(helperFile); os.IsNotExist(err) {
return nil
}
if err := os.Remove(helperFile); err != nil {
return err
}
return nil
}
/*
Deny() prohibits to start/stop services on OS level.
*/
func (s *ServiceHelper) Deny() error {
helperFile := path.Join(s.Rootdir, debianPolicyHelper)
var helper = []byte(`#!/bin/sh
exit 101
`)
if _, err := os.Stat(helperFile); os.IsExist(err) {
return fmt.Errorf("Policy helper file '%s' exists already", debianPolicyHelper)
}
if _, err := os.Stat(path.Dir(helperFile)); os.IsNotExist(err) {
// do not try to do something if ".../usr/sbin" is not exists
return nil
}
pf, err := os.Create(helperFile)
if err != nil {
return err
}
defer pf.Close()
if _, err := pf.Write(helper); err != nil {
return err
}
if err := pf.Chmod(0755); err != nil {
return err
}
return nil
}