#include #include #include #include #include "utmp_defs.h" #include "str_defs.h" #define SD(A,B) !str_diffn(A, B, sizeof(A)) #define SC(A,B) str_copynz(A, B, sizeof(A)) void utmp_do(char *username, char *line, int pid) /*EXTRACT_INCL*/{ off_t pos=0; struct utmp u; char *p; int fd=open(_PATH_UTMP,O_RDWR); while (utmp_io(fd, &u,F_RDLCK)) { if (u.ut_pid == pid || SD(u.ut_line, line)) goto foundone; pos += UTMP_SIZE; } byte_zero(&u,sizeof(u)); pos = lseek(fd,0,SEEK_END); if (pos<0) pos =0; pos = pos - (pos % (UTMP_SIZE)); foundone: if (u.ut_id[0]==0) { p=line; while (str_len(p) > sizeof u.ut_id) p++; SC(u.ut_id, p); } SC(u.ut_line, line); SC(u.ut_user,username); u.ut_type=USER_PROCESS; u.ut_pid=pid; byte_zero(u.ut_host,sizeof u.ut_host); u.ut_tv.tv_sec = time(0); if (fd>=0) { if (lseek(fd,pos,SEEK_SET) == pos) utmp_io(fd,&u,F_WRLCK); close(fd); } fd=open(_PATH_WTMP, O_WRONLY|O_APPEND); if (fd>=0) { write(fd, &u, UTMP_SIZE); close(fd); } }