forked from go-debos/debos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commands: disable services for commands running in chroot
Disabled services start/stop for commands running in chroot. This also prevents services from start during packages install. Signed-off-by: Denis Pynkin <[email protected]>
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |