working commit

This commit is contained in:
2026-01-28 18:17:56 +02:00
parent d6496a427b
commit a9075902a3
8 changed files with 308 additions and 47 deletions
+128 -7
View File
@@ -6,8 +6,8 @@ import (
"os/signal"
"os/user"
//"os/user"
//"path/filepath"
//"strconv"
"path/filepath"
"strconv"
//"sync"
"syscall"
//"time"
@@ -45,11 +45,19 @@ func (srv *Server) Handler() *handler.Handler {
func (srv *Server) Configure() error {
var err error
srv.logg.Infof("Server configure")
srv.conf, err = config.NewConfig()
srv.conf = config.NewConfig()
if err != nil {
return err
}
err = srv.conf.ReadConfigfile()
if err != nil {
//srv.logg.Warningf("Error loading config file: %v", err)
//return err
}
err = srv.conf.ReadOptions()
if err != nil {
return err
}
return err
}
@@ -57,21 +65,39 @@ func (srv *Server) Build() error {
var err error
srv.logg.Infof("Server build")
confDump := srv.conf.String()
srv.logg.Infof("Current server configuration is:\n%s\n", confDump)
if srv.conf.AsDaemon {
logdir := filepath.Dir(srv.conf.Logpath)
srv.logg.Infof("Create log directory %s", logdir)
err = os.MkdirAll(logdir, 0750)
if err != nil {
return err
}
rundir := filepath.Dir(srv.conf.Runpath)
srv.logg.Infof("Create run directory %s", rundir)
err = os.MkdirAll(rundir, 0750)
if err != nil {
return err
}
}
// Database create
srv.logg.Infof("Create database directory")
dbdir := srv.conf.Database.Basepath
srv.logg.Infof("Create database directory %s ", dbdir)
err = os.MkdirAll(dbdir, 0750)
if err != nil {
return err
}
mdb := maindb.NewDatabase(dbdir)
srv.logg.Infof("Open database")
srv.logg.Infof("Open main database")
err = mdb.OpenDatabase()
if err != nil {
return err
}
srv.logg.Infof("Initialize database")
srv.logg.Infof("Initialize main database")
err = mdb.InitDatabase()
if err != nil {
return err
@@ -162,3 +188,98 @@ func (srv *Server) Run() error {
}
return err
}
func (srv *Server) PseudoFork() error {
const successExit int = 0
var keyEnv string = "IMX0LTSELMRF8K"
var err error
_, isChild := os.LookupEnv(keyEnv)
switch {
case !isChild:
os.Setenv(keyEnv, "TRUE")
procAttr := syscall.ProcAttr{}
cwd, err := os.Getwd()
if err != nil {
return err
}
var sysFiles = make([]uintptr, 3)
sysFiles[0] = uintptr(syscall.Stdin)
sysFiles[1] = uintptr(syscall.Stdout)
sysFiles[2] = uintptr(syscall.Stderr)
procAttr.Files = sysFiles
procAttr.Env = os.Environ()
procAttr.Dir = cwd
_, err = syscall.ForkExec(os.Args[0], os.Args, &procAttr)
if err != nil {
return err
}
os.Exit(successExit)
case isChild:
_, err = syscall.Setsid()
if err != nil {
return err
}
}
os.Unsetenv(keyEnv)
return err
}
func (srv *Server) Daemonize() error {
var err error
if srv.conf.AsDaemon {
// Restart process process
err = srv.PseudoFork()
if err != nil {
return err
}
// Redirect stdin
nullFile, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
if err != nil {
return err
}
err = syscall.Dup2(int(nullFile.Fd()), int(os.Stdin.Fd()))
if err != nil {
return err
}
// Redirect stderr and stout
logdir := filepath.Dir(srv.conf.Logpath)
err = os.MkdirAll(logdir, 0750)
if err != nil {
return err
}
logFile, err := os.OpenFile(srv.conf.Logpath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
return err
}
err = syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
if err != nil {
return err
}
err = syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
if err != nil {
return err
}
// Write process ID
rundir := filepath.Dir(srv.conf.Runpath)
err = os.MkdirAll(rundir, 0750)
if err != nil {
return err
}
pidFile, err := os.OpenFile(srv.conf.Runpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return err
}
defer pidFile.Close()
currPid := os.Getpid()
_, err = pidFile.WriteString(strconv.Itoa(currPid))
if err != nil {
return err
}
}
return err
}