Files
latchd/legacy/go/auth/utmpx.go

75 lines
1.2 KiB
Go

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
}