]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bpipe.c
FreeBSD child status code kludge
[bacula/bacula] / bacula / src / lib / bpipe.c
1 /*
2  *   bpipe.c bi-directional pipe
3  * 
4  *    Kern Sibbald, November MMII
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "jcr.h"
31
32
33 #define MAX_ARGV 100
34 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_arg);
35
36
37
38 /*
39  * Run an external program. Optionally wait a specified number
40  *   of seconds. Program killed if wait exceeded. We open
41  *   a bi-directional pipe so that the user can read from and
42  *   write to the program. 
43  */
44 BPIPE *open_bpipe(char *prog, int wait, char *mode)
45 {
46    char *bargv[MAX_ARGV];
47    int bargc;
48    int readp[2], writep[2];
49    POOLMEM *tprog;
50    int mode_read, mode_write;
51    BPIPE *bpipe;
52
53    bpipe = (BPIPE *)malloc(sizeof(BPIPE));
54    memset(bpipe, 0, sizeof(BPIPE));
55    mode_read = (mode[0] == 'r');
56    mode_write = (mode[0] == 'w' || mode[1] == 'w');
57    /* Build arguments for running program. */
58    tprog = get_pool_memory(PM_FNAME);
59    pm_strcpy(&tprog, prog);
60    build_argc_argv(tprog, &bargc, bargv, MAX_ARGV);
61 #ifdef xxxxxx
62    printf("argc=%d\n", bargc);
63    int i;
64    for (i=0; i<bargc; i++) {
65       printf("argc=%d argv=%s:\n", i, bargv[i]);
66    }
67 #endif
68    free_pool_memory(tprog);
69
70    /* Each pipe is one way, write one end, read the other, so we need two */
71    if (mode_write && pipe(writep) == -1) {
72       free(bpipe);
73       return NULL;
74    }
75    if (mode_read && pipe(readp) == -1) {
76       free(bpipe);
77       return NULL;
78    }
79    /* Start worker process */
80    switch (bpipe->worker_pid = fork()) {
81    case -1:
82       free(bpipe);
83       return NULL;
84
85    case 0:                            /* child */
86       if (mode_write) {
87          close(writep[1]);
88          dup2(writep[0], 0);          /* Dup our write to his stdin */
89       }
90       if (mode_read) {
91          close(readp[0]);             /* Close unused child fds */
92          dup2(readp[1], 1);           /* dup our read to his stdout */
93          dup2(readp[1], 2);           /*   and his stderr */
94       }
95       execvp(bargv[0], bargv);        /* call the program */
96       exit(errno);                    /* shouldn't get here */
97
98    default:                           /* parent */
99       break;
100    }
101    if (mode_read) {
102       close(readp[1]);                /* close unused parent fds */
103       bpipe->rfd = fdopen(readp[0], "r"); /* open file descriptor */
104    }
105    if (mode_write) {
106       close(writep[0]);
107       bpipe->wfd = fdopen(writep[1], "w");
108    }
109    bpipe->worker_stime = time(NULL);
110    bpipe->wait = wait;
111    if (wait > 0) {
112       bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
113    }
114    return bpipe;
115 }
116
117 /* Close the write pipe only */
118 int close_wpipe(BPIPE *bpipe)
119 {
120    int stat = 1;
121
122    if (bpipe->wfd) {
123       fflush(bpipe->wfd);
124       if (fclose(bpipe->wfd) != 0) {
125          stat = 0;
126       }
127       bpipe->wfd = NULL;
128    }
129    return stat;
130 }
131
132 /* Close both pipes and free resources */
133 int close_bpipe(BPIPE *bpipe) 
134 {
135    int chldstatus = 0;
136    int stat = 0;    
137    int wait_option;
138    int remaining_wait;
139    pid_t wpid = 0;
140
141
142    /* Close pipes */
143    if (bpipe->rfd) {
144       fclose(bpipe->rfd);
145       bpipe->rfd = NULL;
146    }
147    if (bpipe->wfd) {
148       fclose(bpipe->wfd);
149       bpipe->wfd = NULL;
150    }
151
152    if (bpipe->wait == 0) {
153       wait_option = 0;                /* wait indefinitely */
154    } else {
155       wait_option = WNOHANG;          /* don't hang */
156    }
157    remaining_wait = bpipe->wait;
158
159    /* wait for worker child to exit */
160    for ( ;; ) {
161       wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
162       if (wpid == bpipe->worker_pid || (wpid == -1 && errno != EINTR)) {
163          break;
164       }
165       if (remaining_wait > 0) {
166          sleep(1);                    /* wait one second */
167          remaining_wait--;
168       } else {
169          stat = ETIME;                /* set timeout, if no other status */
170          wpid = -1;
171          break;                       /* don't wait any longer */
172       }
173    }
174    if (wpid != -1 && WIFEXITED(chldstatus)) {
175       stat = WEXITSTATUS(chldstatus);
176    }
177    if (bpipe->timer_id) {
178       stop_child_timer(bpipe->timer_id);
179    }
180    free(bpipe);
181 #ifdef HAVE_FREEBSD_OS
182    stat = 0;  /* kludge because FreeBSD doesn't seem to return valid status */
183 #endif
184    return stat;
185 }
186
187
188 /*
189  * Run an external program. Optionally wait a specified number
190  *   of seconds. Program killed if wait exceeded. Optionally
191  *   return the output from the program (normally a single line).
192  */
193 int run_program(char *prog, int wait, POOLMEM *results)
194 {
195    BPIPE *bpipe;
196    int stat1, stat2;
197    char *mode;
198
199    mode = (char *)(results != NULL ? "r" : "");
200    bpipe = open_bpipe(prog, wait, mode);
201    if (!bpipe) {
202       return 0;
203    }
204    if (results) {
205       results[0] = 0;
206       stat1 = fgets(results, sizeof_pool_memory(results), bpipe->rfd) != NULL;
207    } else {
208       stat1 = 1;
209    }
210    stat2 = close_bpipe(bpipe);
211    return stat1 && stat2;
212 }
213
214
215 /*
216  * Build argc and argv from a string
217  */
218 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
219 {
220    int i, quote;
221    char *p, *q;
222    int argc = 0;
223
224    argc = 0;
225    for (i=0; i<max_argv; i++)
226       bargv[i] = NULL;
227
228    p = cmd;
229    quote = 0;
230    while  (*p && (*p == ' ' || *p == '\t'))
231       p++;
232    if (*p == '\"') {
233       quote = 1;
234       p++;
235    }
236    if (*p) {
237       while (*p && argc < MAX_ARGV) {
238          q = p;
239          if (quote) {
240             while (*q && *q != '\"')
241             q++;
242             quote = 0;
243          } else {
244             while (*q && *q != ' ')
245             q++;
246          }
247          if (*q)
248             *(q++) = '\0';
249          bargv[argc++] = p;
250          p = q;
251          while (*p && (*p == ' ' || *p == '\t'))
252             p++;
253          if (*p == '\"') {
254             quote = 1;
255             p++;
256          }
257       }
258    }
259    *bargc = argc;
260 }