aboutsummaryrefslogtreecommitdiff
path: root/src/util/pty/dump-utmp.c
blob: d4c303fb3af106c998f32a70bf73b8c9e6d8b8b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright 2001 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of
 * M.I.T. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability
 * of this software for any purpose.  It is provided "as is" without
 * express or implied warranty.
 *
 * dump-utmp.c: dump utmp and utmpx format files for debugging purposes.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#ifndef UTMPX
#ifdef HAVE_UTMPX_H
#define UTMPX
#endif
#endif

#if defined(HAVE_UTMPNAME) || defined(HAVE_UTMPXNAME)
#define UTN			/* we can set utmp or utmpx for getut*() */
#endif

#ifdef UTMPX
#include <utmpx.h>
void print_utx(int, const struct utmpx *);
#endif
#include <utmp.h>

void print_ut(int, const struct utmp *);

void usage(const char *);

#if defined (HAVE_STRUCT_UTMP_UT_TYPE) || defined (UTMPX)
char *ut_typename(int);

char *
ut_typename(int t) {
    switch (t) {
#define S(N) case N : return #N
#define S2(N,N2) case N : return #N2
	S(EMPTY);
	S(RUN_LVL);
	S(BOOT_TIME);
	S(OLD_TIME);
	S(NEW_TIME);
	S2(INIT_PROCESS,INIT);
	S2(LOGIN_PROCESS,LOGIN);
	S2(USER_PROCESS,USER);
	S2(DEAD_PROCESS,DEAD);
	S(ACCOUNTING);
    default: return "??";
    }
}
#endif

#define S2D(x) (sizeof(x) * 2.4 + 1.5)

void
print_ut(int all, const struct utmp *u)
{
    int lu, ll;
#ifdef HAVE_STRUCT_UTMP_UT_ID
    int lid;
#endif
#ifdef HAVE_STRUCT_UTMP_UT_PID
    int lpid;
#endif
#ifdef PTY_UTMP_E_EXIT
    int let, lee;
#endif

#ifdef HAVE_STRUCT_UTMP_UT_TYPE
    if (!all && ((u->ut_type == EMPTY) || (u->ut_type == DEAD_PROCESS)))
	return;
#endif

    lu = sizeof(u->ut_name);
    ll = sizeof(u->ut_line);
    printf("%-*.*s:", lu, lu, u->ut_name);
    printf("%-*.*s:", ll, ll, u->ut_line);
#ifdef HAVE_STRUCT_UTMP_UT_ID
    lid = sizeof(u->ut_id);
    printf("%-*.*s:", lid, lid, u->ut_id);
#endif
#ifdef HAVE_STRUCT_UTMP_UT_PID
    lpid = S2D(u->ut_pid);
    printf("%*ld", lpid, (long)u->ut_pid);
#endif
#ifdef PTY_UTMP_E_EXIT
    let = S2D(u->ut_exit.PTY_UTMP_E_TERMINATION);
    lee = S2D(u->ut_exit.PTY_UTMP_E_EXIT);
    printf("(%*ld,", let, (long)u->ut_exit.PTY_UTMP_E_TERMINATION);
    printf("%*ld)", lee, (long)u->ut_exit.PTY_UTMP_E_EXIT);
#endif
#ifdef HAVE_STRUCT_UTMP_UT_TYPE
    printf(" %-9s", ut_typename(u->ut_type));
#endif
    printf(" %s", ctime(&u->ut_time) + 4);
#ifdef HAVE_STRUCT_UTMP_UT_HOST
    if (u->ut_host[0])
	printf(" %.*s\n", (int) sizeof(u->ut_host), u->ut_host);
#endif

    return;
}

#ifdef UTMPX
void
print_utx(int all, const struct utmpx *u)
{
    int lu, ll, lid, lpid;
#ifdef PTY_UTMPX_E_EXIT
    int let, lee;
#endif

    if (!all && ((u->ut_type == EMPTY) || (u->ut_type == DEAD_PROCESS)))
	return;

    lu = sizeof(u->ut_user);
    ll = sizeof(u->ut_line);
    lid = sizeof(u->ut_id);
    printf("%-*.*s:", lu, lu, u->ut_user);
    printf("%-*.*s:", ll, ll, u->ut_line);
    printf("%-*.*s", lid, lid, u->ut_id);
    if (lu + ll + lid >= 60)
	printf("\n");
    else
	printf(":");
    lpid = S2D(u->ut_pid);
    printf("%*ld", lpid, (long)u->ut_pid);
#ifdef PTY_UTMPX_E_EXIT
    let = S2D(u->ut_exit.PTY_UTMPX_E_TERMINATION);
    lee = S2D(u->ut_exit.PTY_UTMPX_E_EXIT);
    printf("(%*ld,", let, (long)u->ut_exit.PTY_UTMPX_E_TERMINATION);
    printf("%*ld)", lee, (long)u->ut_exit.PTY_UTMPX_E_EXIT);
#endif
    printf(" %-9s", ut_typename(u->ut_type));
    printf(" %s", ctime(&u->ut_tv.tv_sec) + 4);
#ifdef HAVE_STRUCT_UTMPX_UT_HOST
    if (u->ut_host[0])
	printf(" %s\n", u->ut_host);
#endif

    return;
}
#endif

#ifdef UTMPX
#define OPTX "x"
#else
#define OPTX
#endif
#ifdef UTN
#define OPTG "g"
#else
#define OPTG
#endif
#define OPTS "a" OPTX OPTG

void
usage(const char *prog)
{
    fprintf(stderr, "usage: %s [-" OPTS "] file\n", prog);
    exit(1);
}

int
main(int argc, char **argv)
{
    int c;
    int all, is_utmpx, do_getut;
    int f;
    char *fn;
    size_t recsize;
    size_t nread;
    union {
	struct utmp ut;
#ifdef UTMPX
	struct utmpx utx;
#endif
    } u;

    all = is_utmpx = do_getut = 0;
    recsize = sizeof(struct utmp);

    while ((c = getopt(argc, argv, OPTS)) != EOF) {
	switch (c) {
	case 'a':
	    all = 1;
	    break;
#ifdef UTMPX
	case 'x':
	    is_utmpx = 1;
	    recsize = sizeof(struct utmpx);
	    break;
#endif
#ifdef UTN
	case 'g':
	    do_getut = 1;
	    break;
#endif
	default:
	    usage(argv[0]);
	}
    }
    if (argc <= optind)
	usage(argv[0]);
    fn = argv[optind];
    if (!do_getut) {
	f = open(fn, O_RDONLY);
	if (f == -1) {
	    perror(fn);
	    exit(1);
	}
	while ((nread = read(f, &u, recsize)) > 0) {
	    if (nread < recsize) {
		fprintf(stderr, "short read");
		close(f);
		exit(1);
	    }
	    if (is_utmpx) {
#ifdef UTMPX
		print_utx(all, &u.utx);
#else
		abort();
#endif
	    } else {
		print_ut(all, &u.ut);
	    }
	}
	if (nread == -1) {
	    perror("read");
	    exit(1);
	}
	close(f);
    } else {
	if (is_utmpx) {
#ifdef UTMPX
#ifdef HAVE_UTMPXNAME
	    struct utmpx *utxp;
	    utmpxname(fn);
	    setutxent();
	    while ((utxp = getutxent()) != NULL)
		print_utx(all, utxp);
#else
	    fprintf(stderr, "no utmpxname(); can't use getutxent()\n");
	    exit(1);
#endif
#else
	    abort();
#endif
	} else {
#ifdef HAVE_UTMPNAME
	    struct utmp *utp;
	    utmpname(fn);
	    setutxent();
	    while ((utp = getutent()) != NULL)
		print_ut(all, utp);
#else
	    fprintf(stderr, "no utmpname(); can't use getutent()\n");
	    exit(1);
#endif
	}
    }
    exit(0);    
}