Login Accounting

 

Chapter 40: Login Accounting
Complete Tutorial Series โ€“ TLPI Chapter 40
๐Ÿ“„ 7 HTML Files
๐Ÿ’ป 18+ Code Examples
โ“ 35 Interview Q&As
๐Ÿง Linux System Programming
What You’ll Learn

This series covers everything about Linux login accounting โ€” how the system tracks who is logged in, how to read and write login records, and how to implement your own login service. Based on Chapter 40 of The Linux Programming Interface by Michael Kerrisk.

Topics Covered

utmp / wtmp files struct utmpx utmpx API getutxent() pututxline() updwtmpx() getlogin() lastlog file login/logout records UID indexing
All Parts
Part 1 โ€“ Overview of utmp and wtmp Files

Understand what login accounting is, the two core files (utmp and wtmp), how they work, file path constants, and how tools like who and last use them.

utmp vs wtmp _PATH_UTMP login flow diagram 2 code examples 5 interview Q&As
Part 2 โ€“ The utmpx API

History of the BSD vs System V split, why the “x” suffix exists, Linux’s hybrid approach, and why to prefer utmpx over utmp API.

SVR4 history SUSv3 standard utmpxname() 3 code examples 5 interview Q&As
Part 3 โ€“ The utmpx Structure and Record Types

Full breakdown of struct utmpx fields, all 9 record type constants (EMPTY through DEAD_PROCESS), ut_line/ut_id derivation, and the complete login lifecycle in wtmp.

struct utmpx fields 9 record types login lifecycle 3 code examples 5 interview Q&As
Part 4 โ€“ Retrieving Records from utmp and wtmp

The 5 core utmpx functions: setutxent, endutxent, getutxent, getutxid, getutxline. The cache problem and how to avoid infinite loops. Full dump_utmpx implementation.

getutxent() getutxid() getutxline() cache/memset fix 3 code examples 5 interview Q&As
Part 5 โ€“ getlogin() and Finding the Login Name

How getlogin() identifies the original login name using utmp, when it fails (daemons), comparison with getpwuid() and LOGNAME, thread-safe getlogin_r(), and implementing getlogin() from scratch.

getlogin() getlogin_r() ttyname() LOGNAME security 3 code examples 5 interview Q&As
Part 6 โ€“ Updating utmp and wtmp for a Login Session

pututxline() and updwtmpx() in detail. Login and logout rules, field requirements, complete utmpx_login.c implementation, signal-safe cleanup, and implementing login(3)/logout(3)/logwtmp(3).

pututxline() updwtmpx() login record fields signal cleanup 3 code examples 5 interview Q&As
Part 7 โ€“ The lastlog File

The lastlog file format, struct lastlog, UID-based direct indexing with lseek, reading/writing lastlog records, and a complete dump utility for all users.

struct lastlog UID indexing lseek/read/write sparse files 3 code examples 5 interview Q&As
Start Learning

Begin with Part 1 and follow the series in order for the best learning experience.

Start Part 1 โ†’ Jump to lastlog

Leave a Reply

Your email address will not be published. Required fields are marked *