Merge commit 'origin/340_inrorrect_handling_of_wildcards'
[midnight-commander.git] / src / background.c
blob55555d72fab932ac97d3224db981fbf97e13baab
1 /* {{{ Copyright */
3 /* Background support.
4 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
5 Free Software Foundation, Inc.
7 Written by: 1996 Miguel de Icaza
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* }}} */
25 /** \file background.c
26 * \brief Source: Background support
29 #include <config.h>
31 #ifdef WITH_BACKGROUND
33 #include <errno.h>
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
43 #include "global.h"
44 #include "background.h"
45 #include "tty.h" /* doupdate() */
46 #include "dialog.h" /* do_refresh() */
47 #include "wtools.h"
48 #include "fileopctx.h" /* FileOpContext */
49 #include "key.h" /* add_select_channel(), delete_select_channel() */
51 enum ReturnType {
52 Return_String,
53 Return_Integer
56 /* If true, this is a background process */
57 int we_are_background = 0;
59 /* File descriptor for talking to our parent */
60 static int parent_fd;
62 #define MAXCALLARGS 4 /* Number of arguments supported */
64 struct TaskList *task_list = NULL;
66 static int background_attention (int fd, void *closure);
68 static void
69 register_task_running (FileOpContext *ctx, pid_t pid, int fd, char *info)
71 TaskList *new;
73 new = g_new (TaskList, 1);
74 new->pid = pid;
75 new->info = info;
76 new->state = Task_Running;
77 new->next = task_list;
78 new->fd = fd;
79 task_list = new;
81 add_select_channel (fd, background_attention, ctx);
84 void
85 unregister_task_running (pid_t pid, int fd)
87 TaskList *p = task_list;
88 TaskList *prev = 0;
90 while (p){
91 if (p->pid == pid){
92 if (prev)
93 prev->next = p->next;
94 else
95 task_list = p->next;
96 g_free (p->info);
97 g_free (p);
98 break;
100 prev = p;
101 p = p->next;
103 delete_select_channel (fd);
107 * Try to make the Midnight Commander a background job
109 * Returns:
110 * 1 for parent
111 * 0 for child
112 * -1 on failure
115 do_background (struct FileOpContext *ctx, char *info)
117 int comm[2]; /* control connection stream */
118 pid_t pid;
120 if (pipe (comm) == -1)
121 return -1;
123 if ((pid = fork ()) == -1) {
124 int saved_errno = errno;
125 (void) close (comm[0]);
126 (void) close (comm[1]);
127 errno = saved_errno;
128 return -1;
131 if (pid == 0) {
132 int nullfd;
134 close (comm[0]);
135 parent_fd = comm[1];
136 we_are_background = 1;
137 current_dlg = NULL;
139 /* Make stdin/stdout/stderr point somewhere */
140 close (0);
141 close (1);
142 close (2);
144 if ((nullfd = open ("/dev/null", O_RDWR)) != -1) {
145 while (dup2 (nullfd, 0) == -1 && errno == EINTR);
146 while (dup2 (nullfd, 1) == -1 && errno == EINTR);
147 while (dup2 (nullfd, 2) == -1 && errno == EINTR);
150 return 0;
151 } else {
152 close (comm[1]);
153 ctx->pid = pid;
154 register_task_running (ctx, pid, comm[0], info);
155 return 1;
159 /* {{{ Parent handlers */
161 /* Parent/child protocol
163 * the child (the background) process send the following:
164 * void *routine -- routine to be invoked in the parent
165 * int nargc -- number of arguments
166 * int type -- Return argument type.
168 * If the routine is zero, then it is a way to tell the parent
169 * that the process is dying.
171 * nargc arguments in the following format:
172 * int size of the coming block
173 * size bytes with the block
175 * Now, the parent loads all those and then invokes
176 * the routine with pointers to the information passed
177 * (we just support pointers).
179 * If the return type is integer:
181 * the parent then writes an int to the child with
182 * the return value from the routine and the values
183 * of any global variable that is modified in the parent
184 * currently: do_append and recursive_result.
186 * If the return type is a string:
188 * the parent writes the resulting string length
189 * if the result string was NULL or the empty string,
190 * then the length is zero.
191 * The parent then writes the string length and frees
192 * the result string.
195 * Receive requests from background process and invoke the
196 * specified routine
199 static int
200 background_attention (int fd, void *closure)
202 FileOpContext *ctx;
203 int have_ctx;
204 union
206 int (*have_ctx1)(int, char *);
207 int (*have_ctx2)(int, char *, char *);
208 int (*have_ctx3)(int, char *, char *, char *);
209 int (*have_ctx4)(int, char *, char *, char *, char *);
211 int (*non_have_ctx1)(FileOpContext *, int, char *);
212 int (*non_have_ctx2)(FileOpContext *, int, char *, char *);
213 int (*non_have_ctx3)(FileOpContext *, int, char *, char *, char *);
214 int (*non_have_ctx4)(FileOpContext *, int, char *, char *, char *, char *);
216 char * (*ret_str1)(char *);
217 char * (*ret_str2)(char *, char *);
218 char * (*ret_str3)(char *, char *, char *);
219 char * (*ret_str4)(char *, char *, char *, char *);
221 void *pointer;
222 } routine;
223 /* void *routine;*/
224 int argc, i, result, status;
225 char *data [MAXCALLARGS];
226 ssize_t bytes;
227 enum ReturnType type;
229 ctx = closure;
231 bytes = read (fd, &routine.pointer, sizeof (routine));
232 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) {
233 const char *background_process_error = _(" Background process error ");
235 unregister_task_running (ctx->pid, fd);
236 if (!waitpid (ctx->pid, &status, WNOHANG)) {
237 /* the process is still running, but it misbehaves - kill it */
238 kill (ctx->pid, SIGTERM);
239 message (D_ERROR, background_process_error, _(" Unknown error in child "));
240 return 0;
243 /* 0 means happy end */
244 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
245 return 0;
247 message (D_ERROR, background_process_error, _(" Child died unexpectedly "));
249 return 0;
252 read (fd, &argc, sizeof (argc));
253 if (argc > MAXCALLARGS){
254 message (D_ERROR, _(" Background protocol error "),
255 _(" Background process sent us a request for more arguments \n"
256 " than we can handle. \n"));
258 read (fd, &type, sizeof (type));
259 read (fd, &have_ctx, sizeof (have_ctx));
260 if (have_ctx)
261 read (fd, ctx, sizeof (FileOpContext));
263 for (i = 0; i < argc; i++){
264 int size;
266 read (fd, &size, sizeof (size));
267 data [i] = g_malloc (size+1);
268 read (fd, data [i], size);
270 data [i][size] = 0; /* NULL terminate the blocks (they could be strings) */
273 /* Handle the call */
274 if (type == Return_Integer){
275 if (!have_ctx)
276 switch (argc){
277 case 1:
278 result = routine.have_ctx1 (Background, data [0]);
279 break;
280 case 2:
281 result = routine.have_ctx2 (Background, data [0], data [1]);
282 break;
283 case 3:
284 result = routine.have_ctx3 (Background, data [0], data [1], data [2]);
285 break;
286 case 4:
287 result = routine.have_ctx4 (Background, data [0], data [1], data [2], data [3]);
288 break;
290 else
291 switch (argc){
292 case 1:
293 result = routine.non_have_ctx1 (ctx, Background, data [0]);
294 break;
295 case 2:
296 result = routine.non_have_ctx2 (ctx, Background, data [0], data [1]);
297 break;
298 case 3:
299 result = routine.non_have_ctx3 (ctx, Background, data [0], data [1], data [2]);
300 break;
301 case 4:
302 result = routine.non_have_ctx4 (ctx, Background, data [0], data [1], data [2], data [3]);
303 break;
306 /* Send the result code and the value for shared variables */
307 write (fd, &result, sizeof (int));
308 if (have_ctx)
309 write (fd, ctx, sizeof (FileOpContext));
310 } else if (type == Return_String) {
311 int len;
312 char *resstr = NULL;
314 /* FIXME: string routines should also use the Foreground/Background
315 * parameter. Currently, this is not used here
317 switch (argc){
318 case 1:
319 resstr = routine.ret_str1 (data [0]);
320 break;
321 case 2:
322 resstr = routine.ret_str2 (data [0], data [1]);
323 break;
324 case 3:
325 resstr = routine.ret_str3 (data [0], data [1], data [2]);
326 break;
327 case 4:
328 resstr = routine.ret_str4 (data [0], data [1], data [2], data [3]);
329 break;
330 default: g_assert_not_reached();
332 if (resstr){
333 len = strlen (resstr);
334 write (fd, &len, sizeof (len));
335 if (len){
336 write (fd, resstr, len);
337 g_free (resstr);
339 } else {
340 len = 0;
341 write (fd, &len, sizeof (len));
344 for (i = 0; i < argc; i++)
345 g_free (data [i]);
347 do_refresh ();
348 mc_refresh ();
349 doupdate ();
350 return 0;
354 /* }}} */
356 /* {{{ client RPC routines */
358 /* Sends the header for a call to a routine in the parent process. If the file
359 * operation context is not NULL, then it requests that the first parameter of
360 * the call be a file operation context.
362 static void
363 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
365 int have_ctx;
367 have_ctx = (ctx != NULL);
369 write (parent_fd, &routine, sizeof (routine));
370 write (parent_fd, &argc, sizeof (int));
371 write (parent_fd, &type, sizeof (type));
372 write (parent_fd, &have_ctx, sizeof (have_ctx));
374 if (have_ctx)
375 write (parent_fd, ctx, sizeof (FileOpContext));
379 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
381 va_list ap;
382 int i;
384 va_start (ap, argc);
385 parent_call_header (routine, argc, Return_Integer, ctx);
386 for (i = 0; i < argc; i++) {
387 int len;
388 void *value;
390 len = va_arg (ap, int);
391 value = va_arg (ap, void *);
392 write (parent_fd, &len, sizeof (int));
393 write (parent_fd, value, len);
395 read (parent_fd, &i, sizeof (int));
396 if (ctx)
397 read (parent_fd, ctx, sizeof (FileOpContext));
399 return i;
402 char *
403 parent_call_string (void *routine, int argc, ...)
405 va_list ap;
406 char *str;
407 int i;
409 va_start (ap, argc);
410 parent_call_header (routine, argc, Return_String, NULL);
411 for (i = 0; i < argc; i++){
412 int len;
413 void *value;
415 len = va_arg (ap, int);
416 value = va_arg (ap, void *);
417 write (parent_fd, &len, sizeof (int));
418 write (parent_fd, value, len);
420 read (parent_fd, &i, sizeof (int));
421 if (!i)
422 return NULL;
423 str = g_malloc (i + 1);
424 read (parent_fd, str, i);
425 str [i] = 0;
426 return str;
429 #endif /* WITH_BACKGROUND */