man-pages/man3/getutent.3.html
2021-03-31 01:06:50 +01:00

537 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETUTENT</TITLE>
</HEAD><BODY>
<H1>GETUTENT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-08-02<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getutent, getutid, getutline, pututline, setutent, endutent,
utmpname - access utmp file entries
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/utmp.h">utmp.h</A>&gt;</B>
<P>
<B>struct utmp *getutent(void);</B>
<BR>
<B>struct utmp *getutid(const struct utmp *</B><I>ut</I><B>);</B>
<BR>
<B>struct utmp *getutline(const struct utmp *</B><I>ut</I><B>);</B>
<P>
<B>struct utmp *pututline(const struct utmp *</B><I>ut</I><B>);</B>
<P>
<B>void setutent(void);</B>
<BR>
<B>void endutent(void);</B>
<P>
<B>int utmpname(const char *</B><I>file</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
New applications should use the POSIX.1-specified &quot;utmpx&quot; versions of
these functions; see CONFORMING TO.
<P>
<B>utmpname</B>()
sets the name of the utmp-format file for the other utmp
functions to access.
If
<B>utmpname</B>()
is not used to set the filename
before the other functions are used, they assume <B>_PATH_UTMP</B>, as
defined in <I>&lt;<A HREF="file:///usr/include/paths.h">paths.h</A>&gt;</I>.
<P>
<B>setutent</B>()
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.
<P>
<B>endutent</B>()
closes the utmp file.
It should be called when the user
code is done accessing the file with the other functions.
<P>
<B>getutent</B>()
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
<B><A HREF="/cgi-bin/man/man2html?5+utmp">utmp</A></B>(5).
<P>
<B>getutid</B>()
searches forward from the current file position in the utmp
file based upon <I>ut</I>.
If <I>ut-&gt;ut_type</I> is one of <B>RUN_LVL</B>,
<B>BOOT_TIME</B>, <B>NEW_TIME</B>, or <B>OLD_TIME</B>,
<B>getutid</B>()
will
find the first entry whose <I>ut_type</I> field matches <I>ut-&gt;ut_type</I>.
If <I>ut-&gt;ut_type</I> is one of <B>INIT_PROCESS</B>, <B>LOGIN_PROCESS</B>,
<B>USER_PROCESS</B>, or <B>DEAD_PROCESS</B>,
<B>getutid</B>()
will find the
first entry whose
<I>ut_id</I>
field matches <I>ut-&gt;ut_id</I>.
<P>
<B>getutline</B>()
searches forward from the current file position in the utmp file.
It scans entries whose
<I>ut_type</I>
is <B>USER_PROCESS</B>
or <B>LOGIN_PROCESS</B> and returns the first one whose
<I>ut_line</I>
field
matches <I>ut-&gt;ut_line</I>.
<P>
<B>pututline</B>()
writes the
<I>utmp</I>
structure <I>ut</I> into the utmp file.
It uses
<B>getutid</B>()
to search for the proper place in the file to insert
the new entry.
If it cannot find an appropriate slot for <I>ut</I>,
<B>pututline</B>()
will append the new entry to the end of the file.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>getutent</B>(),
<B>getutid</B>(),
and
<B>getutline</B>()
return a pointer to a <I>struct utmp</I> on success,
and NULL on failure (which includes the &quot;record not found&quot; case).
This <I>struct utmp</I> is allocated in static storage, and may be
overwritten by subsequent calls.
<P>
On success
<B>pututline</B>()
returns
<I>ut</I>;
on failure, it returns NULL.
<P>
<B>utmpname</B>()
returns 0 if the new name was successfully stored, or -1 on failure.
<P>
In the event of an error, these functions
<I>errno</I>
set to indicate the cause.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>ENOMEM</B>
<DD>
Out of memory.
<DT id="2"><B>ESRCH</B>
<DD>
Record not found.
</DL>
<P>
<B>setutent</B>(),
<B>pututline</B>(),
and the
<B>getut*</B>()
functions can also fail for the reasons described in
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
<A NAME="lbAG">&nbsp;</A>
<H2>FILES</H2>
<DL COMPACT>
<DT id="3"><I>/var/run/utmp</I>
<DD>
database of currently logged-in users
<DT id="4"><I>/var/log/wtmp</I>
<DD>
database of past user logins
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>getutent</B>()
</TD><TD>Thread safety</TD><TD>
MT-Unsafe init race:utent
<BR>
race:utentbuf sig:ALRM timer
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>getutid</B>(),
<BR>
<B>getutline</B>()
</TD><TD>Thread safety</TD><TD>
MT-Unsafe init race:utent
<BR>
sig:ALRM timer
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>pututline</B>()
</TD><TD>Thread safety</TD><TD>
MT-Unsafe race:utent
<BR>
sig:ALRM timer
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>setutent</B>(),
<BR>
<B>endutent</B>(),
<BR>
<B>utmpname</B>()
</TD><TD>Thread safety</TD><TD>MT-Unsafe race:utent<BR></TD></TR>
</TABLE>
<P>
In the above table,
<I>utent</I>
in
<I>race:utent</I>
signifies that if any of the functions
<B>setutent</B>(),
<B>getutent</B>(),
<B>getutid</B>(),
<B>getutline</B>(),
<B>pututline</B>(),
<B>utmpname</B>(),
or
<B>endutent</B>()
are used in parallel in different threads of a program,
then data races could occur.
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
XPG2, SVr4.
<P>
In XPG2 and SVID 2 the function
<B>pututline</B>()
is documented to return void, and that is what it does on many systems
(AIX, HP-UX).
HP-UX introduces a new function
<B>_pututline</B>()
with the prototype given above for
<B>pututline</B>().
<P>
All these functions are obsolete now on non-Linux systems.
POSIX.1-2001 and POSIX.1-2008, following SUSv1,
does not have any of these functions, but instead uses
<P>
<B>#include &lt;<A HREF="file:///usr/include/utmpx.h">utmpx.h</A>&gt;</B>
<P>
<B>struct utmpx *getutxent(void);</B>
<B>struct utmpx *getutxid(const struct utmpx *);</B>
<B>struct utmpx *getutxline(const struct utmpx *);</B>
<B>struct utmpx *pututxline(const struct utmpx *);</B>
<B>void setutxent(void);</B>
<B>void endutxent(void);</B>
<P>
These functions are provided by glibc,
and perform the same task as their equivalents without the &quot;x&quot;, but use
<I>struct utmpx</I>,
defined on Linux to be the same as
<I>struct utmp</I>.
For completeness, glibc also provides
<B>utmpxname</B>(),
although this function is not specified by POSIX.1.
<P>
On some other systems,
the <I>utmpx</I> structure is a superset of the <I>utmp</I> structure,
with additional fields, and larger versions of the existing fields,
and parallel files are maintained, often
<I>/var/*/utmpx</I>
and
<I>/var/*/wtmpx</I>.
<P>
Linux glibc on the other hand does not use a parallel <I>utmpx</I> file
since its <I>utmp</I> structure is already large enough.
The &quot;x&quot; functions listed above are just aliases for
their counterparts without the &quot;x&quot; (e.g.,
<B>getutxent</B>()
is an alias for
<B>getutent</B>()).
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
<A NAME="lbAK">&nbsp;</A>
<H3>Glibc notes</H3>
The above functions are not thread-safe.
Glibc adds reentrant versions
<P>
<B>#include &lt;<A HREF="file:///usr/include/utmp.h">utmp.h</A>&gt;</B>
<P>
<B>int getutent_r(struct utmp *</B><I>ubuf</I><B>, struct utmp **</B><I>ubufp</I><B>);</B>
<P>
<B>int getutid_r(struct utmp *</B><I>ut</I><B>,</B>
<B> struct utmp *</B><I>ubuf</I><B>, struct utmp **</B><I>ubufp</I><B>);</B>
<P>
<B>int getutline_r(struct utmp *</B><I>ut</I><B>,</B>
<B> struct utmp *</B><I>ubuf</I><B>, struct utmp **</B><I>ubufp</I><B>);</B>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>getutent_r</B>(),
<B>getutid_r</B>(),
<B>getutline_r</B>():
<PRE>
_GNU_SOURCE
|| /* since glibc 2.19: */ _DEFAULT_SOURCE
|| /* glibc &lt;= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
</PRE>
<P>
These functions are GNU extensions, analogs of the functions of the
same name without the _r suffix.
The
<I>ubuf</I>
argument gives these functions a place to store their result.
On success, they return 0, and a pointer to the result is written in
<I>*ubufp</I>.
On error, these functions return -1.
There are no utmpx equivalents of the above functions.
(POSIX.1 does not specify such functions.)
<A NAME="lbAL">&nbsp;</A>
<H2>EXAMPLE</H2>
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
<B><A HREF="/cgi-bin/man/man2html?3+getpwuid">getpwuid</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+ttyname">ttyname</A></B>(3).
<P>
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/pwd.h">pwd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/utmp.h">utmp.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;utmp&nbsp;entry;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;system(&quot;echo&nbsp;before&nbsp;adding&nbsp;entry:;who&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;entry.ut_type&nbsp;=&nbsp;USER_PROCESS;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;entry.ut_pid&nbsp;=&nbsp;getpid();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;strcpy(entry.ut_line,&nbsp;ttyname(STDIN_FILENO)&nbsp;+&nbsp;strlen(&quot;/dev/&quot;));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;only&nbsp;correct&nbsp;for&nbsp;ptys&nbsp;named&nbsp;/dev/tty[pqr][0-9a-z]&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;strcpy(entry.ut_id,&nbsp;ttyname(STDIN_FILENO)&nbsp;+&nbsp;strlen(&quot;/dev/tty&quot;));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;time(&amp;entry.ut_time);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;strcpy(entry.ut_user,&nbsp;getpwuid(getuid())-&gt;pw_name);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(entry.ut_host,&nbsp;0,&nbsp;UT_HOSTSIZE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;entry.ut_addr&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setutent();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pututline(&amp;entry);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;system(&quot;echo&nbsp;after&nbsp;adding&nbsp;entry:;who&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;entry.ut_type&nbsp;=&nbsp;DEAD_PROCESS;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(entry.ut_line,&nbsp;0,&nbsp;UT_LINESIZE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;entry.ut_time&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(entry.ut_user,&nbsp;0,&nbsp;UT_NAMESIZE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setutent();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pututline(&amp;entry);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;system(&quot;echo&nbsp;after&nbsp;removing&nbsp;entry:;who&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;endutent();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+getutmp">getutmp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+utmp">utmp</A></B>(5)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">FILES</A><DD>
<DT id="11"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAJ">NOTES</A><DD>
<DL>
<DT id="14"><A HREF="#lbAK">Glibc notes</A><DD>
</DL>
<DT id="15"><A HREF="#lbAL">EXAMPLE</A><DD>
<DT id="16"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:44 GMT, March 31, 2021
</BODY>
</HTML>