mirror of https://github.com/nocturn9x/nimd.git
More work to fix multiprocessing issues
This commit is contained in:
parent
cad0908ccd
commit
63ab7c61e4
|
@ -24,6 +24,8 @@ proc mainLoop*(logger: Logger) =
|
||||||
## NimD's main execution loop
|
## NimD's main execution loop
|
||||||
logger.info("Processing default runlevel")
|
logger.info("Processing default runlevel")
|
||||||
startServices(logger, workers=1, level=Default)
|
startServices(logger, workers=1, level=Default)
|
||||||
|
logger.debug(&"Unblocking signals")
|
||||||
|
unblockSignals(logger)
|
||||||
logger.info("System initialization complete, going idle")
|
logger.info("System initialization complete, going idle")
|
||||||
while true:
|
while true:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -198,4 +198,4 @@ proc startServices*(logger: Logger, level: RunLevel, workers: int = 1) =
|
||||||
servicesCopy.del(0)
|
servicesCopy.del(0)
|
||||||
quit(0)
|
quit(0)
|
||||||
else:
|
else:
|
||||||
discard waitPid(cint(pid), status, WUNTRACED)
|
discard waitPid(cint(pid), status, WUNTRACED)
|
|
@ -20,7 +20,6 @@ import times
|
||||||
|
|
||||||
|
|
||||||
import ../util/logging
|
import ../util/logging
|
||||||
import ../util/misc
|
|
||||||
|
|
||||||
|
|
||||||
type ShutdownHandler* = ref object
|
type ShutdownHandler* = ref object
|
||||||
|
@ -104,7 +103,7 @@ proc nimDExit*(logger: Logger, code: int, emerg: bool = true) =
|
||||||
var t = cpuTime()
|
var t = cpuTime()
|
||||||
# We wait some time for the signals to propagate
|
# We wait some time for the signals to propagate
|
||||||
while anyUserlandProcessLeft() or cpuTime() - t >= sigTermDelay:
|
while anyUserlandProcessLeft() or cpuTime() - t >= sigTermDelay:
|
||||||
sleepSeconds(0.25)
|
sleep(int(0.25 * 1000))
|
||||||
if anyUserlandProcessLeft():
|
if anyUserlandProcessLeft():
|
||||||
logger.info("Terminating child processes with SIGKILL")
|
logger.info("Terminating child processes with SIGKILL")
|
||||||
discard posix.kill(SIGKILL, -1)
|
discard posix.kill(SIGKILL, -1)
|
||||||
|
|
|
@ -21,7 +21,6 @@ import util/[logging, constants, misc]
|
||||||
import core/[mainloop, fs, shutdown, services]
|
import core/[mainloop, fs, shutdown, services]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
proc main(logger: Logger, mountDisks: bool = true, fstab: string = "/etc/fstab") =
|
proc main(logger: Logger, mountDisks: bool = true, fstab: string = "/etc/fstab") =
|
||||||
## NimD's entry point and setup
|
## NimD's entry point and setup
|
||||||
## function
|
## function
|
||||||
|
@ -96,8 +95,7 @@ proc main(logger: Logger, mountDisks: bool = true, fstab: string = "/etc/fstab")
|
||||||
logger.info("Creating directories")
|
logger.info("Creating directories")
|
||||||
createDirectories(logger)
|
createDirectories(logger)
|
||||||
logger.debug("Entering critical fork() section: blocking signals")
|
logger.debug("Entering critical fork() section: blocking signals")
|
||||||
var sigset: Sigset
|
blockSignals(logger)
|
||||||
# TODO
|
|
||||||
logger.info("Processing boot runlevel")
|
logger.info("Processing boot runlevel")
|
||||||
addService(newService(name="echoer", description="prints owo", exec="/bin/echo owo",
|
addService(newService(name="echoer", description="prints owo", exec="/bin/echo owo",
|
||||||
runlevel=Boot, kind=Oneshot, workDir=getCurrentDir(),
|
runlevel=Boot, kind=Oneshot, workDir=getCurrentDir(),
|
||||||
|
|
|
@ -22,6 +22,7 @@ import posix
|
||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import ../core/shutdown
|
||||||
|
|
||||||
|
|
||||||
proc sleepSeconds*(amount: SomeNumber) = sleep(int(amount * 1000))
|
proc sleepSeconds*(amount: SomeNumber) = sleep(int(amount * 1000))
|
||||||
|
@ -33,6 +34,39 @@ proc doSync*(logger: Logger) =
|
||||||
logger.debug(&"Calling sync() syscall has returned {syscall(SYNC)}")
|
logger.debug(&"Calling sync() syscall has returned {syscall(SYNC)}")
|
||||||
|
|
||||||
|
|
||||||
|
proc dummySigHandler(x: cint) {.noconv.} = discard
|
||||||
|
|
||||||
|
|
||||||
|
proc blockSignals*(logger: Logger) =
|
||||||
|
## Temporarily blocks all signals
|
||||||
|
## for critical sections of code
|
||||||
|
|
||||||
|
var tmp: Sigset
|
||||||
|
var sigaction: Sigaction
|
||||||
|
sigaction.sa_handler = dummySigHandler
|
||||||
|
sigaction.sa_flags = SA_RESTART
|
||||||
|
if posix.sigfillset(sigaction.sa_mask) == -1:
|
||||||
|
logger.fatal(&"Could not initialize signal lock (code {posix.errno}, {posix.strerror(posix.errno)}): environment is not safe, exiting now!")
|
||||||
|
nimDExit(logger, 131)
|
||||||
|
if posix.sigprocmask(SIG_SETMASK, sigaction.sa_mask, tmp) == -1:
|
||||||
|
logger.fatal(&"Could not apply signal mask to process (code {posix.errno}, {posix.strerror(posix.errno)}): environment is not safe, exiting now!")
|
||||||
|
nimDExit(logger, 131)
|
||||||
|
|
||||||
|
|
||||||
|
proc unblockSignals*(logger: Logger) =
|
||||||
|
## Unblocks all signals
|
||||||
|
var tmp: Sigset
|
||||||
|
var sigaction: Sigaction
|
||||||
|
sigaction.sa_handler = dummySigHandler
|
||||||
|
sigaction.sa_flags = SA_RESTART
|
||||||
|
if posix.sigemptyset(sigaction.sa_mask) == -1:
|
||||||
|
logger.fatal(&"Could not initialize signal unlock (code {posix.errno}, {posix.strerror(posix.errno)}): environment is not safe, exiting now!")
|
||||||
|
nimDExit(logger, 131)
|
||||||
|
if posix.sigprocmask(SIG_SETMASK, sigaction.sa_mask, tmp) == -1:
|
||||||
|
logger.fatal(&"Could not apply signal mask to process (code {posix.errno}, {posix.strerror(posix.errno)}): environment is not safe, exiting now!")
|
||||||
|
nimDExit(logger, 131)
|
||||||
|
|
||||||
|
|
||||||
proc reapProcess*(logger: Logger) =
|
proc reapProcess*(logger: Logger) =
|
||||||
## Reaps zombie processes. Note: This does not
|
## Reaps zombie processes. Note: This does not
|
||||||
## handle restarting crashed service processes,
|
## handle restarting crashed service processes,
|
||||||
|
|
Loading…
Reference in New Issue