summaryrefslogtreecommitdiff
path: root/os.go
diff options
context:
space:
mode:
authorHéctor Orón Martínez <zumbi@debian.org>2018-01-09 19:12:07 +0100
committerHéctor Orón Martínez <zumbi@debian.org>2018-01-09 19:12:07 +0100
commit4808cb7058c548bf76476ec2f9618d784d76bdda (patch)
tree1dc1e8cc24171783fc8d9da306b1e92798960a15 /os.go
New upstream version 1.0.0+git20171222.87b0d5e
Diffstat (limited to 'os.go')
-rw-r--r--os.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/os.go b/os.go
new file mode 100644
index 0000000..67abba1
--- /dev/null
+++ b/os.go
@@ -0,0 +1,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
+}