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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/time.h>
#include <errno.h>
#include "lastcomm.h"
/* ARGSUSED1 */
static void
skip_group(ea_file_t *ef, uint_t nobjs)
{
ea_object_t curr_obj;
if (ea_previous_object(ef, &curr_obj) == -1) {
(void) fprintf(stderr, gettext("lastcomm: "
"corrupted exacct file\n"));
exit(1);
}
}
static int
ok(int argc, char *argv[], int index, uid_t uid, dev_t tty, char *command)
{
int j;
for (j = index; j < argc; j++)
if (strcmp(getname(uid), argv[j]) &&
strcmp(getdev(tty), argv[j]) &&
strncmp(command, argv[j], fldsiz(acct, ac_comm)))
break;
return (j == argc);
}
static void
disp_group(ea_file_t *ef, uint_t nobjs, int argc, char *argv[], int index)
{
uint_t i;
char *command = NULL;
double cpu_usr_secs = 0.;
double cpu_usr_nsecs = 0.;
double cpu_sys_secs = 0.;
double cpu_sys_nsecs = 0.;
double totalsecs;
dev_t tty = 0;
major_t tty_major = 0;
minor_t tty_minor = 0;
uid_t uid = 0;
time_t time = 0;
uint32_t flag = 0;
for (i = 0; i < nobjs; i++) {
ea_object_t curr_obj;
if (ea_get_object(ef, &curr_obj) == -1) {
(void) fprintf(stderr, gettext("lastcomm: "
"corrupted exacct file\n"));
exit(1);
}
switch (curr_obj.eo_catalog) {
case EXT_STRING | EXC_DEFAULT | EXD_PROC_COMMAND:
command = curr_obj.eo_item.ei_string;
break;
case EXT_UINT32 | EXC_DEFAULT | EXD_PROC_UID:
uid = curr_obj.eo_item.ei_uint32;
break;
case EXT_UINT64 | EXC_DEFAULT | EXD_PROC_CPU_SYS_SEC:
cpu_sys_secs = curr_obj.eo_item.ei_uint64;
break;
case EXT_UINT64 | EXC_DEFAULT | EXD_PROC_CPU_USER_SEC:
cpu_usr_secs = curr_obj.eo_item.ei_uint64;
break;
case EXT_UINT64 | EXC_DEFAULT | EXD_PROC_CPU_SYS_NSEC:
cpu_sys_nsecs = curr_obj.eo_item.ei_uint64;
break;
case EXT_UINT64 | EXC_DEFAULT | EXD_PROC_CPU_USER_NSEC:
cpu_usr_nsecs = curr_obj.eo_item.ei_uint64;
break;
case EXT_UINT32 | EXC_DEFAULT | EXD_PROC_TTY_MAJOR:
tty_major = curr_obj.eo_item.ei_uint32;
break;
case EXT_UINT32 | EXC_DEFAULT | EXD_PROC_TTY_MINOR:
tty_minor = curr_obj.eo_item.ei_uint32;
break;
case EXT_UINT32 | EXC_DEFAULT | EXD_PROC_ACCT_FLAGS:
flag = curr_obj.eo_item.ei_uint32;
break;
case EXT_UINT64 | EXC_DEFAULT | EXD_PROC_START_SEC:
time = (uint32_t)curr_obj.eo_item.ei_uint64;
break;
default:
break;
}
if (curr_obj.eo_type == EO_GROUP)
disp_group(ef, curr_obj.eo_group.eg_nobjs,
argc, argv, index);
}
if (command == NULL) {
(void) fprintf(stderr, gettext("lastcomm: "
"corrupted exacct file\n"));
exit(1);
}
/*
* If a 64-bit kernel returns a major or minor value that would exceed
* the capacity of a 32-bit dev_t (and these also become visible in the
* filesystem), then the 32-bit makedev may be inaccurate and return
* NODEV. When this occurs, we can remedy the problem by providing
* either a function which returns "dev64_t"'s or by providing an LP64
* version of lastcomm.
*/
tty = makedev(tty_major, tty_minor);
/*
* If this record doesn't match the optional arguments, go on to the
* next record.
*/
if (argc > index && !ok(argc, argv, index, uid, tty, command))
return;
totalsecs =
cpu_usr_secs + cpu_usr_nsecs / NANOSEC +
cpu_sys_secs + cpu_sys_nsecs / NANOSEC;
(void) printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
fldsiz(acct, ac_comm), fldsiz(acct, ac_comm), command,
flagbits(flag), NMAX, getname(uid), LMAX, getdev(tty),
totalsecs, ctime(&time));
}
int
lc_exacct(char *filename, int argc, char *argv[], int index)
{
ea_file_t ef;
ea_object_t curr_obj;
if (ea_open(&ef, filename, EXACCT_CREATOR,
EO_TAIL | EO_VALID_HDR, O_RDONLY, 0) < 0) {
switch (ea_error()) {
case EXR_CORRUPT_FILE:
(void) fprintf(stderr, gettext("lastcomm: "
"exacct file corrupted\n"));
break;
case EXR_SYSCALL_FAIL:
(void) fprintf(stderr, gettext("lastcomm: "
"cannot open %s: %s\n"), filename,
strerror(errno));
break;
default:
break;
}
return (1);
}
while (ea_previous_object(&ef, &curr_obj) != -1) {
if (ea_get_object(&ef, &curr_obj) == -1) {
(void) fprintf(stderr, gettext("lastcomm: "
"exacct file corrupted\n"));
exit(1);
}
/*
* lc_exacct(), in parsing the extended process accounting file,
* has knowledge of the fact that process records are top-level
* records.
*/
if ((curr_obj.eo_catalog & EXT_TYPE_MASK) == EXT_GROUP) {
if (curr_obj.eo_catalog ==
(EXT_GROUP | EXC_DEFAULT | EXD_GROUP_PROC))
disp_group(&ef, curr_obj.eo_group.eg_nobjs,
argc, argv, index);
else
skip_group(&ef, curr_obj.eo_group.eg_nobjs);
}
/*
* Back up to the head of the object we just consumed.
*/
if (ea_previous_object(&ef, &curr_obj) == -1) {
if (ea_error() == EXR_EOF)
break;
(void) fprintf(stderr, gettext("lastcomm: "
"exacct file corrupted\n"));
exit(1);
}
}
if (ea_error() != EXR_EOF) {
(void) fprintf(stderr, gettext("lastcomm: "
"exacct file corrupted\n"));
exit(1);
}
(void) ea_close(&ef);
return (0);
}
|