10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3864Sraf * Common Development and Distribution License (the "License").
6*3864Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*3864Sraf
220Sstevel@tonic-gate /*
23*3864Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/systeminfo.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <exacct.h>
320Sstevel@tonic-gate #include <exacct_impl.h>
330Sstevel@tonic-gate #include <sys/exacct_impl.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <thread.h>
41*3864Sraf #include <pthread.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate #define EXACCT_HDR_STR "exacct"
440Sstevel@tonic-gate #define EXACCT_HDR_LEN 7
450Sstevel@tonic-gate
460Sstevel@tonic-gate #define DEFAULT_ENTRIES 4
470Sstevel@tonic-gate #define SYSINFO_BUFSIZE 256
480Sstevel@tonic-gate
49*3864Sraf static thread_key_t errkey = THR_ONCE_KEY;
500Sstevel@tonic-gate static int exacct_errval = 0;
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * extended accounting file access routines
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * exacct_ops.c implements the library-specific routines of libexacct: the
560Sstevel@tonic-gate * operations associated with file access and record traversal. (The
570Sstevel@tonic-gate * complementary routines which permit hierarchy building and record packing
580Sstevel@tonic-gate * are provided in exacct_core.c, which is used by both libexacct and the
590Sstevel@tonic-gate * kernel.) At its heart are the unpack, get, and next routines, which
600Sstevel@tonic-gate * navigate the packed records produced by ea_pack_object.
610Sstevel@tonic-gate */
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Group stack manipulation code. As groups can be nested, we need a mechanism
650Sstevel@tonic-gate * for saving and restoring the current position within the outer groups. This
660Sstevel@tonic-gate * state stack is stored within the ea_file_impl_t structure, in the ef_depth,
670Sstevel@tonic-gate * ef_ndeep and ef_mxdeep members. On error all these functions set
680Sstevel@tonic-gate * exacct_error and return -1.
690Sstevel@tonic-gate */
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * If the stack is NULL, create and initialise it.
730Sstevel@tonic-gate * If is is not NULL, check it still has space - if not, double its size.
740Sstevel@tonic-gate */
stack_check(ea_file_impl_t * f)750Sstevel@tonic-gate static int stack_check(ea_file_impl_t *f)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate if (f->ef_depth == NULL) {
780Sstevel@tonic-gate if ((f->ef_depth =
790Sstevel@tonic-gate ea_alloc(DEFAULT_ENTRIES * sizeof (ea_file_depth_t)))
800Sstevel@tonic-gate == NULL) {
810Sstevel@tonic-gate /* exacct_errno set above. */
820Sstevel@tonic-gate return (-1);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate bzero(f->ef_depth, DEFAULT_ENTRIES * sizeof (ea_file_depth_t));
850Sstevel@tonic-gate f->ef_mxdeep = DEFAULT_ENTRIES;
860Sstevel@tonic-gate f->ef_ndeep = -1;
870Sstevel@tonic-gate } else if (f->ef_ndeep + 1 >= f->ef_mxdeep) {
880Sstevel@tonic-gate ea_file_depth_t *newstack;
890Sstevel@tonic-gate
900Sstevel@tonic-gate if ((newstack =
910Sstevel@tonic-gate ea_alloc(f->ef_mxdeep * 2 * sizeof (ea_file_depth_t)))
920Sstevel@tonic-gate == NULL) {
930Sstevel@tonic-gate /* exacct_errno set above. */
940Sstevel@tonic-gate return (-1);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate bcopy(f->ef_depth, newstack,
970Sstevel@tonic-gate f->ef_mxdeep * sizeof (ea_file_depth_t));
980Sstevel@tonic-gate bzero(newstack + f->ef_mxdeep,
990Sstevel@tonic-gate f->ef_mxdeep * sizeof (ea_file_depth_t));
1000Sstevel@tonic-gate ea_free(f->ef_depth, f->ef_mxdeep * sizeof (ea_file_depth_t));
1010Sstevel@tonic-gate f->ef_mxdeep *= 2;
1020Sstevel@tonic-gate f->ef_depth = newstack;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * Free a stack.
1090Sstevel@tonic-gate */
stack_free(ea_file_impl_t * f)1100Sstevel@tonic-gate static void stack_free(ea_file_impl_t *f)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate if (f->ef_depth != NULL) {
1130Sstevel@tonic-gate ea_free(f->ef_depth, f->ef_mxdeep * sizeof (ea_file_depth_t));
1140Sstevel@tonic-gate f->ef_depth = NULL;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate f->ef_mxdeep = 0;
1170Sstevel@tonic-gate f->ef_ndeep = -1;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate * Add a new group onto the stack, pushing down one frame. nobj is the number
1220Sstevel@tonic-gate * of items in the group. We have to read this many objects before popping
1230Sstevel@tonic-gate * back up to an enclosing group - see next_object() and previous_object()
1240Sstevel@tonic-gate * below.
1250Sstevel@tonic-gate */
stack_new_group(ea_file_impl_t * f,int nobjs)1260Sstevel@tonic-gate static int stack_new_group(ea_file_impl_t *f, int nobjs)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate if (stack_check(f) != 0) {
1290Sstevel@tonic-gate stack_free(f);
1300Sstevel@tonic-gate /* exacct_errno set above. */
1310Sstevel@tonic-gate return (-1);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate f->ef_ndeep++;
1340Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_obj = 0;
1350Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_nobjs = nobjs;
1360Sstevel@tonic-gate return (0);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * Step forwards along the objects within the current group. If we are still
1410Sstevel@tonic-gate * within a group, return 1. If we have reached the end of the current group,
1420Sstevel@tonic-gate * unwind the stack back up to the nearest enclosing group that still has
1430Sstevel@tonic-gate * unprocessed objects and return 0. On EOF or error, set exacct_error
1440Sstevel@tonic-gate * accordingly and return -1. xread() is required so that this function can
1450Sstevel@tonic-gate * work either on files or memory buffers.
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate static int
stack_next_object(ea_file_impl_t * f,size_t (* xread)(ea_file_impl_t *,void *,size_t))1480Sstevel@tonic-gate stack_next_object(
1490Sstevel@tonic-gate ea_file_impl_t *f,
1500Sstevel@tonic-gate size_t (*xread)(ea_file_impl_t *, void *, size_t))
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate uint32_t scratch32;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate * If the stack is empty we are not in a group, so there will be no
1560Sstevel@tonic-gate * stack manipulation to do and no large backskips to step over.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate if (f->ef_ndeep < 0) {
1590Sstevel@tonic-gate return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * Otherwise we must be in a group. If there are objects left in the
1640Sstevel@tonic-gate * group, move onto the next one in the group and return.
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate if (++f->ef_depth[f->ef_ndeep].efd_obj <
1670Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_nobjs) {
1680Sstevel@tonic-gate return (1);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * If we are at the end of a group we need to move backwards up the
1720Sstevel@tonic-gate * stack, consuming the large backskips as we go, until we find a group
1730Sstevel@tonic-gate * that still contains unprocessed items, or until we have unwound back
1740Sstevel@tonic-gate * off the bottom of the stack (i.e. out of all the groups).
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate } else {
1770Sstevel@tonic-gate while (f->ef_ndeep >= 0 &&
1780Sstevel@tonic-gate ++f->ef_depth[f->ef_ndeep].efd_obj >=
1790Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_nobjs) {
1800Sstevel@tonic-gate /* Read the large backskip. */
1810Sstevel@tonic-gate f->ef_ndeep--;
1820Sstevel@tonic-gate if (xread(f, &scratch32, sizeof (scratch32)) !=
1830Sstevel@tonic-gate sizeof (scratch32)) {
1840Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
1850Sstevel@tonic-gate return (-1);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate return (0);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate * Step backwards along the objects within the current group. If we are still
1940Sstevel@tonic-gate * within a group, return 1. If we have reached the end of the current group,
1950Sstevel@tonic-gate * unwind the stack back up to the enclosing group and return 0.
1960Sstevel@tonic-gate */
stack_previous_object(ea_file_impl_t * f)1970Sstevel@tonic-gate static int stack_previous_object(ea_file_impl_t *f)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * If the stack is empty we are not in a group, so there will be no
2010Sstevel@tonic-gate * stack manipulation to do.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate if (f->ef_ndeep < 0) {
2040Sstevel@tonic-gate return (0);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * Otherwise we must be in a group. If there are objects left in the
2090Sstevel@tonic-gate * group, move onto the previous one in the group and return.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate if (--f->ef_depth[f->ef_ndeep].efd_obj >= 0) {
2120Sstevel@tonic-gate return (1);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* Otherwise, step one level back up the group stack. */
2150Sstevel@tonic-gate } else {
2160Sstevel@tonic-gate f->ef_ndeep--;
2170Sstevel@tonic-gate return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * read/seek/pos virtualisation wrappers. Because objects can come either from
2230Sstevel@tonic-gate * a file or memory, the read/seek/pos functions need to be wrapped to allow
2240Sstevel@tonic-gate * them to be used on either a file handle or a memory buffer.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate static size_t
fread_wrapper(ea_file_impl_t * f,void * buf,size_t sz)2280Sstevel@tonic-gate fread_wrapper(ea_file_impl_t *f, void *buf, size_t sz)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate size_t retval;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate retval = fread(buf, 1, sz, f->ef_fp);
2330Sstevel@tonic-gate if (retval == 0 && ferror(f->ef_fp)) {
2340Sstevel@tonic-gate retval = (size_t)-1;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate return (retval);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate static size_t
bufread_wrapper(ea_file_impl_t * f,void * buf,size_t sz)2400Sstevel@tonic-gate bufread_wrapper(ea_file_impl_t *f, void *buf, size_t sz)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate if (f->ef_bufsize == 0 && sz != 0)
2430Sstevel@tonic-gate return ((size_t)0);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (f->ef_bufsize < sz)
2460Sstevel@tonic-gate sz = f->ef_bufsize;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate bcopy(f->ef_buf, buf, sz);
2490Sstevel@tonic-gate f->ef_buf += sz;
2500Sstevel@tonic-gate f->ef_bufsize -= sz;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate return (sz);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate static off_t
fseek_wrapper(ea_file_impl_t * f,off_t adv)2560Sstevel@tonic-gate fseek_wrapper(ea_file_impl_t *f, off_t adv)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate return (fseeko(f->ef_fp, adv, SEEK_CUR));
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate static off_t
bufseek_wrapper(ea_file_impl_t * f,off_t adv)2620Sstevel@tonic-gate bufseek_wrapper(ea_file_impl_t *f, off_t adv)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate if (f->ef_bufsize == 0 && adv != 0)
2650Sstevel@tonic-gate return (-1);
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (f->ef_bufsize < adv)
2680Sstevel@tonic-gate adv = f->ef_bufsize;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate f->ef_buf += adv;
2710Sstevel@tonic-gate f->ef_bufsize -= adv;
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate return (0);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate /*ARGSUSED*/
2770Sstevel@tonic-gate static void *
fpos_wrapper(ea_file_impl_t * f)2780Sstevel@tonic-gate fpos_wrapper(ea_file_impl_t *f)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate return (NULL);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate static void *
bufpos_wrapper(ea_file_impl_t * f)2840Sstevel@tonic-gate bufpos_wrapper(ea_file_impl_t *f)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate return (f->ef_buf);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate * Public API
2910Sstevel@tonic-gate */
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate void
exacct_seterr(int errval)2940Sstevel@tonic-gate exacct_seterr(int errval)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate if (thr_main()) {
2970Sstevel@tonic-gate exacct_errval = errval;
2980Sstevel@tonic-gate return;
2990Sstevel@tonic-gate }
300*3864Sraf (void) thr_keycreate_once(&errkey, 0);
3010Sstevel@tonic-gate (void) thr_setspecific(errkey, (void *)(intptr_t)errval);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate int
ea_error(void)3050Sstevel@tonic-gate ea_error(void)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate if (thr_main())
3080Sstevel@tonic-gate return (exacct_errval);
309*3864Sraf if (errkey == THR_ONCE_KEY)
3100Sstevel@tonic-gate return (EXR_OK);
311*3864Sraf return ((int)(uintptr_t)pthread_getspecific(errkey));
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * ea_next_object(), ea_previous_object(), and ea_get_object() are written such
3160Sstevel@tonic-gate * that the file cursor is always located on an object boundary.
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate ea_object_type_t
ea_next_object(ea_file_t * ef,ea_object_t * obj)3190Sstevel@tonic-gate ea_next_object(ea_file_t *ef, ea_object_t *obj)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
3220Sstevel@tonic-gate ea_size_t len;
3230Sstevel@tonic-gate off_t backup;
3240Sstevel@tonic-gate size_t ret;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * If ef_advance is zero, then we are executing after a get or previous
3280Sstevel@tonic-gate * operation and do not move to the next or previous object. Otherwise,
3290Sstevel@tonic-gate * advance to the next available item. Note that ef_advance does NOT
3300Sstevel@tonic-gate * include the large backskip at the end of a object, this being dealt
3310Sstevel@tonic-gate * with by the depth stack handling in stack_next_object.
3320Sstevel@tonic-gate */
3330Sstevel@tonic-gate if (f->ef_advance != 0) {
3340Sstevel@tonic-gate if (fseeko(f->ef_fp, (off_t)f->ef_advance, SEEK_CUR) == -1) {
3350Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
3360Sstevel@tonic-gate return (EO_ERROR);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate if (stack_next_object(f, fread_wrapper) == -1) {
3390Sstevel@tonic-gate /* exacct_error set above. */
3400Sstevel@tonic-gate return (EO_ERROR);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate f->ef_advance = 0;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate /* Read the catalog tag */
3460Sstevel@tonic-gate ret = fread(&obj->eo_catalog, 1, sizeof (ea_catalog_t), f->ef_fp);
3470Sstevel@tonic-gate if (ret == 0) {
3480Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF);
3490Sstevel@tonic-gate return (EO_ERROR);
3500Sstevel@tonic-gate } else if (ret < sizeof (ea_catalog_t)) {
3510Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
3520Sstevel@tonic-gate return (EO_ERROR);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate exacct_order32(&obj->eo_catalog);
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate backup = sizeof (ea_catalog_t);
3570Sstevel@tonic-gate obj->eo_type = EO_ITEM;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /* Figure out the offset to just before the large backskip. */
3600Sstevel@tonic-gate switch (obj->eo_catalog & EXT_TYPE_MASK) {
3610Sstevel@tonic-gate case EXT_GROUP:
3620Sstevel@tonic-gate obj->eo_type = EO_GROUP;
3630Sstevel@tonic-gate f->ef_advance = sizeof (uint32_t);
3640Sstevel@tonic-gate /* FALLTHROUGH */
3650Sstevel@tonic-gate case EXT_STRING:
3660Sstevel@tonic-gate case EXT_EXACCT_OBJECT:
3670Sstevel@tonic-gate case EXT_RAW:
3680Sstevel@tonic-gate if (fread(&len, 1, sizeof (ea_size_t), f->ef_fp)
3690Sstevel@tonic-gate < sizeof (ea_size_t)) {
3700Sstevel@tonic-gate obj->eo_type = EO_NONE;
3710Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
3720Sstevel@tonic-gate return (EO_ERROR);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate exacct_order64(&len);
3750Sstevel@tonic-gate /* Note: len already includes the size of the backskip. */
3760Sstevel@tonic-gate f->ef_advance += sizeof (ea_catalog_t) +
3770Sstevel@tonic-gate sizeof (ea_size_t) + len;
3780Sstevel@tonic-gate backup += sizeof (ea_size_t);
3790Sstevel@tonic-gate break;
3800Sstevel@tonic-gate case EXT_UINT8:
3810Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint8_t) +
3820Sstevel@tonic-gate sizeof (uint32_t);
3830Sstevel@tonic-gate break;
3840Sstevel@tonic-gate case EXT_UINT16:
3850Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint16_t) +
3860Sstevel@tonic-gate sizeof (uint32_t);
3870Sstevel@tonic-gate break;
3880Sstevel@tonic-gate case EXT_UINT32:
3890Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint32_t) +
3900Sstevel@tonic-gate sizeof (uint32_t);
3910Sstevel@tonic-gate break;
3920Sstevel@tonic-gate case EXT_UINT64:
3930Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint64_t) +
3940Sstevel@tonic-gate sizeof (uint32_t);
3950Sstevel@tonic-gate break;
3960Sstevel@tonic-gate case EXT_DOUBLE:
3970Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (double) +
3980Sstevel@tonic-gate sizeof (uint32_t);
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate default:
4010Sstevel@tonic-gate obj->eo_type = EO_NONE;
4020Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
4030Sstevel@tonic-gate return (EO_ERROR);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /* Reposition to the start of this object. */
4070Sstevel@tonic-gate if (fseeko(f->ef_fp, -backup, SEEK_CUR) == -1) {
4080Sstevel@tonic-gate obj->eo_type = EO_NONE;
4090Sstevel@tonic-gate f->ef_advance = 0;
4100Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
4110Sstevel@tonic-gate return (EO_ERROR);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
4150Sstevel@tonic-gate return (obj->eo_type);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate ea_object_type_t
ea_previous_object(ea_file_t * ef,ea_object_t * obj)4190Sstevel@tonic-gate ea_previous_object(ea_file_t *ef, ea_object_t *obj)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
4220Sstevel@tonic-gate uint32_t bkskip;
4230Sstevel@tonic-gate int r;
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate if (fseeko(f->ef_fp, -((off_t)sizeof (uint32_t)), SEEK_CUR) == -1) {
4260Sstevel@tonic-gate if (errno == EINVAL) {
4270Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF);
4280Sstevel@tonic-gate } else {
4290Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate return (EO_ERROR);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate if ((r = fread(&bkskip, 1, sizeof (uint32_t), f->ef_fp)) !=
4350Sstevel@tonic-gate sizeof (uint32_t)) {
4360Sstevel@tonic-gate if (r == 0) {
4370Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF);
4380Sstevel@tonic-gate } else {
4390Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate return (EO_ERROR);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate exacct_order32(&bkskip);
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate * A backskip of 0 means that the current record can't be skipped over.
4470Sstevel@tonic-gate * This will be true for the header record, and for records longer than
4480Sstevel@tonic-gate * 2^32.
4490Sstevel@tonic-gate */
4500Sstevel@tonic-gate if (bkskip == 0) {
4510Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF);
4520Sstevel@tonic-gate return (EO_ERROR);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate (void) stack_previous_object(f);
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate if (fseeko(f->ef_fp, -((off_t)bkskip), SEEK_CUR) == -1) {
4570Sstevel@tonic-gate if (errno == EINVAL) {
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate * If we attempted to seek past BOF, then the file was
4600Sstevel@tonic-gate * corrupt, as we can only trust the backskip we read.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
4630Sstevel@tonic-gate } else {
4640Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate return (EO_ERROR);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate f->ef_advance = 0;
4700Sstevel@tonic-gate return (ea_next_object(ef, obj));
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * xget_object() contains the logic for extracting an individual object from a
4750Sstevel@tonic-gate * packed buffer, which it consumes using xread() and xseek() operations
4760Sstevel@tonic-gate * provided by the caller. flags may be set to either EUP_ALLOC, in which case
4770Sstevel@tonic-gate * new memory is allocated for the variable length items unpacked, or
4780Sstevel@tonic-gate * EUP_NOALLOC, in which case item data pointer indicate locations within the
4790Sstevel@tonic-gate * buffer, using the provided xpos() function. EUP_NOALLOC is generally not
4800Sstevel@tonic-gate * useful for callers representing interaction with actual file streams, and
4810Sstevel@tonic-gate * should not be specified thereby.
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate static ea_object_type_t
xget_object(ea_file_impl_t * f,ea_object_t * obj,size_t (* xread)(ea_file_impl_t *,void *,size_t),off_t (* xseek)(ea_file_impl_t *,off_t),void * (* xpos)(ea_file_impl_t *),int flags)4840Sstevel@tonic-gate xget_object(
4850Sstevel@tonic-gate ea_file_impl_t *f,
4860Sstevel@tonic-gate ea_object_t *obj,
4870Sstevel@tonic-gate size_t (*xread)(ea_file_impl_t *, void *, size_t),
4880Sstevel@tonic-gate off_t (*xseek)(ea_file_impl_t *, off_t),
4890Sstevel@tonic-gate void *(*xpos)(ea_file_impl_t *),
4900Sstevel@tonic-gate int flags)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate ea_size_t sz;
4930Sstevel@tonic-gate uint32_t gp_backskip, scratch32;
4940Sstevel@tonic-gate void *buf;
4950Sstevel@tonic-gate size_t r;
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate /* Read the catalog tag. */
4980Sstevel@tonic-gate if ((r = xread(f, &obj->eo_catalog, sizeof (ea_catalog_t))) == 0) {
4990Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF);
5000Sstevel@tonic-gate return (EO_ERROR);
5010Sstevel@tonic-gate } else if (r != sizeof (ea_catalog_t)) {
5020Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5030Sstevel@tonic-gate return (EO_ERROR);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate exacct_order32(&obj->eo_catalog);
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * If this is a record group, we treat it separately: only record
5090Sstevel@tonic-gate * groups cause us to allocate new depth frames.
5100Sstevel@tonic-gate */
5110Sstevel@tonic-gate if ((obj->eo_catalog & EXT_TYPE_MASK) == EXT_GROUP) {
5120Sstevel@tonic-gate obj->eo_type = EO_GROUP;
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate /* Read size field, and number of objects. */
5150Sstevel@tonic-gate if (xread(f, &sz, sizeof (ea_size_t)) != sizeof (ea_size_t)) {
5160Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5170Sstevel@tonic-gate return (EO_ERROR);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate exacct_order64(&sz);
5200Sstevel@tonic-gate if (xread(f, &obj->eo_group.eg_nobjs, sizeof (uint32_t)) !=
5210Sstevel@tonic-gate sizeof (uint32_t)) {
5220Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5230Sstevel@tonic-gate return (EO_ERROR);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate exacct_order32(&obj->eo_group.eg_nobjs);
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate /* Now read the group's small backskip. */
5280Sstevel@tonic-gate if (xread(f, &gp_backskip, sizeof (uint32_t)) !=
5290Sstevel@tonic-gate sizeof (uint32_t)) {
5300Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5310Sstevel@tonic-gate return (EO_ERROR);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate /* Push a new depth stack frame. */
5350Sstevel@tonic-gate if (stack_new_group(f, obj->eo_group.eg_nobjs) != 0) {
5360Sstevel@tonic-gate /* exacct_error set above */
5370Sstevel@tonic-gate return (EO_ERROR);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * If the group has no items, we now need to position to the
5420Sstevel@tonic-gate * end of the group, because there will be no subsequent calls
5430Sstevel@tonic-gate * to process the group, it being empty.
5440Sstevel@tonic-gate */
5450Sstevel@tonic-gate if (obj->eo_group.eg_nobjs == 0) {
5460Sstevel@tonic-gate if (stack_next_object(f, xread) == -1) {
5470Sstevel@tonic-gate /* exacct_error set above. */
5480Sstevel@tonic-gate return (EO_ERROR);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate f->ef_advance = 0;
5530Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
5540Sstevel@tonic-gate return (obj->eo_type);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate * Otherwise we are reading an item.
5590Sstevel@tonic-gate */
5600Sstevel@tonic-gate obj->eo_type = EO_ITEM;
5610Sstevel@tonic-gate switch (obj->eo_catalog & EXT_TYPE_MASK) {
5620Sstevel@tonic-gate case EXT_STRING:
5630Sstevel@tonic-gate case EXT_EXACCT_OBJECT:
5640Sstevel@tonic-gate case EXT_RAW:
5650Sstevel@tonic-gate if (xread(f, &sz, sizeof (ea_size_t)) != sizeof (ea_size_t)) {
5660Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5670Sstevel@tonic-gate return (EO_ERROR);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate exacct_order64(&sz);
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate * Subtract backskip value from size.
5720Sstevel@tonic-gate */
5730Sstevel@tonic-gate sz -= sizeof (uint32_t);
5740Sstevel@tonic-gate if ((flags & EUP_ALLOC_MASK) == EUP_NOALLOC) {
5750Sstevel@tonic-gate buf = xpos(f);
5760Sstevel@tonic-gate if (xseek(f, sz) == -1) {
5770Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5780Sstevel@tonic-gate return (EO_ERROR);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate } else {
5810Sstevel@tonic-gate if ((buf = ea_alloc(sz)) == NULL)
5820Sstevel@tonic-gate /* exacct_error set above. */
5830Sstevel@tonic-gate return (EO_ERROR);
5840Sstevel@tonic-gate if (xread(f, buf, sz) != sz) {
5851414Scindi ea_free(buf, sz);
5860Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
5870Sstevel@tonic-gate return (EO_ERROR);
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate obj->eo_item.ei_string = buf;
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate * Maintain our consistent convention that string lengths
5930Sstevel@tonic-gate * include the terminating NULL character.
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate obj->eo_item.ei_size = sz;
5960Sstevel@tonic-gate break;
5970Sstevel@tonic-gate case EXT_UINT8:
5980Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint8, sizeof (uint8_t)) !=
5990Sstevel@tonic-gate sizeof (uint8_t)) {
6000Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6010Sstevel@tonic-gate return (EO_ERROR);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint8_t);
6040Sstevel@tonic-gate break;
6050Sstevel@tonic-gate case EXT_UINT16:
6060Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint16, sizeof (uint16_t)) !=
6070Sstevel@tonic-gate sizeof (uint16_t)) {
6080Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6090Sstevel@tonic-gate return (EO_ERROR);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate exacct_order16(&obj->eo_item.ei_uint16);
6120Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint16_t);
6130Sstevel@tonic-gate break;
6140Sstevel@tonic-gate case EXT_UINT32:
6150Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint32, sizeof (uint32_t)) !=
6160Sstevel@tonic-gate sizeof (uint32_t)) {
6170Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6180Sstevel@tonic-gate return (EO_ERROR);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate exacct_order32(&obj->eo_item.ei_uint32);
6210Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint32_t);
6220Sstevel@tonic-gate break;
6230Sstevel@tonic-gate case EXT_UINT64:
6240Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint64, sizeof (uint64_t)) !=
6250Sstevel@tonic-gate sizeof (uint64_t)) {
6260Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6270Sstevel@tonic-gate return (EO_ERROR);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate exacct_order64(&obj->eo_item.ei_uint64);
6300Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint64_t);
6310Sstevel@tonic-gate break;
6320Sstevel@tonic-gate case EXT_DOUBLE:
6330Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_double, sizeof (double)) !=
6340Sstevel@tonic-gate sizeof (double)) {
6350Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6360Sstevel@tonic-gate return (EO_ERROR);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate exacct_order64((uint64_t *)&obj->eo_item.ei_double);
6390Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (double);
6400Sstevel@tonic-gate break;
6410Sstevel@tonic-gate default:
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate * We've encountered an unknown type value. Flag the error and
6440Sstevel@tonic-gate * exit.
6450Sstevel@tonic-gate */
6460Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6470Sstevel@tonic-gate return (EO_ERROR);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate * Advance over current large backskip value,
6520Sstevel@tonic-gate * and position at the start of the next object.
6530Sstevel@tonic-gate */
6540Sstevel@tonic-gate if (xread(f, &scratch32, sizeof (scratch32)) != sizeof (scratch32)) {
6550Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
6560Sstevel@tonic-gate return (EO_ERROR);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate if (stack_next_object(f, xread) == -1) {
6590Sstevel@tonic-gate /* exacct_error set above. */
6600Sstevel@tonic-gate return (EO_ERROR);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate f->ef_advance = 0;
6640Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
6650Sstevel@tonic-gate return (obj->eo_type);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate ea_object_type_t
ea_get_object(ea_file_t * ef,ea_object_t * obj)6690Sstevel@tonic-gate ea_get_object(ea_file_t *ef, ea_object_t *obj)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate obj->eo_next = NULL;
6720Sstevel@tonic-gate return (xget_object((ea_file_impl_t *)ef, obj, fread_wrapper,
6730Sstevel@tonic-gate fseek_wrapper, fpos_wrapper, EUP_ALLOC));
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate * unpack_group() recursively unpacks record groups from the buffer tucked
6780Sstevel@tonic-gate * within the passed ea_file, and attaches them to grp.
6790Sstevel@tonic-gate */
6800Sstevel@tonic-gate static int
unpack_group(ea_file_impl_t * f,ea_object_t * grp,int flag)6810Sstevel@tonic-gate unpack_group(ea_file_impl_t *f, ea_object_t *grp, int flag)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate ea_object_t *obj;
6840Sstevel@tonic-gate uint_t nobjs = grp->eo_group.eg_nobjs;
6850Sstevel@tonic-gate int i;
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate * Set the group's object count to zero, as we will rebuild it via the
6890Sstevel@tonic-gate * individual object attachments.
6900Sstevel@tonic-gate */
6910Sstevel@tonic-gate grp->eo_group.eg_nobjs = 0;
6920Sstevel@tonic-gate grp->eo_group.eg_objs = NULL;
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate for (i = 0; i < nobjs; i++) {
6950Sstevel@tonic-gate if ((obj = ea_alloc(sizeof (ea_object_t))) == NULL) {
6960Sstevel@tonic-gate /* exacct_errno set above. */
6970Sstevel@tonic-gate return (-1);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate obj->eo_next = NULL;
7000Sstevel@tonic-gate if (xget_object(f, obj, bufread_wrapper, bufseek_wrapper,
7010Sstevel@tonic-gate bufpos_wrapper, flag) == -1) {
7020Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t));
7030Sstevel@tonic-gate /* exacct_errno set above. */
7040Sstevel@tonic-gate return (-1);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate (void) ea_attach_to_group(grp, obj);
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate if (obj->eo_type == EO_GROUP &&
7100Sstevel@tonic-gate unpack_group(f, obj, flag) == -1) {
7110Sstevel@tonic-gate /* exacct_errno set above. */
7120Sstevel@tonic-gate return (-1);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate if (nobjs != grp->eo_group.eg_nobjs) {
7170Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE);
7180Sstevel@tonic-gate return (-1);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
7210Sstevel@tonic-gate return (0);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate * ea_unpack_object() can be considered as a finite series of get operations on
7260Sstevel@tonic-gate * a given buffer, that rebuilds the hierarchy of objects compacted by a pack
7270Sstevel@tonic-gate * operation. Because there is complex state associated with the group depth,
7280Sstevel@tonic-gate * ea_unpack_object() must complete as one operation on a given buffer.
7290Sstevel@tonic-gate */
7300Sstevel@tonic-gate ea_object_type_t
ea_unpack_object(ea_object_t ** objp,int flag,void * buf,size_t bufsize)7310Sstevel@tonic-gate ea_unpack_object(ea_object_t **objp, int flag, void *buf, size_t bufsize)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate ea_file_impl_t fake;
7340Sstevel@tonic-gate ea_object_t *obj;
7350Sstevel@tonic-gate ea_object_type_t first_obj_type;
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate *objp = NULL;
7380Sstevel@tonic-gate if (buf == NULL) {
7390Sstevel@tonic-gate EXACCT_SET_ERR(EXR_INVALID_BUF);
7400Sstevel@tonic-gate return (EO_ERROR);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate /* Set up the structures needed for unpacking */
7440Sstevel@tonic-gate bzero(&fake, sizeof (ea_file_impl_t));
7450Sstevel@tonic-gate if (stack_check(&fake) == -1) {
7460Sstevel@tonic-gate /* exacct_errno set above. */
7470Sstevel@tonic-gate return (EO_ERROR);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate fake.ef_buf = buf;
7500Sstevel@tonic-gate fake.ef_bufsize = bufsize;
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate /* Unpack the first object in the buffer - this should succeed. */
7530Sstevel@tonic-gate if ((obj = ea_alloc(sizeof (ea_object_t))) == NULL) {
7540Sstevel@tonic-gate stack_free(&fake);
7550Sstevel@tonic-gate /* exacct_errno set above. */
7560Sstevel@tonic-gate return (EO_ERROR);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate obj->eo_next = NULL;
7590Sstevel@tonic-gate if ((first_obj_type = xget_object(&fake, obj, bufread_wrapper,
7600Sstevel@tonic-gate bufseek_wrapper, bufpos_wrapper, flag)) == -1) {
7610Sstevel@tonic-gate stack_free(&fake);
7620Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t));
7630Sstevel@tonic-gate /* exacct_errno set above. */
7640Sstevel@tonic-gate return (EO_ERROR);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate if (obj->eo_type == EO_GROUP && unpack_group(&fake, obj, flag) == -1) {
7680Sstevel@tonic-gate stack_free(&fake);
7690Sstevel@tonic-gate ea_free_object(obj, flag);
7700Sstevel@tonic-gate /* exacct_errno set above. */
7710Sstevel@tonic-gate return (EO_ERROR);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate *objp = obj;
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate * There may be other objects in the buffer - if so, chain them onto
7770Sstevel@tonic-gate * the end of the list. We have reached the end of the list when
7780Sstevel@tonic-gate * xget_object() returns -1 with exacct_error set to EXR_EOF.
7790Sstevel@tonic-gate */
7800Sstevel@tonic-gate for (;;) {
7810Sstevel@tonic-gate if ((obj = ea_alloc(sizeof (ea_object_t))) == NULL) {
7820Sstevel@tonic-gate stack_free(&fake);
7830Sstevel@tonic-gate ea_free_object(*objp, flag);
7840Sstevel@tonic-gate *objp = NULL;
7850Sstevel@tonic-gate /* exacct_errno set above. */
7860Sstevel@tonic-gate return (EO_ERROR);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate obj->eo_next = NULL;
7890Sstevel@tonic-gate if (xget_object(&fake, obj, bufread_wrapper, bufseek_wrapper,
7900Sstevel@tonic-gate bufpos_wrapper, flag) == -1) {
7910Sstevel@tonic-gate stack_free(&fake);
7920Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t));
7930Sstevel@tonic-gate if (ea_error() == EXR_EOF) {
7940Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
7950Sstevel@tonic-gate return (first_obj_type);
7960Sstevel@tonic-gate } else {
7970Sstevel@tonic-gate ea_free_object(*objp, flag);
7980Sstevel@tonic-gate *objp = NULL;
7990Sstevel@tonic-gate /* exacct_error set above. */
8000Sstevel@tonic-gate return (EO_ERROR);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate (void) ea_attach_to_object(*objp, obj);
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate if (obj->eo_type == EO_GROUP &&
8070Sstevel@tonic-gate unpack_group(&fake, obj, flag) == -1) {
8080Sstevel@tonic-gate stack_free(&fake);
8090Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t));
8100Sstevel@tonic-gate ea_free_object(*objp, flag);
8110Sstevel@tonic-gate *objp = NULL;
8120Sstevel@tonic-gate /* exacct_errno set above. */
8130Sstevel@tonic-gate return (EO_ERROR);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate int
ea_write_object(ea_file_t * ef,ea_object_t * obj)8190Sstevel@tonic-gate ea_write_object(ea_file_t *ef, ea_object_t *obj)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate ea_size_t sz;
8220Sstevel@tonic-gate void *buf;
8230Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate * If we weren't opened for writing, this call fails.
8270Sstevel@tonic-gate */
8280Sstevel@tonic-gate if ((f->ef_oflags & O_RDWR) == 0 &&
8290Sstevel@tonic-gate (f->ef_oflags & O_WRONLY) == 0) {
8300Sstevel@tonic-gate EXACCT_SET_ERR(EXR_NOTSUPP);
8310Sstevel@tonic-gate return (-1);
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate /* Pack with a null buffer to get the size. */
8350Sstevel@tonic-gate sz = ea_pack_object(obj, NULL, 0);
8360Sstevel@tonic-gate if (sz == -1 || (buf = ea_alloc(sz)) == NULL) {
8370Sstevel@tonic-gate /* exacct_error set above. */
8380Sstevel@tonic-gate return (-1);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate if (ea_pack_object(obj, buf, sz) == (size_t)-1) {
8410Sstevel@tonic-gate ea_free(buf, sz);
8420Sstevel@tonic-gate /* exacct_error set above. */
8430Sstevel@tonic-gate return (-1);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate if (fwrite(buf, sizeof (char), sz, f->ef_fp) != sz) {
8460Sstevel@tonic-gate ea_free(buf, sz);
8470Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
8480Sstevel@tonic-gate return (-1);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate ea_free(buf, sz);
8510Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
8520Sstevel@tonic-gate return (0);
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /*
8560Sstevel@tonic-gate * validate_header() must be kept in sync with write_header(), given below, and
8570Sstevel@tonic-gate * exacct_create_header(), in uts/common/os/exacct.c.
8580Sstevel@tonic-gate */
8590Sstevel@tonic-gate static int
validate_header(ea_file_t * ef,const char * creator)8600Sstevel@tonic-gate validate_header(ea_file_t *ef, const char *creator)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate ea_object_t hdr_grp;
8630Sstevel@tonic-gate ea_object_t scratch_obj;
8640Sstevel@tonic-gate int error = EXR_OK;
8650Sstevel@tonic-gate int saw_creator = 0;
8660Sstevel@tonic-gate int saw_version = 0;
8670Sstevel@tonic-gate int saw_type = 0;
8680Sstevel@tonic-gate int saw_hostname = 0;
8690Sstevel@tonic-gate int n;
8700Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate bzero(&hdr_grp, sizeof (ea_object_t));
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate if (ea_get_object(ef, &hdr_grp) != EO_GROUP) {
8750Sstevel@tonic-gate error = ea_error();
8760Sstevel@tonic-gate goto error_case;
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate
8790Sstevel@tonic-gate if (hdr_grp.eo_catalog !=
8800Sstevel@tonic-gate (EXT_GROUP | EXC_DEFAULT | EXD_GROUP_HEADER)) {
8810Sstevel@tonic-gate error = EXR_CORRUPT_FILE;
8820Sstevel@tonic-gate goto error_case;
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate for (n = 0; n < hdr_grp.eo_group.eg_nobjs; n++) {
8860Sstevel@tonic-gate bzero(&scratch_obj, sizeof (ea_object_t));
8870Sstevel@tonic-gate if (ea_get_object(ef, &scratch_obj) == -1) {
8880Sstevel@tonic-gate error = ea_error();
8890Sstevel@tonic-gate goto error_case;
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate switch (scratch_obj.eo_catalog) {
8930Sstevel@tonic-gate case EXT_UINT32 | EXC_DEFAULT | EXD_VERSION:
8940Sstevel@tonic-gate if (scratch_obj.eo_item.ei_uint32 != EXACCT_VERSION) {
8950Sstevel@tonic-gate error = EXR_UNKN_VERSION;
8960Sstevel@tonic-gate goto error_case;
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate saw_version++;
8990Sstevel@tonic-gate break;
9000Sstevel@tonic-gate case EXT_STRING | EXC_DEFAULT | EXD_FILETYPE:
9010Sstevel@tonic-gate if (strcmp(scratch_obj.eo_item.ei_string,
9020Sstevel@tonic-gate EXACCT_HDR_STR) != 0) {
9030Sstevel@tonic-gate error = EXR_CORRUPT_FILE;
9040Sstevel@tonic-gate goto error_case;
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate saw_type++;
9070Sstevel@tonic-gate break;
9080Sstevel@tonic-gate case EXT_STRING | EXC_DEFAULT | EXD_CREATOR:
9090Sstevel@tonic-gate f->ef_creator =
9100Sstevel@tonic-gate ea_strdup(scratch_obj.eo_item.ei_string);
9110Sstevel@tonic-gate if (f->ef_creator == NULL) {
9120Sstevel@tonic-gate error = ea_error();
9130Sstevel@tonic-gate goto error_case;
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate saw_creator++;
9160Sstevel@tonic-gate break;
9170Sstevel@tonic-gate /* The hostname is an optional field. */
9180Sstevel@tonic-gate case EXT_STRING | EXC_DEFAULT | EXD_HOSTNAME:
9190Sstevel@tonic-gate f->ef_hostname =
9200Sstevel@tonic-gate ea_strdup(scratch_obj.eo_item.ei_string);
9210Sstevel@tonic-gate if (f->ef_hostname == NULL) {
9220Sstevel@tonic-gate error = ea_error();
9230Sstevel@tonic-gate goto error_case;
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate saw_hostname++;
9260Sstevel@tonic-gate break;
9270Sstevel@tonic-gate default:
9280Sstevel@tonic-gate /* ignore unrecognized header members */
9290Sstevel@tonic-gate break;
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate (void) ea_free_item(&scratch_obj, EUP_ALLOC);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate if (saw_version && saw_type && saw_creator) {
9350Sstevel@tonic-gate if (creator && strcmp(f->ef_creator, creator) != 0) {
9360Sstevel@tonic-gate error = EXR_NO_CREATOR;
9370Sstevel@tonic-gate goto error_case;
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
9400Sstevel@tonic-gate return (0);
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate error_case:
9440Sstevel@tonic-gate (void) ea_free_item(&scratch_obj, EUP_ALLOC);
9450Sstevel@tonic-gate if (saw_hostname)
9460Sstevel@tonic-gate ea_strfree(f->ef_hostname);
9470Sstevel@tonic-gate if (saw_creator)
9480Sstevel@tonic-gate ea_strfree(f->ef_creator);
9490Sstevel@tonic-gate EXACCT_SET_ERR(error);
9500Sstevel@tonic-gate return (-1);
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate static int
write_header(ea_file_t * ef)9540Sstevel@tonic-gate write_header(ea_file_t *ef)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate ea_object_t hdr_grp;
9570Sstevel@tonic-gate ea_object_t vers_obj;
9580Sstevel@tonic-gate ea_object_t creator_obj;
9590Sstevel@tonic-gate ea_object_t filetype_obj;
9600Sstevel@tonic-gate ea_object_t hostname_obj;
9610Sstevel@tonic-gate uint32_t bskip;
9620Sstevel@tonic-gate const uint32_t version = EXACCT_VERSION;
9630Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
9640Sstevel@tonic-gate void *buf;
9650Sstevel@tonic-gate size_t bufsize;
9660Sstevel@tonic-gate char hostbuf[SYSINFO_BUFSIZE];
9670Sstevel@tonic-gate int error = EXR_OK;
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate bzero(&hdr_grp, sizeof (ea_object_t));
9700Sstevel@tonic-gate bzero(&vers_obj, sizeof (ea_object_t));
9710Sstevel@tonic-gate bzero(&creator_obj, sizeof (ea_object_t));
9720Sstevel@tonic-gate bzero(&filetype_obj, sizeof (ea_object_t));
9730Sstevel@tonic-gate bzero(&hostname_obj, sizeof (ea_object_t));
9740Sstevel@tonic-gate bzero(hostbuf, SYSINFO_BUFSIZE);
9750Sstevel@tonic-gate
9760Sstevel@tonic-gate (void) sysinfo(SI_HOSTNAME, hostbuf, SYSINFO_BUFSIZE);
9770Sstevel@tonic-gate
9780Sstevel@tonic-gate if (ea_set_item(&vers_obj, EXT_UINT32 | EXC_DEFAULT | EXD_VERSION,
9790Sstevel@tonic-gate (void *)&version, 0) == -1 ||
9800Sstevel@tonic-gate ea_set_item(&creator_obj, EXT_STRING | EXC_DEFAULT | EXD_CREATOR,
9810Sstevel@tonic-gate f->ef_creator, strlen(f->ef_creator)) == -1 ||
9820Sstevel@tonic-gate ea_set_item(&filetype_obj, EXT_STRING | EXC_DEFAULT | EXD_FILETYPE,
9830Sstevel@tonic-gate EXACCT_HDR_STR, strlen(EXACCT_HDR_STR)) == -1 ||
9840Sstevel@tonic-gate ea_set_item(&hostname_obj, EXT_STRING | EXC_DEFAULT | EXD_HOSTNAME,
9850Sstevel@tonic-gate hostbuf, strlen(hostbuf)) == -1) {
9860Sstevel@tonic-gate error = ea_error();
9870Sstevel@tonic-gate goto cleanup1;
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate (void) ea_set_group(&hdr_grp,
9910Sstevel@tonic-gate EXT_GROUP | EXC_DEFAULT | EXD_GROUP_HEADER);
9920Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &vers_obj);
9930Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &creator_obj);
9940Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &filetype_obj);
9950Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &hostname_obj);
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate /* Get the required size by passing a null buffer. */
9980Sstevel@tonic-gate bufsize = ea_pack_object(&hdr_grp, NULL, 0);
9990Sstevel@tonic-gate if ((buf = ea_alloc(bufsize)) == NULL) {
10000Sstevel@tonic-gate error = ea_error();
10010Sstevel@tonic-gate goto cleanup1;
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate if (ea_pack_object(&hdr_grp, buf, bufsize) == (size_t)-1) {
10050Sstevel@tonic-gate error = ea_error();
10060Sstevel@tonic-gate goto cleanup2;
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate
10090Sstevel@tonic-gate /*
10100Sstevel@tonic-gate * To prevent reading the header when reading the file backwards,
10110Sstevel@tonic-gate * set the large backskip of the header group to 0 (last 4 bytes).
10120Sstevel@tonic-gate */
10130Sstevel@tonic-gate bskip = 0;
10140Sstevel@tonic-gate exacct_order32(&bskip);
10150Sstevel@tonic-gate bcopy(&bskip, (char *)buf + bufsize - sizeof (bskip),
10160Sstevel@tonic-gate sizeof (bskip));
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate if (fwrite(buf, sizeof (char), bufsize, f->ef_fp) != bufsize ||
10190Sstevel@tonic-gate fflush(f->ef_fp) == EOF) {
10200Sstevel@tonic-gate error = EXR_SYSCALL_FAIL;
10210Sstevel@tonic-gate goto cleanup2;
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate cleanup2:
10250Sstevel@tonic-gate ea_free(buf, bufsize);
10260Sstevel@tonic-gate cleanup1:
10270Sstevel@tonic-gate (void) ea_free_item(&vers_obj, EUP_ALLOC);
10280Sstevel@tonic-gate (void) ea_free_item(&creator_obj, EUP_ALLOC);
10290Sstevel@tonic-gate (void) ea_free_item(&filetype_obj, EUP_ALLOC);
10300Sstevel@tonic-gate (void) ea_free_item(&hostname_obj, EUP_ALLOC);
10310Sstevel@tonic-gate EXACCT_SET_ERR(error);
10320Sstevel@tonic-gate return (error == EXR_OK ? 0 : -1);
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate
10350Sstevel@tonic-gate const char *
ea_get_creator(ea_file_t * ef)10360Sstevel@tonic-gate ea_get_creator(ea_file_t *ef)
10370Sstevel@tonic-gate {
10380Sstevel@tonic-gate return ((const char *)((ea_file_impl_t *)ef)->ef_creator);
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate const char *
ea_get_hostname(ea_file_t * ef)10420Sstevel@tonic-gate ea_get_hostname(ea_file_t *ef)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate return ((const char *)((ea_file_impl_t *)ef)->ef_hostname);
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate int
ea_fdopen(ea_file_t * ef,int fd,const char * creator,int aflags,int oflags)10480Sstevel@tonic-gate ea_fdopen(ea_file_t *ef, int fd, const char *creator, int aflags, int oflags)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate bzero(f, sizeof (*f));
10530Sstevel@tonic-gate f->ef_oflags = oflags;
10540Sstevel@tonic-gate f->ef_fd = fd;
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate /* Initialize depth stack. */
10570Sstevel@tonic-gate if (stack_check(f) == -1) {
10580Sstevel@tonic-gate /* exacct_error set above. */
10590Sstevel@tonic-gate goto error1;
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate * 1. If we are O_CREAT, then we will need to write a header
10640Sstevel@tonic-gate * after opening name.
10650Sstevel@tonic-gate */
10660Sstevel@tonic-gate if (oflags & O_CREAT) {
10670Sstevel@tonic-gate if (creator == NULL) {
10680Sstevel@tonic-gate EXACCT_SET_ERR(EXR_NO_CREATOR);
10690Sstevel@tonic-gate goto error2;
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate if ((f->ef_creator = ea_strdup(creator)) == NULL) {
10720Sstevel@tonic-gate /* exacct_error set above. */
10730Sstevel@tonic-gate goto error2;
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate if ((f->ef_fp = fdopen(f->ef_fd, "w")) == NULL) {
10760Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
10770Sstevel@tonic-gate goto error3;
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate if (write_header(ef) == -1) {
10800Sstevel@tonic-gate /* exacct_error set above. */
10810Sstevel@tonic-gate goto error3;
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate /*
10850Sstevel@tonic-gate * 2. If we are not O_CREAT, but are RDWR or WRONLY, we need to
10860Sstevel@tonic-gate * seek to EOF so that appends will succeed.
10870Sstevel@tonic-gate */
10880Sstevel@tonic-gate } else if (oflags & O_RDWR || oflags & O_WRONLY) {
10890Sstevel@tonic-gate if ((f->ef_fp = fdopen(f->ef_fd, "r+")) == NULL) {
10900Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
10910Sstevel@tonic-gate goto error2;
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate if ((aflags & EO_VALIDATE_MSK) == EO_VALID_HDR) {
10950Sstevel@tonic-gate if (validate_header(ef, creator) < 0) {
10960Sstevel@tonic-gate /* exacct_error set above. */
10970Sstevel@tonic-gate goto error2;
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate if (fseeko(f->ef_fp, 0, SEEK_END) == -1) {
11020Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
11030Sstevel@tonic-gate goto error2;
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate * 3. This is an undefined manner for opening an exacct file.
11080Sstevel@tonic-gate */
11090Sstevel@tonic-gate } else if (oflags != O_RDONLY) {
11100Sstevel@tonic-gate EXACCT_SET_ERR(EXR_NOTSUPP);
11110Sstevel@tonic-gate goto error2;
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate /*
11140Sstevel@tonic-gate * 4a. If we are RDONLY, then we are in a position such that
11150Sstevel@tonic-gate * either a ea_get_object or an ea_next_object will succeed. If
11160Sstevel@tonic-gate * aflags was set to EO_TAIL, seek to the end of the file.
11170Sstevel@tonic-gate */
11180Sstevel@tonic-gate } else {
11190Sstevel@tonic-gate if ((f->ef_fp = fdopen(f->ef_fd, "r")) == NULL) {
11200Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
11210Sstevel@tonic-gate goto error2;
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate if ((aflags & EO_VALIDATE_MSK) == EO_VALID_HDR) {
11250Sstevel@tonic-gate if (validate_header(ef, creator) == -1) {
11260Sstevel@tonic-gate /* exacct_error set above. */
11270Sstevel@tonic-gate goto error2;
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate * 4b. Handle the "open at end" option, for consumers who want
11330Sstevel@tonic-gate * to go backwards through the file (i.e. lastcomm).
11340Sstevel@tonic-gate */
11350Sstevel@tonic-gate if ((aflags & EO_POSN_MSK) == EO_TAIL) {
11360Sstevel@tonic-gate if (fseeko(f->ef_fp, 0, SEEK_END) < 0) {
11370Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
11380Sstevel@tonic-gate goto error2;
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
11440Sstevel@tonic-gate return (0);
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate /* Error cleanup code */
11470Sstevel@tonic-gate error3:
11480Sstevel@tonic-gate ea_strfree(f->ef_creator);
11490Sstevel@tonic-gate error2:
11500Sstevel@tonic-gate stack_free(f);
11510Sstevel@tonic-gate error1:
11520Sstevel@tonic-gate bzero(f, sizeof (*f));
11530Sstevel@tonic-gate return (-1);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate int
ea_open(ea_file_t * ef,const char * name,const char * creator,int aflags,int oflags,mode_t mode)11570Sstevel@tonic-gate ea_open(ea_file_t *ef, const char *name, const char *creator,
11580Sstevel@tonic-gate int aflags, int oflags, mode_t mode)
11590Sstevel@tonic-gate {
11600Sstevel@tonic-gate int fd;
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate /*
11630Sstevel@tonic-gate * If overwriting an existing file, make sure to truncate it
11640Sstevel@tonic-gate * to prevent the file being created corrupt.
11650Sstevel@tonic-gate */
11660Sstevel@tonic-gate if (oflags & O_CREAT)
11670Sstevel@tonic-gate oflags |= O_TRUNC;
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate if ((fd = open(name, oflags, mode)) == -1) {
11700Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
11710Sstevel@tonic-gate return (-1);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate
11740Sstevel@tonic-gate if (ea_fdopen(ef, fd, creator, aflags, oflags) == -1) {
11750Sstevel@tonic-gate (void) close(fd);
11760Sstevel@tonic-gate return (-1);
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate
11790Sstevel@tonic-gate return (0);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate
11820Sstevel@tonic-gate /*
11830Sstevel@tonic-gate * ea_close() performs all appropriate close operations on the open exacct file,
11840Sstevel@tonic-gate * including releasing any memory allocated while parsing the file.
11850Sstevel@tonic-gate */
11860Sstevel@tonic-gate int
ea_close(ea_file_t * ef)11870Sstevel@tonic-gate ea_close(ea_file_t *ef)
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
11900Sstevel@tonic-gate
11910Sstevel@tonic-gate if (f->ef_creator != NULL)
11920Sstevel@tonic-gate ea_strfree(f->ef_creator);
11930Sstevel@tonic-gate if (f->ef_hostname != NULL)
11940Sstevel@tonic-gate ea_strfree(f->ef_hostname);
11950Sstevel@tonic-gate
11960Sstevel@tonic-gate ea_free(f->ef_depth, f->ef_mxdeep * sizeof (ea_file_depth_t));
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate if (fclose(f->ef_fp)) {
11990Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL);
12000Sstevel@tonic-gate return (-1);
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
12040Sstevel@tonic-gate return (0);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate /*
12080Sstevel@tonic-gate * Empty the input buffer and clear any underlying EOF or error bits set on the
12090Sstevel@tonic-gate * underlying FILE. This can be used by any library clients who wish to handle
12100Sstevel@tonic-gate * files that are in motion or who wish to seek the underlying file descriptor.
12110Sstevel@tonic-gate */
12120Sstevel@tonic-gate void
ea_clear(ea_file_t * ef)12130Sstevel@tonic-gate ea_clear(ea_file_t *ef)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef;
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate (void) fflush(f->ef_fp);
12180Sstevel@tonic-gate clearerr(f->ef_fp);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate /*
12220Sstevel@tonic-gate * Copy an ea_object_t. Note that in the case of a group, just the group
12230Sstevel@tonic-gate * object will be copied, and not its list of members. To recursively copy
12240Sstevel@tonic-gate * a group or a list of items use ea_copy_tree().
12250Sstevel@tonic-gate */
12260Sstevel@tonic-gate ea_object_t *
ea_copy_object(const ea_object_t * src)12270Sstevel@tonic-gate ea_copy_object(const ea_object_t *src)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate ea_object_t *dst;
12300Sstevel@tonic-gate
12310Sstevel@tonic-gate /* Allocate a new object and copy to it. */
12320Sstevel@tonic-gate if ((dst = ea_alloc(sizeof (ea_object_t))) == NULL) {
12330Sstevel@tonic-gate return (NULL);
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate bcopy(src, dst, sizeof (ea_object_t));
12360Sstevel@tonic-gate dst->eo_next = NULL;
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate switch (src->eo_type) {
12390Sstevel@tonic-gate case EO_GROUP:
12400Sstevel@tonic-gate dst->eo_group.eg_nobjs = 0;
12410Sstevel@tonic-gate dst->eo_group.eg_objs = NULL;
12420Sstevel@tonic-gate break;
12430Sstevel@tonic-gate case EO_ITEM:
12440Sstevel@tonic-gate /* Items containing pointers need special treatment. */
12450Sstevel@tonic-gate switch (src->eo_catalog & EXT_TYPE_MASK) {
12460Sstevel@tonic-gate case EXT_STRING:
12470Sstevel@tonic-gate if (src->eo_item.ei_string != NULL) {
12480Sstevel@tonic-gate dst->eo_item.ei_string =
12490Sstevel@tonic-gate ea_strdup(src->eo_item.ei_string);
12500Sstevel@tonic-gate if (dst->eo_item.ei_string == NULL) {
12510Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC);
12520Sstevel@tonic-gate return (NULL);
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate break;
12560Sstevel@tonic-gate case EXT_RAW:
12570Sstevel@tonic-gate if (src->eo_item.ei_raw != NULL) {
12580Sstevel@tonic-gate dst->eo_item.ei_raw =
12590Sstevel@tonic-gate ea_alloc(src->eo_item.ei_size);
12600Sstevel@tonic-gate if (dst->eo_item.ei_raw == NULL) {
12610Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC);
12620Sstevel@tonic-gate return (NULL);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate bcopy(src->eo_item.ei_raw, dst->eo_item.ei_raw,
12650Sstevel@tonic-gate (size_t)src->eo_item.ei_size);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate break;
12680Sstevel@tonic-gate case EXT_EXACCT_OBJECT:
12690Sstevel@tonic-gate if (src->eo_item.ei_object != NULL) {
12700Sstevel@tonic-gate dst->eo_item.ei_object =
12710Sstevel@tonic-gate ea_alloc(src->eo_item.ei_size);
12720Sstevel@tonic-gate if (dst->eo_item.ei_object == NULL) {
12730Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC);
12740Sstevel@tonic-gate return (NULL);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate bcopy(src->eo_item.ei_raw, dst->eo_item.ei_raw,
12770Sstevel@tonic-gate (size_t)src->eo_item.ei_size);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate break;
12800Sstevel@tonic-gate default:
12810Sstevel@tonic-gate /* Other item types require no special handling. */
12820Sstevel@tonic-gate break;
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate break;
12850Sstevel@tonic-gate default:
12860Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC);
12870Sstevel@tonic-gate EXACCT_SET_ERR(EXR_INVALID_OBJ);
12880Sstevel@tonic-gate return (NULL);
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
12910Sstevel@tonic-gate return (dst);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate /*
12950Sstevel@tonic-gate * Recursively copy a list of ea_object_t. All the elements in the eo_next
12960Sstevel@tonic-gate * list will be copied, and any group objects will be recursively copied.
12970Sstevel@tonic-gate */
12980Sstevel@tonic-gate ea_object_t *
ea_copy_object_tree(const ea_object_t * src)12990Sstevel@tonic-gate ea_copy_object_tree(const ea_object_t *src)
13000Sstevel@tonic-gate {
13010Sstevel@tonic-gate ea_object_t *ret_obj, *dst, *last;
13020Sstevel@tonic-gate
13030Sstevel@tonic-gate for (ret_obj = last = NULL; src != NULL;
13040Sstevel@tonic-gate last = dst, src = src->eo_next) {
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate /* Allocate a new object and copy to it. */
13070Sstevel@tonic-gate if ((dst = ea_copy_object(src)) == NULL) {
13080Sstevel@tonic-gate ea_free_object(ret_obj, EUP_ALLOC);
13090Sstevel@tonic-gate return (NULL);
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate /* Groups need the object list copying. */
13130Sstevel@tonic-gate if (src->eo_type == EO_GROUP) {
13140Sstevel@tonic-gate dst->eo_group.eg_objs =
13150Sstevel@tonic-gate ea_copy_object_tree(src->eo_group.eg_objs);
13160Sstevel@tonic-gate if (dst->eo_group.eg_objs == NULL) {
13170Sstevel@tonic-gate ea_free_object(ret_obj, EUP_ALLOC);
13180Sstevel@tonic-gate return (NULL);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate dst->eo_group.eg_nobjs = src->eo_group.eg_nobjs;
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate /* Remember the list head the first time round. */
13240Sstevel@tonic-gate if (ret_obj == NULL) {
13250Sstevel@tonic-gate ret_obj = dst;
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate /* Link together if not at the list head. */
13290Sstevel@tonic-gate if (last != NULL) {
13300Sstevel@tonic-gate last->eo_next = dst;
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
13340Sstevel@tonic-gate return (ret_obj);
13350Sstevel@tonic-gate }
13360Sstevel@tonic-gate
13370Sstevel@tonic-gate /*
13380Sstevel@tonic-gate * Read in the specified number of objects, returning the same data
13390Sstevel@tonic-gate * structure that would have originally been passed to ea_write().
13400Sstevel@tonic-gate */
13410Sstevel@tonic-gate ea_object_t *
ea_get_object_tree(ea_file_t * ef,uint32_t nobj)13420Sstevel@tonic-gate ea_get_object_tree(ea_file_t *ef, uint32_t nobj)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate ea_object_t *first_obj, *prev_obj, *obj;
13450Sstevel@tonic-gate
13460Sstevel@tonic-gate first_obj = prev_obj = NULL;
13470Sstevel@tonic-gate while (nobj--) {
13480Sstevel@tonic-gate /* Allocate space for the new object. */
13490Sstevel@tonic-gate obj = ea_alloc(sizeof (ea_object_t));
13500Sstevel@tonic-gate bzero(obj, sizeof (*obj));
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate /* Read it in. */
13530Sstevel@tonic-gate if (ea_get_object(ef, obj) == -1) {
13540Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t));
13550Sstevel@tonic-gate if (first_obj != NULL) {
13560Sstevel@tonic-gate ea_free_object(first_obj, EUP_ALLOC);
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate return (NULL);
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate
13610Sstevel@tonic-gate /* Link it into the list. */
13620Sstevel@tonic-gate if (first_obj == NULL) {
13630Sstevel@tonic-gate first_obj = obj;
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate if (prev_obj != NULL) {
13660Sstevel@tonic-gate prev_obj->eo_next = obj;
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate prev_obj = obj;
13690Sstevel@tonic-gate
13700Sstevel@tonic-gate /* Recurse if the object is a group with contents. */
13710Sstevel@tonic-gate if (obj->eo_type == EO_GROUP && obj->eo_group.eg_nobjs > 0) {
13720Sstevel@tonic-gate if ((obj->eo_group.eg_objs = ea_get_object_tree(ef,
13730Sstevel@tonic-gate obj->eo_group.eg_nobjs)) == NULL) {
13740Sstevel@tonic-gate /* exacct_error set above. */
13750Sstevel@tonic-gate ea_free_object(first_obj, EUP_ALLOC);
13760Sstevel@tonic-gate return (NULL);
13770Sstevel@tonic-gate }
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK);
13810Sstevel@tonic-gate return (first_obj);
13820Sstevel@tonic-gate }
1383