]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bpipe.c
Update config + write own fgets() + ...
[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(mp_chr(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:                           /* error */
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       Dmsg2(200, "Wait for %d opt=%d\n", bpipe->worker_pid, wait_option);
162       wpid = waitpid(bpipe->worker_pid, &chldstatus, wait_option);
163       if (wpid == bpipe->worker_pid || (wpid == -1 && errno != EINTR)) {
164          Dmsg3(200, "Got break wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
165             wpid==-1?strerror(errno):"none");
166          break;
167       }
168       Dmsg3(200, "Got wpid=%d status=%d ERR=%s\n", wpid, chldstatus,
169             wpid==-1?strerror(errno):"none");
170       if (remaining_wait > 0) {
171          bmicrosleep(1, 0);            /* wait one second */
172          remaining_wait--;
173       } else {
174          stat = 1;                    /* set error status */
175          errno = ETIME;               /* set timed out */
176          wpid = -1;
177          break;                       /* don't wait any longer */
178       }
179    }
180    if (wpid > 0) {
181       if (WIFEXITED(chldstatus)) {           /* process exit()ed */
182          stat = WEXITSTATUS(chldstatus);
183           Dmsg1(200, "status =%d\n", stat);
184       } else if (WIFSIGNALED(chldstatus)) {  /* process died */
185          stat = 1;
186          Dmsg0(200, "Signaled\n");
187       }
188       if (stat != 0) {
189          errno = ECHILD;              /* set child errno */
190       }
191    }  
192    if (bpipe->timer_id) {
193       stop_child_timer(bpipe->timer_id);
194    }
195    free(bpipe);
196    Dmsg1(200, "returning stat = %d\n", stat);
197    return stat;
198 }
199
200
201 /*
202  * Run an external program. Optionally wait a specified number
203  *   of seconds. Program killed if wait exceeded. Optionally
204  *   return the output from the program (normally a single line).
205  *
206  * Contrary to my normal calling conventions, this program 
207  *
208  *  Returns: 0 on success
209  *           non-zero on error
210  */
211 int run_program(char *prog, int wait, POOLMEM *results)
212 {
213    BPIPE *bpipe;
214    int stat1, stat2;
215    char *mode;
216
217    mode = (char *)(results != NULL ? "r" : "");
218    bpipe = open_bpipe(prog, wait, mode);
219    if (!bpipe) {
220       return 0;
221    }
222    if (results) {
223       mp_chr(results)[0] = 0;
224       stat1 = fgets(mp_chr(results), sizeof_pool_memory(results), bpipe->rfd) == NULL;
225    } else {
226       stat1 = 0;
227    }
228    stat2 = close_bpipe(bpipe);
229    return stat2 != 0 ? stat2 : stat1; 
230 }
231
232
233 /*
234  * Build argc and argv from a string
235  */
236 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
237 {
238    int i;       
239    char *p, *q, quote;
240    int argc = 0;
241
242    argc = 0;
243    for (i=0; i<max_argv; i++)
244       bargv[i] = NULL;
245
246    p = cmd;
247    quote = 0;
248    while  (*p && (*p == ' ' || *p == '\t'))
249       p++;
250    if (*p == '\"' || *p == '\'') {
251       quote = *p;
252       p++;
253    }
254    if (*p) {
255       while (*p && argc < MAX_ARGV) {
256          q = p;
257          if (quote) {
258             while (*q && *q != quote)
259             q++;
260             quote = 0;
261          } else {
262             while (*q && *q != ' ')
263             q++;
264          }
265          if (*q)
266             *(q++) = '\0';
267          bargv[argc++] = p;
268          p = q;
269          while (*p && (*p == ' ' || *p == '\t'))
270             p++;
271          if (*p == '\"' || *p == '\'') {
272             quote = *p;
273             p++;
274          }
275       }
276    }
277    *bargc = argc;
278 }