mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
Use process group IDs for terminating service subprocesses
This commit is contained in:
@@ -4,4 +4,5 @@ type: background
|
|||||||
start_cmd: /usr/bin/setsid /sbin/agetty --noclear tty1
|
start_cmd: /usr/bin/setsid /sbin/agetty --noclear tty1
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
crash_on_safe_exit: false
|
crash_on_safe_exit: false
|
||||||
restart: always
|
restart: always
|
||||||
|
setpgid: false
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type: background
|
|||||||
start_cmd: /usr/bin/setsid /sbin/agetty tty2
|
start_cmd: /usr/bin/setsid /sbin/agetty tty2
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
crash_on_safe_exit: false
|
crash_on_safe_exit: false
|
||||||
restart: always
|
restart: always
|
||||||
|
setpgid: false
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type: background
|
|||||||
start_cmd: /usr/bin/setsid /sbin/agetty tty3
|
start_cmd: /usr/bin/setsid /sbin/agetty tty3
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
crash_on_safe_exit: false
|
crash_on_safe_exit: false
|
||||||
restart: always
|
restart: always
|
||||||
|
setpgid: false
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type: background
|
|||||||
start_cmd: /usr/bin/setsid /sbin/agetty tty4
|
start_cmd: /usr/bin/setsid /sbin/agetty tty4
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
crash_on_safe_exit: false
|
crash_on_safe_exit: false
|
||||||
restart: always
|
restart: always
|
||||||
|
setpgid: false
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type: background
|
|||||||
start_cmd: /usr/bin/setsid /sbin/agetty tty5
|
start_cmd: /usr/bin/setsid /sbin/agetty tty5
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
crash_on_safe_exit: false
|
crash_on_safe_exit: false
|
||||||
restart: always
|
restart: always
|
||||||
|
setpgid: false
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type: background
|
|||||||
start_cmd: /usr/bin/setsid /sbin/agetty tty6
|
start_cmd: /usr/bin/setsid /sbin/agetty tty6
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
crash_on_safe_exit: false
|
crash_on_safe_exit: false
|
||||||
restart: always
|
restart: always
|
||||||
|
setpgid: false
|
||||||
|
|||||||
@@ -152,6 +152,7 @@ func Init() {
|
|||||||
ExitMethod: "",
|
ExitMethod: "",
|
||||||
StopCmd: "",
|
StopCmd: "",
|
||||||
Restart: "",
|
Restart: "",
|
||||||
|
Setpgid: true,
|
||||||
CrashOnSafeExit: true,
|
CrashOnSafeExit: true,
|
||||||
LogOutput: true,
|
LogOutput: true,
|
||||||
Filepath: path.Join(serviceConfigDir, "services", entry.Name()),
|
Filepath: path.Join(serviceConfigDir, "services", entry.Name()),
|
||||||
|
|||||||
+12
-2
@@ -48,6 +48,7 @@ type EnitService struct {
|
|||||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||||
Restart string `yaml:"restart,omitempty"`
|
Restart string `yaml:"restart,omitempty"`
|
||||||
ReadyFd int `yaml:"ready_fd"`
|
ReadyFd int `yaml:"ready_fd"`
|
||||||
|
Setpgid bool `yaml:"setpgid"`
|
||||||
LogOutput bool `yaml:"log_output,omitempty"`
|
LogOutput bool `yaml:"log_output,omitempty"`
|
||||||
Filepath string
|
Filepath string
|
||||||
filepathChecksum [32]byte
|
filepathChecksum [32]byte
|
||||||
@@ -157,6 +158,7 @@ func (service *EnitService) ReloadService() {
|
|||||||
ExitMethod: "",
|
ExitMethod: "",
|
||||||
StopCmd: "",
|
StopCmd: "",
|
||||||
Restart: "",
|
Restart: "",
|
||||||
|
Setpgid: true,
|
||||||
CrashOnSafeExit: true,
|
CrashOnSafeExit: true,
|
||||||
LogOutput: true,
|
LogOutput: true,
|
||||||
Filepath: service.Filepath,
|
Filepath: service.Filepath,
|
||||||
@@ -225,6 +227,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("/bin/sh", "-c", "exec "+service.StartCmd)
|
cmd := exec.Command("/bin/sh", "-c", "exec "+service.StartCmd)
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: service.Setpgid, Pgid: 0}
|
||||||
if logFile != nil {
|
if logFile != nil {
|
||||||
cmd.Stdout = logFile
|
cmd.Stdout = logFile
|
||||||
cmd.Stderr = logFile
|
cmd.Stderr = logFile
|
||||||
@@ -278,8 +281,8 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
logFile.Close()
|
logFile.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kill process
|
// Kill process and children
|
||||||
cmd.Process.Kill()
|
syscall.Kill(-service.processID, syscall.SIGKILL)
|
||||||
|
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
service.state = EnitServiceCrashed
|
service.state = EnitServiceCrashed
|
||||||
@@ -302,6 +305,9 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
case <-service.stopChannel:
|
case <-service.stopChannel:
|
||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
default:
|
default:
|
||||||
|
// Kill remaining child processes
|
||||||
|
syscall.Kill(-service.processID, syscall.SIGKILL)
|
||||||
|
|
||||||
if service.Type == "simple" && err == nil {
|
if service.Type == "simple" && err == nil {
|
||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
if service.ExitMethod != "stop_command" {
|
if service.ExitMethod != "stop_command" {
|
||||||
@@ -363,9 +369,13 @@ func (service *EnitService) StopService() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("Stopping service (%s)...", service.Name)
|
logger.Printf("Stopping service (%s)...", service.Name)
|
||||||
|
pid := service.processID
|
||||||
|
|
||||||
newServiceStatus := EnitServiceCrashed
|
newServiceStatus := EnitServiceCrashed
|
||||||
defer func() {
|
defer func() {
|
||||||
|
// Kill remaining child processes
|
||||||
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
|
||||||
service.state = newServiceStatus
|
service.state = newServiceStatus
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user