Merge branch '2678_resize_layout_fix'
[midnight-commander.git] / src / background.c
blobaac4aff8db423494dab62ca525355fd23241a1fb
1 /* {{{ Copyright */
3 /* Background support.
5 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
6 2011
7 The Free Software Foundation, Inc.
9 Written by:
10 Miguel de Icaza, 1996
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /* }}} */
30 /** \file background.c
31 * \brief Source: Background support
34 #include <config.h>
36 #ifdef WITH_BACKGROUND
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h> /* waitpid() */
48 #include <unistd.h>
49 #include <fcntl.h>
51 #include "lib/global.h"
52 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
53 #include "lib/widget.h" /* message() */
54 #include "lib/event-types.h"
56 #include "filemanager/fileopctx.h" /* FileOpContext */
58 #include "background.h"
60 /*** global variables ****************************************************************************/
62 #define MAXCALLARGS 4 /* Number of arguments supported */
64 /*** file scope macro definitions ****************************************************************/
66 /*** file scope type declarations ****************************************************************/
68 enum ReturnType
70 Return_String,
71 Return_Integer
74 /*** file scope variables ************************************************************************/
76 /* File descriptor for talking to our parent */
77 static int parent_fd;
79 /* File descriptor for messages from our parent */
80 static int from_parent_fd;
82 struct TaskList *task_list = NULL;
84 static int background_attention (int fd, void *closure);
86 /*** file scope functions ************************************************************************/
87 /* --------------------------------------------------------------------------------------------- */
89 static void
90 register_task_running (FileOpContext * ctx, pid_t pid, int fd, int to_child, char *info)
92 TaskList *new;
94 new = g_new (TaskList, 1);
95 new->pid = pid;
96 new->info = info;
97 new->state = Task_Running;
98 new->next = task_list;
99 new->fd = fd;
100 new->to_child_fd = to_child;
101 task_list = new;
103 add_select_channel (fd, background_attention, ctx);
106 /* --------------------------------------------------------------------------------------------- */
108 static int
109 destroy_task_and_return_fd (pid_t pid)
111 TaskList *p = task_list;
112 TaskList *prev = 0;
114 while (p)
116 if (p->pid == pid)
118 if (prev)
119 prev->next = p->next;
120 else
121 task_list = p->next;
122 g_free (p->info);
123 g_free (p);
124 return p->fd;
126 prev = p;
127 p = p->next;
130 /* pid not found */
131 return -1;
134 /* --------------------------------------------------------------------------------------------- */
135 /* {{{ Parent handlers */
137 /* Parent/child protocol
139 * the child (the background) process send the following:
140 * void *routine -- routine to be invoked in the parent
141 * int nargc -- number of arguments
142 * int type -- Return argument type.
144 * If the routine is zero, then it is a way to tell the parent
145 * that the process is dying.
147 * nargc arguments in the following format:
148 * int size of the coming block
149 * size bytes with the block
151 * Now, the parent loads all those and then invokes
152 * the routine with pointers to the information passed
153 * (we just support pointers).
155 * If the return type is integer:
157 * the parent then writes an int to the child with
158 * the return value from the routine and the values
159 * of any global variable that is modified in the parent
160 * currently: do_append and recursive_result.
162 * If the return type is a string:
164 * the parent writes the resulting string length
165 * if the result string was NULL or the empty string,
166 * then the length is zero.
167 * The parent then writes the string length and frees
168 * the result string.
171 * Receive requests from background process and invoke the
172 * specified routine
175 static int
176 background_attention (int fd, void *closure)
178 FileOpContext *ctx;
179 int have_ctx;
180 union
182 int (*have_ctx0) (int);
183 int (*have_ctx1) (int, char *);
184 int (*have_ctx2) (int, char *, char *);
185 int (*have_ctx3) (int, char *, char *, char *);
186 int (*have_ctx4) (int, char *, char *, char *, char *);
188 int (*non_have_ctx0) (FileOpContext *, int);
189 int (*non_have_ctx1) (FileOpContext *, int, char *);
190 int (*non_have_ctx2) (FileOpContext *, int, char *, char *);
191 int (*non_have_ctx3) (FileOpContext *, int, char *, char *, char *);
192 int (*non_have_ctx4) (FileOpContext *, int, char *, char *, char *, char *);
194 char *(*ret_str0) ();
195 char *(*ret_str1) (char *);
196 char *(*ret_str2) (char *, char *);
197 char *(*ret_str3) (char *, char *, char *);
198 char *(*ret_str4) (char *, char *, char *, char *);
200 void *pointer;
201 } routine;
202 /* void *routine; */
203 int argc, i, result, status;
204 char *data[MAXCALLARGS];
205 ssize_t bytes, ret;
206 struct TaskList *p;
207 int to_child_fd = -1;
208 enum ReturnType type;
210 ctx = closure;
212 bytes = read (fd, &routine.pointer, sizeof (routine));
213 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
215 const char *background_process_error = _("Background process error");
217 unregister_task_running (ctx->pid, fd);
218 if (!waitpid (ctx->pid, &status, WNOHANG))
220 /* the process is still running, but it misbehaves - kill it */
221 kill (ctx->pid, SIGTERM);
222 message (D_ERROR, background_process_error, _("Unknown error in child"));
223 return 0;
226 /* 0 means happy end */
227 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
228 return 0;
230 message (D_ERROR, background_process_error, _("Child died unexpectedly"));
232 return 0;
235 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
236 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
237 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
239 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
240 return 0;
243 if (argc > MAXCALLARGS)
245 message (D_ERROR, _("Background protocol error"),
246 _("Background process sent us a request for more arguments\n"
247 "than we can handle."));
250 if (have_ctx)
252 if (read (fd, ctx, sizeof (FileOpContext)) != sizeof (FileOpContext))
254 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
255 return 0;
259 for (i = 0; i < argc; i++)
261 int size;
263 if (read (fd, &size, sizeof (size)) != sizeof (size))
265 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
266 return 0;
268 data[i] = g_malloc (size + 1);
269 if (read (fd, data[i], size) != size)
271 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
272 return 0;
274 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
277 /* Find child task info by descriptor */
278 /* Find before call, because process can destroy self after */
279 for (p = task_list; p; p = p->next)
281 if (p->fd == fd)
282 break;
285 if (p)
286 to_child_fd = p->to_child_fd;
288 if (to_child_fd == -1)
289 message (D_ERROR, _("Background process error"), _("Unknown error in child"));
291 /* Handle the call */
292 if (type == Return_Integer)
294 if (!have_ctx)
295 switch (argc)
297 case 0:
298 result = routine.have_ctx0 (Background);
299 break;
300 case 1:
301 result = routine.have_ctx1 (Background, data[0]);
302 break;
303 case 2:
304 result = routine.have_ctx2 (Background, data[0], data[1]);
305 break;
306 case 3:
307 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
308 break;
309 case 4:
310 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
311 break;
313 else
314 switch (argc)
316 case 0:
317 result = routine.non_have_ctx0 (ctx, Background);
318 break;
319 case 1:
320 result = routine.non_have_ctx1 (ctx, Background, data[0]);
321 break;
322 case 2:
323 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
324 break;
325 case 3:
326 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
327 break;
328 case 4:
329 result =
330 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
331 break;
334 /* Send the result code and the value for shared variables */
335 ret = write (to_child_fd, &result, sizeof (int));
336 if (have_ctx && to_child_fd != -1)
337 ret = write (to_child_fd, ctx, sizeof (FileOpContext));
339 else if (type == Return_String)
341 int len;
342 char *resstr = NULL;
344 /* FIXME: string routines should also use the Foreground/Background
345 * parameter. Currently, this is not used here
347 switch (argc)
349 case 0:
350 resstr = routine.ret_str0 ();
351 break;
352 case 1:
353 resstr = routine.ret_str1 (data[0]);
354 break;
355 case 2:
356 resstr = routine.ret_str2 (data[0], data[1]);
357 break;
358 case 3:
359 resstr = routine.ret_str3 (data[0], data[1], data[2]);
360 break;
361 case 4:
362 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
363 break;
364 default:
365 g_assert_not_reached ();
367 if (resstr)
369 len = strlen (resstr);
370 ret = write (to_child_fd, &len, sizeof (len));
371 if (len != 0)
372 ret = write (to_child_fd, resstr, len);
373 g_free (resstr);
375 else
377 len = 0;
378 ret = write (to_child_fd, &len, sizeof (len));
381 for (i = 0; i < argc; i++)
382 g_free (data[i]);
384 repaint_screen ();
385 return 0;
389 /* --------------------------------------------------------------------------------------------- */
390 /* }}} */
392 /* {{{ client RPC routines */
394 /* Sends the header for a call to a routine in the parent process. If the file
395 * operation context is not NULL, then it requests that the first parameter of
396 * the call be a file operation context.
399 static void
400 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext * ctx)
402 int have_ctx;
403 ssize_t ret;
405 have_ctx = (ctx != NULL);
407 ret = write (parent_fd, &routine, sizeof (routine));
408 ret = write (parent_fd, &argc, sizeof (int));
409 ret = write (parent_fd, &type, sizeof (type));
410 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
412 if (have_ctx)
413 ret = write (parent_fd, ctx, sizeof (FileOpContext));
416 /* --------------------------------------------------------------------------------------------- */
418 static int
419 parent_va_call (void *routine, gpointer data, int argc, va_list ap)
421 int i;
422 ssize_t ret;
423 struct FileOpContext *ctx = (struct FileOpContext *) data;
425 parent_call_header (routine, argc, Return_Integer, ctx);
426 for (i = 0; i < argc; i++)
428 int len;
429 void *value;
431 len = va_arg (ap, int);
432 value = va_arg (ap, void *);
433 ret = write (parent_fd, &len, sizeof (int));
434 ret = write (parent_fd, value, len);
437 ret = read (from_parent_fd, &i, sizeof (int));
438 if (ctx)
439 ret = read (from_parent_fd, ctx, sizeof (FileOpContext));
441 return i;
444 /* --------------------------------------------------------------------------------------------- */
446 static char *
447 parent_va_call_string (void *routine, int argc, va_list ap)
449 char *str;
450 int i;
452 parent_call_header (routine, argc, Return_String, NULL);
453 for (i = 0; i < argc; i++)
455 int len;
456 void *value;
458 len = va_arg (ap, int);
459 value = va_arg (ap, void *);
460 if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
461 (write (parent_fd, value, len) != len))
463 return NULL;
467 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
468 return NULL;
469 if (!i)
470 return NULL;
471 str = g_malloc (i + 1);
472 if (read (from_parent_fd, str, i) != i)
474 g_free (str);
475 return NULL;
477 str[i] = 0;
478 return str;
481 /* --------------------------------------------------------------------------------------------- */
482 /*** public functions ****************************************************************************/
483 /* --------------------------------------------------------------------------------------------- */
485 void
486 unregister_task_running (pid_t pid, int fd)
488 destroy_task_and_return_fd (pid);
489 delete_select_channel (fd);
492 /* --------------------------------------------------------------------------------------------- */
494 void
495 unregister_task_with_pid (pid_t pid)
497 int fd = destroy_task_and_return_fd (pid);
498 if (fd != -1)
499 delete_select_channel (fd);
503 /* --------------------------------------------------------------------------------------------- */
505 * Try to make the Midnight Commander a background job
507 * Returns:
508 * 1 for parent
509 * 0 for child
510 * -1 on failure
513 do_background (struct FileOpContext *ctx, char *info)
515 int comm[2]; /* control connection stream */
516 int back_comm[2]; /* back connection */
517 pid_t pid;
519 if (pipe (comm) == -1)
520 return -1;
522 if (pipe (back_comm) == -1)
523 return -1;
525 pid = fork ();
526 if (pid == -1)
528 int saved_errno = errno;
530 (void) close (comm[0]);
531 (void) close (comm[1]);
532 (void) close (back_comm[0]);
533 (void) close (back_comm[1]);
534 errno = saved_errno;
535 return -1;
538 if (pid == 0)
540 int nullfd;
542 parent_fd = comm[1];
543 from_parent_fd = back_comm[0];
545 mc_global.we_are_background = 1;
546 top_dlg = NULL;
548 /* Make stdin/stdout/stderr point somewhere */
549 close (0);
550 close (1);
551 close (2);
553 nullfd = open ("/dev/null", O_RDWR);
554 if (nullfd != -1)
556 while (dup2 (nullfd, 0) == -1 && errno == EINTR)
558 while (dup2 (nullfd, 1) == -1 && errno == EINTR)
560 while (dup2 (nullfd, 2) == -1 && errno == EINTR)
564 return 0;
566 else
568 ctx->pid = pid;
569 register_task_running (ctx, pid, comm[0], back_comm[1], info);
570 return 1;
574 /* --------------------------------------------------------------------------------------------- */
577 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
579 int ret;
580 va_list ap;
582 va_start (ap, argc);
583 ret = parent_va_call (routine, (gpointer) ctx, argc, ap);
584 va_end (ap);
586 return ret;
589 /* --------------------------------------------------------------------------------------------- */
591 char *
592 parent_call_string (void *routine, int argc, ...)
594 va_list ap;
595 char *str;
597 va_start (ap, argc);
598 str = parent_va_call_string (routine, argc, ap);
599 va_end (ap);
601 return str;
604 /* --------------------------------------------------------------------------------------------- */
606 /* event callback */
607 gboolean
608 background_parent_call (const gchar * event_group_name, const gchar * event_name,
609 gpointer init_data, gpointer data)
611 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
613 (void) event_group_name;
614 (void) event_name;
615 (void) init_data;
617 event_data->ret.i =
618 parent_va_call (event_data->routine, event_data->ctx, event_data->argc, event_data->ap);
620 return TRUE;
623 /* --------------------------------------------------------------------------------------------- */
625 /* event callback */
626 gboolean
627 background_parent_call_string (const gchar * event_group_name, const gchar * event_name,
628 gpointer init_data, gpointer data)
630 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
632 (void) event_group_name;
633 (void) event_name;
634 (void) init_data;
636 event_data->ret.s =
637 parent_va_call_string (event_data->routine, event_data->argc, event_data->ap);
639 return TRUE;
642 /* --------------------------------------------------------------------------------------------- */
644 #endif /* WITH_BACKGROUND */