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