A summary of the functions used to find and write utmp files in C
- 2020-04-02 03:19:29
- OfStack
C pututline() function: writes utmp records to a file
The header file:
#include <utmp.h>
Definition function:
void pututline(struct utmp *ut);
Pututline () is used to record the utmp structure of the parameter ut to a utmp file.
Note: you need permission to write /var/run/utmp
sample
#include <utmp.h>
main()
{
struct utmp ut;
ut.ut_type = USER_PROCESS;
ut.ut_pid = getpid();
strcpy(ut.ut_user, "kids");
strcpy(ut.ut_line, "pts/1");
strcpy(ut.ut_host, "www.gnu.org");
pututline(&ut);
}
Perform:
//Observe with the command who-l after executing the example
root pts/0 dec9 19:20
kids pts/1 dec12 10:31(www.gnu.org)
root pts/2 dec12 13:33
C getutline() function: file lookup function (find a specific file from utmp)
The header file:
#include <utmp.h>
Definition function:
struct utmp * getutline(struct utmp *ut);
Getutline () is used to search for records with ut_type USER_PROCESS or LOGIN_PROCESS, and ut_line and ut-, one by one, from the current utmp file read and write locations > Ut_line match. A matched record is found and the data is returned as a utmp structure.
Return value: returns utmp structure data. If NULL is returned, there is no data or an error has occurred.
sample
#include <utmp.h>
main()
{
struct utmp ut, *u;
strcpy(ut.ut_line, "pts/1");
while((u = getutline(&ut)))
{
printf("%d %s %s %s n", u->ut_type, u->ut_user, u->ut_line, u->ut_host);
}
}
Perform:
7 root pts/1
The C getutid() function: looks up a specific record from a utmp file
The header file:
#include <utmp.h>
Definition function:
strcut utmp *getutid(strcut utmp *ut);
Function description:
Getutid () is used to search the record specified by the parameter ut one by one from the read and write location of the current utmp file.
1. If u sub t-
>
Ut_type is RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME, one of them is found with ut-
>
Records that match ut_type;
2, if ut -
>
If the ut_type is one of INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, find the ut-type
>
A record with a ut_id match. A matching record is found and the data is returned as a utmp structure.
Return value: returns utmp structure data. If NULL is returned, there is no data or an error has occurred.
sample
#include <utmp.h>
main()
{
struct utmp ut, *u;
ut.ut_type=RUN_LVL;
while((u = getutid(&ut)))
{
printf("%d %s %s %sn", u->ut_type, u->ut_user, u->ut_line, u->ut_host);
}
}
Perform:
1 runlevel -