Browse Source

Minor changes to error handling in main loop + changes to boot script and config

main
Mattia Giambirtone 9 months ago
parent
commit
38698ec72a
  1. 2
      nimd.conf
  2. 2
      scripts/boot.sh
  3. 79
      src/core/mainloop.nim
  4. 8
      src/main.nim

2
nimd.conf

@ -7,7 +7,7 @@ restartDelay = 10
sigtermDelay = 90
[Logging]
level = info
level = debug
logFile = /var/log/nimd
[Filesystem]

2
scripts/boot.sh

@ -112,4 +112,4 @@ if ! [[ $memory -eq "" ]]
qemumem="-m ${memory}"
fi
qemu-system-x86_64 -net nic,model=virtio,netdev=user.0 -netdev user,id=user.0 -drive file=vm.qcow2,if=virtio -enable-kvm -kernel ${kernel} -initrd ${initrd} ${qemumem} -append 'root=/dev/vda1 rw quiet modules=ext4 console=ttyS0,9600 console=ttyS0 init=/bin/nimd'
qemu-system-x86_64 -net nic,model=virtio,netdev=user.0 -netdev user,id=user.0 -drive file=vm.qcow2,if=virtio -enable-kvm -kernel ${kernel} -initrd ${initrd} ${qemumem} -append 'root=/dev/vda1 rw quiet modules=ext4 console=ttyS0 init=/bin/nimd'

79
src/core/mainloop.nim

@ -40,44 +40,49 @@ proc mainLoop*(logger: Logger, config: NimDConfig, startServices: bool = true) =
logger.switchToFile()
logger.debug("Entering accept() loop")
while true:
serverSocket.accept(clientSocket)
logger.debug(&"Received connection on control socket")
if clientSocket.recv(opType, size=1) == 0:
logger.debug(&"Client has disconnected, waiting for new connections")
continue
logger.debug(&"Received operation type '{opType}' via control socket")
# The operation type is a single byte:
# - 'p' -> poweroff
# - 'r' -> reboot
# - 'h' -> halt
# - 's' -> Services-related operations (start, stop, get status, etc.)
# - 'l' -> Reload in-memory configuration
# - 'c' -> Check NimD status (returns "1" if up)
case opType:
of "p":
logger.info("Received shutdown request")
shutdown(logger)
of "r":
logger.info("Received reboot request")
reboot(logger)
of "h":
logger.info("Received halt request")
halt(logger)
of "s":
logger.info("Received service-related request")
# TODO: Operate on services
of "l":
logger.info("Received reload request")
mainLoop(logger, parseConfig(logger, "/etc/nimd/nimd.conf"), startServices=false)
of "c":
logger.info("Received check request, responding")
clientSocket.send("1")
else:
logger.warning(&"Received unknown operation type '{opType}' via control socket, ignoring it")
discard
clientSocket.close()
try:
serverSocket.accept(clientSocket)
logger.debug(&"Received connection on control socket")
if clientSocket.recv(opType, size=1) == 0:
logger.debug(&"Client has disconnected, waiting for new connections")
continue
logger.debug(&"Received operation type '{opType}' via control socket")
# The operation type is a single byte:
# - 'p' -> poweroff
# - 'r' -> reboot
# - 'h' -> halt
# - 's' -> Services-related operations (start, stop, get status, etc.)
# - 'l' -> Reload in-memory configuration
# - 'c' -> Check NimD status (returns "1" if up)
case opType:
of "p":
logger.info("Received shutdown request")
shutdown(logger)
of "r":
logger.info("Received reboot request")
reboot(logger)
of "h":
logger.info("Received halt request")
halt(logger)
of "s":
logger.info("Received service-related request")
# TODO: Operate on services
of "l":
logger.info("Received reload request")
mainLoop(logger, parseConfig(logger, "/etc/nimd/nimd.conf"), startServices=false)
of "c":
logger.info("Received check request, responding")
clientSocket.send("1")
else:
logger.warning(&"Received unknown operation type '{opType}' via control socket, ignoring it")
discard
except:
logger.error(&"An error occurred while idling on control socket: {getCurrentExceptionMsg()}")
finally:
clientSocket.close()
clientSocket = newSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
except:
logger.critical(&"A critical error has occurred while running, restarting the mainloop in {config.restartDelay} seconds! Error -> {getCurrentExceptionMsg()}")
logger.critical(&"A critical error has occurred while running, restarting the main loop in {config.restartDelay} seconds! Error -> {getCurrentExceptionMsg()}")
sleepSeconds(config.restartDelay)
# We *absolutely* cannot die
mainLoop(logger, config, startServices=false)

8
src/main.nim

@ -59,7 +59,7 @@ proc addStuff =
depends=(@[newDependency(Other, errorer)]), provides=(@[]),
stdin="/dev/null", stderr="", stdout="")
var shell = newService(name="login", description="A simple login shell", kind=Simple,
workDir=getCurrentDir(), runlevel=Boot, exec="/bin/login -f root",
workDir=getCurrentDir(), runlevel=Default, exec="/bin/login -f root",
supervised=true, restart=Always, restartDelay=0, depends=(@[]), provides=(@[]),
useParentStreams=true, stdin="/dev/null", stderr="/proc/self/fd/2", stdout="/proc/self/fd/1"
)
@ -74,8 +74,12 @@ proc checkControlSocket(logger: Logger, config: NimDConfig): bool =
## socket
result = true
var stat_result: Stat
if posix.stat(cstring(config.sock), stat_result) == -1:
if posix.stat(cstring(config.sock), stat_result) == -1 and posix.errno != 2:
logger.warning(&"Could not stat() {config.sock}, assuming NimD instance isn't running")
elif posix.errno == 2:
logger.debug(&"Control socket path is clear, starting up")
posix.errno = 0
# 2 is ENOENT, which means the file does not exist
# I stole this from /usr/lib/python3.10/stat.py
elif (int(stat_result.st_mode) and 0o170000) != 0o140000:
logger.fatal(&"{config.sock} exists and is not a socket")

Loading…
Cancel
Save