Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / background.c
blobf100a518d89d05f85229d0de62a5ac9b59f2827a
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 <stdlib.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h> /* waitpid() */
43 #include <unistd.h>
44 #include <fcntl.h>
46 #include "lib/global.h"
47 #include "background.h"
48 #include "wtools.h"
49 #include "layout.h" /* repaint_screen() */
50 #include "fileopctx.h" /* FileOpContext */
51 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
53 enum ReturnType
55 Return_String,
56 Return_Integer
59 /* If true, this is a background process */
60 int we_are_background = 0;
62 /* File descriptor for talking to our parent */
63 static int parent_fd;
65 /* File descriptor for messages from our parent */
66 static int from_parent_fd;
68 #define MAXCALLARGS 4 /* Number of arguments supported */
70 struct TaskList *task_list = NULL;
72 static int background_attention (int fd, void *closure);
74 static void
75 register_task_running (FileOpContext * ctx, pid_t pid, int fd, int to_child, char *info)
77 TaskList *new;
79 new = g_new (TaskList, 1);
80 new->pid = pid;
81 new->info = info;
82 new->state = Task_Running;
83 new->next = task_list;
84 new->fd = fd;
85 new->to_child_fd = to_child;
86 task_list = new;
88 add_select_channel (fd, background_attention, ctx);
91 static int
92 destroy_task_and_return_fd (pid_t pid)
94 TaskList *p = task_list;
95 TaskList *prev = 0;
97 while (p)
99 if (p->pid == pid)
101 if (prev)
102 prev->next = p->next;
103 else
104 task_list = p->next;
105 g_free (p->info);
106 g_free (p);
107 return p->fd;
109 prev = p;
110 p = p->next;
113 /* pid not found */
114 return -1;
117 void
118 unregister_task_running (pid_t pid, int fd)
120 destroy_task_and_return_fd (pid);
121 delete_select_channel (fd);
124 void
125 unregister_task_with_pid (pid_t pid)
127 int fd = destroy_task_and_return_fd (pid);
128 if (fd != -1)
129 delete_select_channel (fd);
133 * Try to make the Midnight Commander a background job
135 * Returns:
136 * 1 for parent
137 * 0 for child
138 * -1 on failure
141 do_background (struct FileOpContext *ctx, char *info)
143 int comm[2]; /* control connection stream */
144 int back_comm[2]; /* back connection */
145 pid_t pid;
147 if (pipe (comm) == -1)
148 return -1;
150 if (pipe (back_comm) == -1)
151 return -1;
153 pid = fork ();
154 if (pid == -1)
156 int saved_errno = errno;
158 (void) close (comm[0]);
159 (void) close (comm[1]);
160 (void) close (back_comm[0]);
161 (void) close (back_comm[1]);
162 errno = saved_errno;
163 return -1;
166 if (pid == 0)
168 int nullfd;
170 parent_fd = comm[1];
171 from_parent_fd = back_comm[0];
173 we_are_background = 1;
174 top_dlg = NULL;
176 /* Make stdin/stdout/stderr point somewhere */
177 close (0);
178 close (1);
179 close (2);
181 nullfd = open ("/dev/null", O_RDWR);
182 if (nullfd != -1)
184 while (dup2 (nullfd, 0) == -1 && errno == EINTR)
186 while (dup2 (nullfd, 1) == -1 && errno == EINTR)
188 while (dup2 (nullfd, 2) == -1 && errno == EINTR)
192 return 0;
194 else
196 ctx->pid = pid;
197 register_task_running (ctx, pid, comm[0], back_comm[1], info);
198 return 1;
202 /* {{{ Parent handlers */
204 /* Parent/child protocol
206 * the child (the background) process send the following:
207 * void *routine -- routine to be invoked in the parent
208 * int nargc -- number of arguments
209 * int type -- Return argument type.
211 * If the routine is zero, then it is a way to tell the parent
212 * that the process is dying.
214 * nargc arguments in the following format:
215 * int size of the coming block
216 * size bytes with the block
218 * Now, the parent loads all those and then invokes
219 * the routine with pointers to the information passed
220 * (we just support pointers).
222 * If the return type is integer:
224 * the parent then writes an int to the child with
225 * the return value from the routine and the values
226 * of any global variable that is modified in the parent
227 * currently: do_append and recursive_result.
229 * If the return type is a string:
231 * the parent writes the resulting string length
232 * if the result string was NULL or the empty string,
233 * then the length is zero.
234 * The parent then writes the string length and frees
235 * the result string.
238 * Receive requests from background process and invoke the
239 * specified routine
242 static int
243 background_attention (int fd, void *closure)
245 FileOpContext *ctx;
246 int have_ctx;
247 union
249 int (*have_ctx0) (int);
250 int (*have_ctx1) (int, char *);
251 int (*have_ctx2) (int, char *, char *);
252 int (*have_ctx3) (int, char *, char *, char *);
253 int (*have_ctx4) (int, char *, char *, char *, char *);
255 int (*non_have_ctx0) (FileOpContext *, int);
256 int (*non_have_ctx1) (FileOpContext *, int, char *);
257 int (*non_have_ctx2) (FileOpContext *, int, char *, char *);
258 int (*non_have_ctx3) (FileOpContext *, int, char *, char *, char *);
259 int (*non_have_ctx4) (FileOpContext *, int, char *, char *, char *, char *);
261 char *(*ret_str0) ();
262 char *(*ret_str1) (char *);
263 char *(*ret_str2) (char *, char *);
264 char *(*ret_str3) (char *, char *, char *);
265 char *(*ret_str4) (char *, char *, char *, char *);
267 void *pointer;
268 } routine;
269 /* void *routine; */
270 int argc, i, result, status;
271 char *data[MAXCALLARGS];
272 ssize_t bytes, ret;
273 struct TaskList *p;
274 int to_child_fd = -1;
275 enum ReturnType type;
277 ctx = closure;
279 bytes = read (fd, &routine.pointer, sizeof (routine));
280 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
282 const char *background_process_error = _("Background process error");
284 unregister_task_running (ctx->pid, fd);
285 if (!waitpid (ctx->pid, &status, WNOHANG))
287 /* the process is still running, but it misbehaves - kill it */
288 kill (ctx->pid, SIGTERM);
289 message (D_ERROR, background_process_error, _("Unknown error in child"));
290 return 0;
293 /* 0 means happy end */
294 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
295 return 0;
297 message (D_ERROR, background_process_error, _("Child died unexpectedly"));
299 return 0;
302 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
303 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
304 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
306 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
307 return 0;
310 if (argc > MAXCALLARGS)
312 message (D_ERROR, _("Background protocol error"),
313 _("Background process sent us a request for more arguments\n"
314 "than we can handle."));
317 if (have_ctx)
319 if (read (fd, ctx, sizeof (FileOpContext)) != sizeof (FileOpContext))
321 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
322 return 0;
326 for (i = 0; i < argc; i++)
328 int size;
330 if (read (fd, &size, sizeof (size)) != sizeof (size))
332 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
333 return 0;
335 data[i] = g_malloc (size + 1);
336 if (read (fd, data[i], size) != size)
338 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
339 return 0;
341 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
344 /* Find child task info by descriptor */
345 /* Find before call, because process can destroy self after */
346 for (p = task_list; p; p = p->next)
348 if (p->fd == fd)
349 break;
352 if (p)
353 to_child_fd = p->to_child_fd;
355 if (to_child_fd == -1)
356 message (D_ERROR, _("Background process error"), _("Unknown error in child"));
358 /* Handle the call */
359 if (type == Return_Integer)
361 if (!have_ctx)
362 switch (argc)
364 case 0:
365 result = routine.have_ctx0 (Background);
366 break;
367 case 1:
368 result = routine.have_ctx1 (Background, data[0]);
369 break;
370 case 2:
371 result = routine.have_ctx2 (Background, data[0], data[1]);
372 break;
373 case 3:
374 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
375 break;
376 case 4:
377 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
378 break;
380 else
381 switch (argc)
383 case 0:
384 result = routine.non_have_ctx0 (ctx, Background);
385 break;
386 case 1:
387 result = routine.non_have_ctx1 (ctx, Background, data[0]);
388 break;
389 case 2:
390 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
391 break;
392 case 3:
393 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
394 break;
395 case 4:
396 result =
397 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
398 break;
401 /* Send the result code and the value for shared variables */
402 ret = write (to_child_fd, &result, sizeof (int));
403 if (have_ctx && to_child_fd != -1)
404 ret = write (to_child_fd, ctx, sizeof (FileOpContext));
406 else if (type == Return_String)
408 int len;
409 char *resstr = NULL;
411 /* FIXME: string routines should also use the Foreground/Background
412 * parameter. Currently, this is not used here
414 switch (argc)
416 case 0:
417 resstr = routine.ret_str0 ();
418 break;
419 case 1:
420 resstr = routine.ret_str1 (data[0]);
421 break;
422 case 2:
423 resstr = routine.ret_str2 (data[0], data[1]);
424 break;
425 case 3:
426 resstr = routine.ret_str3 (data[0], data[1], data[2]);
427 break;
428 case 4:
429 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
430 break;
431 default:
432 g_assert_not_reached ();
434 if (resstr)
436 len = strlen (resstr);
437 ret = write (to_child_fd, &len, sizeof (len));
438 if (len != 0)
439 ret = write (to_child_fd, resstr, len);
440 g_free (resstr);
442 else
444 len = 0;
445 ret = write (to_child_fd, &len, sizeof (len));
448 for (i = 0; i < argc; i++)
449 g_free (data[i]);
451 repaint_screen ();
452 return 0;
456 /* }}} */
458 /* {{{ client RPC routines */
460 /* Sends the header for a call to a routine in the parent process. If the file
461 * operation context is not NULL, then it requests that the first parameter of
462 * the call be a file operation context.
464 static void
465 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext * ctx)
467 int have_ctx;
468 ssize_t ret;
470 have_ctx = (ctx != NULL);
472 ret = write (parent_fd, &routine, sizeof (routine));
473 ret = write (parent_fd, &argc, sizeof (int));
474 ret = write (parent_fd, &type, sizeof (type));
475 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
477 if (have_ctx)
478 ret = write (parent_fd, ctx, sizeof (FileOpContext));
482 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
484 va_list ap;
485 int i;
486 ssize_t ret;
488 va_start (ap, argc);
489 parent_call_header (routine, argc, Return_Integer, ctx);
490 for (i = 0; i < argc; i++)
492 int len;
493 void *value;
495 len = va_arg (ap, int);
496 value = va_arg (ap, void *);
497 ret = write (parent_fd, &len, sizeof (int));
498 ret = write (parent_fd, value, len);
501 ret = read (from_parent_fd, &i, sizeof (int));
502 if (ctx)
503 ret = read (from_parent_fd, ctx, sizeof (FileOpContext));
505 return i;
508 char *
509 parent_call_string (void *routine, int argc, ...)
511 va_list ap;
512 char *str;
513 int i;
515 va_start (ap, argc);
516 parent_call_header (routine, argc, Return_String, NULL);
517 for (i = 0; i < argc; i++)
519 int len;
520 void *value;
522 len = va_arg (ap, int);
523 value = va_arg (ap, void *);
524 if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
525 (write (parent_fd, value, len) != len))
526 return NULL;
528 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
529 return NULL;
530 if (!i)
531 return NULL;
532 str = g_malloc (i + 1);
533 if (read (from_parent_fd, str, i) != i)
535 g_free (str);
536 return NULL;
538 str[i] = 0;
539 return str;
542 #endif /* WITH_BACKGROUND */