Compare commits
No commits in common. "ec92777135ae60c756ea554f275328859c606670" and "d131c55351c2ae80706fd90d5a45d930a275defc" have entirely different histories.
ec92777135
...
d131c55351
@ -55,43 +55,10 @@ func main() {
|
|||||||
func mountVirtualFilesystems() {
|
func mountVirtualFilesystems() {
|
||||||
fmt.Print("Mounting virtual filesystems... ")
|
fmt.Print("Mounting virtual filesystems... ")
|
||||||
|
|
||||||
commonFlags := uintptr(0 | syscall.MS_NOSUID | syscall.MS_RELATIME)
|
|
||||||
// Mount /proc
|
|
||||||
if err := syscall.Mount("proc", "/proc", "proc", commonFlags|syscall.MS_NODEV|syscall.MS_NOEXEC|syscall.MS_REMOUNT, ""); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount /sys
|
|
||||||
if err := syscall.Mount("sys", "/sys", "sysfs", commonFlags|syscall.MS_NODEV|syscall.MS_NOEXEC|syscall.MS_REMOUNT, ""); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount /dev
|
|
||||||
if err := syscall.Mount("dev", "/dev", "devtmpfs", commonFlags|syscall.MS_REMOUNT, "mode=755,inode64"); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount /run
|
|
||||||
if err := syscall.Mount("run", "/run", "tmpfs", commonFlags|syscall.MS_NODEV|syscall.MS_REMOUNT, "mode=755,inode64"); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount /dev/pts
|
|
||||||
if err := os.Mkdir("/dev/pts", 0755); err != nil {
|
if err := os.Mkdir("/dev/pts", 0755); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := syscall.Mount("devpts", "/dev/pts", "devpts", commonFlags, "gid=5,mode=620,ptmxmode=000"); err != nil {
|
if err := syscall.Mount("none", "/dev/pts", "devpts", syscall.MS_NOSUID|syscall.MS_NOEXEC, ""); err != nil {
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount /dev/shm
|
|
||||||
if err := os.Mkdir("/dev/shm", 0755); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if err := syscall.Mount("shm", "/dev/shm", "tmpfs", commonFlags|syscall.MS_NODEV, "inode64"); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount securityfs
|
|
||||||
if err := syscall.Mount("securityfs", "/sys/kernel/security", "securityfs", commonFlags, ""); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Mount cgroups v2
|
|
||||||
if err := syscall.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonFlags|syscall.MS_NOEXEC, ""); err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,14 +31,12 @@ const (
|
|||||||
type EnitService struct {
|
type EnitService struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Description string `yaml:"description,omitempty"`
|
Description string `yaml:"description,omitempty"`
|
||||||
Dependencies []string `yaml:"dependencies,omitempty"`
|
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
StartCmd string `yaml:"start_cmd"`
|
StartCmd string `yaml:"start_cmd"`
|
||||||
ExitMethod string `yaml:"exit_method"`
|
ExitMethod string `yaml:"exit_method"`
|
||||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||||
Restart bool `yaml:"restart,omitempty"`
|
Restart bool `yaml:"restart,omitempty"`
|
||||||
ServiceRunPath string
|
ServiceRunPath string
|
||||||
restartCount int
|
|
||||||
stopChannel chan bool
|
stopChannel chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +124,6 @@ func Init() {
|
|||||||
logger.Fatalf("Could not initialize ESVM! Error: %s", err)
|
logger.Fatalf("Could not initialize ESVM! Error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and initialize service files
|
|
||||||
for _, entry := range dirEntries {
|
for _, entry := range dirEntries {
|
||||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".esv") {
|
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".esv") {
|
||||||
logger.Printf("Initializing service (%s)...\n", entry.Name())
|
logger.Printf("Initializing service (%s)...\n", entry.Name())
|
||||||
@ -139,14 +136,12 @@ func Init() {
|
|||||||
service := EnitService{
|
service := EnitService{
|
||||||
Name: "",
|
Name: "",
|
||||||
Description: "",
|
Description: "",
|
||||||
Dependencies: make([]string, 0),
|
|
||||||
Type: "",
|
Type: "",
|
||||||
StartCmd: "",
|
StartCmd: "",
|
||||||
ExitMethod: "",
|
ExitMethod: "",
|
||||||
StopCmd: "",
|
StopCmd: "",
|
||||||
Restart: false,
|
Restart: false,
|
||||||
ServiceRunPath: "",
|
ServiceRunPath: "",
|
||||||
restartCount: 0,
|
|
||||||
stopChannel: make(chan bool),
|
stopChannel: make(chan bool),
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
||||||
@ -181,49 +176,14 @@ func Init() {
|
|||||||
|
|
||||||
Services = append(Services, service)
|
Services = append(Services, service)
|
||||||
|
|
||||||
|
if err := service.StartService(); err != nil {
|
||||||
|
logger.Printf("Could not start service %s: %s\n", service.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get services that meet their dependencies
|
|
||||||
servicesWithMetDepends := make([]EnitService, 0)
|
|
||||||
for _, service := range Services {
|
|
||||||
if len(service.GetUnmetDependencies()) == 0 {
|
|
||||||
servicesWithMetDepends = append(servicesWithMetDepends, service)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop until all services have started or timed out
|
|
||||||
for start := time.Now(); time.Since(start) < 60*time.Second; {
|
|
||||||
if len(servicesWithMetDepends) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := len(servicesWithMetDepends) - 1; i >= 0; i-- {
|
|
||||||
service := servicesWithMetDepends[i]
|
|
||||||
canStart := true
|
|
||||||
for _, dependency := range service.Dependencies {
|
|
||||||
if GetServiceByName(dependency).GetCurrentState() != EnitServiceRunning && GetServiceByName(dependency).GetCurrentState() != EnitServiceCompleted {
|
|
||||||
canStart = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if canStart {
|
|
||||||
err := service.StartService()
|
|
||||||
if err != nil {
|
|
||||||
logger.Printf("Could not start service (%s)! Error: %s", service.Name, err)
|
|
||||||
}
|
|
||||||
servicesWithMetDepends = append(servicesWithMetDepends[:i], servicesWithMetDepends[i+1:]...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(servicesWithMetDepends) > 0 {
|
|
||||||
for _, service := range servicesWithMetDepends {
|
|
||||||
logger.Printf("Could not start service (%s)! Error: dependencies not met", service.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Println("ESVM initialized successfully!")
|
logger.Println("ESVM initialized successfully!")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,17 +206,6 @@ func GetServiceByName(name string) *EnitService {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *EnitService) GetUnmetDependencies() (missingDependencies []string) {
|
|
||||||
for _, dependency := range service.Dependencies {
|
|
||||||
depService := GetServiceByName(dependency)
|
|
||||||
if depService == nil {
|
|
||||||
missingDependencies = append(missingDependencies, dependency)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return missingDependencies
|
|
||||||
}
|
|
||||||
|
|
||||||
func (service *EnitService) GetProcess() *os.Process {
|
func (service *EnitService) GetProcess() *os.Process {
|
||||||
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "process"))
|
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "process"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -332,19 +281,15 @@ func (service *EnitService) StartService() error {
|
|||||||
err := cmd.Wait()
|
err := cmd.Wait()
|
||||||
select {
|
select {
|
||||||
case <-service.stopChannel:
|
case <-service.stopChannel:
|
||||||
service.restartCount = 0
|
|
||||||
_ = service.setCurrentState(EnitServiceStopped)
|
_ = service.setCurrentState(EnitServiceStopped)
|
||||||
default:
|
default:
|
||||||
if service.Type == "simple" && err == nil {
|
if service.Type == "simple" && err == nil {
|
||||||
service.restartCount = 0
|
|
||||||
_ = service.setCurrentState(EnitServiceCompleted)
|
_ = service.setCurrentState(EnitServiceCompleted)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
||||||
_ = service.setCurrentState(EnitServiceCrashed)
|
_ = service.setCurrentState(EnitServiceCrashed)
|
||||||
|
|
||||||
if service.Restart && service.restartCount < 5 {
|
if service.Restart {
|
||||||
service.restartCount++
|
|
||||||
_ = service.StartService()
|
_ = service.StartService()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user