4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
40 #define CF_DEFSIZE 32 /* Starting table size */
41 #define CF_GROW 2 /* Table size multiplier on grow */
44 open_conf_pipe(const char *cmd
, char *argv
[], pid_t
*pidp
)
50 /* Create a pipe and fork a child process to run the command */
52 if (pipe(pfds
) == -1) {
53 logerror("failed to create pipe");
57 if ((pid
= fork1()) == -1) {
58 logerror("failed to fork1");
62 /* If we're in the child, run the command and output to the pipe */
66 * We must set up to ignore these signals, which may be
67 * propogated from the calling process.
70 act
.sa_handler
= SIG_IGN
;
72 (void) sigaction(SIGHUP
, &act
, NULL
);
73 (void) sigaction(SIGALRM
, &act
, NULL
);
74 (void) sigaction(SIGUSR1
, &act
, NULL
);
76 (void) close(pfds
[0]);
77 (void) close(STDOUT_FILENO
);
79 if (dup2(pfds
[1], STDOUT_FILENO
) == -1) {
80 logerror("failed to dup to stdout");
81 (void) close(pfds
[1]);
85 (void) execvp(cmd
, argv
);
86 logerror("failed to parse configuration file");
91 /* If we're in the parent, open the read end of the pipe and return */
94 (void) close(pfds
[1]);
95 return (fdopen(pfds
[0], "r"));
98 (void) close(pfds
[0]);
99 (void) close(pfds
[1]);
104 close_conf_pipe(FILE *fp
, pid_t pid
)
108 while (waitpid(pid
, &status
, 0) == -1) {
117 grow_conf_file(conf_t
*cf
)
119 int ndsize
= cf
->cf_dsize
? cf
->cf_dsize
* CF_GROW
: CF_DEFSIZE
;
120 void *ndtab
= realloc(cf
->cf_dtab
, sizeof (char *) * ndsize
);
123 int odsize
, lines
, i
;
126 logerror("failed to allocate config file table");
130 lines
= ndsize
- cf
->cf_dsize
;
131 odsize
= cf
->cf_dsize
;
133 cf
->cf_dtab
= (char **)ndtab
;
134 cf
->cf_dsize
= ndsize
;
136 for (i
= 0; i
< lines
; i
++) {
137 if ((p
= (char *)malloc(BUFSIZ
)) == NULL
) {
138 logerror("failed to allocate config file buffer");
142 cf
->cf_dtab
[odsize
+ i
] = p
;
149 conf_open(conf_t
*cf
, const char *cmd
, char *argv
[])
155 (void) memset(cf
, 0, sizeof (conf_t
));
157 if ((fp
= open_conf_pipe(cmd
, argv
, &pid
)) == NULL
)
161 /* If we need to grow the table, do so now */
163 if (cf
->cf_lines
>= cf
->cf_dsize
) {
164 if (grow_conf_file(cf
) == -1)
168 line
= cf
->cf_dtab
[cf
->cf_lines
];
170 /* Read the next line, and break out if we're done */
172 if (fgets(line
, BUFSIZ
, fp
) == NULL
)
175 /* Strip newline and bump line counter */
177 if ((p
= strchr(line
, '\n')) != NULL
)
183 close_conf_pipe(fp
, pid
);
187 close_conf_pipe(fp
, pid
);
192 conf_rewind(conf_t
*cf
)
198 conf_read(conf_t
*cf
)
200 if (cf
->cf_ptr
< cf
->cf_lines
)
201 return (cf
->cf_dtab
[cf
->cf_ptr
++]);
207 conf_close(conf_t
*cf
)
211 if (cf
->cf_dtab
!= NULL
) {
212 for (i
= 0; i
< cf
->cf_dsize
; i
++)
213 free(cf
->cf_dtab
[i
]);