#include #include #include "libc.h" #define _PATH_UTMPX "/etc/utmp" static FILE *f; static struct utmpx ut; void endutxent(void) { if (f == NULL) return; fclose(f); f == NULL; } void setutxent(void) { if (f == NULL) return; rewind(f); } struct utmpx *getutxent(void) { if (f == NULL) { f = fopen(_PATH_UTMPX, "r+"); if (f == NULL) { f = fopen(_PATH_UTMPX, "r"); if (f == NULL) return NULL; } } if (fread(&ut, sizeof ut, 1, f)) return &ut; return NULL; } struct utmpx *getutxid(const struct utmpx *id) { while(getutxent()) { switch (id->ut_type) { case BOOT_TIME: case OLD_TIME: case NEW_TIME: if (id->ut_type == ut.ut_type) return &ut; break; case INIT_PROCESS: case LOGIN_PROCESS: case USER_PROCESS: case DEAD_PROCESS: if (id->type == ut.ut_type && !strncmp(id->ut_id, ut.ut_id, sizeof ut.ut_id)) return &ut; break; } } return NULL; } struct utmpx *getutxline(const struct utmpx *line) { while(getutxent()) { switch (ut.ut_type) { case LOGIN_PROCESS: case USER_PROCESS: if (!strncmp(line->ut_line, ut.ut_line, sizeof ut.ut_line)) return &ut; break; } } return NULL; } struct utmpx *pututxline(const struct utmpx *utmpx) { struct flock fl; size_t n; fl.l_start = 0; fl.l_len = 0; fl.l_pid = getpid(); fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; if (fcntl(fileno(f), F_SETLKW, &fl) < 0) return NULL; setutxent(); if (getutxid(utmpx)) fseek(f, -((long)(sizeof ut)), SEEK_CUR); n = fwrite(&ut, sizeof ut, 1, f); fl.l_type = F_UNLCK; fcntl(fileno(f), F_SETLK, &fl); if (n == 1) return &ut; return NULL; } void updwtmpx(const char *f, const struct utmpx *u) { } weak_alias(endutxent, endutent); weak_alias(setutxent, setutent); weak_alias(getutxent, getutent); weak_alias(getutxid, getutid); weak_alias(getutxline, getutline); weak_alias(pututxline, pututline); weak_alias(updwtmpx, updwtmp);