(READ_UTMP_USER_PROCESS): New flag
[gnulib.git] / lib / readutmp.h
1 /* Declarations for GNU's read utmp module.
2
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4    2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by jla; revised by djm */
21
22 #ifndef __READUTMP_H__
23 # define __READUTMP_H__
24
25 # include <sys/types.h>
26
27 /* AIX 4.3.3 has both utmp.h and utmpx.h, but only struct utmp
28    has the ut_exit member.  */
29 # if (HAVE_UTMPX_H && HAVE_UTMP_H && HAVE_STRUCT_UTMP_UT_EXIT \
30       && ! HAVE_STRUCT_UTMPX_UT_EXIT)
31 #  undef HAVE_UTMPX_H
32 # endif
33
34 # if HAVE_UTMPX_H
35 #  if HAVE_UTMP_H
36     /* HPUX 10.20 needs utmp.h, for the definition of e.g., UTMP_FILE.  */
37 #   include <utmp.h>
38 #  endif
39 #  include <utmpx.h>
40 #  define UTMP_STRUCT_NAME utmpx
41 #  define UT_TIME_MEMBER(UT_PTR) ((UT_PTR)->ut_tv.tv_sec)
42 #  define SET_UTMP_ENT setutxent
43 #  define GET_UTMP_ENT getutxent
44 #  define END_UTMP_ENT endutxent
45 #  ifdef HAVE_UTMPXNAME
46 #   define UTMP_NAME_FUNCTION utmpxname
47 #  endif
48
49 #  if HAVE_STRUCT_UTMPX_UT_EXIT_E_TERMINATION
50 #   define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.e_termination)
51 #  else
52 #   if HAVE_STRUCT_UTMPX_UT_EXIT_UT_TERMINATION
53 #    define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.ut_termination)
54 #   else
55 #    define UT_EXIT_E_TERMINATION(U) 0
56 #   endif
57 #  endif
58
59 #  if HAVE_STRUCT_UTMPX_UT_EXIT_E_EXIT
60 #   define UT_EXIT_E_EXIT(U) ((U)->ut_exit.e_exit)
61 #  else
62 #   if HAVE_STRUCT_UTMPX_UT_EXIT_UT_EXIT
63 #    define UT_EXIT_E_EXIT(U) ((U)->ut_exit.ut_exit)
64 #   else
65 #    define UT_EXIT_E_EXIT(U) 0
66 #   endif
67 #  endif
68
69 # elif HAVE_UTMP_H
70
71 #  include <utmp.h>
72 #  if !HAVE_DECL_GETUTENT
73     struct utmp *getutent();
74 #  endif
75 #  define UTMP_STRUCT_NAME utmp
76 #  define UT_TIME_MEMBER(UT_PTR) ((UT_PTR)->ut_time)
77 #  define SET_UTMP_ENT setutent
78 #  define GET_UTMP_ENT getutent
79 #  define END_UTMP_ENT endutent
80 #  ifdef HAVE_UTMPNAME
81 #   define UTMP_NAME_FUNCTION utmpname
82 #  endif
83
84 #  if HAVE_STRUCT_UTMP_UT_EXIT_E_TERMINATION
85 #   define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.e_termination)
86 #  else
87 #   if HAVE_STRUCT_UTMP_UT_EXIT_UT_TERMINATION
88 #    define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.ut_termination)
89 #   else
90 #    define UT_EXIT_E_TERMINATION(U) 0
91 #   endif
92 #  endif
93
94 #  if HAVE_STRUCT_UTMP_UT_EXIT_E_EXIT
95 #   define UT_EXIT_E_EXIT(U) ((U)->ut_exit.e_exit)
96 #  else
97 #   if HAVE_STRUCT_UTMP_UT_EXIT_UT_EXIT
98 #    define UT_EXIT_E_EXIT(U) ((U)->ut_exit.ut_exit)
99 #   else
100 #    define UT_EXIT_E_EXIT(U) 0
101 #   endif
102 #  endif
103
104 # endif
105
106 /* Accessor macro for the member named ut_user or ut_name.  */
107 # if HAVE_UTMPX_H
108
109 #  if HAVE_STRUCT_UTMPX_UT_USER
110 #   define UT_USER(Utmp) ((Utmp)->ut_user)
111 #  endif
112 #  if HAVE_STRUCT_UTMPX_UT_NAME
113 #   undef UT_USER
114 #   define UT_USER(Utmp) ((Utmp)->ut_name)
115 #  endif
116
117 # elif HAVE_UTMP_H
118
119 #  if HAVE_STRUCT_UTMP_UT_USER
120 #   define UT_USER(Utmp) ((Utmp)->ut_user)
121 #  endif
122 #  if HAVE_STRUCT_UTMP_UT_NAME
123 #   undef UT_USER
124 #   define UT_USER(Utmp) ((Utmp)->ut_name)
125 #  endif
126
127 # endif
128
129 # define HAVE_STRUCT_XTMP_UT_EXIT \
130     (HAVE_STRUCT_UTMP_UT_EXIT \
131      || HAVE_STRUCT_UTMPX_UT_EXIT)
132
133 # define HAVE_STRUCT_XTMP_UT_ID \
134     (HAVE_STRUCT_UTMP_UT_ID \
135      || HAVE_STRUCT_UTMPX_UT_ID)
136
137 # define HAVE_STRUCT_XTMP_UT_PID \
138     (HAVE_STRUCT_UTMP_UT_PID \
139      || HAVE_STRUCT_UTMPX_UT_PID)
140
141 typedef struct UTMP_STRUCT_NAME STRUCT_UTMP;
142
143 enum { UT_USER_SIZE = sizeof UT_USER ((STRUCT_UTMP *) 0) };
144
145 # if !defined UTMP_FILE && defined _PATH_UTMP
146 #  define UTMP_FILE _PATH_UTMP
147 # endif
148
149 # if !defined WTMP_FILE && defined _PATH_WTMP
150 #  define WTMP_FILE _PATH_WTMP
151 # endif
152
153 # ifdef UTMPX_FILE /* Solaris, SysVr4 */
154 #  undef UTMP_FILE
155 #  define UTMP_FILE UTMPX_FILE
156 # endif
157
158 # ifdef WTMPX_FILE /* Solaris, SysVr4 */
159 #  undef WTMP_FILE
160 #  define WTMP_FILE WTMPX_FILE
161 # endif
162
163 # ifndef UTMP_FILE
164 #  define UTMP_FILE "/etc/utmp"
165 # endif
166
167 # ifndef WTMP_FILE
168 #  define WTMP_FILE "/etc/wtmp"
169 # endif
170
171 # if HAVE_STRUCT_XTMP_UT_PID
172 #  define UT_PID(U) ((U)->ut_pid)
173 # else
174 #  define UT_PID(U) 0
175 # endif
176
177 # if HAVE_STRUCT_UTMP_UT_TYPE || HAVE_STRUCT_UTMPX_UT_TYPE
178 #  define UT_TYPE_EQ(U, V) ((U)->ut_type == (V))
179 #  define UT_TYPE_NOT_DEFINED 0
180 # else
181 #  define UT_TYPE_EQ(U, V) 0
182 #  define UT_TYPE_NOT_DEFINED 1
183 # endif
184
185 # ifdef BOOT_TIME
186 #  define UT_TYPE_BOOT_TIME(U) UT_TYPE_EQ (U, BOOT_TIME)
187 # else
188 #  define UT_TYPE_BOOT_TIME(U) 0
189 # endif
190
191 # ifdef USER_PROCESS
192 #  define UT_TYPE_USER_PROCESS(U) UT_TYPE_EQ (U, USER_PROCESS)
193 # else
194 #  define UT_TYPE_USER_PROCESS(U) 0
195 # endif
196
197 # define IS_USER_PROCESS(U)                                     \
198    (UT_USER (U)[0]                                              \
199     && (UT_TYPE_USER_PROCESS (U)                                \
200         || (UT_TYPE_NOT_DEFINED && UT_TIME_MEMBER (U) != 0)))
201
202 /* Options for read_utmp.  */
203 enum
204   {
205     READ_UTMP_CHECK_PIDS = 1,
206     READ_UTMP_USER_PROCESS = 2
207   };
208
209 char *extract_trimmed_name (const STRUCT_UTMP *ut);
210 int read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
211                int options);
212
213 #endif /* __READUTMP_H__ */