Files
latchd/legacy/go/auth/utmpx.go
Zane Walker faf5b27328 feat: ship Rust login manager and GitLab release pipeline
Replace the legacy Go implementation as the production path, quarantine the reference tree, add deterministic release packaging and version metadata, and run verification through GitLab CI.
2026-09-25 21:33:46 +05:30

78 lines
1.3 KiB
Go

// auth — UTMPX user accounting (glibc only).
//
// Mirrors vendor/src/auth/utmpx.rs.
package auth
/*
#include <utmpx.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "utmpx_time.h"
*/
import "C"
import (
"time"
"unsafe"
)
type UtmpxSession struct {
ok bool
username string
tty uint8
}
func AddUtmpx(user string, tty uint8, pid uint32) *UtmpxSession {
s := &UtmpxSession{username: user, tty: tty}
cUser := C.CString(user)
defer C.free(unsafe.Pointer(cUser))
var ut C.struct_utmpx
C.memset(unsafe.Pointer(&ut), 0, C.sizeof_struct_utmpx)
ut.ut_type = C.USER_PROCESS
ut.ut_pid = C.pid_t(pid)
n := len(user)
if n > 32 {
n = 32
}
for i := 0; i < n; i++ {
ut.ut_user[i] = C.char(user[i])
}
ttyCh := byte('0' + tty)
ut.ut_line[0] = 't'
ut.ut_line[1] = 't'
ut.ut_line[2] = 'y'
ut.ut_line[3] = C.char(ttyCh)
ut.ut_id[0] = C.char(ttyCh)
us := time.Now().UnixMicro()
C.latchd_set_utmpx_time(&ut, C.int64_t(us/1_000_000), C.int32_t(us%1_000_000))
C.setutxent()
C.pututxline(&ut)
s.ok = true
return s
}
func (s *UtmpxSession) Close() {
if !s.ok {
return
}
var ut C.struct_utmpx
C.memset(unsafe.Pointer(&ut), 0, C.sizeof_struct_utmpx)
ut.ut_type = C.DEAD_PROCESS
C.setutxent()
C.pututxline(&ut)
C.endutxent()
s.ok = false
}