main.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Foundation
  2. #if canImport(Glibc)
  3. import Glibc
  4. #endif
  5. // ══════════════════════════════════════════════════════════════════
  6. // Main System Service Daemon Entry Point
  7. // ══════════════════════════════════════════════════════════════════
  8. LogManager.log("SYSTEMD/arkrt: Initializing ArkOS System Services Daemon...")
  9. // ── Resource Controller Verification ──
  10. let initialMemory = ResourceController.checkMemoryUsage()
  11. LogManager.log("SYSTEMD/arkrt: Initial RAM footprint is \(initialMemory / 1024) KB (Cap: 2,097,152 KB)")
  12. // ── Start Phase 2 IPC Server ──
  13. var server = IPCServer()
  14. server.start()
  15. // ── Launch and Monitor isolated Main User UI (swift_splash) ──
  16. func spawnUserUI() -> pid_t {
  17. LogManager.log("SYSTEMD/arkrt: Spawning Main User UI (/swift_splash) in isolated space...")
  18. let pid = fork()
  19. if pid == 0 {
  20. // Child Process
  21. let consoleFd = open("/dev/console", O_RDWR)
  22. if consoleFd >= 0 {
  23. dup2(consoleFd, 1)
  24. dup2(consoleFd, 2)
  25. if consoleFd > 2 {
  26. close(consoleFd)
  27. }
  28. }
  29. let path = "/swift_splash"
  30. let cArg0 = strdup(path)
  31. let argv: [UnsafeMutablePointer<CChar>?] = [cArg0, nil]
  32. let env0 = strdup("PATH=/bin:/usr/bin:/sbin")
  33. let env1 = strdup("HOME=/home/arkos")
  34. let envp: [UnsafeMutablePointer<CChar>?] = [env0, env1, nil]
  35. execve(path, argv, envp)
  36. // If execve fails
  37. LogManager.log("SYSTEMD/arkrt: Failed to execute /swift_splash!")
  38. exit(1)
  39. }
  40. return pid
  41. }
  42. // Start the UI
  43. var uiPid = spawnUserUI()
  44. // Main monitoring thread / run loop
  45. ResourceController.executeOnCorePool {
  46. while true {
  47. var status: Int32 = 0
  48. // waitpid with WNOHANG checks if the child has exited without blocking
  49. let result = waitpid(uiPid, &status, WNOHANG)
  50. if result == uiPid {
  51. LogManager.log("SYSTEMD/arkrt: WARNING: Main User UI exited with status \(status). Respawning...")
  52. sleep(1) // Avoid rapid loop spikes
  53. uiPid = spawnUserUI()
  54. } else if result < 0 {
  55. // Error (e.g. no child process exists, which shouldn't happen)
  56. LogManager.log("SYSTEMD/arkrt: waitpid error. Attempting to respawn...")
  57. sleep(2)
  58. uiPid = spawnUserUI()
  59. }
  60. // Log resource status periodically
  61. let ram = ResourceController.checkMemoryUsage()
  62. if ram > 1_500_000_000 {
  63. LogManager.log("SYSTEMD/arkrt: WARNING: High RAM usage detected: \(ram / 1024 / 1024) MB")
  64. }
  65. sleep(2)
  66. }
  67. }
  68. // Keep the main thread alive to service IPC dispatch queues
  69. dispatchMain()