xref: /titanic_53/usr/src/cmd/syslogd/conf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 (c) 1997-2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
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/wait.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <signal.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 <errno.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "conf.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	CF_DEFSIZE	32	/* Starting table size */
42*7c478bd9Sstevel@tonic-gate #define	CF_GROW		2	/* Table size multiplier on grow */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate extern void logerror(char *);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static FILE *
47*7c478bd9Sstevel@tonic-gate open_conf_pipe(const char *cmd, char *argv[], pid_t *pidp)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	int pfds[2];
50*7c478bd9Sstevel@tonic-gate 	pid_t pid;
51*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	/* Create a pipe and fork a child process to run the command */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (pipe(pfds) == -1) {
56*7c478bd9Sstevel@tonic-gate 		logerror("failed to create pipe");
57*7c478bd9Sstevel@tonic-gate 		return (NULL);
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
61*7c478bd9Sstevel@tonic-gate 		logerror("failed to fork1");
62*7c478bd9Sstevel@tonic-gate 		goto err;
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/* If we're in the child, run the command and output to the pipe */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (pid == 0) {
68*7c478bd9Sstevel@tonic-gate 		/*
69*7c478bd9Sstevel@tonic-gate 		 * We must set up to ignore these signals, which may be
70*7c478bd9Sstevel@tonic-gate 		 * propogated from the calling process.
71*7c478bd9Sstevel@tonic-gate 		 */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 		act.sa_handler = SIG_IGN;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGHUP, &act, NULL);
76*7c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGALRM, &act, NULL);
77*7c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGUSR1, &act, NULL);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 		(void) close(pfds[0]);
80*7c478bd9Sstevel@tonic-gate 		(void) close(STDOUT_FILENO);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		if (dup2(pfds[1], STDOUT_FILENO) == -1) {
83*7c478bd9Sstevel@tonic-gate 			logerror("failed to dup to stdout");
84*7c478bd9Sstevel@tonic-gate 			(void) close(pfds[1]);
85*7c478bd9Sstevel@tonic-gate 			_exit(127);
86*7c478bd9Sstevel@tonic-gate 		}
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 		(void) execvp(cmd, argv);
89*7c478bd9Sstevel@tonic-gate 		logerror("failed to parse configuration file");
90*7c478bd9Sstevel@tonic-gate 		_exit(127);
91*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	/* If we're in the parent, open the read end of the pipe and return */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	*pidp = pid;
97*7c478bd9Sstevel@tonic-gate 	(void) close(pfds[1]);
98*7c478bd9Sstevel@tonic-gate 	return (fdopen(pfds[0], "r"));
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate err:
101*7c478bd9Sstevel@tonic-gate 	(void) close(pfds[0]);
102*7c478bd9Sstevel@tonic-gate 	(void) close(pfds[1]);
103*7c478bd9Sstevel@tonic-gate 	return (NULL);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate static void
107*7c478bd9Sstevel@tonic-gate close_conf_pipe(FILE *fp, pid_t pid)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	int status;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	while (waitpid(pid, &status, 0) == -1) {
112*7c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
113*7c478bd9Sstevel@tonic-gate 			break;
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate static int
120*7c478bd9Sstevel@tonic-gate grow_conf_file(conf_t *cf)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	int ndsize = cf->cf_dsize ? cf->cf_dsize * CF_GROW : CF_DEFSIZE;
123*7c478bd9Sstevel@tonic-gate 	void *ndtab = realloc(cf->cf_dtab, sizeof (char *) * ndsize);
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	register char *p;
126*7c478bd9Sstevel@tonic-gate 	int odsize, lines, i;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (ndtab == NULL) {
129*7c478bd9Sstevel@tonic-gate 		logerror("failed to allocate config file table");
130*7c478bd9Sstevel@tonic-gate 		return (-1);
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	lines = ndsize - cf->cf_dsize;
134*7c478bd9Sstevel@tonic-gate 	odsize = cf->cf_dsize;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	cf->cf_dtab = (char **)ndtab;
137*7c478bd9Sstevel@tonic-gate 	cf->cf_dsize = ndsize;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < lines; i++) {
140*7c478bd9Sstevel@tonic-gate 		if ((p = (char *)malloc(BUFSIZ)) == NULL) {
141*7c478bd9Sstevel@tonic-gate 			logerror("failed to allocate config file buffer");
142*7c478bd9Sstevel@tonic-gate 			return (-1);
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 		cf->cf_dtab[odsize + i] = p;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	return (0);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate int
152*7c478bd9Sstevel@tonic-gate conf_open(conf_t *cf, const char *cmd, char *argv[])
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	char *line, *p;
155*7c478bd9Sstevel@tonic-gate 	pid_t pid;
156*7c478bd9Sstevel@tonic-gate 	FILE *fp;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	(void) memset(cf, 0, sizeof (conf_t));
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	if ((fp = open_conf_pipe(cmd, argv, &pid)) == NULL)
161*7c478bd9Sstevel@tonic-gate 		return (-1);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	for (;;) {
164*7c478bd9Sstevel@tonic-gate 		/* If we need to grow the table, do so now */
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 		if (cf->cf_lines >= cf->cf_dsize) {
167*7c478bd9Sstevel@tonic-gate 			if (grow_conf_file(cf) == -1)
168*7c478bd9Sstevel@tonic-gate 				goto err;
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		line = cf->cf_dtab[cf->cf_lines];
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 		/* Read the next line, and break out if we're done */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 		if (fgets(line, BUFSIZ, fp) == NULL)
176*7c478bd9Sstevel@tonic-gate 			break;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 		/* Strip newline and bump line counter */
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		if ((p = strchr(line, '\n')) != NULL)
181*7c478bd9Sstevel@tonic-gate 			*p = '\0';
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 		cf->cf_lines++;
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	close_conf_pipe(fp, pid);
187*7c478bd9Sstevel@tonic-gate 	return (0);
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate err:
190*7c478bd9Sstevel@tonic-gate 	close_conf_pipe(fp, pid);
191*7c478bd9Sstevel@tonic-gate 	return (-1);
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate void
195*7c478bd9Sstevel@tonic-gate conf_rewind(conf_t *cf)
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	cf->cf_ptr = 0;
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate char *
201*7c478bd9Sstevel@tonic-gate conf_read(conf_t *cf)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	if (cf->cf_ptr < cf->cf_lines)
204*7c478bd9Sstevel@tonic-gate 		return (cf->cf_dtab[cf->cf_ptr++]);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return (NULL);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate void
210*7c478bd9Sstevel@tonic-gate conf_close(conf_t *cf)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	int i;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	if (cf->cf_dtab != NULL) {
215*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < cf->cf_dsize; i++)
216*7c478bd9Sstevel@tonic-gate 			free(cf->cf_dtab[i]);
217*7c478bd9Sstevel@tonic-gate 		free(cf->cf_dtab);
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate }
220