#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
#define CF_GROW 2
static FILE *
open_conf_pipe(const char *cmd, char *argv[], pid_t *pidp)
{
int pfds[2];
pid_t pid;
struct sigaction act;
if (pipe(pfds) == -1) {
logerror("failed to create pipe");
return (NULL);
}
if ((pid = fork1()) == -1) {
logerror("failed to fork1");
goto err;
}
if (pid == 0) {
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);
}
*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 (cf->cf_lines >= cf->cf_dsize) {
if (grow_conf_file(cf) == -1)
goto err;
}
line = cf->cf_dtab[cf->cf_lines];
if (fgets(line, BUFSIZ, fp) == NULL)
break;
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);
}
}