Set WOP_TOP_SELECT options for panel widgets: WPanel, WView and WTree.
[midnight-commander.git] / src / background.c
blob54b6e7d032495fa086357875722367b56a05c56b
1 /* {{{ Copyright */
3 /* Background support.
5 Copyright (C) 1996-2016
6 Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1996
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /* }}} */
29 /** \file background.c
30 * \brief Source: Background support
33 #include <config.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h> /* waitpid() */
46 #include "lib/global.h"
48 #include "lib/unixcompat.h"
49 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
50 #include "lib/widget.h" /* message() */
51 #include "lib/event-types.h"
53 #include "filemanager/fileopctx.h" /* file_op_context_t */
55 #include "background.h"
57 /*** global variables ****************************************************************************/
59 #define MAXCALLARGS 4 /* Number of arguments supported */
61 /*** file scope macro definitions ****************************************************************/
63 /*** file scope type declarations ****************************************************************/
65 enum ReturnType
67 Return_String,
68 Return_Integer
71 /*** file scope variables ************************************************************************/
73 /* File descriptor for talking to our parent */
74 static int parent_fd;
76 /* File descriptor for messages from our parent */
77 static int from_parent_fd;
79 TaskList *task_list = NULL;
81 static int background_attention (int fd, void *closure);
83 /*** file scope functions ************************************************************************/
84 /* --------------------------------------------------------------------------------------------- */
86 static void
87 register_task_running (file_op_context_t * ctx, pid_t pid, int fd, int to_child, char *info)
89 TaskList *new;
91 new = g_new (TaskList, 1);
92 new->pid = pid;
93 new->info = info;
94 new->state = Task_Running;
95 new->next = task_list;
96 new->fd = fd;
97 new->to_child_fd = to_child;
98 task_list = new;
100 add_select_channel (fd, background_attention, ctx);
103 /* --------------------------------------------------------------------------------------------- */
105 static int
106 destroy_task_and_return_fd (pid_t pid)
108 TaskList *p = task_list;
109 TaskList *prev = 0;
111 while (p)
113 if (p->pid == pid)
115 if (prev)
116 prev->next = p->next;
117 else
118 task_list = p->next;
119 g_free (p->info);
120 g_free (p);
121 return p->fd;
123 prev = p;
124 p = p->next;
127 /* pid not found */
128 return -1;
131 /* --------------------------------------------------------------------------------------------- */
132 /* {{{ Parent handlers */
134 /* Parent/child protocol
136 * the child (the background) process send the following:
137 * void *routine -- routine to be invoked in the parent
138 * int nargc -- number of arguments
139 * int type -- Return argument type.
141 * If the routine is zero, then it is a way to tell the parent
142 * that the process is dying.
144 * nargc arguments in the following format:
145 * int size of the coming block
146 * size bytes with the block
148 * Now, the parent loads all those and then invokes
149 * the routine with pointers to the information passed
150 * (we just support pointers).
152 * If the return type is integer:
154 * the parent then writes an int to the child with
155 * the return value from the routine and the values
156 * of any global variable that is modified in the parent
157 * currently: do_append and recursive_result.
159 * If the return type is a string:
161 * the parent writes the resulting string length
162 * if the result string was NULL or the empty string,
163 * then the length is zero.
164 * The parent then writes the string length and frees
165 * the result string.
168 * Receive requests from background process and invoke the
169 * specified routine
172 static int
173 reading_failed (int i, char **data)
175 while (i >= 0)
176 g_free (data[i--]);
177 message (D_ERROR, _("Background protocol error"), "%s", _("Reading failed"));
178 return 0;
181 /* --------------------------------------------------------------------------------------------- */
183 static int
184 background_attention (int fd, void *closure)
186 file_op_context_t *ctx;
187 int have_ctx;
188 union
190 int (*have_ctx0) (int);
191 int (*have_ctx1) (int, char *);
192 int (*have_ctx2) (int, char *, char *);
193 int (*have_ctx3) (int, char *, char *, char *);
194 int (*have_ctx4) (int, char *, char *, char *, char *);
196 int (*non_have_ctx0) (file_op_context_t *, int);
197 int (*non_have_ctx1) (file_op_context_t *, int, char *);
198 int (*non_have_ctx2) (file_op_context_t *, int, char *, char *);
199 int (*non_have_ctx3) (file_op_context_t *, int, char *, char *, char *);
200 int (*non_have_ctx4) (file_op_context_t *, int, char *, char *, char *, char *);
202 char *(*ret_str0) (void);
203 char *(*ret_str1) (char *);
204 char *(*ret_str2) (char *, char *);
205 char *(*ret_str3) (char *, char *, char *);
206 char *(*ret_str4) (char *, char *, char *, char *);
208 void *pointer;
209 } routine;
210 /* void *routine; */
211 int argc, i, status;
212 char *data[MAXCALLARGS];
213 ssize_t bytes, ret;
214 TaskList *p;
215 int to_child_fd = -1;
216 enum ReturnType type;
218 ctx = closure;
220 bytes = read (fd, &routine.pointer, sizeof (routine));
221 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
223 const char *background_process_error = _("Background process error");
225 unregister_task_running (ctx->pid, fd);
226 if (!waitpid (ctx->pid, &status, WNOHANG))
228 /* the process is still running, but it misbehaves - kill it */
229 kill (ctx->pid, SIGTERM);
230 message (D_ERROR, background_process_error, _("Unknown error in child"));
231 return 0;
234 /* 0 means happy end */
235 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
236 return 0;
238 message (D_ERROR, background_process_error, _("Child died unexpectedly"));
240 return 0;
243 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
244 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
245 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
247 return reading_failed (-1, data);
250 if (argc > MAXCALLARGS)
252 message (D_ERROR, _("Background protocol error"),
253 _("Background process sent us a request for more arguments\n"
254 "than we can handle."));
257 if (have_ctx)
259 if (read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
261 return reading_failed (-1, data);
265 for (i = 0; i < argc; i++)
267 int size;
269 if (read (fd, &size, sizeof (size)) != sizeof (size))
271 return reading_failed (i - 1, data);
273 data[i] = g_malloc (size + 1);
274 if (read (fd, data[i], size) != size)
276 return reading_failed (i, data);
278 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
281 /* Find child task info by descriptor */
282 /* Find before call, because process can destroy self after */
283 for (p = task_list; p; p = p->next)
285 if (p->fd == fd)
286 break;
289 if (p)
290 to_child_fd = p->to_child_fd;
292 if (to_child_fd == -1)
293 message (D_ERROR, _("Background process error"), _("Unknown error in child"));
295 /* Handle the call */
296 if (type == Return_Integer)
298 int result = 0;
300 if (!have_ctx)
301 switch (argc)
303 case 0:
304 result = routine.have_ctx0 (Background);
305 break;
306 case 1:
307 result = routine.have_ctx1 (Background, data[0]);
308 break;
309 case 2:
310 result = routine.have_ctx2 (Background, data[0], data[1]);
311 break;
312 case 3:
313 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
314 break;
315 case 4:
316 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
317 break;
318 default:
319 break;
321 else
322 switch (argc)
324 case 0:
325 result = routine.non_have_ctx0 (ctx, Background);
326 break;
327 case 1:
328 result = routine.non_have_ctx1 (ctx, Background, data[0]);
329 break;
330 case 2:
331 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
332 break;
333 case 3:
334 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
335 break;
336 case 4:
337 result =
338 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
339 break;
340 default:
341 break;
344 /* Send the result code and the value for shared variables */
345 ret = write (to_child_fd, &result, sizeof (result));
346 if (have_ctx && to_child_fd != -1)
347 ret = write (to_child_fd, ctx, sizeof (*ctx));
349 else if (type == Return_String)
351 int len;
352 char *resstr = NULL;
354 /* FIXME: string routines should also use the Foreground/Background
355 * parameter. Currently, this is not used here
357 switch (argc)
359 case 0:
360 resstr = routine.ret_str0 ();
361 break;
362 case 1:
363 resstr = routine.ret_str1 (data[0]);
364 break;
365 case 2:
366 resstr = routine.ret_str2 (data[0], data[1]);
367 break;
368 case 3:
369 resstr = routine.ret_str3 (data[0], data[1], data[2]);
370 break;
371 case 4:
372 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
373 break;
374 default:
375 g_assert_not_reached ();
377 if (resstr)
379 len = strlen (resstr);
380 ret = write (to_child_fd, &len, sizeof (len));
381 if (len != 0)
382 ret = write (to_child_fd, resstr, len);
383 g_free (resstr);
385 else
387 len = 0;
388 ret = write (to_child_fd, &len, sizeof (len));
391 for (i = 0; i < argc; i++)
392 g_free (data[i]);
394 repaint_screen ();
395 (void) ret;
396 return 0;
400 /* --------------------------------------------------------------------------------------------- */
401 /* }}} */
403 /* {{{ client RPC routines */
405 /* Sends the header for a call to a routine in the parent process. If the file
406 * operation context is not NULL, then it requests that the first parameter of
407 * the call be a file operation context.
410 static void
411 parent_call_header (void *routine, int argc, enum ReturnType type, file_op_context_t * ctx)
413 int have_ctx;
414 ssize_t ret;
416 have_ctx = (ctx != NULL);
418 ret = write (parent_fd, &routine, sizeof (routine));
419 ret = write (parent_fd, &argc, sizeof (argc));
420 ret = write (parent_fd, &type, sizeof (type));
421 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
423 if (have_ctx)
424 ret = write (parent_fd, ctx, sizeof (*ctx));
425 (void) ret;
428 /* --------------------------------------------------------------------------------------------- */
430 static int
431 parent_va_call (void *routine, gpointer data, int argc, va_list ap)
433 int i;
434 ssize_t ret;
435 file_op_context_t *ctx = (file_op_context_t *) data;
437 parent_call_header (routine, argc, Return_Integer, ctx);
438 for (i = 0; i < argc; i++)
440 int len;
441 void *value;
443 len = va_arg (ap, int);
444 value = va_arg (ap, void *);
445 ret = write (parent_fd, &len, sizeof (len));
446 ret = write (parent_fd, value, len);
449 ret = read (from_parent_fd, &i, sizeof (i));
450 if (ctx)
451 ret = read (from_parent_fd, ctx, sizeof (*ctx));
453 (void) ret;
454 return i;
457 /* --------------------------------------------------------------------------------------------- */
459 static char *
460 parent_va_call_string (void *routine, int argc, va_list ap)
462 char *str;
463 int i;
465 parent_call_header (routine, argc, Return_String, NULL);
466 for (i = 0; i < argc; i++)
468 int len;
469 void *value;
471 len = va_arg (ap, int);
472 value = va_arg (ap, void *);
473 if ((write (parent_fd, &len, sizeof (len)) != sizeof (len)) ||
474 (write (parent_fd, value, len) != len))
476 return NULL;
480 if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i))
481 return NULL;
482 if (!i)
483 return NULL;
484 str = g_malloc (i + 1);
485 if (read (from_parent_fd, str, i) != i)
487 g_free (str);
488 return NULL;
490 str[i] = 0;
491 return str;
494 /* --------------------------------------------------------------------------------------------- */
495 /*** public functions ****************************************************************************/
496 /* --------------------------------------------------------------------------------------------- */
498 void
499 unregister_task_running (pid_t pid, int fd)
501 destroy_task_and_return_fd (pid);
502 delete_select_channel (fd);
505 /* --------------------------------------------------------------------------------------------- */
507 void
508 unregister_task_with_pid (pid_t pid)
510 int fd = destroy_task_and_return_fd (pid);
511 if (fd != -1)
512 delete_select_channel (fd);
516 /* --------------------------------------------------------------------------------------------- */
518 * Try to make the Midnight Commander a background job
520 * Returns:
521 * 1 for parent
522 * 0 for child
523 * -1 on failure
526 do_background (file_op_context_t * ctx, char *info)
528 int comm[2]; /* control connection stream */
529 int back_comm[2]; /* back connection */
530 pid_t pid;
532 if (pipe (comm) == -1)
533 return -1;
535 if (pipe (back_comm) == -1)
536 return -1;
538 pid = fork ();
539 if (pid == -1)
541 int saved_errno = errno;
543 (void) close (comm[0]);
544 (void) close (comm[1]);
545 (void) close (back_comm[0]);
546 (void) close (back_comm[1]);
547 errno = saved_errno;
548 return -1;
551 if (pid == 0)
553 int nullfd;
555 parent_fd = comm[1];
556 from_parent_fd = back_comm[0];
558 mc_global.we_are_background = TRUE;
559 top_dlg = NULL;
561 /* Make stdin/stdout/stderr point somewhere */
562 close (STDIN_FILENO);
563 close (STDOUT_FILENO);
564 close (STDERR_FILENO);
566 nullfd = open ("/dev/null", O_RDWR);
567 if (nullfd != -1)
569 while (dup2 (nullfd, STDIN_FILENO) == -1 && errno == EINTR)
571 while (dup2 (nullfd, STDOUT_FILENO) == -1 && errno == EINTR)
573 while (dup2 (nullfd, STDERR_FILENO) == -1 && errno == EINTR)
577 return 0;
579 else
581 ctx->pid = pid;
582 register_task_running (ctx, pid, comm[0], back_comm[1], info);
583 return 1;
587 /* --------------------------------------------------------------------------------------------- */
590 parent_call (void *routine, file_op_context_t * ctx, int argc, ...)
592 int ret;
593 va_list ap;
595 va_start (ap, argc);
596 ret = parent_va_call (routine, (gpointer) ctx, argc, ap);
597 va_end (ap);
599 return ret;
602 /* --------------------------------------------------------------------------------------------- */
604 char *
605 parent_call_string (void *routine, int argc, ...)
607 va_list ap;
608 char *str;
610 va_start (ap, argc);
611 str = parent_va_call_string (routine, argc, ap);
612 va_end (ap);
614 return str;
617 /* --------------------------------------------------------------------------------------------- */
619 /* event callback */
620 gboolean
621 background_parent_call (const gchar * event_group_name, const gchar * event_name,
622 gpointer init_data, gpointer data)
624 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
626 (void) event_group_name;
627 (void) event_name;
628 (void) init_data;
630 event_data->ret.i =
631 parent_va_call (event_data->routine, event_data->ctx, event_data->argc, event_data->ap);
633 return TRUE;
636 /* --------------------------------------------------------------------------------------------- */
638 /* event callback */
639 gboolean
640 background_parent_call_string (const gchar * event_group_name, const gchar * event_name,
641 gpointer init_data, gpointer data)
643 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
645 (void) event_group_name;
646 (void) event_name;
647 (void) init_data;
649 event_data->ret.s =
650 parent_va_call_string (event_data->routine, event_data->argc, event_data->ap);
652 return TRUE;
655 /* --------------------------------------------------------------------------------------------- */