Skip to content

Commit

Permalink
have sand daemon check env for socket fd
Browse files Browse the repository at this point in the history
  • Loading branch information
sullyj3 committed Aug 1, 2024
1 parent b4e6e3f commit fa4439f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/release/
/release.tar.zst
.direnv/
__pycache__/
42 changes: 38 additions & 4 deletions src/Sand/SandDaemon.lean
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,46 @@ def handleClient : CmdHandlerT IO Unit := do

partial def forever (act : IO α) : IO β := act *> forever act

inductive EnvFdError
| varNotFound
| couldntParse

def EnvFdError.toString : EnvFdError → String
| .varNotFound => "SAND_SOCKFD not present in environment"
| .couldntParse => "Found SAND_SOCKFD but couldn't parse it as a string"

def envFd : BaseIO (Except EnvFdError UInt32) := do
let some str ← IO.getEnv "SAND_SOCKFD"
| return .error .varNotFound
let some n := str.toNat?
| return .error .couldntParse
return pure n.toUInt32

def systemdSockFd : UInt32 := 3

def SandDaemon.main (_args : List String) : IO α := do
let systemdSockFd := 3
let sock ← Socket.fromFd systemdSockFd

IO.eprintln "sandd started"
IO.eprintln "listening..."
let fd? ← envFd
let fd : UInt32 ← match fd? with
| .error e@(.couldntParse) => do
IO.eprintln s!"Error: {e.toString}"
IO.Process.exit 1
| .error .varNotFound => do
IO.eprintln "SAND_SOCKFD not found, falling back on default."
pure systemdSockFd
| .ok fd => do
IO.eprintln "found SAND_SOCKFD."
pure fd

let sock ← match ← (Socket.fromFd fd).toBaseIO with
| .ok sock => pure sock
| .error e => do
IO.eprintln s!"Error creating socket from file descriptor {fd}:"
IO.eprintln s!" {e}"
IO.Process.exit 1


IO.eprintln s!"Sand daemon started. listening on fd {fd}"

let state ← DaemonState.initial

Expand Down

0 comments on commit fa4439f

Please sign in to comment.