(load_keymap_from_section): remove unneeded keybind conversions.
[midnight-commander.git] / src / background.c
blob8b39a454661ee86c518a52567ce9c926630f4049
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 "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
48 #include "lib/widget.h" /* message() */
49 #include "lib/event-types.h"
51 #include "filemanager/fileopctx.h" /* FileOpContext */
53 #include "background.h"
55 /*** global variables ****************************************************************************/
57 #define MAXCALLARGS 4 /* Number of arguments supported */
59 /*** file scope macro definitions ****************************************************************/
61 /*** file scope type declarations ****************************************************************/
63 enum ReturnType
65 Return_String,
66 Return_Integer
69 /*** file scope variables ************************************************************************/
71 /* File descriptor for talking to our parent */
72 static int parent_fd;
74 /* File descriptor for messages from our parent */
75 static int from_parent_fd;
77 struct TaskList *task_list = NULL;
79 static int background_attention (int fd, void *closure);
81 /*** file scope functions ************************************************************************/
82 /* --------------------------------------------------------------------------------------------- */
84 static void
85 register_task_running (FileOpContext * ctx, pid_t pid, int fd, int to_child, char *info)
87 TaskList *new;
89 new = g_new (TaskList, 1);
90 new->pid = pid;
91 new->info = info;
92 new->state = Task_Running;
93 new->next = task_list;
94 new->fd = fd;
95 new->to_child_fd = to_child;
96 task_list = new;
98 add_select_channel (fd, background_attention, ctx);
101 /* --------------------------------------------------------------------------------------------- */
103 static int
104 destroy_task_and_return_fd (pid_t pid)
106 TaskList *p = task_list;
107 TaskList *prev = 0;
109 while (p)
111 if (p->pid == pid)
113 if (prev)
114 prev->next = p->next;
115 else
116 task_list = p->next;
117 g_free (p->info);
118 g_free (p);
119 return p->fd;
121 prev = p;
122 p = p->next;
125 /* pid not found */
126 return -1;
129 /* --------------------------------------------------------------------------------------------- */
130 /* {{{ Parent handlers */
132 /* Parent/child protocol
134 * the child (the background) process send the following:
135 * void *routine -- routine to be invoked in the parent
136 * int nargc -- number of arguments
137 * int type -- Return argument type.
139 * If the routine is zero, then it is a way to tell the parent
140 * that the process is dying.
142 * nargc arguments in the following format:
143 * int size of the coming block
144 * size bytes with the block
146 * Now, the parent loads all those and then invokes
147 * the routine with pointers to the information passed
148 * (we just support pointers).
150 * If the return type is integer:
152 * the parent then writes an int to the child with
153 * the return value from the routine and the values
154 * of any global variable that is modified in the parent
155 * currently: do_append and recursive_result.
157 * If the return type is a string:
159 * the parent writes the resulting string length
160 * if the result string was NULL or the empty string,
161 * then the length is zero.
162 * The parent then writes the string length and frees
163 * the result string.
166 * Receive requests from background process and invoke the
167 * specified routine
170 static int
171 background_attention (int fd, void *closure)
173 FileOpContext *ctx;
174 int have_ctx;
175 union
177 int (*have_ctx0) (int);
178 int (*have_ctx1) (int, char *);
179 int (*have_ctx2) (int, char *, char *);
180 int (*have_ctx3) (int, char *, char *, char *);
181 int (*have_ctx4) (int, char *, char *, char *, char *);
183 int (*non_have_ctx0) (FileOpContext *, int);
184 int (*non_have_ctx1) (FileOpContext *, int, char *);
185 int (*non_have_ctx2) (FileOpContext *, int, char *, char *);
186 int (*non_have_ctx3) (FileOpContext *, int, char *, char *, char *);
187 int (*non_have_ctx4) (FileOpContext *, int, char *, char *, char *, char *);
189 char *(*ret_str0) ();
190 char *(*ret_str1) (char *);
191 char *(*ret_str2) (char *, char *);
192 char *(*ret_str3) (char *, char *, char *);
193 char *(*ret_str4) (char *, char *, char *, char *);
195 void *pointer;
196 } routine;
197 /* void *routine; */
198 int argc, i, result, status;
199 char *data[MAXCALLARGS];
200 ssize_t bytes, ret;
201 struct TaskList *p;
202 int to_child_fd = -1;
203 enum ReturnType type;
205 ctx = closure;
207 bytes = read (fd, &routine.pointer, sizeof (routine));
208 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
210 const char *background_process_error = _("Background process error");
212 unregister_task_running (ctx->pid, fd);
213 if (!waitpid (ctx->pid, &status, WNOHANG))
215 /* the process is still running, but it misbehaves - kill it */
216 kill (ctx->pid, SIGTERM);
217 message (D_ERROR, background_process_error, _("Unknown error in child"));
218 return 0;
221 /* 0 means happy end */
222 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
223 return 0;
225 message (D_ERROR, background_process_error, _("Child died unexpectedly"));
227 return 0;
230 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
231 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
232 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
234 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
235 return 0;
238 if (argc > MAXCALLARGS)
240 message (D_ERROR, _("Background protocol error"),
241 _("Background process sent us a request for more arguments\n"
242 "than we can handle."));
245 if (have_ctx)
247 if (read (fd, ctx, sizeof (FileOpContext)) != sizeof (FileOpContext))
249 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
250 return 0;
254 for (i = 0; i < argc; i++)
256 int size;
258 if (read (fd, &size, sizeof (size)) != sizeof (size))
260 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
261 return 0;
263 data[i] = g_malloc (size + 1);
264 if (read (fd, data[i], size) != size)
266 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
267 return 0;
269 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
272 /* Find child task info by descriptor */
273 /* Find before call, because process can destroy self after */
274 for (p = task_list; p; p = p->next)
276 if (p->fd == fd)
277 break;
280 if (p)
281 to_child_fd = p->to_child_fd;
283 if (to_child_fd == -1)
284 message (D_ERROR, _("Background process error"), _("Unknown error in child"));
286 /* Handle the call */
287 if (type == Return_Integer)
289 if (!have_ctx)
290 switch (argc)
292 case 0:
293 result = routine.have_ctx0 (Background);
294 break;
295 case 1:
296 result = routine.have_ctx1 (Background, data[0]);
297 break;
298 case 2:
299 result = routine.have_ctx2 (Background, data[0], data[1]);
300 break;
301 case 3:
302 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
303 break;
304 case 4:
305 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
306 break;
308 else
309 switch (argc)
311 case 0:
312 result = routine.non_have_ctx0 (ctx, Background);
313 break;
314 case 1:
315 result = routine.non_have_ctx1 (ctx, Background, data[0]);
316 break;
317 case 2:
318 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
319 break;
320 case 3:
321 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
322 break;
323 case 4:
324 result =
325 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
326 break;
329 /* Send the result code and the value for shared variables */
330 ret = write (to_child_fd, &result, sizeof (int));
331 if (have_ctx && to_child_fd != -1)
332 ret = write (to_child_fd, ctx, sizeof (FileOpContext));
334 else if (type == Return_String)
336 int len;
337 char *resstr = NULL;
339 /* FIXME: string routines should also use the Foreground/Background
340 * parameter. Currently, this is not used here
342 switch (argc)
344 case 0:
345 resstr = routine.ret_str0 ();
346 break;
347 case 1:
348 resstr = routine.ret_str1 (data[0]);
349 break;
350 case 2:
351 resstr = routine.ret_str2 (data[0], data[1]);
352 break;
353 case 3:
354 resstr = routine.ret_str3 (data[0], data[1], data[2]);
355 break;
356 case 4:
357 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
358 break;
359 default:
360 g_assert_not_reached ();
362 if (resstr)
364 len = strlen (resstr);
365 ret = write (to_child_fd, &len, sizeof (len));
366 if (len != 0)
367 ret = write (to_child_fd, resstr, len);
368 g_free (resstr);
370 else
372 len = 0;
373 ret = write (to_child_fd, &len, sizeof (len));
376 for (i = 0; i < argc; i++)
377 g_free (data[i]);
379 repaint_screen ();
380 return 0;
384 /* --------------------------------------------------------------------------------------------- */
385 /* }}} */
387 /* {{{ client RPC routines */
389 /* Sends the header for a call to a routine in the parent process. If the file
390 * operation context is not NULL, then it requests that the first parameter of
391 * the call be a file operation context.
394 static void
395 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext * ctx)
397 int have_ctx;
398 ssize_t ret;
400 have_ctx = (ctx != NULL);
402 ret = write (parent_fd, &routine, sizeof (routine));
403 ret = write (parent_fd, &argc, sizeof (int));
404 ret = write (parent_fd, &type, sizeof (type));
405 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
407 if (have_ctx)
408 ret = write (parent_fd, ctx, sizeof (FileOpContext));
411 /* --------------------------------------------------------------------------------------------- */
413 static int
414 parent_va_call (void *routine, gpointer data, int argc, va_list ap)
416 int i;
417 ssize_t ret;
418 struct FileOpContext *ctx = (struct FileOpContext *) data;
420 parent_call_header (routine, argc, Return_Integer, ctx);
421 for (i = 0; i < argc; i++)
423 int len;
424 void *value;
426 len = va_arg (ap, int);
427 value = va_arg (ap, void *);
428 ret = write (parent_fd, &len, sizeof (int));
429 ret = write (parent_fd, value, len);
432 ret = read (from_parent_fd, &i, sizeof (int));
433 if (ctx)
434 ret = read (from_parent_fd, ctx, sizeof (FileOpContext));
436 return i;
439 /* --------------------------------------------------------------------------------------------- */
441 static char *
442 parent_va_call_string (void *routine, int argc, va_list ap)
444 char *str;
445 int i;
447 parent_call_header (routine, argc, Return_String, NULL);
448 for (i = 0; i < argc; i++)
450 int len;
451 void *value;
453 len = va_arg (ap, int);
454 value = va_arg (ap, void *);
455 if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
456 (write (parent_fd, value, len) != len))
458 return NULL;
462 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
463 return NULL;
464 if (!i)
465 return NULL;
466 str = g_malloc (i + 1);
467 if (read (from_parent_fd, str, i) != i)
469 g_free (str);
470 return NULL;
472 str[i] = 0;
473 return str;
476 /* --------------------------------------------------------------------------------------------- */
477 /*** public functions ****************************************************************************/
478 /* --------------------------------------------------------------------------------------------- */
480 void
481 unregister_task_running (pid_t pid, int fd)
483 destroy_task_and_return_fd (pid);
484 delete_select_channel (fd);
487 /* --------------------------------------------------------------------------------------------- */
489 void
490 unregister_task_with_pid (pid_t pid)
492 int fd = destroy_task_and_return_fd (pid);
493 if (fd != -1)
494 delete_select_channel (fd);
498 /* --------------------------------------------------------------------------------------------- */
500 * Try to make the Midnight Commander a background job
502 * Returns:
503 * 1 for parent
504 * 0 for child
505 * -1 on failure
508 do_background (struct FileOpContext *ctx, char *info)
510 int comm[2]; /* control connection stream */
511 int back_comm[2]; /* back connection */
512 pid_t pid;
514 if (pipe (comm) == -1)
515 return -1;
517 if (pipe (back_comm) == -1)
518 return -1;
520 pid = fork ();
521 if (pid == -1)
523 int saved_errno = errno;
525 (void) close (comm[0]);
526 (void) close (comm[1]);
527 (void) close (back_comm[0]);
528 (void) close (back_comm[1]);
529 errno = saved_errno;
530 return -1;
533 if (pid == 0)
535 int nullfd;
537 parent_fd = comm[1];
538 from_parent_fd = back_comm[0];
540 mc_global.we_are_background = 1;
541 top_dlg = NULL;
543 /* Make stdin/stdout/stderr point somewhere */
544 close (0);
545 close (1);
546 close (2);
548 nullfd = open ("/dev/null", O_RDWR);
549 if (nullfd != -1)
551 while (dup2 (nullfd, 0) == -1 && errno == EINTR)
553 while (dup2 (nullfd, 1) == -1 && errno == EINTR)
555 while (dup2 (nullfd, 2) == -1 && errno == EINTR)
559 return 0;
561 else
563 ctx->pid = pid;
564 register_task_running (ctx, pid, comm[0], back_comm[1], info);
565 return 1;
569 /* --------------------------------------------------------------------------------------------- */
572 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
574 int ret;
575 va_list ap;
577 va_start (ap, argc);
578 ret = parent_va_call (routine, (gpointer) ctx, argc, ap);
579 va_end (ap);
581 return ret;
584 /* --------------------------------------------------------------------------------------------- */
586 char *
587 parent_call_string (void *routine, int argc, ...)
589 va_list ap;
590 char *str;
592 va_start (ap, argc);
593 str = parent_va_call_string (routine, argc, ap);
594 va_end (ap);
596 return str;
599 /* --------------------------------------------------------------------------------------------- */
601 /* event callback */
602 gboolean
603 background_parent_call (const gchar * event_group_name, const gchar * event_name,
604 gpointer init_data, gpointer data)
606 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
608 (void) event_group_name;
609 (void) event_name;
610 (void) init_data;
612 event_data->ret.i =
613 parent_va_call (event_data->routine, event_data->ctx, event_data->argc, event_data->ap);
615 return TRUE;
618 /* --------------------------------------------------------------------------------------------- */
620 /* event callback */
621 gboolean
622 background_parent_call_string (const gchar * event_group_name, const gchar * event_name,
623 gpointer init_data, gpointer data)
625 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
627 (void) event_group_name;
628 (void) event_name;
629 (void) init_data;
631 event_data->ret.s =
632 parent_va_call_string (event_data->routine, event_data->argc, event_data->ap);
634 return TRUE;
637 /* --------------------------------------------------------------------------------------------- */
639 #endif /* WITH_BACKGROUND */