summaryrefslogtreecommitdiff
path: root/usr/src/cmd/logadm/conf.c
blob: 936238e8480599d1e5a17433c5407fbdd30046eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * logadm/conf.c -- configuration file module
 */

#include <stdio.h>
#include <libintl.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <ctype.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include "err.h"
#include "lut.h"
#include "fn.h"
#include "opts.h"
#include "conf.h"

/* forward declarations of functions private to this module */
static void fillconflist(int lineno, const char *entry, char **args,
    struct opts *opts, const char *com, int flags);
static void fillargs(char *arg);
static char *nexttok(char **ptrptr);
static void conf_print(FILE *stream);

static const char *Confname;	/* name of the confile file */
static char *Confbuf;		/* copy of the config file (a la mmap()) */
static int Conflen;		/* length of mmap'd area */
static int Conffd = -1;		/* file descriptor for config file */
static boolean_t Confchanged;	/* true if we need to write changes back */

/*
 * our structured representation of the configuration file
 * is made up of a list of these
 */
struct confinfo {
	struct confinfo *cf_next;
	int cf_lineno;		/* line number in file */
	const char *cf_entry;	/* name of entry, if line has an entry */
	char **cf_args;		/* raw rhs of entry */
	struct opts *cf_opts;	/* parsed rhs of entry */
	const char *cf_com;	/* any comment text found */
	int cf_flags;
};

#define	CONFF_DELETED	1	/* entry should be deleted on write back */

static struct confinfo *Confinfo;	/* the entries in the config file */
static struct confinfo *Confinfolast;	/* end of list */
static struct lut *Conflut;		/* lookup table keyed by entry name */
static struct fn_list *Confentries;	/* list of valid entry names */

/* allocate & fill in another entry in our list */
static void
fillconflist(int lineno, const char *entry, char **args,
    struct opts *opts, const char *com, int flags)
{
	struct confinfo *cp = MALLOC(sizeof (*cp));

	cp->cf_next = NULL;
	cp->cf_lineno = lineno;
	cp->cf_entry = entry;
	cp->cf_args = args;
	cp->cf_opts = opts;
	cp->cf_com = com;
	cp->cf_flags = flags;
	if (entry != NULL) {
		Conflut = lut_add(Conflut, entry, cp);
		fn_list_adds(Confentries, entry);
	}
	if (Confinfo == NULL)
		Confinfo = Confinfolast = cp;
	else {
		Confinfolast->cf_next = cp;
		Confinfolast = cp;
	}
}

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 */
#define	CONF_ARGS_INC	1024

/* callback for lut_walk to build a cmdargs vector */
static void
fillargs(char *arg)
{
	if (ArgsI >= ArgsN) {
		/* need bigger table */
		Args = REALLOC(Args, sizeof (char *) * (ArgsN + CONF_ARGS_INC));
		ArgsN += CONF_ARGS_INC;
	}
	Args[ArgsI++] = arg;
}

/* isolate and return the next token */
static char *
nexttok(char **ptrptr)
{
	char *ptr = *ptrptr;
	char *eptr;
	char *quote = NULL;

	while (*ptr && isspace(*ptr))
		ptr++;

	if (*ptr == '"' || *ptr == '\'')
		quote = ptr++;

	for (eptr = ptr; *eptr; eptr++)
		if (quote && *eptr == *quote) {
			/* found end quote */
			*eptr++ = '\0';
			*ptrptr = eptr;
			return (ptr);
		} else if (!quote && isspace(*eptr)) {
			/* found end of unquoted area */
			*eptr++ = '\0';
			*ptrptr = eptr;
			return (ptr);
		}

	if (quote != NULL)
		err(EF_FILE|EF_JMP, "Unbalanced %c quote", *quote);
		/*NOTREACHED*/

	*ptrptr = eptr;

	if (ptr == eptr)
		return (NULL);
	else
		return (ptr);
}

/*
 * conf_open -- open the configuration file, lock it if we have write perms
 */
void
conf_open(const char *fname, int needwrite)
{
	struct stat stbuf;
	int lineno = 0;
	char *line;
	char *eline;
	char *ebuf;
	char *comment;

	Confname = fname;
	Confentries = fn_list_new(NULL);

	/* special case this so we don't even try locking the file */
	if (strcmp(Confname, "/dev/null") == 0)
		return;

	if ((Conffd = open(Confname, (needwrite) ? O_RDWR : O_RDONLY)) < 0)
		err(EF_SYS, "%s", Confname);

	if (fstat(Conffd, &stbuf) < 0)
		err(EF_SYS, "fstat on %s", Confname);

	if (needwrite && lockf(Conffd, F_LOCK, 0) < 0)
		err(EF_SYS, "lockf on %s", Confname);

	if (stbuf.st_size == 0)
		return;	/* empty file, don't bother parsing it */

	if ((Confbuf = (char *)mmap(0, stbuf.st_size,
	    PROT_READ | PROT_WRITE, MAP_PRIVATE, Conffd, 0)) == (char *)-1)
		err(EF_SYS, "mmap on %s", Confname);

	Conflen = stbuf.st_size;
	Confchanged = B_FALSE;

	ebuf = &Confbuf[Conflen];

	if (Confbuf[Conflen - 1] != '\n')
		err(EF_WARN|EF_FILE, "config file doesn't end with "
		    "newline, last line ignored.");

	line = Confbuf;
	while (line < ebuf) {
		lineno++;
		err_fileline(Confname, lineno);
		eline = line;
		comment = NULL;
		for (; eline < ebuf; eline++) {
			/* check for continued lines */
			if (comment == NULL && *eline == '\\' &&
			    eline + 1 < ebuf && *(eline + 1) == '\n') {
				*eline = ' ';
				*(eline + 1) = ' ';
				lineno++;
				err_fileline(Confname, lineno);
				continue;
			}

			/* check for comments */
			if (comment == NULL && *eline == '#') {
				*eline = '\0';
				comment = (eline + 1);
				continue;
			}

			/* check for end of line */
			if (*eline == '\n')
				break;
		}
		if (comment >= ebuf)
			comment = NULL;
		if (eline < ebuf) {
			char *entry;

			*eline++ = '\0';

			/*
			 * now we have the entry, if any, at "line"
			 * and the comment, if any, at "comment"
			 */

			/* entry is first token */
			if ((entry = nexttok(&line)) != NULL &&
			    strcmp(entry, "logadm-version") == 0) {
				/*
				 * 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.
				 */
				if ((entry = nexttok(&line)) != NULL &&
				    strcmp(entry, "1") != 0)
					err(0, "%s version not "
					    "supported by "
					    "this version of logadm.",
					    Confname);
			} else if (entry) {
				char *ap;
				char **args;
				int i;

				ArgsI = 0;
				while (ap = nexttok(&line))
					fillargs(ap);
				if (ArgsI == 0) {
					/* short entry allowed */
					fillconflist(lineno, entry,
					    NULL, NULL, comment, 0);
				} else {
					Args[ArgsI++] = NULL;
					args = MALLOC(sizeof (char *) * ArgsI);
					for (i = 0; i < ArgsI; i++)
						args[i] = Args[i];
					fillconflist(lineno, entry,
					    args, NULL, comment, 0);
				}
			} else
				fillconflist(lineno, entry, NULL, NULL,
				    comment, 0);
		}
		line = eline;
	}
	/*
	 * possible future enhancement:  go through and mark any entries:
	 * 		logfile -P <date>
	 * as DELETED if the logfile doesn't exist
	 */
}

/*
 * conf_close -- close the configuration file
 */
void
conf_close(struct opts *opts)
{
	FILE *fp;

	if (Confchanged && opts_count(opts, "n") == 0 && Conffd != -1) {
		if (opts_count(opts, "v"))
			(void) out("# writing changes to %s\n", Confname);
		if (Debug > 1) {
			(void) fprintf(stderr, "conf_close, %s changed to:\n",
			    Confname);
			conf_print(stderr);
		}
		if (lseek(Conffd, (off_t)0, SEEK_SET) < 0)
			err(EF_SYS, "lseek on %s", Confname);
		if (ftruncate(Conffd, (off_t)0) < 0)
			err(EF_SYS, "ftruncate on %s", Confname);
		if ((fp = fdopen(Conffd, "w")) == NULL)
			err(EF_SYS, "fdopen on %s", Confname);
		conf_print(fp);
		if (fclose(fp) < 0)
			err(EF_SYS, "fclose on %s", Confname);
		Conffd = -1;
		Confchanged = B_FALSE;
	} else if (opts_count(opts, "v")) {
		(void) out("# %s unchanged\n", Confname);
	}

	if (Conffd != -1) {
		(void) close(Conffd);
		Conffd = -1;
	}
	if (Conflut) {
		lut_free(Conflut, free);
		Conflut = NULL;
	}
	if (Confentries) {
		fn_list_free(Confentries);
		Confentries = NULL;
	}
}

/*
 * conf_lookup -- lookup an entry in the config file
 */
char **
conf_lookup(const char *lhs)
{
	struct confinfo *cp = lut_lookup(Conflut, lhs);

	if (cp != NULL) {
		err_fileline(Confname, cp->cf_lineno);
		return (cp->cf_args);
	} else
		return (NULL);
}

/*
 * conf_opts -- return the parsed opts for an entry
 */
struct opts *
conf_opts(const char *lhs)
{
	struct confinfo *cp = lut_lookup(Conflut, lhs);

	if (cp != NULL) {
		if (cp->cf_opts)
			return (cp->cf_opts);	/* already parsed */
		err_fileline(Confname, cp->cf_lineno);
		cp->cf_opts = opts_parse(cp->cf_args, OPTF_CONF);
		return (cp->cf_opts);
	}
	return (opts_parse(NULL, OPTF_CONF));
}

/*
 * conf_replace -- replace an entry in the config file
 */
void
conf_replace(const char *lhs, struct opts *newopts)
{
	struct confinfo *cp = lut_lookup(Conflut, lhs);

	if (Conffd == -1)
		return;

	if (cp != NULL) {
		cp->cf_opts = newopts;
		cp->cf_args = NULL;
		if (newopts == NULL)
			cp->cf_flags |= CONFF_DELETED;
	} else
		fillconflist(0, lhs, NULL, newopts, NULL, 0);
	Confchanged = B_TRUE;
}

/*
 * conf_set -- set options for an entry in the config file
 */
void
conf_set(const char *entry, char *o, const char *optarg)
{
	struct confinfo *cp = lut_lookup(Conflut, entry);

	if (Conffd == -1)
		return;

	if (cp != NULL) {
		if (cp->cf_opts == NULL)
			cp->cf_opts = opts_parse(cp->cf_args, OPTF_CONF);
		cp->cf_flags &= ~CONFF_DELETED;
	} else {
		fillconflist(0, STRDUP(entry), NULL,
		    opts_parse(NULL, OPTF_CONF), NULL, 0);
		if ((cp = lut_lookup(Conflut, entry)) == NULL)
			err(0, "conf_set internal error");
	}
	(void) opts_set(cp->cf_opts, o, optarg);
	Confchanged = B_TRUE;
}

/*
 * conf_entries -- list all the entry names
 */
struct fn_list *
conf_entries(void)
{
	return (Confentries);
}

/* print the config file */
static void
conf_print(FILE *stream)
{
	struct confinfo *cp;

	for (cp = Confinfo; cp; cp = cp->cf_next) {
		if (cp->cf_flags & CONFF_DELETED)
			continue;
		if (cp->cf_entry) {
			char **p;

			opts_printword(cp->cf_entry, stream);
			if (cp->cf_opts) {
				/* existence of opts overrides args */
				opts_print(cp->cf_opts, stream, "fhnrvVw");
			} else if (cp->cf_args) {
				for (p = cp->cf_args; *p; p++) {
					(void) fprintf(stream, " ");
					opts_printword(*p, stream);
				}
			}
		}
		if (cp->cf_com) {
			if (cp->cf_entry)
				(void) fprintf(stream, " ");
			(void) fprintf(stream, "#%s", cp->cf_com);
		}
		(void) fprintf(stream, "\n");
	}
}

#ifdef	TESTMODULE

/*
 * test main for conf module, usage: a.out conffile
 */
int
main(int argc, char *argv[])
{
	err_init(argv[0]);
	setbuf(stdout, NULL);

	if (argc != 2)
		err(EF_RAW, "usage: %s conffile\n", argv[0]);

	conf_open(argv[1], 1);

	printf("conffile <%s>:\n", argv[1]);
	conf_print(stdout);

	conf_close(opts_parse(NULL, 0));

	err_done(0);
	/* NOTREACHED */
	return (0);
}

#endif	/* TESTMODULE */