summaryrefslogtreecommitdiff
path: root/usr/src/man/man3exacct/ea_pack_object.3exacct
blob: e910751ed86f92da56a35bb587c2e566f4db70f1 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'\" te
.\" Copyright (c) 2007, Sun Microsystems, Inc.  All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (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]
.TH EA_PACK_OBJECT 3EXACCT "Oct 4, 2007"
.SH NAME
ea_pack_object, ea_unpack_object, ea_get_creator, ea_get_hostname,
ea_next_object, ea_previous_object, ea_get_object, ea_write_object,
ea_copy_object, ea_copy_object_tree, ea_get_object_tree \- construct, read, and
write extended accounting records
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-lexacct\fR [ \fIlibrary\fR\&.\|.\|. ]
#include <exacct.h>

\fBsize_t\fR \fBea_pack_object\fR(\fBea_object_t *\fR\fIobj\fR, \fBvoid *\fR\fIbuf\fR,
     \fBsize_t\fR \fIbufsize\fR);
.fi

.LP
.nf
\fBea_object_type_t\fR \fBea_unpack_object\fR(\fBea_object_t **\fR\fIobjp\fR, \fBint\fR \fIflag\fR,
     \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fIbufsize\fR);
.fi

.LP
.nf
\fBconst char *\fR\fBea_get_creator\fR(\fBea_file_t *\fR\fIef\fR);
.fi

.LP
.nf
\fBconst char *\fR\fBea_get_hostname\fR(\fBea_file_t *\fR\fIef\fR);
.fi

.LP
.nf
\fBea_object_type_t\fR \fBea_next_object\fR(\fBea_file_t *\fR\fIef\fR, \fBea_object_t *\fR\fIobj\fR);
.fi

.LP
.nf
\fBea_object_type_t\fR \fBea_previous_object\fR(\fBea_file_t *\fR\fIef\fR,
     \fBea_object_t *\fR\fIobj\fR);
.fi

.LP
.nf
\fBea_object_type_t\fR \fBea_get_object\fR(\fBea_file_t *\fR\fIef\fR, \fBea_object_t *\fR\fIobj\fR);
.fi

.LP
.nf
\fBint\fR \fBea_write_object\fR(\fBea_file_t *\fR\fIef\fR, \fBea_object_t *\fR\fIobj\fR);
.fi

.LP
.nf
\fBea_object_type_t *\fR\fBea_copy_object\fR(\fBconst ea_object_t *\fR\fIsrc\fR);
.fi

.LP
.nf
\fBea_object_type_t *\fR\fBea_copy_object_tree\fR(\fBconst ea_object_t *\fR\fIsrc\fR);
.fi

.LP
.nf
\fBea_object_type_t *\fR\fBea_get_object_tree\fR(\fBea_file_t *\fR\fIef\fR,
     \fBuint32_t\fR\fInobj\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBea_pack_object()\fR function converts \fBexacct\fR objects from their
in-memory representation to their file representation. It is passed an object
pointer that points to the top of an \fBexacct\fR object hierarchy representing
one or more \fBexacct\fR records. It returns the size of the buffer required to
contain the packed buffer representing the object hierarchy. To obtain the
correct size of the required buffer, the \fIbuf\fR and \fIbufsize\fR parameters
can be set to \fINULL\fR and 0 respectively, and the required buffer size will
be returned. The resulting packed record can be passed to \fBputacct\fR(2) or
to \fBea_set_item\fR(3EXACCT) when constructing an object of type
\fBEXT_EXACCT_OBJECT\fR.
.sp
.LP
The \fBea_unpack_object()\fR function reverses the packing process performed by
\fBea_pack_object()\fR. A packed buffer passed to \fBea_unpack_object()\fR is
unpacked into the original hierarchy of objects.  If the unpack operation fails
(for example, due to a corrupted or incomplete buffer), it returns
\fBEO_ERROR\fR; otherwise, the object type of the first object in the hierarchy
is returned.  If \fBea_unpack_object()\fR is invoked with \fIflag\fR equal to
\fBEUP_ALLOC\fR, it allocates memory for the variable-length data in the
included objects.  Otherwise, with \fIflag\fR equal to \fBEUP_NOALLOC\fR, it
sets the variable length data pointers within the unpacked object structures to
point within the buffer indicated by \fIbuf\fR. In both cases,
\fBea_unpack_object()\fR allocates all the necessary \fBexacct\fR objects to
represent the unpacked record. The resulting object hierarchy can be freed
using \fBea_free_object\fR(3EXACCT) with the same \fIflag\fR value.
.sp
.LP
The \fBea_get_creator()\fR function returns a pointer to a string representing
the recorded creator of the \fBexacct\fR file. The \fBea_get_hostname()\fR
function returns a pointer to a string representing the recorded hostname on
which the \fBexacct\fR file was created.  These functions will return
\fINULL\fR if their respective field was not recorded in the exacct file
header.
.sp
.LP
The \fBea_next_object()\fR function reads the basic fields (\fBeo_catalog\fR
and \fBeo_type\fR) into the \fBea_object_t\fR indicated by \fIobj\fR from the
\fBexacct\fR file referred to by \fIef\fR and rewinds to the head of the
record.  If the read object is corrupted, \fBea_next_object()\fR returns
\fBEO_ERROR\fR and records the extended accounting error code, accessible with
\fBea_error\fR(3EXACCT). If end-of-file is reached, \fBEO_ERROR\fR is returned
and the extended accounting error code is set to \fBEXR_EOF\fR.
.sp
.LP
The \fBea_previous_object()\fR function skips back one object in the file and
reads its basic fields (\fBeo_catalog\fR and \fBeo_type\fR) into the indicated
\fBea_object_t\fR.  If the read object is corrupted, \fBea_previous_object()\fR
returns \fBEO_ERROR\fR and records the extended accounting error code,
accessible with \fBea_error\fR(3EXACCT). If end-of-file is reached,
\fBEO_ERROR\fR is returned and the extended accounting error code is set to
\fBEXR_EOF\fR.
.sp
.LP
The \fBea_get_object()\fR function reads the value fields into the
\fBea_object_t\fR indicated by \fIobj\fR, allocating memory as necessary, and
advances to the head of the next record. Once a record group object is
retrieved using \fBea_get_object()\fR, subsequent calls to
\fBea_get_object()\fR and \fBea_next_object()\fR will track through the objects
within the record group, and on reaching the end of the group, will return the
next object at the same level as the group from the file. If the read object is
corrupted, \fBea_get_object()\fR returns \fBEO_ERROR\fR and records the
extended accounting error code, accessible with \fBea_error\fR(3EXACCT). If
end-of-file is reached, \fBEO_ERROR\fR is returned and the extended accounting
error code is set to \fBEXR_EOF\fR.
.sp
.LP
The \fBea_write_object()\fR function appends the given object to the open
\fBexacct\fR file indicated by \fIef\fR and returns 0. If the write fails,
\fBea_write_object()\fR returns \(mi1 and sets the extended accounting error
code to indicate the error, accessible with \fBea_error\fR(3EXACCT).
.sp
.LP
The \fBea_copy_object()\fR function copies an \fBea_object_t\fR. If the source
object is part of a chain, only the current object is copied. If the source
object is a group, only the group object is copied without its list of members
and the \fBeg_nobjs\fR and \fBeg_objs\fR fields are set to 0 and \fINULL\fR,
respectively. Use \fBea_copy_tree()\fR to copy recursively a group or a list of
items.
.sp
.LP
The \fBea_copy_object_tree()\fR function recursively copies an
\fBea_object_t\fR. All elements in the \fBeo_next\fR list are copied, and any
group objects are recursively copied. The returned object can be completely
freed with \fBea_free_object\fR(3EXACCT) by specifying the \fBEUP_ALLOC\fR
flag.
.sp
.LP
The \fBea_get_object_tree()\fR function reads in \fInobj\fR top-level objects
from the file, returning the same data structure that would have originally
been passed to \fBea_write_object()\fR. On encountering a group object, the
\fBea_get_object()\fR function reads only the group header part of the group,
whereas \fBea_get_object_tree()\fR reads the group and all its member items,
recursing into sub-records if necessary. The returned object data structure can
be completely freed with \fBea_free_object()\fR by specifying the
\fBEUP_ALLOC\fR flag.
.SH RETURN VALUES
.sp
.LP
The \fBea_pack_object()\fR function returns the number of bytes required to
hold the \fBexacct\fR object being operated upon. If the returned size exceeds
\fIbufsize\fR, the pack operation does not complete and the function returns
(\fBsize_t\fR) -1 and sets the extended accounting error code to indicate the
error.
.sp
.LP
The \fBea_get_object()\fR function returns the \fBea_object_type\fR of the
object if the object was retrieved successfully. Otherwise, it returns
\fBEO_ERROR\fR and sets the extended accounting error code to indicate the
error.
.sp
.LP
The \fBea_next_object()\fR function returns the \fBea_object_type\fR of the
next \fBexacct\fR object in the file.  It returns \fBEO_ERROR\fR if the
\fBexacct\fR file is corrupted sets the extended accounting error code to
indicate the error.
.sp
.LP
The \fBea_unpack_object()\fR function returns the \fBea_object_type\fR of the
first \fBexacct\fR object unpacked from the buffer. It returns \fBEO_ERROR\fR
if the exacct file is corrupted, and sets the extended accounting error code to
indicate the error.
.sp
.LP
The \fBea_write_object()\fR function returns 0 on success. Otherwise it returns
\(mi1 and sets the extended accounting error code to indicate the error.
.sp
.LP
The \fBea_copy_object()\fR and \fBea_copy_object_tree()\fR functions return the
copied object on success. Otherwise they return \fINULL\fR and set the extended
accounting error code to indicate the error.
.sp
.LP
The \fBea_get_object_tree()\fR function returns the list of objects read from
the file on success. Otherwise it returns \fINULL\fR and sets the extended
accounting error code to indicate the error.
.sp
.LP
The extended account error code can be retrieved using \fBea_error\fR(3EXACCT).
.SH ERRORS
.sp
.LP
These functions may fail if:
.sp
.ne 2
.na
\fB\fBEXR_SYSCALL_FAIL\fR\fR
.ad
.sp .6
.RS 4n
A system call invoked by the function failed. The \fBerrno\fR variable contains
the error value set by the underlying call. On memory allocation failure,
\fBerrno\fR will be set to \fBENOMEM\fR.
.RE

.sp
.ne 2
.na
\fB\fBEXR_CORRUPT_FILE\fR\fR
.ad
.sp .6
.RS 4n
The file referred to by \fIname\fR is not a valid \fBexacct\fR file, or is
unparsable, and therefore appears corrupted. This error is also used by
\fBea_unpack_buffer()\fR to indicate a corrupted buffer.
.RE

.sp
.ne 2
.na
\fB\fBEXR_EOF\fR\fR
.ad
.sp .6
.RS 4n
The end of the file has been reached.  In the case of
\fBea_previous_record()\fR, the previous record could not be reached, either
because the head of the file was encountered or because the previous record
could not be skipped over.
.RE

.SH USAGE
.sp
.LP
The \fBexacct\fR file format can be used to represent data other than that in
the extended accounting format.  By using a unique creator type in the file
header, application writers can develop their own format suited to the needs of
their application.
.SH EXAMPLES
.LP
\fBExample 1 \fROpen and close \fBexacct\fR file.
.sp
.LP
The following example opens the extended accounting data file for processes.
The \fBexacct\fR file is then closed.

.sp
.in +2
.nf
#include <stdio.h>
#include <exacct.h>

ea_file_t ef;
ea_object_t *obj;

\&...

ea_open(&ef, "foo", O_RDONLY, ...);

while ((obj = ea_get_object_tree(&ef, 1)) != NULL) {
    if (obj->eo_type == EO_ITEM) {
        /* handle item */
    } else {
        /* handle group */
    }
    ea_free_object(obj, EUP_ALLOC);
}

if (ea_error() != EXR_EOF) {
    /* handle error */
}

ea_close(&ef);
.fi
.in -2

.LP
\fBExample 2 \fRConstruct an \fBexacct\fR file consisting of a single object
containing the current process ID.
.sp
.in +2
.nf
#include <sys/types.h>
#include <unistd.h>
#include <exacct.h>

\&...

ea_file_t ef;
ea_object_t obj;
pid_t my_pid;

ea_open(&ef, "foo", O_CREAT | O_WRONLY, ...);

my_pid = getpid();
ea_set_item(&obj, EXT_UINT32 | EXC_DEFAULT | EXT_PROC_PID, &my_pid, 0);
(void) ea_write_object(&ef, &obj);

ea_close(&ef);

\&...
.fi
.in -2

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
.BR read (2),
.BR ea_error (3EXACCT),
.BR ea_open (3EXACCT),
.BR ea_set_item (3EXACCT),
.BR libexacct (3LIB),
.BR attributes (7)