From bcff6fecf8c0f2a230142159a5b1da9f6762426b Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 18 Nov 2025 19:47:29 +0200 Subject: [PATCH] Separate root remounting into new function and improve killProcesses function --- src/enit/main.go | 8 +++++-- src/enit/mount.go | 60 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/enit/main.go b/src/enit/main.go index febab4f..69d6e0a 100644 --- a/src/enit/main.go +++ b/src/enit/main.go @@ -195,7 +195,8 @@ func killProcesses() { return } for _, process := range processes { - if process.Pid() == 1 { + sid, _, _ := syscall.Syscall(syscall.SYS_GETSID, uintptr(process.Pid()), 0, 0) + if process.Pid() == 1 || sid == 1 { continue } @@ -210,7 +211,8 @@ func killProcesses() { return } for _, process := range processes { - if process.Pid() == 1 { + sid, _, _ := syscall.Syscall(syscall.SYS_GETSID, uintptr(process.Pid()), 0, 0) + if process.Pid() == 1 || sid == 1 { continue } @@ -270,6 +272,7 @@ func shutdownSystem() { stopServiceManager() killProcesses() unmountFilesystems() + remountRootReadonly() fmt.Print("Syncing disks... ") syscall.Sync() @@ -288,6 +291,7 @@ func rebootSystem() { stopServiceManager() killProcesses() unmountFilesystems() + remountRootReadonly() fmt.Print("Syncing disks... ") syscall.Sync() diff --git a/src/enit/mount.go b/src/enit/mount.go index a27ad5d..c936aa5 100644 --- a/src/enit/mount.go +++ b/src/enit/mount.go @@ -241,11 +241,6 @@ func unmountFilesystems() { log.Fatal(err) } - // Reserve variables for root filesytem - rootSource := "" - rootFilesystem := "" - rootData := "" - // Unmount filesystems entries := strings.Split(string(data), "\n") slices.Reverse(entries) @@ -259,17 +254,18 @@ func unmountFilesystems() { 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 filesystem + if mountpoint == "/" { + continue + } + // Skip root and ignored filesystems ignoredFilesystems := []string{ "devtmpfs", @@ -277,12 +273,6 @@ func unmountFilesystems() { "sysfs", "tmpfs", } - if mountpoint == "/" { - rootSource = source - rootFilesystem = filesystem - _, rootData, _ = convertMountOptions(data) - continue - } if slices.Contains(ignoredFilesystems, filesystem) { continue @@ -311,11 +301,49 @@ func unmountFilesystems() { } } } +} +func remountRootReadonly() { fmt.Print("Remounting root as read-only...") + + data, err := os.ReadFile("/proc/self/mountinfo") + if err != nil { + log.Fatal(err) + } + + filesystem := "" + source := "" + fsData := "" + + // Get root 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] + for i := 6; i < len(fields); i++ { + if fields[i] == "-" { + filesystem = fields[i+1] + source = fields[i+2] + fsData = fields[i+3] + break + } + } + + if mountpoint == "/" { + break + } + } + tries := 0 for { - err = unix.Mount(rootSource, "/", rootFilesystem, syscall.MS_RDONLY|syscall.MS_REMOUNT, rootData) + err := unix.Mount(source, "/", filesystem, syscall.MS_RDONLY|syscall.MS_REMOUNT, fsData) if errors.Is(err, syscall.EBUSY) { fmt.Print(".") tries++