Skip to content

Commit

Permalink
Refs #96, initital commit of user exit script.
Browse files Browse the repository at this point in the history
  • Loading branch information
tvrzna committed Oct 29, 2023
1 parent 9dead6a commit 2a8c318
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package src
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)

const (
Expand All @@ -26,6 +30,10 @@ const (
envXdgSessDesktop = "XDG_SESSION_DESKTOP"
envUid = "UID"
envXdgCurrDesktop = "XDG_CURRENT_DESKTOP"

userExitScript = ".config/emptty-exit"
exitScriptKey = "TIMEOUT"
exitScriptTimeout = 3
)

// session defines basic functions expected from desktop session
Expand Down Expand Up @@ -111,6 +119,8 @@ func (s *commonSession) start() {

carrierErr := s.finishCarrier()

s.runExitScript()

endUtmpEntry(utmpEntry)
logPrint("Ended utmp entry")

Expand Down Expand Up @@ -208,3 +218,42 @@ func (s *commonSession) getLoginShell() string {
}
return "/bin/sh"
}

// Runs session exit script
func (s *commonSession) runExitScript() {
filePath := filepath.Join(s.auth.usr().homedir, userExitScript)
if fileExists(filePath) {
timeout := exitScriptTimeout
err := readProperties(filePath, func(key, value string) {
if key == exitScriptKey {
if v, err := strconv.Atoi(value); err == nil {
timeout = v
}
}
})
if err != nil {
logPrint(err)
return
}

c := make(chan error)
cmd := cmdAsUser(s.auth.usr(), s.getLoginShell(), filePath)
if err := cmd.Start(); err != nil {
logPrint("error during start of exit script", err)
return
}
go func(c chan error) {
c <- cmd.Wait()
}(c)

select {
case <-time.After(time.Duration(timeout) * time.Second):
syscall.Kill(cmd.Process.Pid, syscall.SIGKILL)
case err := <-c:
if err != nil {
logPrint(err)
}
}
close(c)
}
}

0 comments on commit 2a8c318

Please sign in to comment.