1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/acctctl.h> 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <unistd.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 36*7c478bd9Sstevel@tonic-gate #include <stdio.h> 37*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate #include <limits.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include "aconf.h" 42*7c478bd9Sstevel@tonic-gate #include "utils.h" 43*7c478bd9Sstevel@tonic-gate #include "res.h" 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #define BUFSZ (PATH_MAX + 80) 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate typedef struct ac_token { 48*7c478bd9Sstevel@tonic-gate char *tok_name; 49*7c478bd9Sstevel@tonic-gate int tok_type; 50*7c478bd9Sstevel@tonic-gate int (*tok_parse)(acctconf_t *, char *, int); 51*7c478bd9Sstevel@tonic-gate int (*tok_print)(acctconf_t *, FILE *, int); 52*7c478bd9Sstevel@tonic-gate } ac_token_t; 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate static int print_enable(acctconf_t *, FILE *, int); 55*7c478bd9Sstevel@tonic-gate static int print_file(acctconf_t *, FILE *, int); 56*7c478bd9Sstevel@tonic-gate static int print_tracked(acctconf_t *, FILE *, int); 57*7c478bd9Sstevel@tonic-gate static int print_untracked(acctconf_t *, FILE *, int); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static ac_token_t tokens[] = { 60*7c478bd9Sstevel@tonic-gate { "ACCTADM_PROC_ENABLE", 61*7c478bd9Sstevel@tonic-gate AC_PROC, aconf_str2enable, print_enable }, 62*7c478bd9Sstevel@tonic-gate { "ACCTADM_PROC_FILE", 63*7c478bd9Sstevel@tonic-gate AC_PROC, aconf_str2file, print_file }, 64*7c478bd9Sstevel@tonic-gate { "ACCTADM_PROC_TRACKED", 65*7c478bd9Sstevel@tonic-gate AC_PROC, aconf_str2tracked, print_tracked }, 66*7c478bd9Sstevel@tonic-gate { "ACCTADM_PROC_UNTRACKED", 67*7c478bd9Sstevel@tonic-gate AC_PROC, aconf_str2untracked, print_untracked }, 68*7c478bd9Sstevel@tonic-gate { "ACCTADM_TASK_ENABLE", 69*7c478bd9Sstevel@tonic-gate AC_TASK, aconf_str2enable, print_enable }, 70*7c478bd9Sstevel@tonic-gate { "ACCTADM_TASK_FILE", 71*7c478bd9Sstevel@tonic-gate AC_TASK, aconf_str2file, print_file }, 72*7c478bd9Sstevel@tonic-gate { "ACCTADM_TASK_TRACKED", 73*7c478bd9Sstevel@tonic-gate AC_TASK, aconf_str2tracked, print_tracked }, 74*7c478bd9Sstevel@tonic-gate { "ACCTADM_TASK_UNTRACKED", 75*7c478bd9Sstevel@tonic-gate AC_TASK, aconf_str2untracked, print_untracked }, 76*7c478bd9Sstevel@tonic-gate { "ACCTADM_FLOW_ENABLE", 77*7c478bd9Sstevel@tonic-gate AC_FLOW, aconf_str2enable, print_enable }, 78*7c478bd9Sstevel@tonic-gate { "ACCTADM_FLOW_FILE", 79*7c478bd9Sstevel@tonic-gate AC_FLOW, aconf_str2file, print_file }, 80*7c478bd9Sstevel@tonic-gate { "ACCTADM_FLOW_TRACKED", 81*7c478bd9Sstevel@tonic-gate AC_FLOW, aconf_str2tracked, print_tracked }, 82*7c478bd9Sstevel@tonic-gate { "ACCTADM_FLOW_UNTRACKED", 83*7c478bd9Sstevel@tonic-gate AC_FLOW, aconf_str2untracked, print_untracked }, 84*7c478bd9Sstevel@tonic-gate { NULL, 85*7c478bd9Sstevel@tonic-gate AC_NONE, NULL, NULL } 86*7c478bd9Sstevel@tonic-gate }; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate void 89*7c478bd9Sstevel@tonic-gate aconf_init(acctconf_t *acp) 90*7c478bd9Sstevel@tonic-gate { 91*7c478bd9Sstevel@tonic-gate void *buf; 92*7c478bd9Sstevel@tonic-gate void *pathname; 93*7c478bd9Sstevel@tonic-gate char *tracked, *untracked; 94*7c478bd9Sstevel@tonic-gate int state; 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate if ((buf = malloc(AC_BUFSIZE)) == NULL || 97*7c478bd9Sstevel@tonic-gate (pathname = malloc(MAXPATHLEN)) == NULL) 98*7c478bd9Sstevel@tonic-gate die(gettext("not enough memory\n")); 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate /* 101*7c478bd9Sstevel@tonic-gate * Initialize process accounting settings 102*7c478bd9Sstevel@tonic-gate */ 103*7c478bd9Sstevel@tonic-gate (void) memset(pathname, 0, MAXPATHLEN); 104*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_STATE_GET, &state, sizeof (int)) == -1) 105*7c478bd9Sstevel@tonic-gate die(gettext("cannot get process accounting state\n")); 106*7c478bd9Sstevel@tonic-gate acp->ac_proc_state = state; 107*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_FILE_GET, pathname, MAXPATHLEN) == -1) { 108*7c478bd9Sstevel@tonic-gate if (errno == ENOTACTIVE) 109*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_file, 110*7c478bd9Sstevel@tonic-gate AC_STR_NONE, MAXPATHLEN); 111*7c478bd9Sstevel@tonic-gate else 112*7c478bd9Sstevel@tonic-gate die(gettext("cannot get process accounting file name")); 113*7c478bd9Sstevel@tonic-gate } else { 114*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_file, pathname, MAXPATHLEN); 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate (void) memset(buf, 0, AC_BUFSIZE); 117*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_RES_GET, buf, AC_BUFSIZE) == -1) 118*7c478bd9Sstevel@tonic-gate die(gettext("cannot obtain the list of enabled resources\n")); 119*7c478bd9Sstevel@tonic-gate tracked = buf2str(buf, AC_BUFSIZE, AC_ON, AC_PROC); 120*7c478bd9Sstevel@tonic-gate untracked = buf2str(buf, AC_BUFSIZE, AC_OFF, AC_PROC); 121*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_tracked, tracked, MAXRESLEN); 122*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_untracked, untracked, MAXRESLEN); 123*7c478bd9Sstevel@tonic-gate free(tracked); 124*7c478bd9Sstevel@tonic-gate free(untracked); 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate /* 127*7c478bd9Sstevel@tonic-gate * Initialize flow accounting settings 128*7c478bd9Sstevel@tonic-gate */ 129*7c478bd9Sstevel@tonic-gate (void) memset(pathname, 0, MAXPATHLEN); 130*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_STATE_GET, &state, sizeof (int)) == -1) 131*7c478bd9Sstevel@tonic-gate die(gettext("cannot get flow accounting state\n")); 132*7c478bd9Sstevel@tonic-gate acp->ac_flow_state = state; 133*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_FILE_GET, pathname, MAXPATHLEN) == -1) { 134*7c478bd9Sstevel@tonic-gate if (errno == ENOTACTIVE) 135*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_file, 136*7c478bd9Sstevel@tonic-gate AC_STR_NONE, MAXPATHLEN); 137*7c478bd9Sstevel@tonic-gate else 138*7c478bd9Sstevel@tonic-gate die(gettext("cannot get flow accounting file name")); 139*7c478bd9Sstevel@tonic-gate } else { 140*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_file, pathname, MAXPATHLEN); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate (void) memset(buf, 0, AC_BUFSIZE); 143*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_RES_GET, buf, AC_BUFSIZE) == -1) 144*7c478bd9Sstevel@tonic-gate die(gettext("cannot obtain the list of enabled resources\n")); 145*7c478bd9Sstevel@tonic-gate tracked = buf2str(buf, AC_BUFSIZE, AC_ON, AC_FLOW); 146*7c478bd9Sstevel@tonic-gate untracked = buf2str(buf, AC_BUFSIZE, AC_OFF, AC_FLOW); 147*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_tracked, tracked, MAXRESLEN); 148*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_untracked, untracked, MAXRESLEN); 149*7c478bd9Sstevel@tonic-gate free(tracked); 150*7c478bd9Sstevel@tonic-gate free(untracked); 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* 153*7c478bd9Sstevel@tonic-gate * Initialize task accounting settings 154*7c478bd9Sstevel@tonic-gate */ 155*7c478bd9Sstevel@tonic-gate (void) memset(pathname, 0, MAXPATHLEN); 156*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_STATE_GET, &state, sizeof (int)) == -1) 157*7c478bd9Sstevel@tonic-gate die(gettext("cannot get task accounting state\n")); 158*7c478bd9Sstevel@tonic-gate acp->ac_task_state = state; 159*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_FILE_GET, pathname, MAXPATHLEN) == -1) { 160*7c478bd9Sstevel@tonic-gate if (errno == ENOTACTIVE) 161*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_file, 162*7c478bd9Sstevel@tonic-gate AC_STR_NONE, MAXPATHLEN); 163*7c478bd9Sstevel@tonic-gate else 164*7c478bd9Sstevel@tonic-gate die(gettext("cannot get task accounting file name")); 165*7c478bd9Sstevel@tonic-gate } else { 166*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_file, pathname, MAXPATHLEN); 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate (void) memset(buf, 0, AC_BUFSIZE); 169*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_RES_GET, buf, AC_BUFSIZE) == -1) 170*7c478bd9Sstevel@tonic-gate die(gettext("cannot obtain the list of enabled resources\n")); 171*7c478bd9Sstevel@tonic-gate tracked = buf2str(buf, AC_BUFSIZE, AC_ON, AC_TASK); 172*7c478bd9Sstevel@tonic-gate untracked = buf2str(buf, AC_BUFSIZE, AC_OFF, AC_TASK); 173*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_tracked, tracked, MAXRESLEN); 174*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_untracked, untracked, MAXRESLEN); 175*7c478bd9Sstevel@tonic-gate free(pathname); 176*7c478bd9Sstevel@tonic-gate free(buf); 177*7c478bd9Sstevel@tonic-gate free(tracked); 178*7c478bd9Sstevel@tonic-gate free(untracked); 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate int 182*7c478bd9Sstevel@tonic-gate aconf_create(acctconf_t *acp, const char *fpath) 183*7c478bd9Sstevel@tonic-gate { 184*7c478bd9Sstevel@tonic-gate if ((acp->ac_conf_fd = open(fpath, O_RDWR | O_CREAT, AC_PERM)) == -1) { 185*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open %s"), fpath); 186*7c478bd9Sstevel@tonic-gate return (-1); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate if ((acp->ac_conf_fp = fdopen(acp->ac_conf_fd, "r+")) == NULL) { 189*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open stream for %s"), fpath); 190*7c478bd9Sstevel@tonic-gate return (-1); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate return (0); 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate int 196*7c478bd9Sstevel@tonic-gate aconf_open(acctconf_t *acp, const char *fpath) 197*7c478bd9Sstevel@tonic-gate { 198*7c478bd9Sstevel@tonic-gate char buf[BUFSZ]; 199*7c478bd9Sstevel@tonic-gate int line; 200*7c478bd9Sstevel@tonic-gate int ret = 0; 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate if ((acp->ac_conf_fd = open(fpath, O_RDONLY, AC_PERM)) == -1) { 203*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open %s"), fpath); 204*7c478bd9Sstevel@tonic-gate return (-1); 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate if ((acp->ac_conf_fp = fdopen(acp->ac_conf_fd, "r")) == NULL) { 207*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open stream for %s"), fpath); 208*7c478bd9Sstevel@tonic-gate return (-1); 209*7c478bd9Sstevel@tonic-gate } 210*7c478bd9Sstevel@tonic-gate for (line = 1; fgets(buf, BUFSZ, acp->ac_conf_fp) != NULL; line++) { 211*7c478bd9Sstevel@tonic-gate char name[BUFSZ], value[BUFSZ]; 212*7c478bd9Sstevel@tonic-gate ac_token_t *tokp; 213*7c478bd9Sstevel@tonic-gate int len; 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate if (buf[0] == '#' || buf[0] == '\n') 216*7c478bd9Sstevel@tonic-gate continue; 217*7c478bd9Sstevel@tonic-gate /* 218*7c478bd9Sstevel@tonic-gate * Look for "name=value", with optional whitespace on either 219*7c478bd9Sstevel@tonic-gate * side, terminated by a newline, and consuming the whole line. 220*7c478bd9Sstevel@tonic-gate */ 221*7c478bd9Sstevel@tonic-gate /* LINTED - unbounded string specifier */ 222*7c478bd9Sstevel@tonic-gate if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 && 223*7c478bd9Sstevel@tonic-gate name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) { 224*7c478bd9Sstevel@tonic-gate /* 225*7c478bd9Sstevel@tonic-gate * Locate a matching token in the tokens[] table, 226*7c478bd9Sstevel@tonic-gate * and invoke its parsing function. 227*7c478bd9Sstevel@tonic-gate */ 228*7c478bd9Sstevel@tonic-gate for (tokp = tokens; tokp->tok_name != NULL; tokp++) { 229*7c478bd9Sstevel@tonic-gate if (strcmp(name, tokp->tok_name) == 0) { 230*7c478bd9Sstevel@tonic-gate if (tokp->tok_parse(acp, value, 231*7c478bd9Sstevel@tonic-gate tokp->tok_type) == -1) { 232*7c478bd9Sstevel@tonic-gate warn(gettext("\"%s\", line %d: " 233*7c478bd9Sstevel@tonic-gate "warning: invalid %s\n"), 234*7c478bd9Sstevel@tonic-gate fpath, line, name); 235*7c478bd9Sstevel@tonic-gate ret = -1; 236*7c478bd9Sstevel@tonic-gate } 237*7c478bd9Sstevel@tonic-gate break; 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate } 240*7c478bd9Sstevel@tonic-gate /* 241*7c478bd9Sstevel@tonic-gate * If we hit the end of the tokens[] table, 242*7c478bd9Sstevel@tonic-gate * no matching token was found. 243*7c478bd9Sstevel@tonic-gate */ 244*7c478bd9Sstevel@tonic-gate if (tokp->tok_name == NULL) { 245*7c478bd9Sstevel@tonic-gate warn(gettext("\"%s\", line %d: warning: " 246*7c478bd9Sstevel@tonic-gate "invalid token: %s\n"), fpath, line, name); 247*7c478bd9Sstevel@tonic-gate ret = -1; 248*7c478bd9Sstevel@tonic-gate } 249*7c478bd9Sstevel@tonic-gate } else { 250*7c478bd9Sstevel@tonic-gate warn(gettext("\"%s\", line %d: syntax error\n"), 251*7c478bd9Sstevel@tonic-gate fpath, line); 252*7c478bd9Sstevel@tonic-gate ret = -1; 253*7c478bd9Sstevel@tonic-gate } 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate if (line == 1) { 256*7c478bd9Sstevel@tonic-gate warn(gettext("cannot read settings from %s\n"), fpath); 257*7c478bd9Sstevel@tonic-gate ret = -1; 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate return (ret); 260*7c478bd9Sstevel@tonic-gate } 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate int 263*7c478bd9Sstevel@tonic-gate aconf_setup(acctconf_t *acp) 264*7c478bd9Sstevel@tonic-gate { 265*7c478bd9Sstevel@tonic-gate void *buf; 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate if ((buf = malloc(AC_BUFSIZE)) == NULL) 268*7c478bd9Sstevel@tonic-gate die(gettext("not enough memory\n")); 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate /* 271*7c478bd9Sstevel@tonic-gate * Setup process accounting 272*7c478bd9Sstevel@tonic-gate */ 273*7c478bd9Sstevel@tonic-gate (void) memset(buf, 0, AC_BUFSIZE); 274*7c478bd9Sstevel@tonic-gate str2buf(buf, acp->ac_proc_untracked, AC_OFF, AC_PROC); 275*7c478bd9Sstevel@tonic-gate str2buf(buf, acp->ac_proc_tracked, AC_ON, AC_PROC); 276*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_RES_SET, buf, AC_BUFSIZE) == -1) { 277*7c478bd9Sstevel@tonic-gate warn(gettext("cannot enable or disable resources\n")); 278*7c478bd9Sstevel@tonic-gate return (-1); 279*7c478bd9Sstevel@tonic-gate } 280*7c478bd9Sstevel@tonic-gate if (strcmp(acp->ac_proc_file, AC_STR_NONE) != 0) { 281*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_FILE_SET, 282*7c478bd9Sstevel@tonic-gate acp->ac_proc_file, strlen(acp->ac_proc_file) + 1) == -1) { 283*7c478bd9Sstevel@tonic-gate warn(gettext("cannot open accounting file")); 284*7c478bd9Sstevel@tonic-gate return (-1); 285*7c478bd9Sstevel@tonic-gate } 286*7c478bd9Sstevel@tonic-gate } else { 287*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_FILE_SET, NULL, 0) == -1) { 288*7c478bd9Sstevel@tonic-gate warn(gettext("cannot close accounting file\n")); 289*7c478bd9Sstevel@tonic-gate return (-1); 290*7c478bd9Sstevel@tonic-gate } 291*7c478bd9Sstevel@tonic-gate } 292*7c478bd9Sstevel@tonic-gate if (acctctl(AC_PROC | AC_STATE_SET, &acp->ac_proc_state, 293*7c478bd9Sstevel@tonic-gate sizeof (int)) == -1) { 294*7c478bd9Sstevel@tonic-gate warn(gettext("cannot enable/disable process accounting")); 295*7c478bd9Sstevel@tonic-gate return (-1); 296*7c478bd9Sstevel@tonic-gate } 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate /* 299*7c478bd9Sstevel@tonic-gate * Setup flow accounting 300*7c478bd9Sstevel@tonic-gate */ 301*7c478bd9Sstevel@tonic-gate (void) memset(buf, 0, AC_BUFSIZE); 302*7c478bd9Sstevel@tonic-gate str2buf(buf, acp->ac_flow_untracked, AC_OFF, AC_FLOW); 303*7c478bd9Sstevel@tonic-gate str2buf(buf, acp->ac_flow_tracked, AC_ON, AC_FLOW); 304*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_RES_SET, buf, AC_BUFSIZE) == -1) { 305*7c478bd9Sstevel@tonic-gate warn(gettext("cannot enable or disable resources\n")); 306*7c478bd9Sstevel@tonic-gate return (-1); 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate if (strcmp(acp->ac_flow_file, AC_STR_NONE) != 0) { 309*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_FILE_SET, 310*7c478bd9Sstevel@tonic-gate acp->ac_flow_file, strlen(acp->ac_flow_file) + 1) == -1) { 311*7c478bd9Sstevel@tonic-gate warn(gettext("cannot open accounting file")); 312*7c478bd9Sstevel@tonic-gate return (-1); 313*7c478bd9Sstevel@tonic-gate } 314*7c478bd9Sstevel@tonic-gate } else { 315*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_FILE_SET, NULL, 0) == -1) { 316*7c478bd9Sstevel@tonic-gate warn(gettext("cannot close accounting file\n")); 317*7c478bd9Sstevel@tonic-gate return (-1); 318*7c478bd9Sstevel@tonic-gate } 319*7c478bd9Sstevel@tonic-gate } 320*7c478bd9Sstevel@tonic-gate if (acctctl(AC_FLOW | AC_STATE_SET, &acp->ac_flow_state, 321*7c478bd9Sstevel@tonic-gate sizeof (int)) == -1) { 322*7c478bd9Sstevel@tonic-gate warn(gettext("cannot enable/disable flow accounting")); 323*7c478bd9Sstevel@tonic-gate return (-1); 324*7c478bd9Sstevel@tonic-gate } 325*7c478bd9Sstevel@tonic-gate 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate /* 328*7c478bd9Sstevel@tonic-gate * Setup task accounting 329*7c478bd9Sstevel@tonic-gate */ 330*7c478bd9Sstevel@tonic-gate (void) memset(buf, 0, AC_BUFSIZE); 331*7c478bd9Sstevel@tonic-gate str2buf(buf, acp->ac_task_untracked, AC_OFF, AC_TASK); 332*7c478bd9Sstevel@tonic-gate str2buf(buf, acp->ac_task_tracked, AC_ON, AC_TASK); 333*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_RES_SET, buf, AC_BUFSIZE) == -1) { 334*7c478bd9Sstevel@tonic-gate warn(gettext("cannot enable or disable resources\n")); 335*7c478bd9Sstevel@tonic-gate return (-1); 336*7c478bd9Sstevel@tonic-gate } 337*7c478bd9Sstevel@tonic-gate if (strcmp(acp->ac_task_file, AC_STR_NONE) != 0) { 338*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_FILE_SET, 339*7c478bd9Sstevel@tonic-gate acp->ac_task_file, strlen(acp->ac_task_file) + 1) == -1) { 340*7c478bd9Sstevel@tonic-gate warn(gettext("cannot set accounting file")); 341*7c478bd9Sstevel@tonic-gate return (-1); 342*7c478bd9Sstevel@tonic-gate } 343*7c478bd9Sstevel@tonic-gate } else { 344*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_FILE_SET, NULL, 0) == -1) { 345*7c478bd9Sstevel@tonic-gate warn(gettext("cannot close accounting file\n")); 346*7c478bd9Sstevel@tonic-gate return (-1); 347*7c478bd9Sstevel@tonic-gate } 348*7c478bd9Sstevel@tonic-gate } 349*7c478bd9Sstevel@tonic-gate if (acctctl(AC_TASK | AC_STATE_SET, &acp->ac_task_state, 350*7c478bd9Sstevel@tonic-gate sizeof (int)) == -1) { 351*7c478bd9Sstevel@tonic-gate warn(gettext("cannot enable/disable task accounting")); 352*7c478bd9Sstevel@tonic-gate return (-1); 353*7c478bd9Sstevel@tonic-gate } 354*7c478bd9Sstevel@tonic-gate 355*7c478bd9Sstevel@tonic-gate free(buf); 356*7c478bd9Sstevel@tonic-gate return (0); 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate 359*7c478bd9Sstevel@tonic-gate int 360*7c478bd9Sstevel@tonic-gate aconf_close(acctconf_t *acp) 361*7c478bd9Sstevel@tonic-gate { 362*7c478bd9Sstevel@tonic-gate if (acp->ac_conf_fp != NULL && fclose(acp->ac_conf_fp) != 0) 363*7c478bd9Sstevel@tonic-gate return (-1); 364*7c478bd9Sstevel@tonic-gate else 365*7c478bd9Sstevel@tonic-gate return (0); 366*7c478bd9Sstevel@tonic-gate } 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate int 369*7c478bd9Sstevel@tonic-gate aconf_write(acctconf_t *acp) 370*7c478bd9Sstevel@tonic-gate { 371*7c478bd9Sstevel@tonic-gate ac_token_t *tokp; 372*7c478bd9Sstevel@tonic-gate 373*7c478bd9Sstevel@tonic-gate if (fseeko(acp->ac_conf_fp, (off_t)0, SEEK_SET) == -1) { 374*7c478bd9Sstevel@tonic-gate warn(gettext("failed to seek config file")); 375*7c478bd9Sstevel@tonic-gate return (-1); 376*7c478bd9Sstevel@tonic-gate } 377*7c478bd9Sstevel@tonic-gate if (ftruncate(acp->ac_conf_fd, (off_t)0) == -1) { 378*7c478bd9Sstevel@tonic-gate warn(gettext("failed to truncate config file")); 379*7c478bd9Sstevel@tonic-gate return (-1); 380*7c478bd9Sstevel@tonic-gate } 381*7c478bd9Sstevel@tonic-gate (void) fputs("#\n# acctadm.conf\n#\n" 382*7c478bd9Sstevel@tonic-gate "# Configuration parameters for extended accounting.\n" 383*7c478bd9Sstevel@tonic-gate "# Do NOT edit this file by hand -- use acctadm(1m) instead.\n" 384*7c478bd9Sstevel@tonic-gate "#\n", acp->ac_conf_fp); 385*7c478bd9Sstevel@tonic-gate for (tokp = tokens; tokp->tok_name != NULL; tokp++) { 386*7c478bd9Sstevel@tonic-gate if (fprintf(acp->ac_conf_fp, "%s=", tokp->tok_name) == -1 || 387*7c478bd9Sstevel@tonic-gate tokp->tok_print(acp, acp->ac_conf_fp, 388*7c478bd9Sstevel@tonic-gate tokp->tok_type) == -1) { 389*7c478bd9Sstevel@tonic-gate warn(gettext("failed to write token")); 390*7c478bd9Sstevel@tonic-gate return (-1); 391*7c478bd9Sstevel@tonic-gate } 392*7c478bd9Sstevel@tonic-gate } 393*7c478bd9Sstevel@tonic-gate if (fflush(acp->ac_conf_fp) != 0) 394*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to flush config file")); 395*7c478bd9Sstevel@tonic-gate if (fsync(acp->ac_conf_fd) == -1) 396*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to sync config file to disk")); 397*7c478bd9Sstevel@tonic-gate if (fchmod(acp->ac_conf_fd, AC_PERM) == -1) 398*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to reset mode on config file")); 399*7c478bd9Sstevel@tonic-gate if (fchown(acp->ac_conf_fd, AC_OWNER, AC_GROUP) == -1) 400*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to reset owner on config file")); 401*7c478bd9Sstevel@tonic-gate return (0); 402*7c478bd9Sstevel@tonic-gate } 403*7c478bd9Sstevel@tonic-gate 404*7c478bd9Sstevel@tonic-gate void 405*7c478bd9Sstevel@tonic-gate aconf_print(acctconf_t *acp, FILE *fp, int type) 406*7c478bd9Sstevel@tonic-gate { 407*7c478bd9Sstevel@tonic-gate if (type & AC_TASK) { 408*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 409*7c478bd9Sstevel@tonic-gate gettext(" Task accounting: %s\n"), 410*7c478bd9Sstevel@tonic-gate acp->ac_task_state ? 411*7c478bd9Sstevel@tonic-gate gettext("active") : gettext("inactive")); 412*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 413*7c478bd9Sstevel@tonic-gate gettext(" Task accounting file: %s\n"), 414*7c478bd9Sstevel@tonic-gate acp->ac_task_file); 415*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 416*7c478bd9Sstevel@tonic-gate gettext(" Tracked task resources: %s\n"), 417*7c478bd9Sstevel@tonic-gate acp->ac_task_tracked); 418*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 419*7c478bd9Sstevel@tonic-gate gettext(" Untracked task resources: %s\n"), 420*7c478bd9Sstevel@tonic-gate acp->ac_task_untracked); 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate if (type & AC_PROC) { 423*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 424*7c478bd9Sstevel@tonic-gate gettext(" Process accounting: %s\n"), 425*7c478bd9Sstevel@tonic-gate acp->ac_proc_state ? 426*7c478bd9Sstevel@tonic-gate gettext("active") : gettext("inactive")); 427*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 428*7c478bd9Sstevel@tonic-gate gettext(" Process accounting file: %s\n"), 429*7c478bd9Sstevel@tonic-gate acp->ac_proc_file); 430*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 431*7c478bd9Sstevel@tonic-gate gettext(" Tracked process resources: %s\n"), 432*7c478bd9Sstevel@tonic-gate acp->ac_proc_tracked); 433*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 434*7c478bd9Sstevel@tonic-gate gettext("Untracked process resources: %s\n"), 435*7c478bd9Sstevel@tonic-gate acp->ac_proc_untracked); 436*7c478bd9Sstevel@tonic-gate } 437*7c478bd9Sstevel@tonic-gate if (type & AC_FLOW) { 438*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 439*7c478bd9Sstevel@tonic-gate gettext(" Flow accounting: %s\n"), 440*7c478bd9Sstevel@tonic-gate acp->ac_flow_state ? 441*7c478bd9Sstevel@tonic-gate gettext("active") : gettext("inactive")); 442*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 443*7c478bd9Sstevel@tonic-gate gettext(" Flow accounting file: %s\n"), 444*7c478bd9Sstevel@tonic-gate acp->ac_flow_file); 445*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 446*7c478bd9Sstevel@tonic-gate gettext(" Tracked flow resources: %s\n"), 447*7c478bd9Sstevel@tonic-gate acp->ac_flow_tracked); 448*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 449*7c478bd9Sstevel@tonic-gate gettext(" Untracked flow resources: %s\n"), 450*7c478bd9Sstevel@tonic-gate acp->ac_flow_untracked); 451*7c478bd9Sstevel@tonic-gate } 452*7c478bd9Sstevel@tonic-gate } 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate int 455*7c478bd9Sstevel@tonic-gate aconf_str2enable(acctconf_t *acp, char *buf, int type) 456*7c478bd9Sstevel@tonic-gate { 457*7c478bd9Sstevel@tonic-gate int state; 458*7c478bd9Sstevel@tonic-gate 459*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, AC_STR_YES) == 0) 460*7c478bd9Sstevel@tonic-gate state = AC_ON; 461*7c478bd9Sstevel@tonic-gate else if (strcasecmp(buf, AC_STR_NO) == 0) 462*7c478bd9Sstevel@tonic-gate state = AC_OFF; 463*7c478bd9Sstevel@tonic-gate else 464*7c478bd9Sstevel@tonic-gate return (-1); 465*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 466*7c478bd9Sstevel@tonic-gate acp->ac_proc_state = state; 467*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 468*7c478bd9Sstevel@tonic-gate acp->ac_task_state = state; 469*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 470*7c478bd9Sstevel@tonic-gate acp->ac_flow_state = state; 471*7c478bd9Sstevel@tonic-gate else 472*7c478bd9Sstevel@tonic-gate return (-1); 473*7c478bd9Sstevel@tonic-gate return (0); 474*7c478bd9Sstevel@tonic-gate } 475*7c478bd9Sstevel@tonic-gate 476*7c478bd9Sstevel@tonic-gate int 477*7c478bd9Sstevel@tonic-gate aconf_str2file(acctconf_t *acp, char *buf, int type) 478*7c478bd9Sstevel@tonic-gate { 479*7c478bd9Sstevel@tonic-gate if (strcmp(buf, AC_STR_NONE) != 0 && !valid_abspath(buf)) 480*7c478bd9Sstevel@tonic-gate return (-1); 481*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 482*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_file, buf, MAXPATHLEN); 483*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 484*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_file, buf, MAXPATHLEN); 485*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 486*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_file, buf, MAXPATHLEN); 487*7c478bd9Sstevel@tonic-gate return (0); 488*7c478bd9Sstevel@tonic-gate } 489*7c478bd9Sstevel@tonic-gate 490*7c478bd9Sstevel@tonic-gate int 491*7c478bd9Sstevel@tonic-gate aconf_str2tracked(acctconf_t *acp, char *buf, int type) 492*7c478bd9Sstevel@tonic-gate { 493*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 494*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_tracked, buf, MAXRESLEN); 495*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 496*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_tracked, buf, MAXRESLEN); 497*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 498*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_tracked, buf, MAXRESLEN); 499*7c478bd9Sstevel@tonic-gate else 500*7c478bd9Sstevel@tonic-gate return (-1); 501*7c478bd9Sstevel@tonic-gate return (0); 502*7c478bd9Sstevel@tonic-gate } 503*7c478bd9Sstevel@tonic-gate 504*7c478bd9Sstevel@tonic-gate int 505*7c478bd9Sstevel@tonic-gate aconf_str2untracked(acctconf_t *acp, char *buf, int type) 506*7c478bd9Sstevel@tonic-gate { 507*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 508*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_proc_untracked, buf, MAXRESLEN); 509*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 510*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_task_untracked, buf, MAXRESLEN); 511*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 512*7c478bd9Sstevel@tonic-gate (void) strlcpy(acp->ac_flow_untracked, buf, MAXRESLEN); 513*7c478bd9Sstevel@tonic-gate else 514*7c478bd9Sstevel@tonic-gate return (-1); 515*7c478bd9Sstevel@tonic-gate return (0); 516*7c478bd9Sstevel@tonic-gate } 517*7c478bd9Sstevel@tonic-gate 518*7c478bd9Sstevel@tonic-gate static int 519*7c478bd9Sstevel@tonic-gate print_enable(acctconf_t *acp, FILE *fp, int type) 520*7c478bd9Sstevel@tonic-gate { 521*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 522*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", (acp->ac_proc_state == AC_OFF) ? 523*7c478bd9Sstevel@tonic-gate AC_STR_NO : AC_STR_YES)); 524*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 525*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", (acp->ac_task_state == AC_OFF) ? 526*7c478bd9Sstevel@tonic-gate AC_STR_NO : AC_STR_YES)); 527*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 528*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", (acp->ac_flow_state == AC_OFF) ? 529*7c478bd9Sstevel@tonic-gate AC_STR_NO : AC_STR_YES)); 530*7c478bd9Sstevel@tonic-gate else 531*7c478bd9Sstevel@tonic-gate return (-1); 532*7c478bd9Sstevel@tonic-gate } 533*7c478bd9Sstevel@tonic-gate 534*7c478bd9Sstevel@tonic-gate static int 535*7c478bd9Sstevel@tonic-gate print_file(acctconf_t *acp, FILE *fp, int type) 536*7c478bd9Sstevel@tonic-gate { 537*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 538*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_proc_file)); 539*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 540*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_task_file)); 541*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 542*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_flow_file)); 543*7c478bd9Sstevel@tonic-gate else 544*7c478bd9Sstevel@tonic-gate return (-1); 545*7c478bd9Sstevel@tonic-gate } 546*7c478bd9Sstevel@tonic-gate 547*7c478bd9Sstevel@tonic-gate static int 548*7c478bd9Sstevel@tonic-gate print_tracked(acctconf_t *acp, FILE *fp, int type) 549*7c478bd9Sstevel@tonic-gate { 550*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 551*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_proc_tracked)); 552*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 553*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_task_tracked)); 554*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 555*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_flow_tracked)); 556*7c478bd9Sstevel@tonic-gate else 557*7c478bd9Sstevel@tonic-gate return (-1); 558*7c478bd9Sstevel@tonic-gate } 559*7c478bd9Sstevel@tonic-gate 560*7c478bd9Sstevel@tonic-gate static int 561*7c478bd9Sstevel@tonic-gate print_untracked(acctconf_t *acp, FILE *fp, int type) 562*7c478bd9Sstevel@tonic-gate { 563*7c478bd9Sstevel@tonic-gate if (type == AC_PROC) 564*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_proc_untracked)); 565*7c478bd9Sstevel@tonic-gate else if (type == AC_TASK) 566*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_task_untracked)); 567*7c478bd9Sstevel@tonic-gate else if (type == AC_FLOW) 568*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", acp->ac_flow_untracked)); 569*7c478bd9Sstevel@tonic-gate else 570*7c478bd9Sstevel@tonic-gate return (-1); 571*7c478bd9Sstevel@tonic-gate } 572