| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import Foundation
- #if canImport(Glibc)
- import Glibc
- #endif
- // ══════════════════════════════════════════════════════════════════
- // Main System Service Daemon Entry Point
- // ══════════════════════════════════════════════════════════════════
- LogManager.log("SYSTEMD/arkrt: Initializing ArkOS System Services Daemon...")
- // ── Resource Controller Verification ──
- let initialMemory = ResourceController.checkMemoryUsage()
- LogManager.log("SYSTEMD/arkrt: Initial RAM footprint is \(initialMemory / 1024) KB (Cap: 2,097,152 KB)")
- // ── Start Phase 2 IPC Server ──
- var server = IPCServer()
- server.start()
- // ── Launch and Monitor isolated Main User UI (swift_splash) ──
- func spawnUserUI() -> pid_t {
- LogManager.log("SYSTEMD/arkrt: Spawning Main User UI (/swift_splash) in isolated space...")
- let pid = fork()
- if pid == 0 {
- // Child Process
- let consoleFd = open("/dev/console", O_RDWR)
- if consoleFd >= 0 {
- dup2(consoleFd, 1)
- dup2(consoleFd, 2)
- if consoleFd > 2 {
- close(consoleFd)
- }
- }
-
- let path = "/swift_splash"
-
- let cArg0 = strdup(path)
- let argv: [UnsafeMutablePointer<CChar>?] = [cArg0, nil]
-
- let env0 = strdup("PATH=/bin:/usr/bin:/sbin")
- let env1 = strdup("HOME=/home/arkos")
- let envp: [UnsafeMutablePointer<CChar>?] = [env0, env1, nil]
-
- execve(path, argv, envp)
-
- // If execve fails
- LogManager.log("SYSTEMD/arkrt: Failed to execute /swift_splash!")
- exit(1)
- }
- return pid
- }
- // Start the UI
- var uiPid = spawnUserUI()
- // Main monitoring thread / run loop
- ResourceController.executeOnCorePool {
- while true {
- var status: Int32 = 0
- // waitpid with WNOHANG checks if the child has exited without blocking
- let result = waitpid(uiPid, &status, WNOHANG)
-
- if result == uiPid {
- LogManager.log("SYSTEMD/arkrt: WARNING: Main User UI exited with status \(status). Respawning...")
- sleep(1) // Avoid rapid loop spikes
- uiPid = spawnUserUI()
- } else if result < 0 {
- // Error (e.g. no child process exists, which shouldn't happen)
- LogManager.log("SYSTEMD/arkrt: waitpid error. Attempting to respawn...")
- sleep(2)
- uiPid = spawnUserUI()
- }
-
- // Log resource status periodically
- let ram = ResourceController.checkMemoryUsage()
- if ram > 1_500_000_000 {
- LogManager.log("SYSTEMD/arkrt: WARNING: High RAM usage detected: \(ram / 1024 / 1024) MB")
- }
-
- sleep(2)
- }
- }
- // Keep the main thread alive to service IPC dispatch queues
- dispatchMain()
|