getutent, getutid, getutline, pututline, setutent, endutent, utmpname - access utmp file entries
getutent, getutid, getutline, pututline, setutent, endutent, utmpname - 获取utmp文件的入口
所需头文件
#include <utmp.h>
struct utmp *getutent(void);
struct utmp *getutid(struct utmp *ut);
struct utmp *getutline(struct utmp *ut);
struct utmp *pututline(struct utmp *ut);
void setutent(void);
void endutent(void);
int utmpname(const char *file);
utmpname() sets the name of the utmp-format file for the other utmp functions to access. If utmpname() is not used to set
the filename before the other functions are used, they assume _PATH_UTMP, as defined in <paths.h>.
utmpname函数utmp格式文件的名字给其他utmp函数使用,如果utmpname函数没有在其他函数之前使用,那么他们假设使用_PATH_UTMP,在<paths.h>中定义
setutent() rewinds the file pointer to the beginning of the utmp file. It is generally a good idea to call it before any of
the other functions.
setutent函数回滚uptmp文件的文件描述符到文件开头,在其他函数之前调用这个函数是一个好主意
endutent() closes the utmp file. It should be called when the user code is done accessing the file with the other func‐
tions.
endutent函数关闭utmp文件,应该在其他函数调用完毕之后调用这个函数
getutent() reads a line from the current file position in the utmp file. It returns a pointer to a structure containing the
fields of the line. The definition of this structure is shown in utmp(5).
getutent函数从utmp文件中读取当前行,返回一个指向包含当前行内容的结构体指针,结构体的定义如下utmp
getutid() searches forward from the current file position in the utmp file based upon ut. If ut->ut_type is one of RUN_LVL,
BOOT_TIME, NEW_TIME, or OLD_TIME, getutid() will find the first entry whose ut_type field matches ut->ut_type. If
ut->ut_type is one of INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, getutid() will find the first entry whose
ut_id field matches ut->ut_id.
getutid函数基于ut从utmp文件的当前位置查找,如果ut->ut_type是RUN_LVL,BOOT_TIME, NEW_TIME或者OLD_TIME其中的一种,getutid将找到第一个ut_type是ut->ut_type的入口,如果ut->ut_type是INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS或者DEAD_PROCESS其中的一个,getutid将找到第一个ut_id是ut->ut_id的入口
getutline() searches forward from the current file position in the utmp file. It scans entries whose ut_type is
USER_PROCESS or LOGIN_PROCESS and returns the first one whose ut_line field matches ut->ut_line.
getutline从utmp文件中查找当前位置,他查找ut_type是USER_PROCESS或者LOGIN_PROCES的入口,返回第一个ut_line的值是ut->ut_line入口
pututline() writes the utmp structure ut into the utmp file. It uses getutid() to search for the proper place in the file
to insert the new entry. If it cannot find an appropriate slot for ut, pututline() will append the new entry to the end of
the file.
pututline把utmp结构的ut数据写到tump文件,他使用getutid来查找文件中合适的位置来插入新的入口,如果找不到适合ut的位置,那么pututline将会把新入口的位置加在文件的结尾
getutent(), getutid(), and getutline() return a pointer to a struct utmp on success, and NULL on failure (which includes the
"record not found" case). This struct utmp is allocated in static storage, and may be overwritten by subsequent calls.
getutent,getutid和getutline在成功的时候返回一个指向utmp结构体的指针,在失败的时候返回NULL,utmp结构体在静态存储区分配,所以会被后续的调用覆盖
On success pututline() returns ut; on failure, it returns NULL.
putline在成功的时候返回ut,失败的时候返回NULL
utmpname() returns 0 if the new name was successfully stored, or -1 on failure.
utmpname在成功存储的时候返回0,失败的时候返回-1
ERRORS
ENOMEM Out of memory.
内存溢出
ESRCH Record not found.
存档未找到
setutent(), pututline(), and the getut* () functions can also fail for the reasons described in open(2).
setutent, pututline和getut函数会失败,具体原因详见open
/var/run/utmp database of currently logged-in users
当前已登陆用户的数据库
/var/log/wtmp database of past user logins
以前登陆用户的数据库
#include <utmpx.h>
struct utmpx *getutxent(void);
struct utmpx *getutxid(const struct utmpx *);
struct utmpx *getutxline(const struct utmpx *);
struct utmpx *pututxline(const struct utmpx *);
void setutxent(void);
void endutxent(void);
These functions are provided by glibc, and perform the same task as their equivalents without the "x", but use struct utmpx,
defined on Linux to be the same as struct utmp. For completeness, glibc also provides utmpxname(), although this function
is not specified by POSIX.1.
这些函数在glibc中被提供,他们可以完成跟没有x的函数同样的任务,但是使用utmpx结构体,在linux系统中定义,跟utmp结构体一样,为了完整,glibc也使用utmpxname,尽管这个函数没有被POSIX.1实现
On some other systems, the utmpx structure is a superset of the utmp structure, with additional fields, and larger versions
of the existing fields, and parallel files are maintained, often /var/*/utmpx and /var/*/wtmpx.
在其他的系统中,utmpx是utmp结构体的超集
Linux glibc on the other hand does not use a parallel utmpx file since its utmp structure is already large enough. The
functions getutxent() etc. are aliases for getutent() etc.
linux glibc另一方面不使用类似的utmpx文件,因为utmp结构体已经足够大了,getutxent系列的函数是getutent函数的别名
The above functions are not thread-safe. Glibc adds reentrant versions
上面的函数都不是线程安全的,glibc提供了可重入的版本
#define _GNU_SOURCE /* or _SVID_SOURCE or _BSD_SOURCE;
see feature_test_macros(7) */
#include <utmp.h>
int getutent_r(struct utmp *ubuf, struct utmp **ubufp);
int getutid_r(struct utmp *ut,
struct utmp *ubuf, struct utmp **ubufp);
int getutline_r(struct utmp *ut,
struct utmp *ubuf, struct utmp **ubufp);
These functions are GNU extensions, analogs of the functions of the same name without the _r suffix. The ubuf argument
gives these functions a place to store their result. On success they return 0, and a pointer to the result is written in
*ubufp. On error these functions return -1. There are no utmpx equivalents of the above functions. (POSIX.1 does not
specify such functions.)
这些函数都是GNU实现,功能相同的函数名字跟这个一样,只是没有_r后缀,ubuf参数是提供用来存储结果的,成功的时候返回0,指向结果的指针被写到*ubufp中,当错误的时候返回-1,upmpx系列函数没有上面的可重入版本
The following example adds and removes a utmp record, assuming it is run from within a pseudo terminal. For usage in a real
application, you should check the return values of getpwuid(3) and ttyname(3).
下面的例子增加或删除utmp记录,假设运行在伪终端,如果需要在程序中使用,必须检测getpwuid和ttyname的返回值
getutent, getutid, getutline, pututline, setutent, endutent, utmpname - 获取utmp文件的入口
所需头文件
#include <utmp.h>
struct utmp *getutent(void);
struct utmp *getutid(struct utmp *ut);
struct utmp *getutline(struct utmp *ut);
struct utmp *pututline(struct utmp *ut);
void setutent(void);
void endutent(void);
int utmpname(const char *file);
utmpname() sets the name of the utmp-format file for the other utmp functions to access. If utmpname() is not used to set
the filename before the other functions are used, they assume _PATH_UTMP, as defined in <paths.h>.
utmpname函数utmp格式文件的名字给其他utmp函数使用,如果utmpname函数没有在其他函数之前使用,那么他们假设使用_PATH_UTMP,在<paths.h>中定义
setutent() rewinds the file pointer to the beginning of the utmp file. It is generally a good idea to call it before any of
the other functions.
setutent函数回滚uptmp文件的文件描述符到文件开头,在其他函数之前调用这个函数是一个好主意
endutent() closes the utmp file. It should be called when the user code is done accessing the file with the other func‐
tions.
endutent函数关闭utmp文件,应该在其他函数调用完毕之后调用这个函数
getutent() reads a line from the current file position in the utmp file. It returns a pointer to a structure containing the
fields of the line. The definition of this structure is shown in utmp(5).
getutent函数从utmp文件中读取当前行,返回一个指向包含当前行内容的结构体指针,结构体的定义如下utmp
getutid() searches forward from the current file position in the utmp file based upon ut. If ut->ut_type is one of RUN_LVL,
BOOT_TIME, NEW_TIME, or OLD_TIME, getutid() will find the first entry whose ut_type field matches ut->ut_type. If
ut->ut_type is one of INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, getutid() will find the first entry whose
ut_id field matches ut->ut_id.
getutid函数基于ut从utmp文件的当前位置查找,如果ut->ut_type是RUN_LVL,BOOT_TIME, NEW_TIME或者OLD_TIME其中的一种,getutid将找到第一个ut_type是ut->ut_type的入口,如果ut->ut_type是INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS或者DEAD_PROCESS其中的一个,getutid将找到第一个ut_id是ut->ut_id的入口
getutline() searches forward from the current file position in the utmp file. It scans entries whose ut_type is
USER_PROCESS or LOGIN_PROCESS and returns the first one whose ut_line field matches ut->ut_line.
getutline从utmp文件中查找当前位置,他查找ut_type是USER_PROCESS或者LOGIN_PROCES的入口,返回第一个ut_line的值是ut->ut_line入口
pututline() writes the utmp structure ut into the utmp file. It uses getutid() to search for the proper place in the file
to insert the new entry. If it cannot find an appropriate slot for ut, pututline() will append the new entry to the end of
the file.
pututline把utmp结构的ut数据写到tump文件,他使用getutid来查找文件中合适的位置来插入新的入口,如果找不到适合ut的位置,那么pututline将会把新入口的位置加在文件的结尾
getutent(), getutid(), and getutline() return a pointer to a struct utmp on success, and NULL on failure (which includes the
"record not found" case). This struct utmp is allocated in static storage, and may be overwritten by subsequent calls.
getutent,getutid和getutline在成功的时候返回一个指向utmp结构体的指针,在失败的时候返回NULL,utmp结构体在静态存储区分配,所以会被后续的调用覆盖
On success pututline() returns ut; on failure, it returns NULL.
putline在成功的时候返回ut,失败的时候返回NULL
utmpname() returns 0 if the new name was successfully stored, or -1 on failure.
utmpname在成功存储的时候返回0,失败的时候返回-1
ERRORS
ENOMEM Out of memory.
内存溢出
ESRCH Record not found.
存档未找到
setutent(), pututline(), and the getut* () functions can also fail for the reasons described in open(2).
setutent, pututline和getut函数会失败,具体原因详见open
/var/run/utmp database of currently logged-in users
当前已登陆用户的数据库
/var/log/wtmp database of past user logins
以前登陆用户的数据库
#include <utmpx.h>
struct utmpx *getutxent(void);
struct utmpx *getutxid(const struct utmpx *);
struct utmpx *getutxline(const struct utmpx *);
struct utmpx *pututxline(const struct utmpx *);
void setutxent(void);
void endutxent(void);
These functions are provided by glibc, and perform the same task as their equivalents without the "x", but use struct utmpx,
defined on Linux to be the same as struct utmp. For completeness, glibc also provides utmpxname(), although this function
is not specified by POSIX.1.
这些函数在glibc中被提供,他们可以完成跟没有x的函数同样的任务,但是使用utmpx结构体,在linux系统中定义,跟utmp结构体一样,为了完整,glibc也使用utmpxname,尽管这个函数没有被POSIX.1实现
On some other systems, the utmpx structure is a superset of the utmp structure, with additional fields, and larger versions
of the existing fields, and parallel files are maintained, often /var/*/utmpx and /var/*/wtmpx.
在其他的系统中,utmpx是utmp结构体的超集
Linux glibc on the other hand does not use a parallel utmpx file since its utmp structure is already large enough. The
functions getutxent() etc. are aliases for getutent() etc.
linux glibc另一方面不使用类似的utmpx文件,因为utmp结构体已经足够大了,getutxent系列的函数是getutent函数的别名
The above functions are not thread-safe. Glibc adds reentrant versions
上面的函数都不是线程安全的,glibc提供了可重入的版本
#define _GNU_SOURCE /* or _SVID_SOURCE or _BSD_SOURCE;
see feature_test_macros(7) */
#include <utmp.h>
int getutent_r(struct utmp *ubuf, struct utmp **ubufp);
int getutid_r(struct utmp *ut,
struct utmp *ubuf, struct utmp **ubufp);
int getutline_r(struct utmp *ut,
struct utmp *ubuf, struct utmp **ubufp);
These functions are GNU extensions, analogs of the functions of the same name without the _r suffix. The ubuf argument
gives these functions a place to store their result. On success they return 0, and a pointer to the result is written in
*ubufp. On error these functions return -1. There are no utmpx equivalents of the above functions. (POSIX.1 does not
specify such functions.)
这些函数都是GNU实现,功能相同的函数名字跟这个一样,只是没有_r后缀,ubuf参数是提供用来存储结果的,成功的时候返回0,指向结果的指针被写到*ubufp中,当错误的时候返回-1,upmpx系列函数没有上面的可重入版本
The following example adds and removes a utmp record, assuming it is run from within a pseudo terminal. For usage in a real
application, you should check the return values of getpwuid(3) and ttyname(3).
下面的例子增加或删除utmp记录,假设运行在伪终端,如果需要在程序中使用,必须检测getpwuid和ttyname的返回值
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <utmp.h>
int
main(int argc, char *argv[])
{
struct utmp entry;
system("echo before adding entry:;who");
entry.ut_type = USER_PROCESS;
entry.ut_pid = getpid();
strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
/* only correct for ptys named /dev/tty[pqr][0-9a-z] */
strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
time(&entry.ut_time);
strcpy(entry.ut_user, getpwuid(getuid())->pw_name);
memset(entry.ut_host, 0, UT_HOSTSIZE);
entry.ut_addr = 0;
setutent();
pututline(&entry);
system("echo after adding entry:;who");
entry.ut_type = DEAD_PROCESS;
memset(entry.ut_line, 0, UT_LINESIZE);
entry.ut_time = 0;
memset(entry.ut_user, 0, UT_NAMESIZE);
setutent();
pututline(&entry);
system("echo after removing entry:;who");
endutent();
exit(EXIT_SUCCESS);
}