(panel_callback) [MSG_FOCUS]: remove self-draw here.
[midnight-commander.git] / src / background.c
blob72a0eda73e2dd02413f3ed539bd90a12bcce782b
1 /* {{{ Copyright */
3 /* Background support.
5 Copyright (C) 1996-2024
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"
52 #include "lib/util.h" /* my_fork() */
54 #include "filemanager/fileopctx.h" /* file_op_context_t */
56 #include "background.h"
58 /*** global variables ****************************************************************************/
60 #define MAXCALLARGS 4 /* Number of arguments supported */
62 /*** file scope macro definitions ****************************************************************/
64 /*** file scope type declarations ****************************************************************/
66 enum ReturnType
68 Return_String,
69 Return_Integer
72 /*** forward declarations (file scope functions) *************************************************/
74 static int background_attention (int fd, void *closure);
76 /*** file scope variables ************************************************************************/
78 /* File descriptor for talking to our parent */
79 static int parent_fd;
81 /* File descriptor for messages from our parent */
82 static int from_parent_fd;
84 TaskList *task_list = NULL;
86 /* --------------------------------------------------------------------------------------------- */
87 /*** file scope functions ************************************************************************/
88 /* --------------------------------------------------------------------------------------------- */
90 static void
91 register_task_running (file_op_context_t *ctx, pid_t pid, int fd, int to_child, char *info)
93 TaskList *new;
95 new = g_new (TaskList, 1);
96 new->pid = pid;
97 new->info = info;
98 new->state = Task_Running;
99 new->next = task_list;
100 new->fd = fd;
101 new->to_child_fd = to_child;
102 task_list = new;
104 add_select_channel (fd, background_attention, ctx);
107 /* --------------------------------------------------------------------------------------------- */
109 static int
110 destroy_task (pid_t pid)
112 TaskList *p = task_list;
113 TaskList *prev = NULL;
115 while (p != NULL)
117 if (p->pid == pid)
119 int fd = p->fd;
121 if (prev != NULL)
122 prev->next = p->next;
123 else
124 task_list = p->next;
125 g_free (p->info);
126 g_free (p);
127 return fd;
129 prev = p;
130 p = p->next;
133 /* pid not found */
134 return (-1);
137 /* --------------------------------------------------------------------------------------------- */
138 /* {{{ Parent handlers */
140 /* Parent/child protocol
142 * the child (the background) process send the following:
143 * void *routine -- routine to be invoked in the parent
144 * int nargc -- number of arguments
145 * int type -- Return argument type.
147 * If the routine is zero, then it is a way to tell the parent
148 * that the process is dying.
150 * nargc arguments in the following format:
151 * int size of the coming block
152 * size bytes with the block
154 * Now, the parent loads all those and then invokes
155 * the routine with pointers to the information passed
156 * (we just support pointers).
158 * If the return type is integer:
160 * the parent then writes an int to the child with
161 * the return value from the routine and the values
162 * of any global variable that is modified in the parent
163 * currently: do_append and recursive_result.
165 * If the return type is a string:
167 * the parent writes the resulting string length
168 * if the result string was NULL or the empty string,
169 * then the length is zero.
170 * The parent then writes the string length and frees
171 * the result string.
174 * Receive requests from background process and invoke the
175 * specified routine
178 static int
179 reading_failed (int i, char **data)
181 while (i >= 0)
182 g_free (data[i--]);
183 message (D_ERROR, _("Background protocol error"), "%s", _("Reading failed"));
184 return 0;
187 /* --------------------------------------------------------------------------------------------- */
189 static int
190 background_attention (int fd, void *closure)
192 file_op_context_t *ctx;
193 int have_ctx;
194 union
196 int (*have_ctx0) (int);
197 int (*have_ctx1) (int, char *);
198 int (*have_ctx2) (int, char *, char *);
199 int (*have_ctx3) (int, char *, char *, char *);
200 int (*have_ctx4) (int, char *, char *, char *, char *);
202 int (*non_have_ctx0) (file_op_context_t *, int);
203 int (*non_have_ctx1) (file_op_context_t *, int, char *);
204 int (*non_have_ctx2) (file_op_context_t *, int, char *, char *);
205 int (*non_have_ctx3) (file_op_context_t *, int, char *, char *, char *);
206 int (*non_have_ctx4) (file_op_context_t *, int, char *, char *, char *, char *);
208 char *(*ret_str0) (void);
209 char *(*ret_str1) (char *);
210 char *(*ret_str2) (char *, char *);
211 char *(*ret_str3) (char *, char *, char *);
212 char *(*ret_str4) (char *, char *, char *, char *);
214 void *pointer;
215 } routine;
216 /* void *routine; */
217 int argc, i, status;
218 char *data[MAXCALLARGS];
219 ssize_t bytes, ret;
220 TaskList *p;
221 int to_child_fd = -1;
222 enum ReturnType type;
223 const char *background_process_error = _("Background process error");
225 ctx = closure;
227 bytes = read (fd, &routine.pointer, sizeof (routine));
228 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
230 unregister_task_running (ctx->pid, fd);
232 if (waitpid (ctx->pid, &status, WNOHANG) == 0)
234 /* the process is still running, but it misbehaves - kill it */
235 kill (ctx->pid, SIGTERM);
236 message (D_ERROR, background_process_error, "%s", _("Unknown error in child"));
237 return 0;
240 /* 0 means happy end */
241 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
242 return 0;
244 message (D_ERROR, background_process_error, "%s", _("Child died unexpectedly"));
246 return 0;
249 if (read (fd, &argc, sizeof (argc)) != sizeof (argc) ||
250 read (fd, &type, sizeof (type)) != sizeof (type) ||
251 read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx))
252 return reading_failed (-1, data);
254 if (argc > MAXCALLARGS)
255 message (D_ERROR, _("Background protocol error"), "%s",
256 _("Background process sent us a request for more arguments\n"
257 "than we can handle."));
259 if (have_ctx != 0 && read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
260 return reading_failed (-1, data);
262 for (i = 0; i < argc; i++)
264 int size;
266 if (read (fd, &size, sizeof (size)) != sizeof (size))
267 return reading_failed (i - 1, data);
269 data[i] = g_malloc (size + 1);
271 if (read (fd, data[i], size) != size)
272 return reading_failed (i, data);
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 != NULL; p = p->next)
280 if (p->fd == fd)
281 break;
283 if (p != NULL)
284 to_child_fd = p->to_child_fd;
286 if (to_child_fd == -1)
287 message (D_ERROR, background_process_error, "%s", _("Unknown error in child"));
288 else if (type == Return_Integer)
290 /* Handle the call */
292 int result = 0;
294 if (have_ctx == 0)
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;
312 default:
313 break;
315 else
316 switch (argc)
318 case 0:
319 result = routine.non_have_ctx0 (ctx, Background);
320 break;
321 case 1:
322 result = routine.non_have_ctx1 (ctx, Background, data[0]);
323 break;
324 case 2:
325 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
326 break;
327 case 3:
328 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
329 break;
330 case 4:
331 result =
332 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
333 break;
334 default:
335 break;
338 /* Send the result code and the value for shared variables */
339 ret = write (to_child_fd, &result, sizeof (result));
340 if (have_ctx != 0 && to_child_fd != -1)
341 ret = write (to_child_fd, ctx, sizeof (*ctx));
343 else if (type == Return_String)
345 int len;
346 char *resstr = NULL;
348 /* FIXME: string routines should also use the Foreground/Background
349 * parameter. Currently, this is not used here
351 switch (argc)
353 case 0:
354 resstr = routine.ret_str0 ();
355 break;
356 case 1:
357 resstr = routine.ret_str1 (data[0]);
358 break;
359 case 2:
360 resstr = routine.ret_str2 (data[0], data[1]);
361 break;
362 case 3:
363 resstr = routine.ret_str3 (data[0], data[1], data[2]);
364 break;
365 case 4:
366 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
367 break;
368 default:
369 g_assert_not_reached ();
372 if (resstr != NULL)
374 len = strlen (resstr);
375 ret = write (to_child_fd, &len, sizeof (len));
376 if (len != 0)
377 ret = write (to_child_fd, resstr, len);
378 g_free (resstr);
380 else
382 len = 0;
383 ret = write (to_child_fd, &len, sizeof (len));
387 for (i = 0; i < argc; i++)
388 g_free (data[i]);
390 repaint_screen ();
392 (void) ret;
394 return 0;
397 /* --------------------------------------------------------------------------------------------- */
398 /* }}} */
400 /* {{{ client RPC routines */
402 /* Sends the header for a call to a routine in the parent process. If the file
403 * operation context is not NULL, then it requests that the first parameter of
404 * the call be a file operation context.
407 static void
408 parent_call_header (void *routine, int argc, enum ReturnType type, file_op_context_t *ctx)
410 int have_ctx;
411 ssize_t ret;
413 have_ctx = ctx != NULL ? 1 : 0;
415 ret = write (parent_fd, &routine, sizeof (routine));
416 ret = write (parent_fd, &argc, sizeof (argc));
417 ret = write (parent_fd, &type, sizeof (type));
418 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
419 if (have_ctx != 0)
420 ret = write (parent_fd, ctx, sizeof (*ctx));
422 (void) ret;
425 /* --------------------------------------------------------------------------------------------- */
427 static int
428 parent_va_call (void *routine, gpointer data, int argc, va_list ap)
430 int i;
431 ssize_t ret;
432 file_op_context_t *ctx = (file_op_context_t *) data;
434 parent_call_header (routine, argc, Return_Integer, ctx);
435 for (i = 0; i < argc; i++)
437 int len;
438 void *value;
440 len = va_arg (ap, int);
441 value = va_arg (ap, void *);
442 ret = write (parent_fd, &len, sizeof (len));
443 ret = write (parent_fd, value, len);
446 ret = read (from_parent_fd, &i, sizeof (i));
447 if (ctx != NULL)
448 ret = read (from_parent_fd, ctx, sizeof (*ctx));
450 (void) ret;
452 return i;
455 /* --------------------------------------------------------------------------------------------- */
457 static char *
458 parent_va_call_string (void *routine, int argc, va_list ap)
460 char *str;
461 int i;
463 parent_call_header (routine, argc, Return_String, NULL);
464 for (i = 0; i < argc; i++)
466 int len;
467 void *value;
469 len = va_arg (ap, int);
470 value = va_arg (ap, void *);
471 if (write (parent_fd, &len, sizeof (len)) != sizeof (len) ||
472 write (parent_fd, value, len) != len)
473 return NULL;
476 if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i) || i == 0)
477 return NULL;
479 str = g_malloc (i + 1);
480 if (read (from_parent_fd, str, i) != i)
482 g_free (str);
483 return NULL;
485 str[i] = '\0';
486 return str;
489 /* --------------------------------------------------------------------------------------------- */
490 /*** public functions ****************************************************************************/
491 /* --------------------------------------------------------------------------------------------- */
493 void
494 unregister_task_running (pid_t pid, int fd)
496 destroy_task (pid);
497 delete_select_channel (fd);
500 /* --------------------------------------------------------------------------------------------- */
502 void
503 unregister_task_with_pid (pid_t pid)
505 int fd;
507 fd = destroy_task (pid);
508 if (fd != -1)
509 delete_select_channel (fd);
512 /* --------------------------------------------------------------------------------------------- */
514 * Try to make the Midnight Commander a background job
516 * Returns:
517 * 1 for parent
518 * 0 for child
519 * -1 on failure
522 do_background (file_op_context_t *ctx, char *info)
524 int comm[2]; /* control connection stream */
525 int back_comm[2]; /* back connection */
526 pid_t pid;
528 if (pipe (comm) == -1)
529 return (-1);
531 if (pipe (back_comm) == -1)
533 (void) close (comm[0]);
534 (void) close (comm[1]);
536 return (-1);
539 pid = my_fork ();
540 if (pid == -1)
542 int saved_errno = errno;
544 (void) close (comm[0]);
545 (void) close (comm[1]);
546 (void) close (back_comm[0]);
547 (void) close (back_comm[1]);
548 errno = saved_errno;
550 return (-1);
553 if (pid == 0)
555 int nullfd;
557 (void) close (comm[0]);
558 parent_fd = comm[1];
560 (void) close (back_comm[1]);
561 from_parent_fd = back_comm[0];
563 mc_global.we_are_background = TRUE;
564 top_dlg = NULL;
566 /* Make stdin/stdout/stderr point somewhere */
567 close (STDIN_FILENO);
568 close (STDOUT_FILENO);
569 close (STDERR_FILENO);
571 nullfd = open ("/dev/null", O_RDWR);
572 if (nullfd != -1)
574 while (dup2 (nullfd, STDIN_FILENO) == -1 && errno == EINTR)
576 while (dup2 (nullfd, STDOUT_FILENO) == -1 && errno == EINTR)
578 while (dup2 (nullfd, STDERR_FILENO) == -1 && errno == EINTR)
580 close (nullfd);
583 return 0;
585 else
587 (void) close (comm[1]);
588 (void) close (back_comm[0]);
589 ctx->pid = pid;
590 register_task_running (ctx, pid, comm[0], back_comm[1], info);
591 return 1;
595 /* --------------------------------------------------------------------------------------------- */
598 parent_call (void *routine, file_op_context_t *ctx, int argc, ...)
600 int ret;
601 va_list ap;
603 va_start (ap, argc);
604 ret = parent_va_call (routine, (gpointer) ctx, argc, ap);
605 va_end (ap);
607 return ret;
610 /* --------------------------------------------------------------------------------------------- */
612 char *
613 parent_call_string (void *routine, int argc, ...)
615 va_list ap;
616 char *str;
618 va_start (ap, argc);
619 str = parent_va_call_string (routine, argc, ap);
620 va_end (ap);
622 return str;
625 /* --------------------------------------------------------------------------------------------- */
627 /* event callback */
628 gboolean
629 background_parent_call (const gchar *event_group_name, const gchar *event_name,
630 gpointer init_data, gpointer data)
632 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
634 (void) event_group_name;
635 (void) event_name;
636 (void) init_data;
638 event_data->ret.i =
639 parent_va_call (event_data->routine, event_data->ctx, event_data->argc, event_data->ap);
641 return TRUE;
644 /* --------------------------------------------------------------------------------------------- */
646 /* event callback */
647 gboolean
648 background_parent_call_string (const gchar *event_group_name, const gchar *event_name,
649 gpointer init_data, gpointer data)
651 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
653 (void) event_group_name;
654 (void) event_name;
655 (void) init_data;
657 event_data->ret.s =
658 parent_va_call_string (event_data->routine, event_data->argc, event_data->ap);
660 return TRUE;
663 /* --------------------------------------------------------------------------------------------- */