]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bpipe.c
dd3827edb64f55c06fb59299674ecb57bc6d4d1f
[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       if (fclose(bpipe->wfd) != 0) {
124          stat = 0;
125       }
126       bpipe->wfd = NULL;
127    }
128    return stat;
129 }
130
131 /* Close both pipes and free resources */
132 int close_bpipe(BPIPE *bpipe) 
133 {
134    int chldstatus = 0;
135    int stat = ETIME;
136    int wait_option;
137    int remaining_wait;
138
139    /* Close pipes */
140    if (bpipe->rfd) {
141       fclose(bpipe->rfd);
142       bpipe->rfd = NULL;
143    }
144    if (bpipe->wfd) {
145       fclose(bpipe->wfd);
146       bpipe->wfd = NULL;
147    }
148
149    if (bpipe->wait == 0) {
150       wait_option = 0;                /* wait indefinitely */
151    } else {
152       wait_option = WNOHANG;          /* don't hang */
153    }
154    remaining_wait = bpipe->wait;
155
156    /* wait for worker child to exit */
157    for ( ;; ) {
158       pid_t wpid;
159       wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
160       if (wpid == bpipe->worker_pid || (wpid == -1 && errno != EINTR)) {
161          break;
162       }
163       if (remaining_wait > 0) {
164          sleep(1);                    /* wait one second */
165          remaining_wait--;
166       } else {
167          break;                       /* don't wait any longer */
168       }
169    }
170    if (WIFEXITED(chldstatus)) {
171       stat = WEXITSTATUS(chldstatus);
172    }
173    if (bpipe->timer_id) {
174       stop_child_timer(bpipe->timer_id);
175    }
176    free(bpipe);
177    return stat;
178 }
179
180
181 /*
182  * Run an external program. Optionally wait a specified number
183  *   of seconds. Program killed if wait exceeded. Optionally
184  *   return the output from the program (normally a single line).
185  */
186 int run_program(char *prog, int wait, POOLMEM *results)
187 {
188    BPIPE *bpipe;
189    int stat1, stat2;
190    char *mode;
191
192    mode = (char *)(results != NULL ? "r" : "");
193    bpipe = open_bpipe(prog, wait, mode);
194    if (!bpipe) {
195       return 0;
196    }
197    if (results) {
198       results[0] = 0;
199       stat1 = fgets(results, sizeof_pool_memory(results), bpipe->rfd) != NULL;
200    } else {
201       stat1 = 1;
202    }
203    stat2 = close_bpipe(bpipe);
204    return stat1 && stat2;
205 }
206
207
208 /*
209  * Build argc and argv from a string
210  */
211 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
212 {
213    int i, quote;
214    char *p, *q;
215    int argc = 0;
216
217    argc = 0;
218    for (i=0; i<max_argv; i++)
219       bargv[i] = NULL;
220
221    p = cmd;
222    quote = 0;
223    while  (*p && (*p == ' ' || *p == '\t'))
224       p++;
225    if (*p == '\"') {
226       quote = 1;
227       p++;
228    }
229    if (*p) {
230       while (*p && argc < MAX_ARGV) {
231          q = p;
232          if (quote) {
233             while (*q && *q != '\"')
234             q++;
235             quote = 0;
236          } else {
237             while (*q && *q != ' ')
238             q++;
239          }
240          if (*q)
241             *(q++) = '\0';
242          bargv[argc++] = p;
243          p = q;
244          while (*p && (*p == ' ' || *p == '\t'))
245             p++;
246          if (*p == '\"') {
247             quote = 1;
248             p++;
249          }
250       }
251    }
252    *bargc = argc;
253 }