1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/systeminfo.h> 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include <exacct.h> 32*0Sstevel@tonic-gate #include <exacct_impl.h> 33*0Sstevel@tonic-gate #include <sys/exacct_impl.h> 34*0Sstevel@tonic-gate #include <fcntl.h> 35*0Sstevel@tonic-gate #include <unistd.h> 36*0Sstevel@tonic-gate #include <strings.h> 37*0Sstevel@tonic-gate #include <stdlib.h> 38*0Sstevel@tonic-gate #include <stdio.h> 39*0Sstevel@tonic-gate #include <errno.h> 40*0Sstevel@tonic-gate #include <thread.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #define EXACCT_HDR_STR "exacct" 43*0Sstevel@tonic-gate #define EXACCT_HDR_LEN 7 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #define DEFAULT_ENTRIES 4 46*0Sstevel@tonic-gate #define SYSINFO_BUFSIZE 256 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate static mutex_t keylock; 49*0Sstevel@tonic-gate static thread_key_t errkey; 50*0Sstevel@tonic-gate static int keyonce = 0; 51*0Sstevel@tonic-gate static int exacct_errval = 0; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate /* 54*0Sstevel@tonic-gate * extended accounting file access routines 55*0Sstevel@tonic-gate * 56*0Sstevel@tonic-gate * exacct_ops.c implements the library-specific routines of libexacct: the 57*0Sstevel@tonic-gate * operations associated with file access and record traversal. (The 58*0Sstevel@tonic-gate * complementary routines which permit hierarchy building and record packing 59*0Sstevel@tonic-gate * are provided in exacct_core.c, which is used by both libexacct and the 60*0Sstevel@tonic-gate * kernel.) At its heart are the unpack, get, and next routines, which 61*0Sstevel@tonic-gate * navigate the packed records produced by ea_pack_object. 62*0Sstevel@tonic-gate */ 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* 65*0Sstevel@tonic-gate * Group stack manipulation code. As groups can be nested, we need a mechanism 66*0Sstevel@tonic-gate * for saving and restoring the current position within the outer groups. This 67*0Sstevel@tonic-gate * state stack is stored within the ea_file_impl_t structure, in the ef_depth, 68*0Sstevel@tonic-gate * ef_ndeep and ef_mxdeep members. On error all these functions set 69*0Sstevel@tonic-gate * exacct_error and return -1. 70*0Sstevel@tonic-gate */ 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* 73*0Sstevel@tonic-gate * If the stack is NULL, create and initialise it. 74*0Sstevel@tonic-gate * If is is not NULL, check it still has space - if not, double its size. 75*0Sstevel@tonic-gate */ 76*0Sstevel@tonic-gate static int stack_check(ea_file_impl_t *f) 77*0Sstevel@tonic-gate { 78*0Sstevel@tonic-gate if (f->ef_depth == NULL) { 79*0Sstevel@tonic-gate if ((f->ef_depth = 80*0Sstevel@tonic-gate ea_alloc(DEFAULT_ENTRIES * sizeof (ea_file_depth_t))) 81*0Sstevel@tonic-gate == NULL) { 82*0Sstevel@tonic-gate /* exacct_errno set above. */ 83*0Sstevel@tonic-gate return (-1); 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate bzero(f->ef_depth, DEFAULT_ENTRIES * sizeof (ea_file_depth_t)); 86*0Sstevel@tonic-gate f->ef_mxdeep = DEFAULT_ENTRIES; 87*0Sstevel@tonic-gate f->ef_ndeep = -1; 88*0Sstevel@tonic-gate } else if (f->ef_ndeep + 1 >= f->ef_mxdeep) { 89*0Sstevel@tonic-gate ea_file_depth_t *newstack; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate if ((newstack = 92*0Sstevel@tonic-gate ea_alloc(f->ef_mxdeep * 2 * sizeof (ea_file_depth_t))) 93*0Sstevel@tonic-gate == NULL) { 94*0Sstevel@tonic-gate /* exacct_errno set above. */ 95*0Sstevel@tonic-gate return (-1); 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate bcopy(f->ef_depth, newstack, 98*0Sstevel@tonic-gate f->ef_mxdeep * sizeof (ea_file_depth_t)); 99*0Sstevel@tonic-gate bzero(newstack + f->ef_mxdeep, 100*0Sstevel@tonic-gate f->ef_mxdeep * sizeof (ea_file_depth_t)); 101*0Sstevel@tonic-gate ea_free(f->ef_depth, f->ef_mxdeep * sizeof (ea_file_depth_t)); 102*0Sstevel@tonic-gate f->ef_mxdeep *= 2; 103*0Sstevel@tonic-gate f->ef_depth = newstack; 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate return (0); 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * Free a stack. 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate static void stack_free(ea_file_impl_t *f) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate if (f->ef_depth != NULL) { 114*0Sstevel@tonic-gate ea_free(f->ef_depth, f->ef_mxdeep * sizeof (ea_file_depth_t)); 115*0Sstevel@tonic-gate f->ef_depth = NULL; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate f->ef_mxdeep = 0; 118*0Sstevel@tonic-gate f->ef_ndeep = -1; 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* 122*0Sstevel@tonic-gate * Add a new group onto the stack, pushing down one frame. nobj is the number 123*0Sstevel@tonic-gate * of items in the group. We have to read this many objects before popping 124*0Sstevel@tonic-gate * back up to an enclosing group - see next_object() and previous_object() 125*0Sstevel@tonic-gate * below. 126*0Sstevel@tonic-gate */ 127*0Sstevel@tonic-gate static int stack_new_group(ea_file_impl_t *f, int nobjs) 128*0Sstevel@tonic-gate { 129*0Sstevel@tonic-gate if (stack_check(f) != 0) { 130*0Sstevel@tonic-gate stack_free(f); 131*0Sstevel@tonic-gate /* exacct_errno set above. */ 132*0Sstevel@tonic-gate return (-1); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate f->ef_ndeep++; 135*0Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_obj = 0; 136*0Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_nobjs = nobjs; 137*0Sstevel@tonic-gate return (0); 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate /* 141*0Sstevel@tonic-gate * Step forwards along the objects within the current group. If we are still 142*0Sstevel@tonic-gate * within a group, return 1. If we have reached the end of the current group, 143*0Sstevel@tonic-gate * unwind the stack back up to the nearest enclosing group that still has 144*0Sstevel@tonic-gate * unprocessed objects and return 0. On EOF or error, set exacct_error 145*0Sstevel@tonic-gate * accordingly and return -1. xread() is required so that this function can 146*0Sstevel@tonic-gate * work either on files or memory buffers. 147*0Sstevel@tonic-gate */ 148*0Sstevel@tonic-gate static int 149*0Sstevel@tonic-gate stack_next_object( 150*0Sstevel@tonic-gate ea_file_impl_t *f, 151*0Sstevel@tonic-gate size_t (*xread)(ea_file_impl_t *, void *, size_t)) 152*0Sstevel@tonic-gate { 153*0Sstevel@tonic-gate uint32_t scratch32; 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate /* 156*0Sstevel@tonic-gate * If the stack is empty we are not in a group, so there will be no 157*0Sstevel@tonic-gate * stack manipulation to do and no large backskips to step over. 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate if (f->ef_ndeep < 0) { 160*0Sstevel@tonic-gate return (0); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate /* 164*0Sstevel@tonic-gate * Otherwise we must be in a group. If there are objects left in the 165*0Sstevel@tonic-gate * group, move onto the next one in the group and return. 166*0Sstevel@tonic-gate */ 167*0Sstevel@tonic-gate if (++f->ef_depth[f->ef_ndeep].efd_obj < 168*0Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_nobjs) { 169*0Sstevel@tonic-gate return (1); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate /* 172*0Sstevel@tonic-gate * If we are at the end of a group we need to move backwards up the 173*0Sstevel@tonic-gate * stack, consuming the large backskips as we go, until we find a group 174*0Sstevel@tonic-gate * that still contains unprocessed items, or until we have unwound back 175*0Sstevel@tonic-gate * off the bottom of the stack (i.e. out of all the groups). 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate } else { 178*0Sstevel@tonic-gate while (f->ef_ndeep >= 0 && 179*0Sstevel@tonic-gate ++f->ef_depth[f->ef_ndeep].efd_obj >= 180*0Sstevel@tonic-gate f->ef_depth[f->ef_ndeep].efd_nobjs) { 181*0Sstevel@tonic-gate /* Read the large backskip. */ 182*0Sstevel@tonic-gate f->ef_ndeep--; 183*0Sstevel@tonic-gate if (xread(f, &scratch32, sizeof (scratch32)) != 184*0Sstevel@tonic-gate sizeof (scratch32)) { 185*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 186*0Sstevel@tonic-gate return (-1); 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate return (0); 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate /* 194*0Sstevel@tonic-gate * Step backwards along the objects within the current group. If we are still 195*0Sstevel@tonic-gate * within a group, return 1. If we have reached the end of the current group, 196*0Sstevel@tonic-gate * unwind the stack back up to the enclosing group and return 0. 197*0Sstevel@tonic-gate */ 198*0Sstevel@tonic-gate static int stack_previous_object(ea_file_impl_t *f) 199*0Sstevel@tonic-gate { 200*0Sstevel@tonic-gate /* 201*0Sstevel@tonic-gate * If the stack is empty we are not in a group, so there will be no 202*0Sstevel@tonic-gate * stack manipulation to do. 203*0Sstevel@tonic-gate */ 204*0Sstevel@tonic-gate if (f->ef_ndeep < 0) { 205*0Sstevel@tonic-gate return (0); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate /* 209*0Sstevel@tonic-gate * Otherwise we must be in a group. If there are objects left in the 210*0Sstevel@tonic-gate * group, move onto the previous one in the group and return. 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate if (--f->ef_depth[f->ef_ndeep].efd_obj >= 0) { 213*0Sstevel@tonic-gate return (1); 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate /* Otherwise, step one level back up the group stack. */ 216*0Sstevel@tonic-gate } else { 217*0Sstevel@tonic-gate f->ef_ndeep--; 218*0Sstevel@tonic-gate return (0); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate /* 223*0Sstevel@tonic-gate * read/seek/pos virtualisation wrappers. Because objects can come either from 224*0Sstevel@tonic-gate * a file or memory, the read/seek/pos functions need to be wrapped to allow 225*0Sstevel@tonic-gate * them to be used on either a file handle or a memory buffer. 226*0Sstevel@tonic-gate */ 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate static size_t 229*0Sstevel@tonic-gate fread_wrapper(ea_file_impl_t *f, void *buf, size_t sz) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate size_t retval; 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate retval = fread(buf, 1, sz, f->ef_fp); 234*0Sstevel@tonic-gate if (retval == 0 && ferror(f->ef_fp)) { 235*0Sstevel@tonic-gate retval = (size_t)-1; 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate return (retval); 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate static size_t 241*0Sstevel@tonic-gate bufread_wrapper(ea_file_impl_t *f, void *buf, size_t sz) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate if (f->ef_bufsize == 0 && sz != 0) 244*0Sstevel@tonic-gate return ((size_t)0); 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate if (f->ef_bufsize < sz) 247*0Sstevel@tonic-gate sz = f->ef_bufsize; 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate bcopy(f->ef_buf, buf, sz); 250*0Sstevel@tonic-gate f->ef_buf += sz; 251*0Sstevel@tonic-gate f->ef_bufsize -= sz; 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate return (sz); 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate static off_t 257*0Sstevel@tonic-gate fseek_wrapper(ea_file_impl_t *f, off_t adv) 258*0Sstevel@tonic-gate { 259*0Sstevel@tonic-gate return (fseeko(f->ef_fp, adv, SEEK_CUR)); 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate static off_t 263*0Sstevel@tonic-gate bufseek_wrapper(ea_file_impl_t *f, off_t adv) 264*0Sstevel@tonic-gate { 265*0Sstevel@tonic-gate if (f->ef_bufsize == 0 && adv != 0) 266*0Sstevel@tonic-gate return (-1); 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate if (f->ef_bufsize < adv) 269*0Sstevel@tonic-gate adv = f->ef_bufsize; 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate f->ef_buf += adv; 272*0Sstevel@tonic-gate f->ef_bufsize -= adv; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate return (0); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate /*ARGSUSED*/ 278*0Sstevel@tonic-gate static void * 279*0Sstevel@tonic-gate fpos_wrapper(ea_file_impl_t *f) 280*0Sstevel@tonic-gate { 281*0Sstevel@tonic-gate return (NULL); 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate static void * 285*0Sstevel@tonic-gate bufpos_wrapper(ea_file_impl_t *f) 286*0Sstevel@tonic-gate { 287*0Sstevel@tonic-gate return (f->ef_buf); 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate /* 291*0Sstevel@tonic-gate * Public API 292*0Sstevel@tonic-gate */ 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate void 295*0Sstevel@tonic-gate exacct_seterr(int errval) 296*0Sstevel@tonic-gate { 297*0Sstevel@tonic-gate if (thr_main()) { 298*0Sstevel@tonic-gate exacct_errval = errval; 299*0Sstevel@tonic-gate return; 300*0Sstevel@tonic-gate } 301*0Sstevel@tonic-gate if (keyonce == 0) { 302*0Sstevel@tonic-gate (void) mutex_lock(&keylock); 303*0Sstevel@tonic-gate if (keyonce == 0) { 304*0Sstevel@tonic-gate (void) thr_keycreate(&errkey, 0); 305*0Sstevel@tonic-gate keyonce++; 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate (void) mutex_unlock(&keylock); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate (void) thr_setspecific(errkey, (void *)(intptr_t)errval); 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate int 313*0Sstevel@tonic-gate ea_error(void) 314*0Sstevel@tonic-gate { 315*0Sstevel@tonic-gate intptr_t errvalp; 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate if (thr_main()) 318*0Sstevel@tonic-gate return (exacct_errval); 319*0Sstevel@tonic-gate if (keyonce == 0) 320*0Sstevel@tonic-gate return (EXR_OK); 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate (void) thr_getspecific(errkey, (void**)&errvalp); 323*0Sstevel@tonic-gate return ((int)errvalp); 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate /* 327*0Sstevel@tonic-gate * ea_next_object(), ea_previous_object(), and ea_get_object() are written such 328*0Sstevel@tonic-gate * that the file cursor is always located on an object boundary. 329*0Sstevel@tonic-gate */ 330*0Sstevel@tonic-gate ea_object_type_t 331*0Sstevel@tonic-gate ea_next_object(ea_file_t *ef, ea_object_t *obj) 332*0Sstevel@tonic-gate { 333*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 334*0Sstevel@tonic-gate ea_size_t len; 335*0Sstevel@tonic-gate off_t backup; 336*0Sstevel@tonic-gate size_t ret; 337*0Sstevel@tonic-gate 338*0Sstevel@tonic-gate /* 339*0Sstevel@tonic-gate * If ef_advance is zero, then we are executing after a get or previous 340*0Sstevel@tonic-gate * operation and do not move to the next or previous object. Otherwise, 341*0Sstevel@tonic-gate * advance to the next available item. Note that ef_advance does NOT 342*0Sstevel@tonic-gate * include the large backskip at the end of a object, this being dealt 343*0Sstevel@tonic-gate * with by the depth stack handling in stack_next_object. 344*0Sstevel@tonic-gate */ 345*0Sstevel@tonic-gate if (f->ef_advance != 0) { 346*0Sstevel@tonic-gate if (fseeko(f->ef_fp, (off_t)f->ef_advance, SEEK_CUR) == -1) { 347*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 348*0Sstevel@tonic-gate return (EO_ERROR); 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate if (stack_next_object(f, fread_wrapper) == -1) { 351*0Sstevel@tonic-gate /* exacct_error set above. */ 352*0Sstevel@tonic-gate return (EO_ERROR); 353*0Sstevel@tonic-gate } 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate f->ef_advance = 0; 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate /* Read the catalog tag */ 358*0Sstevel@tonic-gate ret = fread(&obj->eo_catalog, 1, sizeof (ea_catalog_t), f->ef_fp); 359*0Sstevel@tonic-gate if (ret == 0) { 360*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF); 361*0Sstevel@tonic-gate return (EO_ERROR); 362*0Sstevel@tonic-gate } else if (ret < sizeof (ea_catalog_t)) { 363*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 364*0Sstevel@tonic-gate return (EO_ERROR); 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate exacct_order32(&obj->eo_catalog); 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate backup = sizeof (ea_catalog_t); 369*0Sstevel@tonic-gate obj->eo_type = EO_ITEM; 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate /* Figure out the offset to just before the large backskip. */ 372*0Sstevel@tonic-gate switch (obj->eo_catalog & EXT_TYPE_MASK) { 373*0Sstevel@tonic-gate case EXT_GROUP: 374*0Sstevel@tonic-gate obj->eo_type = EO_GROUP; 375*0Sstevel@tonic-gate f->ef_advance = sizeof (uint32_t); 376*0Sstevel@tonic-gate /* FALLTHROUGH */ 377*0Sstevel@tonic-gate case EXT_STRING: 378*0Sstevel@tonic-gate case EXT_EXACCT_OBJECT: 379*0Sstevel@tonic-gate case EXT_RAW: 380*0Sstevel@tonic-gate if (fread(&len, 1, sizeof (ea_size_t), f->ef_fp) 381*0Sstevel@tonic-gate < sizeof (ea_size_t)) { 382*0Sstevel@tonic-gate obj->eo_type = EO_NONE; 383*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 384*0Sstevel@tonic-gate return (EO_ERROR); 385*0Sstevel@tonic-gate } 386*0Sstevel@tonic-gate exacct_order64(&len); 387*0Sstevel@tonic-gate /* Note: len already includes the size of the backskip. */ 388*0Sstevel@tonic-gate f->ef_advance += sizeof (ea_catalog_t) + 389*0Sstevel@tonic-gate sizeof (ea_size_t) + len; 390*0Sstevel@tonic-gate backup += sizeof (ea_size_t); 391*0Sstevel@tonic-gate break; 392*0Sstevel@tonic-gate case EXT_UINT8: 393*0Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint8_t) + 394*0Sstevel@tonic-gate sizeof (uint32_t); 395*0Sstevel@tonic-gate break; 396*0Sstevel@tonic-gate case EXT_UINT16: 397*0Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint16_t) + 398*0Sstevel@tonic-gate sizeof (uint32_t); 399*0Sstevel@tonic-gate break; 400*0Sstevel@tonic-gate case EXT_UINT32: 401*0Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint32_t) + 402*0Sstevel@tonic-gate sizeof (uint32_t); 403*0Sstevel@tonic-gate break; 404*0Sstevel@tonic-gate case EXT_UINT64: 405*0Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (uint64_t) + 406*0Sstevel@tonic-gate sizeof (uint32_t); 407*0Sstevel@tonic-gate break; 408*0Sstevel@tonic-gate case EXT_DOUBLE: 409*0Sstevel@tonic-gate f->ef_advance = sizeof (ea_catalog_t) + sizeof (double) + 410*0Sstevel@tonic-gate sizeof (uint32_t); 411*0Sstevel@tonic-gate break; 412*0Sstevel@tonic-gate default: 413*0Sstevel@tonic-gate obj->eo_type = EO_NONE; 414*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 415*0Sstevel@tonic-gate return (EO_ERROR); 416*0Sstevel@tonic-gate } 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate /* Reposition to the start of this object. */ 419*0Sstevel@tonic-gate if (fseeko(f->ef_fp, -backup, SEEK_CUR) == -1) { 420*0Sstevel@tonic-gate obj->eo_type = EO_NONE; 421*0Sstevel@tonic-gate f->ef_advance = 0; 422*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 423*0Sstevel@tonic-gate return (EO_ERROR); 424*0Sstevel@tonic-gate } 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 427*0Sstevel@tonic-gate return (obj->eo_type); 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate ea_object_type_t 431*0Sstevel@tonic-gate ea_previous_object(ea_file_t *ef, ea_object_t *obj) 432*0Sstevel@tonic-gate { 433*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 434*0Sstevel@tonic-gate uint32_t bkskip; 435*0Sstevel@tonic-gate int r; 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate if (fseeko(f->ef_fp, -((off_t)sizeof (uint32_t)), SEEK_CUR) == -1) { 438*0Sstevel@tonic-gate if (errno == EINVAL) { 439*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF); 440*0Sstevel@tonic-gate } else { 441*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate return (EO_ERROR); 444*0Sstevel@tonic-gate } 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate if ((r = fread(&bkskip, 1, sizeof (uint32_t), f->ef_fp)) != 447*0Sstevel@tonic-gate sizeof (uint32_t)) { 448*0Sstevel@tonic-gate if (r == 0) { 449*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF); 450*0Sstevel@tonic-gate } else { 451*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate return (EO_ERROR); 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate exacct_order32(&bkskip); 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gate /* 458*0Sstevel@tonic-gate * A backskip of 0 means that the current record can't be skipped over. 459*0Sstevel@tonic-gate * This will be true for the header record, and for records longer than 460*0Sstevel@tonic-gate * 2^32. 461*0Sstevel@tonic-gate */ 462*0Sstevel@tonic-gate if (bkskip == 0) { 463*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF); 464*0Sstevel@tonic-gate return (EO_ERROR); 465*0Sstevel@tonic-gate } 466*0Sstevel@tonic-gate (void) stack_previous_object(f); 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate if (fseeko(f->ef_fp, -((off_t)bkskip), SEEK_CUR) == -1) { 469*0Sstevel@tonic-gate if (errno == EINVAL) { 470*0Sstevel@tonic-gate /* 471*0Sstevel@tonic-gate * If we attempted to seek past BOF, then the file was 472*0Sstevel@tonic-gate * corrupt, as we can only trust the backskip we read. 473*0Sstevel@tonic-gate */ 474*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 475*0Sstevel@tonic-gate } else { 476*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 477*0Sstevel@tonic-gate } 478*0Sstevel@tonic-gate return (EO_ERROR); 479*0Sstevel@tonic-gate } 480*0Sstevel@tonic-gate 481*0Sstevel@tonic-gate f->ef_advance = 0; 482*0Sstevel@tonic-gate return (ea_next_object(ef, obj)); 483*0Sstevel@tonic-gate } 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate /* 486*0Sstevel@tonic-gate * xget_object() contains the logic for extracting an individual object from a 487*0Sstevel@tonic-gate * packed buffer, which it consumes using xread() and xseek() operations 488*0Sstevel@tonic-gate * provided by the caller. flags may be set to either EUP_ALLOC, in which case 489*0Sstevel@tonic-gate * new memory is allocated for the variable length items unpacked, or 490*0Sstevel@tonic-gate * EUP_NOALLOC, in which case item data pointer indicate locations within the 491*0Sstevel@tonic-gate * buffer, using the provided xpos() function. EUP_NOALLOC is generally not 492*0Sstevel@tonic-gate * useful for callers representing interaction with actual file streams, and 493*0Sstevel@tonic-gate * should not be specified thereby. 494*0Sstevel@tonic-gate */ 495*0Sstevel@tonic-gate static ea_object_type_t 496*0Sstevel@tonic-gate xget_object( 497*0Sstevel@tonic-gate ea_file_impl_t *f, 498*0Sstevel@tonic-gate ea_object_t *obj, 499*0Sstevel@tonic-gate size_t (*xread)(ea_file_impl_t *, void *, size_t), 500*0Sstevel@tonic-gate off_t (*xseek)(ea_file_impl_t *, off_t), 501*0Sstevel@tonic-gate void *(*xpos)(ea_file_impl_t *), 502*0Sstevel@tonic-gate int flags) 503*0Sstevel@tonic-gate { 504*0Sstevel@tonic-gate ea_size_t sz; 505*0Sstevel@tonic-gate uint32_t gp_backskip, scratch32; 506*0Sstevel@tonic-gate void *buf; 507*0Sstevel@tonic-gate size_t r; 508*0Sstevel@tonic-gate 509*0Sstevel@tonic-gate /* Read the catalog tag. */ 510*0Sstevel@tonic-gate if ((r = xread(f, &obj->eo_catalog, sizeof (ea_catalog_t))) == 0) { 511*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_EOF); 512*0Sstevel@tonic-gate return (EO_ERROR); 513*0Sstevel@tonic-gate } else if (r != sizeof (ea_catalog_t)) { 514*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 515*0Sstevel@tonic-gate return (EO_ERROR); 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate exacct_order32(&obj->eo_catalog); 518*0Sstevel@tonic-gate 519*0Sstevel@tonic-gate /* 520*0Sstevel@tonic-gate * If this is a record group, we treat it separately: only record 521*0Sstevel@tonic-gate * groups cause us to allocate new depth frames. 522*0Sstevel@tonic-gate */ 523*0Sstevel@tonic-gate if ((obj->eo_catalog & EXT_TYPE_MASK) == EXT_GROUP) { 524*0Sstevel@tonic-gate obj->eo_type = EO_GROUP; 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate /* Read size field, and number of objects. */ 527*0Sstevel@tonic-gate if (xread(f, &sz, sizeof (ea_size_t)) != sizeof (ea_size_t)) { 528*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 529*0Sstevel@tonic-gate return (EO_ERROR); 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate exacct_order64(&sz); 532*0Sstevel@tonic-gate if (xread(f, &obj->eo_group.eg_nobjs, sizeof (uint32_t)) != 533*0Sstevel@tonic-gate sizeof (uint32_t)) { 534*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 535*0Sstevel@tonic-gate return (EO_ERROR); 536*0Sstevel@tonic-gate } 537*0Sstevel@tonic-gate exacct_order32(&obj->eo_group.eg_nobjs); 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate /* Now read the group's small backskip. */ 540*0Sstevel@tonic-gate if (xread(f, &gp_backskip, sizeof (uint32_t)) != 541*0Sstevel@tonic-gate sizeof (uint32_t)) { 542*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 543*0Sstevel@tonic-gate return (EO_ERROR); 544*0Sstevel@tonic-gate } 545*0Sstevel@tonic-gate 546*0Sstevel@tonic-gate /* Push a new depth stack frame. */ 547*0Sstevel@tonic-gate if (stack_new_group(f, obj->eo_group.eg_nobjs) != 0) { 548*0Sstevel@tonic-gate /* exacct_error set above */ 549*0Sstevel@tonic-gate return (EO_ERROR); 550*0Sstevel@tonic-gate } 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gate /* 553*0Sstevel@tonic-gate * If the group has no items, we now need to position to the 554*0Sstevel@tonic-gate * end of the group, because there will be no subsequent calls 555*0Sstevel@tonic-gate * to process the group, it being empty. 556*0Sstevel@tonic-gate */ 557*0Sstevel@tonic-gate if (obj->eo_group.eg_nobjs == 0) { 558*0Sstevel@tonic-gate if (stack_next_object(f, xread) == -1) { 559*0Sstevel@tonic-gate /* exacct_error set above. */ 560*0Sstevel@tonic-gate return (EO_ERROR); 561*0Sstevel@tonic-gate } 562*0Sstevel@tonic-gate } 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate f->ef_advance = 0; 565*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 566*0Sstevel@tonic-gate return (obj->eo_type); 567*0Sstevel@tonic-gate } 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate /* 570*0Sstevel@tonic-gate * Otherwise we are reading an item. 571*0Sstevel@tonic-gate */ 572*0Sstevel@tonic-gate obj->eo_type = EO_ITEM; 573*0Sstevel@tonic-gate switch (obj->eo_catalog & EXT_TYPE_MASK) { 574*0Sstevel@tonic-gate case EXT_STRING: 575*0Sstevel@tonic-gate case EXT_EXACCT_OBJECT: 576*0Sstevel@tonic-gate case EXT_RAW: 577*0Sstevel@tonic-gate if (xread(f, &sz, sizeof (ea_size_t)) != sizeof (ea_size_t)) { 578*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 579*0Sstevel@tonic-gate return (EO_ERROR); 580*0Sstevel@tonic-gate } 581*0Sstevel@tonic-gate exacct_order64(&sz); 582*0Sstevel@tonic-gate /* 583*0Sstevel@tonic-gate * Subtract backskip value from size. 584*0Sstevel@tonic-gate */ 585*0Sstevel@tonic-gate sz -= sizeof (uint32_t); 586*0Sstevel@tonic-gate if ((flags & EUP_ALLOC_MASK) == EUP_NOALLOC) { 587*0Sstevel@tonic-gate buf = xpos(f); 588*0Sstevel@tonic-gate if (xseek(f, sz) == -1) { 589*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 590*0Sstevel@tonic-gate return (EO_ERROR); 591*0Sstevel@tonic-gate } 592*0Sstevel@tonic-gate } else { 593*0Sstevel@tonic-gate if ((buf = ea_alloc(sz)) == NULL) 594*0Sstevel@tonic-gate /* exacct_error set above. */ 595*0Sstevel@tonic-gate return (EO_ERROR); 596*0Sstevel@tonic-gate if (xread(f, buf, sz) != sz) { 597*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 598*0Sstevel@tonic-gate return (EO_ERROR); 599*0Sstevel@tonic-gate } 600*0Sstevel@tonic-gate } 601*0Sstevel@tonic-gate obj->eo_item.ei_string = buf; 602*0Sstevel@tonic-gate /* 603*0Sstevel@tonic-gate * Maintain our consistent convention that string lengths 604*0Sstevel@tonic-gate * include the terminating NULL character. 605*0Sstevel@tonic-gate */ 606*0Sstevel@tonic-gate obj->eo_item.ei_size = sz; 607*0Sstevel@tonic-gate break; 608*0Sstevel@tonic-gate case EXT_UINT8: 609*0Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint8, sizeof (uint8_t)) != 610*0Sstevel@tonic-gate sizeof (uint8_t)) { 611*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 612*0Sstevel@tonic-gate return (EO_ERROR); 613*0Sstevel@tonic-gate } 614*0Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint8_t); 615*0Sstevel@tonic-gate break; 616*0Sstevel@tonic-gate case EXT_UINT16: 617*0Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint16, sizeof (uint16_t)) != 618*0Sstevel@tonic-gate sizeof (uint16_t)) { 619*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 620*0Sstevel@tonic-gate return (EO_ERROR); 621*0Sstevel@tonic-gate } 622*0Sstevel@tonic-gate exacct_order16(&obj->eo_item.ei_uint16); 623*0Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint16_t); 624*0Sstevel@tonic-gate break; 625*0Sstevel@tonic-gate case EXT_UINT32: 626*0Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint32, sizeof (uint32_t)) != 627*0Sstevel@tonic-gate sizeof (uint32_t)) { 628*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 629*0Sstevel@tonic-gate return (EO_ERROR); 630*0Sstevel@tonic-gate } 631*0Sstevel@tonic-gate exacct_order32(&obj->eo_item.ei_uint32); 632*0Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint32_t); 633*0Sstevel@tonic-gate break; 634*0Sstevel@tonic-gate case EXT_UINT64: 635*0Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_uint64, sizeof (uint64_t)) != 636*0Sstevel@tonic-gate sizeof (uint64_t)) { 637*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 638*0Sstevel@tonic-gate return (EO_ERROR); 639*0Sstevel@tonic-gate } 640*0Sstevel@tonic-gate exacct_order64(&obj->eo_item.ei_uint64); 641*0Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (uint64_t); 642*0Sstevel@tonic-gate break; 643*0Sstevel@tonic-gate case EXT_DOUBLE: 644*0Sstevel@tonic-gate if (xread(f, &obj->eo_item.ei_double, sizeof (double)) != 645*0Sstevel@tonic-gate sizeof (double)) { 646*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 647*0Sstevel@tonic-gate return (EO_ERROR); 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate exacct_order64((uint64_t *)&obj->eo_item.ei_double); 650*0Sstevel@tonic-gate obj->eo_item.ei_size = sizeof (double); 651*0Sstevel@tonic-gate break; 652*0Sstevel@tonic-gate default: 653*0Sstevel@tonic-gate /* 654*0Sstevel@tonic-gate * We've encountered an unknown type value. Flag the error and 655*0Sstevel@tonic-gate * exit. 656*0Sstevel@tonic-gate */ 657*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 658*0Sstevel@tonic-gate return (EO_ERROR); 659*0Sstevel@tonic-gate } 660*0Sstevel@tonic-gate 661*0Sstevel@tonic-gate /* 662*0Sstevel@tonic-gate * Advance over current large backskip value, 663*0Sstevel@tonic-gate * and position at the start of the next object. 664*0Sstevel@tonic-gate */ 665*0Sstevel@tonic-gate if (xread(f, &scratch32, sizeof (scratch32)) != sizeof (scratch32)) { 666*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 667*0Sstevel@tonic-gate return (EO_ERROR); 668*0Sstevel@tonic-gate } 669*0Sstevel@tonic-gate if (stack_next_object(f, xread) == -1) { 670*0Sstevel@tonic-gate /* exacct_error set above. */ 671*0Sstevel@tonic-gate return (EO_ERROR); 672*0Sstevel@tonic-gate } 673*0Sstevel@tonic-gate 674*0Sstevel@tonic-gate f->ef_advance = 0; 675*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 676*0Sstevel@tonic-gate return (obj->eo_type); 677*0Sstevel@tonic-gate } 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate ea_object_type_t 680*0Sstevel@tonic-gate ea_get_object(ea_file_t *ef, ea_object_t *obj) 681*0Sstevel@tonic-gate { 682*0Sstevel@tonic-gate obj->eo_next = NULL; 683*0Sstevel@tonic-gate return (xget_object((ea_file_impl_t *)ef, obj, fread_wrapper, 684*0Sstevel@tonic-gate fseek_wrapper, fpos_wrapper, EUP_ALLOC)); 685*0Sstevel@tonic-gate } 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gate /* 688*0Sstevel@tonic-gate * unpack_group() recursively unpacks record groups from the buffer tucked 689*0Sstevel@tonic-gate * within the passed ea_file, and attaches them to grp. 690*0Sstevel@tonic-gate */ 691*0Sstevel@tonic-gate static int 692*0Sstevel@tonic-gate unpack_group(ea_file_impl_t *f, ea_object_t *grp, int flag) 693*0Sstevel@tonic-gate { 694*0Sstevel@tonic-gate ea_object_t *obj; 695*0Sstevel@tonic-gate uint_t nobjs = grp->eo_group.eg_nobjs; 696*0Sstevel@tonic-gate int i; 697*0Sstevel@tonic-gate 698*0Sstevel@tonic-gate /* 699*0Sstevel@tonic-gate * Set the group's object count to zero, as we will rebuild it via the 700*0Sstevel@tonic-gate * individual object attachments. 701*0Sstevel@tonic-gate */ 702*0Sstevel@tonic-gate grp->eo_group.eg_nobjs = 0; 703*0Sstevel@tonic-gate grp->eo_group.eg_objs = NULL; 704*0Sstevel@tonic-gate 705*0Sstevel@tonic-gate for (i = 0; i < nobjs; i++) { 706*0Sstevel@tonic-gate if ((obj = ea_alloc(sizeof (ea_object_t))) == NULL) { 707*0Sstevel@tonic-gate /* exacct_errno set above. */ 708*0Sstevel@tonic-gate return (-1); 709*0Sstevel@tonic-gate } 710*0Sstevel@tonic-gate obj->eo_next = NULL; 711*0Sstevel@tonic-gate if (xget_object(f, obj, bufread_wrapper, bufseek_wrapper, 712*0Sstevel@tonic-gate bufpos_wrapper, flag) == -1) { 713*0Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t)); 714*0Sstevel@tonic-gate /* exacct_errno set above. */ 715*0Sstevel@tonic-gate return (-1); 716*0Sstevel@tonic-gate } 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate (void) ea_attach_to_group(grp, obj); 719*0Sstevel@tonic-gate 720*0Sstevel@tonic-gate if (obj->eo_type == EO_GROUP && 721*0Sstevel@tonic-gate unpack_group(f, obj, flag) == -1) { 722*0Sstevel@tonic-gate /* exacct_errno set above. */ 723*0Sstevel@tonic-gate return (-1); 724*0Sstevel@tonic-gate } 725*0Sstevel@tonic-gate } 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate if (nobjs != grp->eo_group.eg_nobjs) { 728*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_CORRUPT_FILE); 729*0Sstevel@tonic-gate return (-1); 730*0Sstevel@tonic-gate } 731*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 732*0Sstevel@tonic-gate return (0); 733*0Sstevel@tonic-gate } 734*0Sstevel@tonic-gate 735*0Sstevel@tonic-gate /* 736*0Sstevel@tonic-gate * ea_unpack_object() can be considered as a finite series of get operations on 737*0Sstevel@tonic-gate * a given buffer, that rebuilds the hierarchy of objects compacted by a pack 738*0Sstevel@tonic-gate * operation. Because there is complex state associated with the group depth, 739*0Sstevel@tonic-gate * ea_unpack_object() must complete as one operation on a given buffer. 740*0Sstevel@tonic-gate */ 741*0Sstevel@tonic-gate ea_object_type_t 742*0Sstevel@tonic-gate ea_unpack_object(ea_object_t **objp, int flag, void *buf, size_t bufsize) 743*0Sstevel@tonic-gate { 744*0Sstevel@tonic-gate ea_file_impl_t fake; 745*0Sstevel@tonic-gate ea_object_t *obj; 746*0Sstevel@tonic-gate ea_object_type_t first_obj_type; 747*0Sstevel@tonic-gate 748*0Sstevel@tonic-gate *objp = NULL; 749*0Sstevel@tonic-gate if (buf == NULL) { 750*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_INVALID_BUF); 751*0Sstevel@tonic-gate return (EO_ERROR); 752*0Sstevel@tonic-gate } 753*0Sstevel@tonic-gate 754*0Sstevel@tonic-gate /* Set up the structures needed for unpacking */ 755*0Sstevel@tonic-gate bzero(&fake, sizeof (ea_file_impl_t)); 756*0Sstevel@tonic-gate if (stack_check(&fake) == -1) { 757*0Sstevel@tonic-gate /* exacct_errno set above. */ 758*0Sstevel@tonic-gate return (EO_ERROR); 759*0Sstevel@tonic-gate } 760*0Sstevel@tonic-gate fake.ef_buf = buf; 761*0Sstevel@tonic-gate fake.ef_bufsize = bufsize; 762*0Sstevel@tonic-gate 763*0Sstevel@tonic-gate /* Unpack the first object in the buffer - this should succeed. */ 764*0Sstevel@tonic-gate if ((obj = ea_alloc(sizeof (ea_object_t))) == NULL) { 765*0Sstevel@tonic-gate stack_free(&fake); 766*0Sstevel@tonic-gate /* exacct_errno set above. */ 767*0Sstevel@tonic-gate return (EO_ERROR); 768*0Sstevel@tonic-gate } 769*0Sstevel@tonic-gate obj->eo_next = NULL; 770*0Sstevel@tonic-gate if ((first_obj_type = xget_object(&fake, obj, bufread_wrapper, 771*0Sstevel@tonic-gate bufseek_wrapper, bufpos_wrapper, flag)) == -1) { 772*0Sstevel@tonic-gate stack_free(&fake); 773*0Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t)); 774*0Sstevel@tonic-gate /* exacct_errno set above. */ 775*0Sstevel@tonic-gate return (EO_ERROR); 776*0Sstevel@tonic-gate } 777*0Sstevel@tonic-gate 778*0Sstevel@tonic-gate if (obj->eo_type == EO_GROUP && unpack_group(&fake, obj, flag) == -1) { 779*0Sstevel@tonic-gate stack_free(&fake); 780*0Sstevel@tonic-gate ea_free_object(obj, flag); 781*0Sstevel@tonic-gate /* exacct_errno set above. */ 782*0Sstevel@tonic-gate return (EO_ERROR); 783*0Sstevel@tonic-gate } 784*0Sstevel@tonic-gate *objp = obj; 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate /* 787*0Sstevel@tonic-gate * There may be other objects in the buffer - if so, chain them onto 788*0Sstevel@tonic-gate * the end of the list. We have reached the end of the list when 789*0Sstevel@tonic-gate * xget_object() returns -1 with exacct_error set to EXR_EOF. 790*0Sstevel@tonic-gate */ 791*0Sstevel@tonic-gate for (;;) { 792*0Sstevel@tonic-gate if ((obj = ea_alloc(sizeof (ea_object_t))) == NULL) { 793*0Sstevel@tonic-gate stack_free(&fake); 794*0Sstevel@tonic-gate ea_free_object(*objp, flag); 795*0Sstevel@tonic-gate *objp = NULL; 796*0Sstevel@tonic-gate /* exacct_errno set above. */ 797*0Sstevel@tonic-gate return (EO_ERROR); 798*0Sstevel@tonic-gate } 799*0Sstevel@tonic-gate obj->eo_next = NULL; 800*0Sstevel@tonic-gate if (xget_object(&fake, obj, bufread_wrapper, bufseek_wrapper, 801*0Sstevel@tonic-gate bufpos_wrapper, flag) == -1) { 802*0Sstevel@tonic-gate stack_free(&fake); 803*0Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t)); 804*0Sstevel@tonic-gate if (ea_error() == EXR_EOF) { 805*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 806*0Sstevel@tonic-gate return (first_obj_type); 807*0Sstevel@tonic-gate } else { 808*0Sstevel@tonic-gate ea_free_object(*objp, flag); 809*0Sstevel@tonic-gate *objp = NULL; 810*0Sstevel@tonic-gate /* exacct_error set above. */ 811*0Sstevel@tonic-gate return (EO_ERROR); 812*0Sstevel@tonic-gate } 813*0Sstevel@tonic-gate } 814*0Sstevel@tonic-gate 815*0Sstevel@tonic-gate (void) ea_attach_to_object(*objp, obj); 816*0Sstevel@tonic-gate 817*0Sstevel@tonic-gate if (obj->eo_type == EO_GROUP && 818*0Sstevel@tonic-gate unpack_group(&fake, obj, flag) == -1) { 819*0Sstevel@tonic-gate stack_free(&fake); 820*0Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t)); 821*0Sstevel@tonic-gate ea_free_object(*objp, flag); 822*0Sstevel@tonic-gate *objp = NULL; 823*0Sstevel@tonic-gate /* exacct_errno set above. */ 824*0Sstevel@tonic-gate return (EO_ERROR); 825*0Sstevel@tonic-gate } 826*0Sstevel@tonic-gate } 827*0Sstevel@tonic-gate } 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate int 830*0Sstevel@tonic-gate ea_write_object(ea_file_t *ef, ea_object_t *obj) 831*0Sstevel@tonic-gate { 832*0Sstevel@tonic-gate ea_size_t sz; 833*0Sstevel@tonic-gate void *buf; 834*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 835*0Sstevel@tonic-gate 836*0Sstevel@tonic-gate /* 837*0Sstevel@tonic-gate * If we weren't opened for writing, this call fails. 838*0Sstevel@tonic-gate */ 839*0Sstevel@tonic-gate if ((f->ef_oflags & O_RDWR) == 0 && 840*0Sstevel@tonic-gate (f->ef_oflags & O_WRONLY) == 0) { 841*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_NOTSUPP); 842*0Sstevel@tonic-gate return (-1); 843*0Sstevel@tonic-gate } 844*0Sstevel@tonic-gate 845*0Sstevel@tonic-gate /* Pack with a null buffer to get the size. */ 846*0Sstevel@tonic-gate sz = ea_pack_object(obj, NULL, 0); 847*0Sstevel@tonic-gate if (sz == -1 || (buf = ea_alloc(sz)) == NULL) { 848*0Sstevel@tonic-gate /* exacct_error set above. */ 849*0Sstevel@tonic-gate return (-1); 850*0Sstevel@tonic-gate } 851*0Sstevel@tonic-gate if (ea_pack_object(obj, buf, sz) == (size_t)-1) { 852*0Sstevel@tonic-gate ea_free(buf, sz); 853*0Sstevel@tonic-gate /* exacct_error set above. */ 854*0Sstevel@tonic-gate return (-1); 855*0Sstevel@tonic-gate } 856*0Sstevel@tonic-gate if (fwrite(buf, sizeof (char), sz, f->ef_fp) != sz) { 857*0Sstevel@tonic-gate ea_free(buf, sz); 858*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 859*0Sstevel@tonic-gate return (-1); 860*0Sstevel@tonic-gate } 861*0Sstevel@tonic-gate ea_free(buf, sz); 862*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 863*0Sstevel@tonic-gate return (0); 864*0Sstevel@tonic-gate } 865*0Sstevel@tonic-gate 866*0Sstevel@tonic-gate /* 867*0Sstevel@tonic-gate * validate_header() must be kept in sync with write_header(), given below, and 868*0Sstevel@tonic-gate * exacct_create_header(), in uts/common/os/exacct.c. 869*0Sstevel@tonic-gate */ 870*0Sstevel@tonic-gate static int 871*0Sstevel@tonic-gate validate_header(ea_file_t *ef, const char *creator) 872*0Sstevel@tonic-gate { 873*0Sstevel@tonic-gate ea_object_t hdr_grp; 874*0Sstevel@tonic-gate ea_object_t scratch_obj; 875*0Sstevel@tonic-gate int error = EXR_OK; 876*0Sstevel@tonic-gate int saw_creator = 0; 877*0Sstevel@tonic-gate int saw_version = 0; 878*0Sstevel@tonic-gate int saw_type = 0; 879*0Sstevel@tonic-gate int saw_hostname = 0; 880*0Sstevel@tonic-gate int n; 881*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 882*0Sstevel@tonic-gate 883*0Sstevel@tonic-gate bzero(&hdr_grp, sizeof (ea_object_t)); 884*0Sstevel@tonic-gate 885*0Sstevel@tonic-gate if (ea_get_object(ef, &hdr_grp) != EO_GROUP) { 886*0Sstevel@tonic-gate error = ea_error(); 887*0Sstevel@tonic-gate goto error_case; 888*0Sstevel@tonic-gate } 889*0Sstevel@tonic-gate 890*0Sstevel@tonic-gate if (hdr_grp.eo_catalog != 891*0Sstevel@tonic-gate (EXT_GROUP | EXC_DEFAULT | EXD_GROUP_HEADER)) { 892*0Sstevel@tonic-gate error = EXR_CORRUPT_FILE; 893*0Sstevel@tonic-gate goto error_case; 894*0Sstevel@tonic-gate } 895*0Sstevel@tonic-gate 896*0Sstevel@tonic-gate for (n = 0; n < hdr_grp.eo_group.eg_nobjs; n++) { 897*0Sstevel@tonic-gate bzero(&scratch_obj, sizeof (ea_object_t)); 898*0Sstevel@tonic-gate if (ea_get_object(ef, &scratch_obj) == -1) { 899*0Sstevel@tonic-gate error = ea_error(); 900*0Sstevel@tonic-gate goto error_case; 901*0Sstevel@tonic-gate } 902*0Sstevel@tonic-gate 903*0Sstevel@tonic-gate switch (scratch_obj.eo_catalog) { 904*0Sstevel@tonic-gate case EXT_UINT32 | EXC_DEFAULT | EXD_VERSION: 905*0Sstevel@tonic-gate if (scratch_obj.eo_item.ei_uint32 != EXACCT_VERSION) { 906*0Sstevel@tonic-gate error = EXR_UNKN_VERSION; 907*0Sstevel@tonic-gate goto error_case; 908*0Sstevel@tonic-gate } 909*0Sstevel@tonic-gate saw_version++; 910*0Sstevel@tonic-gate break; 911*0Sstevel@tonic-gate case EXT_STRING | EXC_DEFAULT | EXD_FILETYPE: 912*0Sstevel@tonic-gate if (strcmp(scratch_obj.eo_item.ei_string, 913*0Sstevel@tonic-gate EXACCT_HDR_STR) != 0) { 914*0Sstevel@tonic-gate error = EXR_CORRUPT_FILE; 915*0Sstevel@tonic-gate goto error_case; 916*0Sstevel@tonic-gate } 917*0Sstevel@tonic-gate saw_type++; 918*0Sstevel@tonic-gate break; 919*0Sstevel@tonic-gate case EXT_STRING | EXC_DEFAULT | EXD_CREATOR: 920*0Sstevel@tonic-gate f->ef_creator = 921*0Sstevel@tonic-gate ea_strdup(scratch_obj.eo_item.ei_string); 922*0Sstevel@tonic-gate if (f->ef_creator == NULL) { 923*0Sstevel@tonic-gate error = ea_error(); 924*0Sstevel@tonic-gate goto error_case; 925*0Sstevel@tonic-gate } 926*0Sstevel@tonic-gate saw_creator++; 927*0Sstevel@tonic-gate break; 928*0Sstevel@tonic-gate /* The hostname is an optional field. */ 929*0Sstevel@tonic-gate case EXT_STRING | EXC_DEFAULT | EXD_HOSTNAME: 930*0Sstevel@tonic-gate f->ef_hostname = 931*0Sstevel@tonic-gate ea_strdup(scratch_obj.eo_item.ei_string); 932*0Sstevel@tonic-gate if (f->ef_hostname == NULL) { 933*0Sstevel@tonic-gate error = ea_error(); 934*0Sstevel@tonic-gate goto error_case; 935*0Sstevel@tonic-gate } 936*0Sstevel@tonic-gate saw_hostname++; 937*0Sstevel@tonic-gate break; 938*0Sstevel@tonic-gate default: 939*0Sstevel@tonic-gate /* ignore unrecognized header members */ 940*0Sstevel@tonic-gate break; 941*0Sstevel@tonic-gate } 942*0Sstevel@tonic-gate (void) ea_free_item(&scratch_obj, EUP_ALLOC); 943*0Sstevel@tonic-gate } 944*0Sstevel@tonic-gate 945*0Sstevel@tonic-gate if (saw_version && saw_type && saw_creator) { 946*0Sstevel@tonic-gate if (creator && strcmp(f->ef_creator, creator) != 0) { 947*0Sstevel@tonic-gate error = EXR_NO_CREATOR; 948*0Sstevel@tonic-gate goto error_case; 949*0Sstevel@tonic-gate } 950*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 951*0Sstevel@tonic-gate return (0); 952*0Sstevel@tonic-gate } 953*0Sstevel@tonic-gate 954*0Sstevel@tonic-gate error_case: 955*0Sstevel@tonic-gate (void) ea_free_item(&scratch_obj, EUP_ALLOC); 956*0Sstevel@tonic-gate if (saw_hostname) 957*0Sstevel@tonic-gate ea_strfree(f->ef_hostname); 958*0Sstevel@tonic-gate if (saw_creator) 959*0Sstevel@tonic-gate ea_strfree(f->ef_creator); 960*0Sstevel@tonic-gate EXACCT_SET_ERR(error); 961*0Sstevel@tonic-gate return (-1); 962*0Sstevel@tonic-gate } 963*0Sstevel@tonic-gate 964*0Sstevel@tonic-gate static int 965*0Sstevel@tonic-gate write_header(ea_file_t *ef) 966*0Sstevel@tonic-gate { 967*0Sstevel@tonic-gate ea_object_t hdr_grp; 968*0Sstevel@tonic-gate ea_object_t vers_obj; 969*0Sstevel@tonic-gate ea_object_t creator_obj; 970*0Sstevel@tonic-gate ea_object_t filetype_obj; 971*0Sstevel@tonic-gate ea_object_t hostname_obj; 972*0Sstevel@tonic-gate uint32_t bskip; 973*0Sstevel@tonic-gate const uint32_t version = EXACCT_VERSION; 974*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 975*0Sstevel@tonic-gate void *buf; 976*0Sstevel@tonic-gate size_t bufsize; 977*0Sstevel@tonic-gate char hostbuf[SYSINFO_BUFSIZE]; 978*0Sstevel@tonic-gate int error = EXR_OK; 979*0Sstevel@tonic-gate 980*0Sstevel@tonic-gate bzero(&hdr_grp, sizeof (ea_object_t)); 981*0Sstevel@tonic-gate bzero(&vers_obj, sizeof (ea_object_t)); 982*0Sstevel@tonic-gate bzero(&creator_obj, sizeof (ea_object_t)); 983*0Sstevel@tonic-gate bzero(&filetype_obj, sizeof (ea_object_t)); 984*0Sstevel@tonic-gate bzero(&hostname_obj, sizeof (ea_object_t)); 985*0Sstevel@tonic-gate bzero(hostbuf, SYSINFO_BUFSIZE); 986*0Sstevel@tonic-gate 987*0Sstevel@tonic-gate (void) sysinfo(SI_HOSTNAME, hostbuf, SYSINFO_BUFSIZE); 988*0Sstevel@tonic-gate 989*0Sstevel@tonic-gate if (ea_set_item(&vers_obj, EXT_UINT32 | EXC_DEFAULT | EXD_VERSION, 990*0Sstevel@tonic-gate (void *)&version, 0) == -1 || 991*0Sstevel@tonic-gate ea_set_item(&creator_obj, EXT_STRING | EXC_DEFAULT | EXD_CREATOR, 992*0Sstevel@tonic-gate f->ef_creator, strlen(f->ef_creator)) == -1 || 993*0Sstevel@tonic-gate ea_set_item(&filetype_obj, EXT_STRING | EXC_DEFAULT | EXD_FILETYPE, 994*0Sstevel@tonic-gate EXACCT_HDR_STR, strlen(EXACCT_HDR_STR)) == -1 || 995*0Sstevel@tonic-gate ea_set_item(&hostname_obj, EXT_STRING | EXC_DEFAULT | EXD_HOSTNAME, 996*0Sstevel@tonic-gate hostbuf, strlen(hostbuf)) == -1) { 997*0Sstevel@tonic-gate error = ea_error(); 998*0Sstevel@tonic-gate goto cleanup1; 999*0Sstevel@tonic-gate } 1000*0Sstevel@tonic-gate 1001*0Sstevel@tonic-gate (void) ea_set_group(&hdr_grp, 1002*0Sstevel@tonic-gate EXT_GROUP | EXC_DEFAULT | EXD_GROUP_HEADER); 1003*0Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &vers_obj); 1004*0Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &creator_obj); 1005*0Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &filetype_obj); 1006*0Sstevel@tonic-gate (void) ea_attach_to_group(&hdr_grp, &hostname_obj); 1007*0Sstevel@tonic-gate 1008*0Sstevel@tonic-gate /* Get the required size by passing a null buffer. */ 1009*0Sstevel@tonic-gate bufsize = ea_pack_object(&hdr_grp, NULL, 0); 1010*0Sstevel@tonic-gate if ((buf = ea_alloc(bufsize)) == NULL) { 1011*0Sstevel@tonic-gate error = ea_error(); 1012*0Sstevel@tonic-gate goto cleanup1; 1013*0Sstevel@tonic-gate } 1014*0Sstevel@tonic-gate 1015*0Sstevel@tonic-gate if (ea_pack_object(&hdr_grp, buf, bufsize) == (size_t)-1) { 1016*0Sstevel@tonic-gate error = ea_error(); 1017*0Sstevel@tonic-gate goto cleanup2; 1018*0Sstevel@tonic-gate } 1019*0Sstevel@tonic-gate 1020*0Sstevel@tonic-gate /* 1021*0Sstevel@tonic-gate * To prevent reading the header when reading the file backwards, 1022*0Sstevel@tonic-gate * set the large backskip of the header group to 0 (last 4 bytes). 1023*0Sstevel@tonic-gate */ 1024*0Sstevel@tonic-gate bskip = 0; 1025*0Sstevel@tonic-gate exacct_order32(&bskip); 1026*0Sstevel@tonic-gate bcopy(&bskip, (char *)buf + bufsize - sizeof (bskip), 1027*0Sstevel@tonic-gate sizeof (bskip)); 1028*0Sstevel@tonic-gate 1029*0Sstevel@tonic-gate if (fwrite(buf, sizeof (char), bufsize, f->ef_fp) != bufsize || 1030*0Sstevel@tonic-gate fflush(f->ef_fp) == EOF) { 1031*0Sstevel@tonic-gate error = EXR_SYSCALL_FAIL; 1032*0Sstevel@tonic-gate goto cleanup2; 1033*0Sstevel@tonic-gate } 1034*0Sstevel@tonic-gate 1035*0Sstevel@tonic-gate cleanup2: 1036*0Sstevel@tonic-gate ea_free(buf, bufsize); 1037*0Sstevel@tonic-gate cleanup1: 1038*0Sstevel@tonic-gate (void) ea_free_item(&vers_obj, EUP_ALLOC); 1039*0Sstevel@tonic-gate (void) ea_free_item(&creator_obj, EUP_ALLOC); 1040*0Sstevel@tonic-gate (void) ea_free_item(&filetype_obj, EUP_ALLOC); 1041*0Sstevel@tonic-gate (void) ea_free_item(&hostname_obj, EUP_ALLOC); 1042*0Sstevel@tonic-gate EXACCT_SET_ERR(error); 1043*0Sstevel@tonic-gate return (error == EXR_OK ? 0 : -1); 1044*0Sstevel@tonic-gate } 1045*0Sstevel@tonic-gate 1046*0Sstevel@tonic-gate const char * 1047*0Sstevel@tonic-gate ea_get_creator(ea_file_t *ef) 1048*0Sstevel@tonic-gate { 1049*0Sstevel@tonic-gate return ((const char *)((ea_file_impl_t *)ef)->ef_creator); 1050*0Sstevel@tonic-gate } 1051*0Sstevel@tonic-gate 1052*0Sstevel@tonic-gate const char * 1053*0Sstevel@tonic-gate ea_get_hostname(ea_file_t *ef) 1054*0Sstevel@tonic-gate { 1055*0Sstevel@tonic-gate return ((const char *)((ea_file_impl_t *)ef)->ef_hostname); 1056*0Sstevel@tonic-gate } 1057*0Sstevel@tonic-gate 1058*0Sstevel@tonic-gate int 1059*0Sstevel@tonic-gate ea_fdopen(ea_file_t *ef, int fd, const char *creator, int aflags, int oflags) 1060*0Sstevel@tonic-gate { 1061*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 1062*0Sstevel@tonic-gate 1063*0Sstevel@tonic-gate bzero(f, sizeof (*f)); 1064*0Sstevel@tonic-gate f->ef_oflags = oflags; 1065*0Sstevel@tonic-gate f->ef_fd = fd; 1066*0Sstevel@tonic-gate 1067*0Sstevel@tonic-gate /* Initialize depth stack. */ 1068*0Sstevel@tonic-gate if (stack_check(f) == -1) { 1069*0Sstevel@tonic-gate /* exacct_error set above. */ 1070*0Sstevel@tonic-gate goto error1; 1071*0Sstevel@tonic-gate } 1072*0Sstevel@tonic-gate 1073*0Sstevel@tonic-gate /* 1074*0Sstevel@tonic-gate * 1. If we are O_CREAT, then we will need to write a header 1075*0Sstevel@tonic-gate * after opening name. 1076*0Sstevel@tonic-gate */ 1077*0Sstevel@tonic-gate if (oflags & O_CREAT) { 1078*0Sstevel@tonic-gate if (creator == NULL) { 1079*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_NO_CREATOR); 1080*0Sstevel@tonic-gate goto error2; 1081*0Sstevel@tonic-gate } 1082*0Sstevel@tonic-gate if ((f->ef_creator = ea_strdup(creator)) == NULL) { 1083*0Sstevel@tonic-gate /* exacct_error set above. */ 1084*0Sstevel@tonic-gate goto error2; 1085*0Sstevel@tonic-gate } 1086*0Sstevel@tonic-gate if ((f->ef_fp = fdopen(f->ef_fd, "w")) == NULL) { 1087*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1088*0Sstevel@tonic-gate goto error3; 1089*0Sstevel@tonic-gate } 1090*0Sstevel@tonic-gate if (write_header(ef) == -1) { 1091*0Sstevel@tonic-gate /* exacct_error set above. */ 1092*0Sstevel@tonic-gate goto error3; 1093*0Sstevel@tonic-gate } 1094*0Sstevel@tonic-gate 1095*0Sstevel@tonic-gate /* 1096*0Sstevel@tonic-gate * 2. If we are not O_CREAT, but are RDWR or WRONLY, we need to 1097*0Sstevel@tonic-gate * seek to EOF so that appends will succeed. 1098*0Sstevel@tonic-gate */ 1099*0Sstevel@tonic-gate } else if (oflags & O_RDWR || oflags & O_WRONLY) { 1100*0Sstevel@tonic-gate if ((f->ef_fp = fdopen(f->ef_fd, "r+")) == NULL) { 1101*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1102*0Sstevel@tonic-gate goto error2; 1103*0Sstevel@tonic-gate } 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gate if ((aflags & EO_VALIDATE_MSK) == EO_VALID_HDR) { 1106*0Sstevel@tonic-gate if (validate_header(ef, creator) < 0) { 1107*0Sstevel@tonic-gate /* exacct_error set above. */ 1108*0Sstevel@tonic-gate goto error2; 1109*0Sstevel@tonic-gate } 1110*0Sstevel@tonic-gate } 1111*0Sstevel@tonic-gate 1112*0Sstevel@tonic-gate if (fseeko(f->ef_fp, 0, SEEK_END) == -1) { 1113*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1114*0Sstevel@tonic-gate goto error2; 1115*0Sstevel@tonic-gate } 1116*0Sstevel@tonic-gate 1117*0Sstevel@tonic-gate /* 1118*0Sstevel@tonic-gate * 3. This is an undefined manner for opening an exacct file. 1119*0Sstevel@tonic-gate */ 1120*0Sstevel@tonic-gate } else if (oflags != O_RDONLY) { 1121*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_NOTSUPP); 1122*0Sstevel@tonic-gate goto error2; 1123*0Sstevel@tonic-gate 1124*0Sstevel@tonic-gate /* 1125*0Sstevel@tonic-gate * 4a. If we are RDONLY, then we are in a position such that 1126*0Sstevel@tonic-gate * either a ea_get_object or an ea_next_object will succeed. If 1127*0Sstevel@tonic-gate * aflags was set to EO_TAIL, seek to the end of the file. 1128*0Sstevel@tonic-gate */ 1129*0Sstevel@tonic-gate } else { 1130*0Sstevel@tonic-gate if ((f->ef_fp = fdopen(f->ef_fd, "r")) == NULL) { 1131*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1132*0Sstevel@tonic-gate goto error2; 1133*0Sstevel@tonic-gate } 1134*0Sstevel@tonic-gate 1135*0Sstevel@tonic-gate if ((aflags & EO_VALIDATE_MSK) == EO_VALID_HDR) { 1136*0Sstevel@tonic-gate if (validate_header(ef, creator) == -1) { 1137*0Sstevel@tonic-gate /* exacct_error set above. */ 1138*0Sstevel@tonic-gate goto error2; 1139*0Sstevel@tonic-gate } 1140*0Sstevel@tonic-gate } 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gate /* 1143*0Sstevel@tonic-gate * 4b. Handle the "open at end" option, for consumers who want 1144*0Sstevel@tonic-gate * to go backwards through the file (i.e. lastcomm). 1145*0Sstevel@tonic-gate */ 1146*0Sstevel@tonic-gate if ((aflags & EO_POSN_MSK) == EO_TAIL) { 1147*0Sstevel@tonic-gate if (fseeko(f->ef_fp, 0, SEEK_END) < 0) { 1148*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1149*0Sstevel@tonic-gate goto error2; 1150*0Sstevel@tonic-gate } 1151*0Sstevel@tonic-gate } 1152*0Sstevel@tonic-gate } 1153*0Sstevel@tonic-gate 1154*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 1155*0Sstevel@tonic-gate return (0); 1156*0Sstevel@tonic-gate 1157*0Sstevel@tonic-gate /* Error cleanup code */ 1158*0Sstevel@tonic-gate error3: 1159*0Sstevel@tonic-gate ea_strfree(f->ef_creator); 1160*0Sstevel@tonic-gate error2: 1161*0Sstevel@tonic-gate stack_free(f); 1162*0Sstevel@tonic-gate error1: 1163*0Sstevel@tonic-gate bzero(f, sizeof (*f)); 1164*0Sstevel@tonic-gate return (-1); 1165*0Sstevel@tonic-gate } 1166*0Sstevel@tonic-gate 1167*0Sstevel@tonic-gate int 1168*0Sstevel@tonic-gate ea_open(ea_file_t *ef, const char *name, const char *creator, 1169*0Sstevel@tonic-gate int aflags, int oflags, mode_t mode) 1170*0Sstevel@tonic-gate { 1171*0Sstevel@tonic-gate int fd; 1172*0Sstevel@tonic-gate 1173*0Sstevel@tonic-gate /* 1174*0Sstevel@tonic-gate * If overwriting an existing file, make sure to truncate it 1175*0Sstevel@tonic-gate * to prevent the file being created corrupt. 1176*0Sstevel@tonic-gate */ 1177*0Sstevel@tonic-gate if (oflags & O_CREAT) 1178*0Sstevel@tonic-gate oflags |= O_TRUNC; 1179*0Sstevel@tonic-gate 1180*0Sstevel@tonic-gate if ((fd = open(name, oflags, mode)) == -1) { 1181*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1182*0Sstevel@tonic-gate return (-1); 1183*0Sstevel@tonic-gate } 1184*0Sstevel@tonic-gate 1185*0Sstevel@tonic-gate if (ea_fdopen(ef, fd, creator, aflags, oflags) == -1) { 1186*0Sstevel@tonic-gate (void) close(fd); 1187*0Sstevel@tonic-gate return (-1); 1188*0Sstevel@tonic-gate } 1189*0Sstevel@tonic-gate 1190*0Sstevel@tonic-gate return (0); 1191*0Sstevel@tonic-gate } 1192*0Sstevel@tonic-gate 1193*0Sstevel@tonic-gate /* 1194*0Sstevel@tonic-gate * ea_close() performs all appropriate close operations on the open exacct file, 1195*0Sstevel@tonic-gate * including releasing any memory allocated while parsing the file. 1196*0Sstevel@tonic-gate */ 1197*0Sstevel@tonic-gate int 1198*0Sstevel@tonic-gate ea_close(ea_file_t *ef) 1199*0Sstevel@tonic-gate { 1200*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 1201*0Sstevel@tonic-gate 1202*0Sstevel@tonic-gate if (f->ef_creator != NULL) 1203*0Sstevel@tonic-gate ea_strfree(f->ef_creator); 1204*0Sstevel@tonic-gate if (f->ef_hostname != NULL) 1205*0Sstevel@tonic-gate ea_strfree(f->ef_hostname); 1206*0Sstevel@tonic-gate 1207*0Sstevel@tonic-gate ea_free(f->ef_depth, f->ef_mxdeep * sizeof (ea_file_depth_t)); 1208*0Sstevel@tonic-gate 1209*0Sstevel@tonic-gate if (fclose(f->ef_fp)) { 1210*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_SYSCALL_FAIL); 1211*0Sstevel@tonic-gate return (-1); 1212*0Sstevel@tonic-gate } 1213*0Sstevel@tonic-gate 1214*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 1215*0Sstevel@tonic-gate return (0); 1216*0Sstevel@tonic-gate } 1217*0Sstevel@tonic-gate 1218*0Sstevel@tonic-gate /* 1219*0Sstevel@tonic-gate * Empty the input buffer and clear any underlying EOF or error bits set on the 1220*0Sstevel@tonic-gate * underlying FILE. This can be used by any library clients who wish to handle 1221*0Sstevel@tonic-gate * files that are in motion or who wish to seek the underlying file descriptor. 1222*0Sstevel@tonic-gate */ 1223*0Sstevel@tonic-gate void 1224*0Sstevel@tonic-gate ea_clear(ea_file_t *ef) 1225*0Sstevel@tonic-gate { 1226*0Sstevel@tonic-gate ea_file_impl_t *f = (ea_file_impl_t *)ef; 1227*0Sstevel@tonic-gate 1228*0Sstevel@tonic-gate (void) fflush(f->ef_fp); 1229*0Sstevel@tonic-gate clearerr(f->ef_fp); 1230*0Sstevel@tonic-gate } 1231*0Sstevel@tonic-gate 1232*0Sstevel@tonic-gate /* 1233*0Sstevel@tonic-gate * Copy an ea_object_t. Note that in the case of a group, just the group 1234*0Sstevel@tonic-gate * object will be copied, and not its list of members. To recursively copy 1235*0Sstevel@tonic-gate * a group or a list of items use ea_copy_tree(). 1236*0Sstevel@tonic-gate */ 1237*0Sstevel@tonic-gate ea_object_t * 1238*0Sstevel@tonic-gate ea_copy_object(const ea_object_t *src) 1239*0Sstevel@tonic-gate { 1240*0Sstevel@tonic-gate ea_object_t *dst; 1241*0Sstevel@tonic-gate 1242*0Sstevel@tonic-gate /* Allocate a new object and copy to it. */ 1243*0Sstevel@tonic-gate if ((dst = ea_alloc(sizeof (ea_object_t))) == NULL) { 1244*0Sstevel@tonic-gate return (NULL); 1245*0Sstevel@tonic-gate } 1246*0Sstevel@tonic-gate bcopy(src, dst, sizeof (ea_object_t)); 1247*0Sstevel@tonic-gate dst->eo_next = NULL; 1248*0Sstevel@tonic-gate 1249*0Sstevel@tonic-gate switch (src->eo_type) { 1250*0Sstevel@tonic-gate case EO_GROUP: 1251*0Sstevel@tonic-gate dst->eo_group.eg_nobjs = 0; 1252*0Sstevel@tonic-gate dst->eo_group.eg_objs = NULL; 1253*0Sstevel@tonic-gate break; 1254*0Sstevel@tonic-gate case EO_ITEM: 1255*0Sstevel@tonic-gate /* Items containing pointers need special treatment. */ 1256*0Sstevel@tonic-gate switch (src->eo_catalog & EXT_TYPE_MASK) { 1257*0Sstevel@tonic-gate case EXT_STRING: 1258*0Sstevel@tonic-gate if (src->eo_item.ei_string != NULL) { 1259*0Sstevel@tonic-gate dst->eo_item.ei_string = 1260*0Sstevel@tonic-gate ea_strdup(src->eo_item.ei_string); 1261*0Sstevel@tonic-gate if (dst->eo_item.ei_string == NULL) { 1262*0Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC); 1263*0Sstevel@tonic-gate return (NULL); 1264*0Sstevel@tonic-gate } 1265*0Sstevel@tonic-gate } 1266*0Sstevel@tonic-gate break; 1267*0Sstevel@tonic-gate case EXT_RAW: 1268*0Sstevel@tonic-gate if (src->eo_item.ei_raw != NULL) { 1269*0Sstevel@tonic-gate dst->eo_item.ei_raw = 1270*0Sstevel@tonic-gate ea_alloc(src->eo_item.ei_size); 1271*0Sstevel@tonic-gate if (dst->eo_item.ei_raw == NULL) { 1272*0Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC); 1273*0Sstevel@tonic-gate return (NULL); 1274*0Sstevel@tonic-gate } 1275*0Sstevel@tonic-gate bcopy(src->eo_item.ei_raw, dst->eo_item.ei_raw, 1276*0Sstevel@tonic-gate (size_t)src->eo_item.ei_size); 1277*0Sstevel@tonic-gate } 1278*0Sstevel@tonic-gate break; 1279*0Sstevel@tonic-gate case EXT_EXACCT_OBJECT: 1280*0Sstevel@tonic-gate if (src->eo_item.ei_object != NULL) { 1281*0Sstevel@tonic-gate dst->eo_item.ei_object = 1282*0Sstevel@tonic-gate ea_alloc(src->eo_item.ei_size); 1283*0Sstevel@tonic-gate if (dst->eo_item.ei_object == NULL) { 1284*0Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC); 1285*0Sstevel@tonic-gate return (NULL); 1286*0Sstevel@tonic-gate } 1287*0Sstevel@tonic-gate bcopy(src->eo_item.ei_raw, dst->eo_item.ei_raw, 1288*0Sstevel@tonic-gate (size_t)src->eo_item.ei_size); 1289*0Sstevel@tonic-gate } 1290*0Sstevel@tonic-gate break; 1291*0Sstevel@tonic-gate default: 1292*0Sstevel@tonic-gate /* Other item types require no special handling. */ 1293*0Sstevel@tonic-gate break; 1294*0Sstevel@tonic-gate } 1295*0Sstevel@tonic-gate break; 1296*0Sstevel@tonic-gate default: 1297*0Sstevel@tonic-gate ea_free_object(dst, EUP_ALLOC); 1298*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_INVALID_OBJ); 1299*0Sstevel@tonic-gate return (NULL); 1300*0Sstevel@tonic-gate } 1301*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 1302*0Sstevel@tonic-gate return (dst); 1303*0Sstevel@tonic-gate } 1304*0Sstevel@tonic-gate 1305*0Sstevel@tonic-gate /* 1306*0Sstevel@tonic-gate * Recursively copy a list of ea_object_t. All the elements in the eo_next 1307*0Sstevel@tonic-gate * list will be copied, and any group objects will be recursively copied. 1308*0Sstevel@tonic-gate */ 1309*0Sstevel@tonic-gate ea_object_t * 1310*0Sstevel@tonic-gate ea_copy_object_tree(const ea_object_t *src) 1311*0Sstevel@tonic-gate { 1312*0Sstevel@tonic-gate ea_object_t *ret_obj, *dst, *last; 1313*0Sstevel@tonic-gate 1314*0Sstevel@tonic-gate for (ret_obj = last = NULL; src != NULL; 1315*0Sstevel@tonic-gate last = dst, src = src->eo_next) { 1316*0Sstevel@tonic-gate 1317*0Sstevel@tonic-gate /* Allocate a new object and copy to it. */ 1318*0Sstevel@tonic-gate if ((dst = ea_copy_object(src)) == NULL) { 1319*0Sstevel@tonic-gate ea_free_object(ret_obj, EUP_ALLOC); 1320*0Sstevel@tonic-gate return (NULL); 1321*0Sstevel@tonic-gate } 1322*0Sstevel@tonic-gate 1323*0Sstevel@tonic-gate /* Groups need the object list copying. */ 1324*0Sstevel@tonic-gate if (src->eo_type == EO_GROUP) { 1325*0Sstevel@tonic-gate dst->eo_group.eg_objs = 1326*0Sstevel@tonic-gate ea_copy_object_tree(src->eo_group.eg_objs); 1327*0Sstevel@tonic-gate if (dst->eo_group.eg_objs == NULL) { 1328*0Sstevel@tonic-gate ea_free_object(ret_obj, EUP_ALLOC); 1329*0Sstevel@tonic-gate return (NULL); 1330*0Sstevel@tonic-gate } 1331*0Sstevel@tonic-gate dst->eo_group.eg_nobjs = src->eo_group.eg_nobjs; 1332*0Sstevel@tonic-gate } 1333*0Sstevel@tonic-gate 1334*0Sstevel@tonic-gate /* Remember the list head the first time round. */ 1335*0Sstevel@tonic-gate if (ret_obj == NULL) { 1336*0Sstevel@tonic-gate ret_obj = dst; 1337*0Sstevel@tonic-gate } 1338*0Sstevel@tonic-gate 1339*0Sstevel@tonic-gate /* Link together if not at the list head. */ 1340*0Sstevel@tonic-gate if (last != NULL) { 1341*0Sstevel@tonic-gate last->eo_next = dst; 1342*0Sstevel@tonic-gate } 1343*0Sstevel@tonic-gate } 1344*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 1345*0Sstevel@tonic-gate return (ret_obj); 1346*0Sstevel@tonic-gate } 1347*0Sstevel@tonic-gate 1348*0Sstevel@tonic-gate /* 1349*0Sstevel@tonic-gate * Read in the specified number of objects, returning the same data 1350*0Sstevel@tonic-gate * structure that would have originally been passed to ea_write(). 1351*0Sstevel@tonic-gate */ 1352*0Sstevel@tonic-gate ea_object_t * 1353*0Sstevel@tonic-gate ea_get_object_tree(ea_file_t *ef, uint32_t nobj) 1354*0Sstevel@tonic-gate { 1355*0Sstevel@tonic-gate ea_object_t *first_obj, *prev_obj, *obj; 1356*0Sstevel@tonic-gate 1357*0Sstevel@tonic-gate first_obj = prev_obj = NULL; 1358*0Sstevel@tonic-gate while (nobj--) { 1359*0Sstevel@tonic-gate /* Allocate space for the new object. */ 1360*0Sstevel@tonic-gate obj = ea_alloc(sizeof (ea_object_t)); 1361*0Sstevel@tonic-gate bzero(obj, sizeof (*obj)); 1362*0Sstevel@tonic-gate 1363*0Sstevel@tonic-gate /* Read it in. */ 1364*0Sstevel@tonic-gate if (ea_get_object(ef, obj) == -1) { 1365*0Sstevel@tonic-gate ea_free(obj, sizeof (ea_object_t)); 1366*0Sstevel@tonic-gate if (first_obj != NULL) { 1367*0Sstevel@tonic-gate ea_free_object(first_obj, EUP_ALLOC); 1368*0Sstevel@tonic-gate } 1369*0Sstevel@tonic-gate return (NULL); 1370*0Sstevel@tonic-gate } 1371*0Sstevel@tonic-gate 1372*0Sstevel@tonic-gate /* Link it into the list. */ 1373*0Sstevel@tonic-gate if (first_obj == NULL) { 1374*0Sstevel@tonic-gate first_obj = obj; 1375*0Sstevel@tonic-gate } 1376*0Sstevel@tonic-gate if (prev_obj != NULL) { 1377*0Sstevel@tonic-gate prev_obj->eo_next = obj; 1378*0Sstevel@tonic-gate } 1379*0Sstevel@tonic-gate prev_obj = obj; 1380*0Sstevel@tonic-gate 1381*0Sstevel@tonic-gate /* Recurse if the object is a group with contents. */ 1382*0Sstevel@tonic-gate if (obj->eo_type == EO_GROUP && obj->eo_group.eg_nobjs > 0) { 1383*0Sstevel@tonic-gate if ((obj->eo_group.eg_objs = ea_get_object_tree(ef, 1384*0Sstevel@tonic-gate obj->eo_group.eg_nobjs)) == NULL) { 1385*0Sstevel@tonic-gate /* exacct_error set above. */ 1386*0Sstevel@tonic-gate ea_free_object(first_obj, EUP_ALLOC); 1387*0Sstevel@tonic-gate return (NULL); 1388*0Sstevel@tonic-gate } 1389*0Sstevel@tonic-gate } 1390*0Sstevel@tonic-gate } 1391*0Sstevel@tonic-gate EXACCT_SET_ERR(EXR_OK); 1392*0Sstevel@tonic-gate return (first_obj); 1393*0Sstevel@tonic-gate } 1394