-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go build -o init |
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,12 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Init() { | ||
fmt.Println("Welcome to " + config["os_name"].(string) + " " + config["os_version"].(string) + " (" + config["os_codename"].(string) + ")!\n") | ||
|
||
ScanOnServices(servicesPath) | ||
InitInittab() | ||
} |
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,68 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func ParseInitTab(path string) []string { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
fmt.Println("pinit: Fatal error: an error occured while opening inittab file\nMore: " + err.Error()) | ||
os.Exit(2) | ||
} | ||
|
||
var inittab string | ||
stat, _ := file.Stat() | ||
data := make([]byte, stat.Size()) | ||
length, err := file.Read(data) | ||
if err != nil { | ||
fmt.Println("pinit: Fatal error: an error occured while reading inittab file\nMore: " + err.Error()) | ||
os.Exit(2) | ||
} | ||
|
||
inittab = string(data[:length]) | ||
|
||
itLines := strings.Split(inittab, "\n") | ||
|
||
return itLines | ||
} | ||
|
||
func ExecSysInit(script string) bool { | ||
cmd := exec.Command(script) | ||
err := cmd.Start() | ||
if err != nil { | ||
fmt.Println("pinit: Error: Failed to run sysinit script\nMore: " + err.Error()) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func InitInittab() { | ||
var inittab []string | ||
switch runtime.GOOS { | ||
case "linux": | ||
inittab = ParseInitTab("/etc/inittab") | ||
case "windows": | ||
inittab = ParseInitTab("C:\\pinit\\inittab") | ||
default: | ||
fmt.Println("pinit: unsupported operating system") | ||
os.Exit(3) | ||
} | ||
|
||
for i := 0; i < len(inittab); i++ { | ||
args := strings.SplitN(inittab[i], ":", 4) | ||
switch args[2] { | ||
case "sysinit": | ||
ExecSysInit(args[3]) | ||
case "respawn": | ||
args := strings.Split(args[3], " ") | ||
exec := args[0] | ||
StartRespawnProcess(exec, args) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,7 +6,10 @@ import ( | |
"runtime" | ||
) | ||
|
||
var config Config | ||
var config map[string]interface{} | ||
var servicesPath string | ||
|
||
//var startedService []os.Process | ||
|
||
func main() { | ||
//fmt.Println("pinit 0.1 Copyright (c) 2018 Maksim Pinigin <[email protected]>") | ||
|
@@ -18,12 +21,16 @@ func main() { | |
case "windows": | ||
loadConfResult = LoadConfig("C:\\pinit\\configuration.json") | ||
default: | ||
fmt.Println("pinit: not supported operating system, sorry bro") | ||
fmt.Println("pinit: unsupported operating system, sorry bro") | ||
os.Exit(3) | ||
} | ||
|
||
if loadConfResult != true { | ||
fmt.Println("pinit: Fatal error: unknown error loadConfResult = false") | ||
os.Exit(2) | ||
} | ||
|
||
servicesPath = config["services_path"].(string) | ||
|
||
Init() | ||
} |
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,76 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func ScanOnServices(rcdpath string) { | ||
dir, err := ioutil.ReadDir(rcdpath) | ||
if err != nil { | ||
fmt.Println("pinit: Error: failed to scan services directory\nMore: " + err.Error()) | ||
} | ||
|
||
servicesPath = rcdpath | ||
|
||
for i := 0; i < len(dir); i++ { | ||
srvcName := dir[i].Name() | ||
proc := StartService(srvcName, true) | ||
if proc != nil { | ||
fmt.Println("Started service " + srvcName) | ||
//startedService = append(startedService, *proc) | ||
} | ||
} | ||
} | ||
|
||
func StartService(service string, checkOnEnabled bool) *os.Process { | ||
serviceConf, err := os.Open(servicesPath + "/" + service) | ||
if err != nil { | ||
fmt.Println("pinit: Failed to open " + service + " service configuration file\nMore: " + err.Error()) | ||
return nil | ||
} | ||
|
||
var decoded map[string]interface{} | ||
|
||
stat, err := serviceConf.Stat() | ||
if err != nil { | ||
fmt.Println("pinit: Failed to read " + service + " service configuration file\nMore: " + err.Error()) | ||
return nil | ||
} | ||
data := make([]byte, stat.Size()) | ||
_, err = serviceConf.Read(data) | ||
if err != nil { | ||
fmt.Println("pinit: Failed to read " + service + " service configuration file\nMore: " + err.Error()) | ||
return nil | ||
} | ||
err = json.Unmarshal(data, &decoded) | ||
if err != nil { | ||
fmt.Println("pinit: Failed to parse " + service + " service configuration file\nMore: " + err.Error()) | ||
return nil | ||
} | ||
|
||
if checkOnEnabled == true { | ||
if decoded["enabled"].(bool) == false { | ||
return nil | ||
} else { | ||
fmt.Println("Starting service " + service + "...") | ||
} | ||
} | ||
|
||
var procAttr os.ProcAttr | ||
var args []string | ||
procAttr.Dir = "/" | ||
if decoded["args"].(string) != "" { | ||
args = []string{decoded["exec"].(string), decoded["args"].(string)} | ||
} else { | ||
args = []string{decoded["exec"].(string)} | ||
} | ||
process, err := os.StartProcess(decoded["exec"].(string), args, &procAttr) | ||
if err != nil { | ||
fmt.Println("pinit: Failed to start service " + service + "\nMore: " + err.Error()) | ||
} | ||
|
||
return process | ||
} |
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,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func StartRespawnProcess(exec string, args []string) { | ||
var procAttr os.ProcAttr | ||
procAttr.Dir = "/" | ||
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr} | ||
process, err := os.StartProcess(exec, args, &procAttr) | ||
if err != nil { | ||
fmt.Println("pinit: Failed to start respawn process " + exec + "\nMore: " + err.Error()) | ||
} | ||
state, _ := process.Wait() | ||
if state.Exited() == true { | ||
StartRespawnProcess(exec, args) | ||
} | ||
} |
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 @@ | ||
@go run main.go init.go inittab.go config.go process.go |