| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // Copyright 2026 Aarav Ravindra Kharade
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- import Foundation
- #if canImport(Glibc)
- import Glibc
- #endif
- // -
- // ArkOS Runtime Daemon (arkrt)
- // -
- // The primary system daemon, running as PID 2 (spawned by init).
- // Responsible for:
- //
- // 1. Mounting system and vendor partitions
- // 2. Configuring network interfaces
- // 3. Starting the IPC server for inter-process communication
- // 4. Discovering and starting system services (display, UI, etc.)
- // 5. Launching the boot splash animation
- // 6. Monitoring all child processes and respawning on crash
- //
- // Boot Sequence:
- // init (PID 1)
- // - arkrt (PID 2)
- // - IPC Server (thread)
- // - NetworkService (thread)
- // - display service (child process)
- // - swift_splash (child process — temporary)
- // - ui_test (child process — launched by ServiceManager)
- //
- // -
- // - Phase 1: System Initialization -
- LogManager.log("arkrt: ArkOS Runtime Daemon starting...")
- LogManager.log("arkrt: PID=\(getpid()), UID=\(getuid())")
- // Report system resources
- let cpuCores = ResourceController.getCPUCoreCount()
- let totalRAM = ResourceController.getTotalMemory() / 1024 / 1024
- let initialRSS = ResourceController.checkMemoryUsage() / 1024
- LogManager.log("arkrt: System: \(cpuCores) CPU cores, \(totalRAM) MB RAM, RSS=\(initialRSS) KB")
- // - Phase 2: Mount System Partitions -
- LogManager.log("arkrt: Mounting system partitions...")
- if let items = try? FileManager.default.contentsOfDirectory(atPath: "/dev") {
- LogManager.log("arkrt: /dev contents: \(items.joined(separator: ", "))")
- }
- /// Mount a block device to a mount point. Creates the directory if needed.
- func mountPartition(devices: [String], mountPoint: String, fsType: String) {
- mkdir(mountPoint, 0o755)
- for device in devices {
- // Wait up to 3 seconds for the device to appear
- var waited = 0
- if fsType != "tmpfs" {
- while access(device, F_OK) != 0 && waited < 30 {
- usleep(100_000) // 100ms
- waited += 1
- }
- }
-
- if fsType == "tmpfs" || access(device, F_OK) == 0 {
- let result = mount(device, mountPoint, fsType, 0, nil)
- if result == 0 {
- LogManager.log("arkrt: Mounted \(device) → \(mountPoint) (\(fsType))")
- return
- }
- }
- }
- LogManager.log("arkrt: Failed to mount any device to \(mountPoint)", level: .warn)
- }
- // Mount tmpfs for runtime state
- mountPartition(devices: ["tmpfs"], mountPoint: "/run", fsType: "tmpfs")
- mountPartition(devices: ["tmpfs"], mountPoint: "/tmp", fsType: "tmpfs")
- // Create essential runtime directories
- mkdir("/run", 0o755)
- mkdir("/var/log", 0o755)
- mkdir("/etc", 0o755)
- mkdir("/system", 0o755)
- mkdir("/vendor", 0o755)
- // Attempt to mount system and vendor from disk images (block devices)
- let systemDevices = ["/dev/vdb", "/dev/sdb", "/dev/hdb", "/dev/sda2"]
- let vendorDevices = ["/dev/vdc", "/dev/sdc", "/dev/hdc", "/dev/sda3"]
- mountPartition(devices: systemDevices, mountPoint: "/system", fsType: "ext4")
- mountPartition(devices: vendorDevices, mountPoint: "/vendor", fsType: "ext4")
- // - Phase 3: Network Configuration -
- LogManager.log("arkrt: Configuring network interfaces...")
- NetworkService.shared.configureInterfaces()
- NetworkService.shared.startMonitoring()
- // - Phase 4: IPC Server -
- LogManager.log("arkrt: Starting IPC server...")
- var server = IPCServer()
- server.start()
- LogManager.log("arkrt: Starting Input server...")
- InputService.start()
- // - Phase 5: Service Discovery -
- // We wait to start services until the splash completes so ui_daemon
- // doesn't fight over the framebuffer.
- // Boot splash animation has been moved into the ui_daemon service
- // - Phase 7: Main Monitoring Loop -
- LogManager.log("arkrt: Starting system services...")
- ServiceManager.shared.startAllServices()
- var lastResolution: String = ""
- ResourceController.executeOnCorePool {
- while true {
- // Monitor all managed services for crashes
- ServiceManager.shared.monitorServices()
- // Periodic resource health check
- let ram = ResourceController.checkMemoryUsage()
- if ram > 1_500_000_000 { // 1.5 GB
- LogManager.log("arkrt: HIGH MEMORY WARNING: \(ram / 1024 / 1024) MB RSS", level: .warn)
- }
- // Check for resolution changes
- if let fp = fopen("/sys/class/graphics/fb0/virtual_size", "r") {
- var buf = [CChar](repeating: 0, count: 64)
- if fgets(&buf, Int32(buf.count), fp) != nil {
- let currentRes = String(cString: buf).trimmingCharacters(in: .whitespacesAndNewlines)
- if lastResolution != "" && currentRes != lastResolution {
- LogManager.log("arkrt: Resolution changed from \(lastResolution) to \(currentRes). Restarting display service...")
- ServiceManager.shared.stopService(name: "display")
- ServiceManager.shared.startService(name: "display")
- }
- lastResolution = currentRes
- }
- fclose(fp)
- }
- // Log heartbeat every ~60 iterations (2 min)
- sleep(2)
- }
- }
- // - Keep Main Thread Alive -
- // The main thread must remain alive to service Foundation dispatch
- // queues (used by IPC, network monitor, and service manager).
- dispatchMain()
|