conf.c revision 7ea027865de56bac0c639f5fe006cc752ce41413
* The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * See the License for the specific language governing permissions * and limitations under the License. * When distributing Covered Code, include this CDDL HEADER in each * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright 2013, Joyent, Inc. All rights reserved. /* forward declarations of functions private to this module */ static const char *
Confname;
/* name of the confile file */ static int Conffd = -
1;
/* file descriptor for config file */ static char *
Confbuf;
/* copy of the config file (a la mmap()) */ static int Conflen;
/* length of mmap'd config file area */ static const char *
Timesname;
/* name of the timestamps file */ static int Timesfd = -
1;
/* file descriptor for timestamps file */ static char *
Timesbuf;
/* copy of the timestamps file (a la mmap()) */ static int Timeslen;
/* length of mmap'd timestamps area */ static int Singlefile;
/* Conf and Times in the same file */ static int Changed;
/* what changes need to be written back */ static int Canchange;
/* what changes can be written back */ static int Changing;
/* what changes have been requested */ * our structured representation of the configuration file * is made up of a list of these const char *
cf_entry;
/* name of entry, if line has an entry */ const char *
cf_com;
/* any comment text found */ #
define CONFF_DELETED 1 /* entry should be deleted on write back */static struct lut *
Conflut;
/* lookup table keyed by entry name */ /* allocate & fill in another entry in our list */ static char **
Args;
/* static buffer for args */ static int ArgsN;
/* size of our static buffer */ static int ArgsI;
/* index into Cmdargs as we walk table */ /* callback for lut_walk to build a cmdargs vector */ /* isolate and return the next token */ if (*
ptr ==
'"' || *
ptr ==
'\'')
/* found end of unquoted area */ * scan the memory image of a file * returns: 0: error, 1: ok, 3: -P option found "last line ignored.",
fname);
/* check for continued lines */ /* check for end of line */ /* discard trailing unterminated line */ * now we have the entry, if any, at "line" * and the comment, if any, at "comment" /* entry is first token */ /* it's just a comment line */ * we somehow opened some future format * conffile that we likely don't understand. * if the given version is "1" then go on, * otherwise someone is mixing versions * and we can't help them other than to * print an error and exit. err(0,
"%s version not supported " "by this version of logadm.",
* If there is no next token on the line, make sure that * we get a non-NULL Args array. /* append to config options */ * If we're not doing timescan, we track this * entry. If we are doing timescan and have * what looks like an orphaned entry (cp == * NULL) then we also have to track. See the * comment in rotatelog. We need to allow for * the case where the logname is not the same as * conf_open -- open the configuration file, lock it if we have write perms /* special case this so we don't even try locking the file */ /* wait until after file is locked to get filesize */ /* verify that we've got a lock on the active file */ /* wrong config file, try again */ /* wait until after file is locked to get filesize */ /* verify that we've got a lock on the active file */ /* wrong timestamp file, try again */ /* check that Timesname isn't an alias for Confname */ err(0,
"Timestamp file %s can't refer to " return (
1);
/* empty file, don't bother parsing it */ * arrange to transfer any timestamps * from conf_file to timestamps_file * possible future enhancement: go through and mark any entries: * as DELETED if the logfile doesn't exist * conf_close -- close the configuration file (
void)
out(
"# %s and %s unchanged\n",
"to update %s without locking",
Confname);
(
void)
out(
"# writing changes to %s\n",
cuname);
/* just toss away the configuration data */ (
void)
out(
"# writing changes to %s\n",
tuname);
err(
EF_JMP,
"unsafe to update configuration file " /* rename updated files into place */ * conf_lookup -- lookup an entry in the config file * conf_opts -- return the parsed opts for an entry * conf_replace -- replace an entry in the config file /* cp->cf_args = NULL; */ * conf_set -- set options for an entry in the config file err(0,
"conf_set internal error");
* conf_entries -- list all the entry names /* print the config file */ "# This file holds internal data for logadm(1M).\n" /* output timestamps to tstream */ * test main for conf module, usage: a.out conffile