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
|
/*
* 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"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "conf.h"
#define CF_DEFSIZE 32 /* Starting table size */
#define CF_GROW 2 /* Table size multiplier on grow */
static FILE *
open_conf_pipe(const char *cmd, char *argv[], pid_t *pidp)
{
int pfds[2];
pid_t pid;
struct sigaction act;
/* Create a pipe and fork a child process to run the command */
if (pipe(pfds) == -1) {
logerror("failed to create pipe");
return (NULL);
}
if ((pid = fork1()) == -1) {
logerror("failed to fork1");
goto err;
}
/* If we're in the child, run the command and output to the pipe */
if (pid == 0) {
/*
* We must set up to ignore these signals, which may be
* propogated from the calling process.
*/
act.sa_handler = SIG_IGN;
(void) sigaction(SIGHUP, &act, NULL);
(void) sigaction(SIGALRM, &act, NULL);
(void) sigaction(SIGUSR1, &act, NULL);
(void) close(pfds[0]);
(void) close(STDOUT_FILENO);
if (dup2(pfds[1], STDOUT_FILENO) == -1) {
logerror("failed to dup to stdout");
(void) close(pfds[1]);
_exit(127);
}
(void) execvp(cmd, argv);
logerror("failed to parse configuration file");
_exit(127);
/*NOTREACHED*/
}
/* If we're in the parent, open the read end of the pipe and return */
*pidp = pid;
(void) close(pfds[1]);
return (fdopen(pfds[0], "r"));
err:
(void) close(pfds[0]);
(void) close(pfds[1]);
return (NULL);
}
static void
close_conf_pipe(FILE *fp, pid_t pid)
{
int status;
while (waitpid(pid, &status, 0) == -1) {
if (errno != EINTR)
break;
}
(void) fclose(fp);
}
static int
grow_conf_file(conf_t *cf)
{
int ndsize = cf->cf_dsize ? cf->cf_dsize * CF_GROW : CF_DEFSIZE;
void *ndtab = realloc(cf->cf_dtab, sizeof (char *) * ndsize);
register char *p;
int odsize, lines, i;
if (ndtab == NULL) {
logerror("failed to allocate config file table");
return (-1);
}
lines = ndsize - cf->cf_dsize;
odsize = cf->cf_dsize;
cf->cf_dtab = (char **)ndtab;
cf->cf_dsize = ndsize;
for (i = 0; i < lines; i++) {
if ((p = (char *)malloc(BUFSIZ)) == NULL) {
logerror("failed to allocate config file buffer");
return (-1);
}
cf->cf_dtab[odsize + i] = p;
}
return (0);
}
int
conf_open(conf_t *cf, const char *cmd, char *argv[])
{
char *line, *p;
pid_t pid;
FILE *fp;
(void) memset(cf, 0, sizeof (conf_t));
if ((fp = open_conf_pipe(cmd, argv, &pid)) == NULL)
return (-1);
for (;;) {
/* If we need to grow the table, do so now */
if (cf->cf_lines >= cf->cf_dsize) {
if (grow_conf_file(cf) == -1)
goto err;
}
line = cf->cf_dtab[cf->cf_lines];
/* Read the next line, and break out if we're done */
if (fgets(line, BUFSIZ, fp) == NULL)
break;
/* Strip newline and bump line counter */
if ((p = strchr(line, '\n')) != NULL)
*p = '\0';
cf->cf_lines++;
}
close_conf_pipe(fp, pid);
return (0);
err:
close_conf_pipe(fp, pid);
return (-1);
}
void
conf_rewind(conf_t *cf)
{
cf->cf_ptr = 0;
}
char *
conf_read(conf_t *cf)
{
if (cf->cf_ptr < cf->cf_lines)
return (cf->cf_dtab[cf->cf_ptr++]);
return (NULL);
}
void
conf_close(conf_t *cf)
{
int i;
if (cf->cf_dtab != NULL) {
for (i = 0; i < cf->cf_dsize; i++)
free(cf->cf_dtab[i]);
free(cf->cf_dtab);
}
}
|