mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-25 15:06:11 +00:00
Compare commits
5
Commits
7f37da2091
...
7bbfa9f198
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bbfa9f198
|
||
|
|
81421b0cc4
|
||
|
|
2aef0c0ff9
|
||
|
|
33e7479044
|
||
|
|
92587d8099
|
+4
-1
@@ -2,4 +2,7 @@ module enit
|
|||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
require golang.org/x/sys v0.31.0
|
require (
|
||||||
|
github.com/mitchellh/go-ps v1.0.0
|
||||||
|
golang.org/x/sys v0.31.0
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
|
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
|
||||||
|
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
|
||||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
|||||||
+63
-15
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -12,6 +13,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/mitchellh/go-ps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Build-time variables
|
// Build-time variables
|
||||||
@@ -78,42 +81,42 @@ func mountVirtualFilesystems() {
|
|||||||
|
|
||||||
// Mount /proc
|
// Mount /proc
|
||||||
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /proc: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /sys
|
// Mount /sys
|
||||||
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev
|
// Mount /dev
|
||||||
if err := mount("dev", "/dev", "devtmpfs", commonOptions+",mode=755,inode64", false); err != nil {
|
if err := mount("dev", "/dev", "devtmpfs", commonOptions+",mode=755,inode64", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /dev: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /run
|
// Mount /run
|
||||||
if err := mount("run", "/run", "tmpfs", commonOptions+",nodev,mode=755,inode64", false); err != nil {
|
if err := mount("run", "/run", "tmpfs", commonOptions+",nodev,mode=755,inode64", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /run: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev/pts
|
// Mount /dev/pts
|
||||||
if err := mount("devpts", "/dev/pts", "devpts", commonOptions+",gid=5,mode=620,ptmxmode=000", true); err != nil {
|
if err := mount("devpts", "/dev/pts", "devpts", commonOptions+",gid=5,mode=620,ptmxmode=000", true); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /dev/pts: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev/shm
|
// Mount /dev/shm
|
||||||
if err := mount("shm", "/dev/shm", "tmpfs", commonOptions+",nodev,inode64", true); err != nil {
|
if err := mount("shm", "/dev/shm", "tmpfs", commonOptions+",nodev,inode64", true); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /dev/shm: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount securityfs
|
// Mount securityfs
|
||||||
if err := mount("securityfs", "/sys/kernel/security", "securityfs", commonOptions, false); err != nil {
|
if err := mount("securityfs", "/sys/kernel/security", "securityfs", commonOptions, false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys/kernel/security: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount cgroups v2
|
// Mount cgroups v2
|
||||||
if err := mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonOptions+",noexec,nsdelegate,memory_recursiveprot", false); err != nil {
|
if err := mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonOptions+",noexec,nsdelegate,memory_recursiveprot", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys/fs/cgroup: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Done.")
|
fmt.Println("Done.")
|
||||||
@@ -122,9 +125,8 @@ func mountVirtualFilesystems() {
|
|||||||
func mountFilesystems() {
|
func mountFilesystems() {
|
||||||
fmt.Print("Mounting fstab entries... ")
|
fmt.Print("Mounting fstab entries... ")
|
||||||
|
|
||||||
if err := mountFstabEntries(); err != nil {
|
if err, line := mountFstabEntries(); err != nil {
|
||||||
log.Println("Could not mount fstab entries!")
|
printErrorAndReboot("Error: could not mount fstab entry on line %d: %s", line, err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Done.")
|
fmt.Println("Done.")
|
||||||
@@ -138,8 +140,7 @@ func startServiceManager() {
|
|||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
err := cmd.Start()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Could not initialize service manager!")
|
printErrorAndReboot("Error: could not initialize service manager: %s", err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
serviceManagerPid = cmd.Process.Pid
|
serviceManagerPid = cmd.Process.Pid
|
||||||
|
|
||||||
@@ -185,6 +186,40 @@ func stopServiceManager() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func killProcesses() {
|
||||||
|
fmt.Print("Killing processes... ")
|
||||||
|
|
||||||
|
// Send sigterm to all processes
|
||||||
|
processes, err := ps.Processes()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, process := range processes {
|
||||||
|
if process.Pid() == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
syscall.Kill(process.Pid(), syscall.SIGTERM)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
// Send sigkill to remaining processes
|
||||||
|
processes, err = ps.Processes()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, process := range processes {
|
||||||
|
if process.Pid() == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
syscall.Kill(process.Pid(), syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Done.")
|
||||||
|
}
|
||||||
|
|
||||||
func setHostname() {
|
func setHostname() {
|
||||||
fmt.Print("Setting hostname... ")
|
fmt.Print("Setting hostname... ")
|
||||||
|
|
||||||
@@ -233,9 +268,12 @@ func shutdownSystem() {
|
|||||||
fmt.Println("Shutting down...")
|
fmt.Println("Shutting down...")
|
||||||
|
|
||||||
stopServiceManager()
|
stopServiceManager()
|
||||||
|
killProcesses()
|
||||||
|
unmountFilesystems()
|
||||||
|
|
||||||
fmt.Println("Syncing disks...")
|
fmt.Print("Syncing disks... ")
|
||||||
syscall.Sync()
|
syscall.Sync()
|
||||||
|
fmt.Println("Done.")
|
||||||
|
|
||||||
fmt.Println("Sending shutdown syscall...")
|
fmt.Println("Sending shutdown syscall...")
|
||||||
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
|
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
|
||||||
@@ -248,9 +286,12 @@ func rebootSystem() {
|
|||||||
fmt.Println("Rebooting...")
|
fmt.Println("Rebooting...")
|
||||||
|
|
||||||
stopServiceManager()
|
stopServiceManager()
|
||||||
|
killProcesses()
|
||||||
|
unmountFilesystems()
|
||||||
|
|
||||||
fmt.Println("Syncing disks...")
|
fmt.Print("Syncing disks... ")
|
||||||
syscall.Sync()
|
syscall.Sync()
|
||||||
|
fmt.Println("Done.")
|
||||||
|
|
||||||
fmt.Println("Sending reboot syscall...")
|
fmt.Println("Sending reboot syscall...")
|
||||||
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
|
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
|
||||||
@@ -258,3 +299,10 @@ func rebootSystem() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printErrorAndReboot(format string, v ...any) {
|
||||||
|
log.Printf(format, v...)
|
||||||
|
fmt.Println("Press 'Enter' to reboot...")
|
||||||
|
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
||||||
|
rebootSystem()
|
||||||
|
}
|
||||||
|
|||||||
+193
-18
@@ -1,10 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -28,12 +32,14 @@ var flagsEquivalence = map[string]uintptr{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Split string flags to mount flags and mount data
|
// Split string flags to mount flags and mount data
|
||||||
func convertMountOptions(options string) (flags []uintptr, data string) {
|
func convertMountOptions(options string) (flags []uintptr, data string, extra []string) {
|
||||||
for _, flag := range strings.Split(options, ",") {
|
for _, flag := range strings.Split(options, ",") {
|
||||||
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
||||||
flags = append(flags, unixFlag)
|
flags = append(flags, unixFlag)
|
||||||
} else {
|
} else {
|
||||||
if data == "" {
|
if flag == "noauto" || flag == "nofail" {
|
||||||
|
extra = append(extra, flag)
|
||||||
|
} else if data == "" {
|
||||||
data = flag
|
data = flag
|
||||||
} else {
|
} else {
|
||||||
data += "," + flag
|
data += "," + flag
|
||||||
@@ -41,7 +47,7 @@ func convertMountOptions(options string) (flags []uintptr, data string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return flags, data
|
return flags, data, extra
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine a unix flag slice or array into a single uintptr
|
// Combine a unix flag slice or array into a single uintptr
|
||||||
@@ -84,7 +90,7 @@ func isMountpoint(mountpoint string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mount(source, target, fstype string, options string, mkdir bool) error {
|
func mount(source, target, fstype string, options string, mkdir bool) error {
|
||||||
flags, data := convertMountOptions(options)
|
flags, data, _ := convertMountOptions(options)
|
||||||
|
|
||||||
if isMountpoint(target) && !slices.Contains(flags, unix.MS_REMOUNT) {
|
if isMountpoint(target) && !slices.Contains(flags, unix.MS_REMOUNT) {
|
||||||
flags = append(flags, unix.MS_REMOUNT)
|
flags = append(flags, unix.MS_REMOUNT)
|
||||||
@@ -104,34 +110,68 @@ func mount(source, target, fstype string, options string, mkdir bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mountFstabEntries() error {
|
func mountFstabEntries() (error, int) {
|
||||||
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
||||||
return nil
|
return nil, 0
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes, err := os.ReadFile("/etc/fstab")
|
bytes, err := os.ReadFile("/etc/fstab")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
swapPriority := -2
|
swapPriority := -2
|
||||||
|
|
||||||
for _, line := range strings.Split(string(bytes), "\n") {
|
for i, line := range strings.Split(string(bytes), "\n") {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if strings.HasPrefix(line, "#") || line == "" {
|
if strings.HasPrefix(line, "#") || line == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
source := strings.Split(line, " ")[0]
|
// Get fields from line
|
||||||
target := strings.Split(line, " ")[1]
|
fields := []string{}
|
||||||
fstype := strings.Split(line, " ")[2]
|
sb := &strings.Builder{}
|
||||||
options := strings.Split(line, " ")[3]
|
quoted := false
|
||||||
|
for _, r := range line {
|
||||||
|
if r == '"' {
|
||||||
|
quoted = !quoted
|
||||||
|
} else if !quoted && r == ' ' {
|
||||||
|
str := sb.String()
|
||||||
|
if len(strings.TrimSpace(str)) > 0 {
|
||||||
|
fields = append(fields, sb.String())
|
||||||
|
}
|
||||||
|
sb.Reset()
|
||||||
|
} else {
|
||||||
|
sb.WriteRune(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if sb.Len() > 0 {
|
||||||
|
fields = append(fields, sb.String())
|
||||||
|
}
|
||||||
|
if len(fields) < 4 {
|
||||||
|
return fmt.Errorf("Not enough fields"), i + 1
|
||||||
|
}
|
||||||
|
source := fields[0]
|
||||||
|
target := fields[1]
|
||||||
|
fstype := fields[2]
|
||||||
|
options := fields[3]
|
||||||
|
|
||||||
flags, data := convertMountOptions(options)
|
// Replace device prefixes
|
||||||
|
if cutSource, ok := strings.CutPrefix(source, "LABEL="); ok {
|
||||||
|
source = "/dev/disk/by-label/" + strings.ReplaceAll(cutSource, " ", "\\x20")
|
||||||
|
} else if cutSource, ok := strings.CutPrefix(source, "UUID="); ok {
|
||||||
|
source = "/dev/disk/by-uuid/" + cutSource
|
||||||
|
} else if cutSource, ok := strings.CutPrefix(source, "PARTLABEL="); ok {
|
||||||
|
source = "/dev/disk/by-partlabel/" + cutSource
|
||||||
|
} else if cutSource, ok := strings.CutPrefix(source, "PARTUUID="); ok {
|
||||||
|
source = "/dev/disk/by-partuuid/" + cutSource
|
||||||
|
}
|
||||||
|
|
||||||
if slices.Contains(strings.Split(data, ","), "noauto") {
|
flags, data, extra := convertMountOptions(options)
|
||||||
|
|
||||||
|
if slices.Contains(extra, "noauto") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +182,11 @@ func mountFstabEntries() error {
|
|||||||
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
||||||
swapPriority--
|
swapPriority--
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return fmt.Errorf("swapon syscall returned none-zero error code: %d", err)
|
if slices.Contains(extra, "nofail") {
|
||||||
|
fmt.Printf("Warning: could not mount fstab entry on line %d: swapon syscall returned non-zero exit code: %d\n", i+1, err)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("swapon syscall returned non-zero exit code: %d", err), i + 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -152,9 +196,140 @@ func mountFstabEntries() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := unix.Mount(source, target, fstype, combineUnixFlags(flags), data); err != nil {
|
if err := unix.Mount(source, target, fstype, combineUnixFlags(flags), data); err != nil {
|
||||||
return err
|
if slices.Contains(extra, "nofail") {
|
||||||
|
log.Printf("Warning: could not mount fstab entry on line %d: %s\n", i+1, err)
|
||||||
|
} else {
|
||||||
|
return err, i + 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmountFilesystems() {
|
||||||
|
// Disable all swap memory
|
||||||
|
data, err := os.ReadFile("/proc/swaps")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, entry := range strings.Split(string(data), "\n") {
|
||||||
|
if i == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry = strings.TrimSpace(entry)
|
||||||
|
if len(entry) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mountpoint := strings.Fields(entry)[0]
|
||||||
|
|
||||||
|
// Unmount swap at mountpoint
|
||||||
|
fmt.Printf("Disabling swap at %s... ", mountpoint)
|
||||||
|
b := append([]byte(mountpoint), 0)
|
||||||
|
_, _, err := unix.Syscall(unix.SYS_SWAPOFF, uintptr(unsafe.Pointer(&b[0])), 0, 0)
|
||||||
|
if err == 0 {
|
||||||
|
fmt.Println("Done.")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err = os.ReadFile("/proc/self/mountinfo")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reserve variables for root filesytem
|
||||||
|
rootSource := ""
|
||||||
|
rootFilesystem := ""
|
||||||
|
rootData := ""
|
||||||
|
|
||||||
|
// Unmount filesystems
|
||||||
|
entries := strings.Split(string(data), "\n")
|
||||||
|
slices.Reverse(entries)
|
||||||
|
for _, entry := range entries {
|
||||||
|
entry = strings.TrimSpace(entry)
|
||||||
|
if len(entry) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get entry fields
|
||||||
|
fields := strings.Fields(entry)
|
||||||
|
mountpoint := fields[4]
|
||||||
|
filesystem := ""
|
||||||
|
source := ""
|
||||||
|
data := ""
|
||||||
|
for i := 6; i < len(fields); i++ {
|
||||||
|
if fields[i] == "-" {
|
||||||
|
filesystem = fields[i+1]
|
||||||
|
source = fields[i+2]
|
||||||
|
data = fields[i+3]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip root and ignored filesystems
|
||||||
|
ignoredFilesystems := []string{
|
||||||
|
"devtmpfs",
|
||||||
|
"proc",
|
||||||
|
"sysfs",
|
||||||
|
"tmpfs",
|
||||||
|
}
|
||||||
|
if mountpoint == "/" {
|
||||||
|
rootSource = source
|
||||||
|
rootFilesystem = filesystem
|
||||||
|
_, rootData, _ = convertMountOptions(data)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if slices.Contains(ignoredFilesystems, filesystem) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmount filesystem at mountpoint
|
||||||
|
fmt.Printf("Unmounting %s...", mountpoint)
|
||||||
|
tries := 0
|
||||||
|
for {
|
||||||
|
err := unix.Unmount(mountpoint, 0)
|
||||||
|
if errors.Is(err, syscall.EBUSY) {
|
||||||
|
fmt.Print(".")
|
||||||
|
tries++
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
if tries >= 60 {
|
||||||
|
unix.Unmount(mountpoint, syscall.MNT_FORCE)
|
||||||
|
fmt.Println(" Timeout.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Printf(" Error: %s\n", err.Error())
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
fmt.Println(" Done.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print("Remounting root as read-only...")
|
||||||
|
tries := 0
|
||||||
|
for {
|
||||||
|
err = unix.Mount(rootSource, "/", rootFilesystem, syscall.MS_RDONLY|syscall.MS_REMOUNT, rootData)
|
||||||
|
if errors.Is(err, syscall.EBUSY) {
|
||||||
|
fmt.Print(".")
|
||||||
|
tries++
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
if tries >= 60 {
|
||||||
|
fmt.Println(" Timeout.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Printf(" Error: %s\n", err.Error())
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
fmt.Println(" Done.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-21
@@ -246,6 +246,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pid := cmd.Process.Pid
|
||||||
service.processID = cmd.Process.Pid
|
service.processID = cmd.Process.Pid
|
||||||
service.state = EnitServiceStarting
|
service.state = EnitServiceStarting
|
||||||
|
|
||||||
@@ -260,7 +261,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Kill process and children
|
// Kill process and children
|
||||||
syscall.Kill(-service.processID, syscall.SIGKILL)
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
service.state = EnitServiceCrashed
|
service.state = EnitServiceCrashed
|
||||||
@@ -284,7 +285,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
default:
|
default:
|
||||||
// Kill remaining child processes
|
// Kill remaining child processes
|
||||||
syscall.Kill(-service.processID, syscall.SIGKILL)
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
|
||||||
if service.Type == "simple" && err == nil {
|
if service.Type == "simple" && err == nil {
|
||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
@@ -352,7 +353,9 @@ func (service *EnitService) StopService() error {
|
|||||||
newServiceStatus := EnitServiceCrashed
|
newServiceStatus := EnitServiceCrashed
|
||||||
defer func() {
|
defer func() {
|
||||||
// Kill remaining child processes
|
// Kill remaining child processes
|
||||||
syscall.Kill(-pid, syscall.SIGKILL)
|
if pid != 0 {
|
||||||
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
|
||||||
service.state = newServiceStatus
|
service.state = newServiceStatus
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
@@ -381,31 +384,33 @@ func (service *EnitService) StopService() error {
|
|||||||
service.GetProcess().Signal(syscall.SIGKILL)
|
service.GetProcess().Signal(syscall.SIGKILL)
|
||||||
return fmt.Errorf("could not stop process gracefully")
|
return fmt.Errorf("could not stop process gracefully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the process has stopped gracefully, otherwise send sigkill on timeout
|
|
||||||
exited := make(chan bool)
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exited <- true
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-exited:
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
service.GetProcess().Signal(syscall.SIGKILL)
|
|
||||||
return fmt.Errorf("could not stop process gracefully")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
go func() { service.stopChannel <- true }()
|
||||||
|
|
||||||
cmd := exec.Command("/bin/sh", "-c", service.StopCmd)
|
cmd := exec.Command("/bin/sh", "-c", service.StopCmd)
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the process has stopped gracefully, otherwise send sigkill on timeout
|
||||||
|
exited := make(chan bool)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
if err := syscall.Kill(pid, syscall.Signal(0)); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exited <- true
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-exited:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
service.GetProcess().Signal(syscall.SIGKILL)
|
||||||
|
return fmt.Errorf("could not stop process gracefully")
|
||||||
|
}
|
||||||
|
|
||||||
newServiceStatus = EnitServiceStopped
|
newServiceStatus = EnitServiceStopped
|
||||||
logger.Printf("Service (%s) has stopped!\n", service.Name)
|
logger.Printf("Service (%s) has stopped!\n", service.Name)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user