(message): Use "%s" to fix incorrect translation with printf pattern(s)
[midnight-commander.git] / src / background.c
blob88d48b75aa82142724632ec0efa02e7f12ed1142
1 /* {{{ Copyright */
3 /* Background support.
5 Copyright (C) 1996-2017
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;
217 const char *background_process_error = _("Background process error");
219 ctx = closure;
221 bytes = read (fd, &routine.pointer, sizeof (routine));
222 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
224 unregister_task_running (ctx->pid, fd);
225 if (!waitpid (ctx->pid, &status, WNOHANG))
227 /* the process is still running, but it misbehaves - kill it */
228 kill (ctx->pid, SIGTERM);
229 message (D_ERROR, background_process_error, "%s", _("Unknown error in child"));
230 return 0;
233 /* 0 means happy end */
234 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
235 return 0;
237 message (D_ERROR, background_process_error, "%s", _("Child died unexpectedly"));
239 return 0;
242 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
243 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
244 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
246 return reading_failed (-1, data);
249 if (argc > MAXCALLARGS)
251 message (D_ERROR, _("Background protocol error"), "%s",
252 _("Background process sent us a request for more arguments\n"
253 "than we can handle."));
256 if (have_ctx)
258 if (read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
260 return reading_failed (-1, data);
264 for (i = 0; i < argc; i++)
266 int size;
268 if (read (fd, &size, sizeof (size)) != sizeof (size))
270 return reading_failed (i - 1, data);
272 data[i] = g_malloc (size + 1);
273 if (read (fd, data[i], size) != size)
275 return reading_failed (i, data);
277 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
280 /* Find child task info by descriptor */
281 /* Find before call, because process can destroy self after */
282 for (p = task_list; p; p = p->next)
284 if (p->fd == fd)
285 break;
288 if (p)
289 to_child_fd = p->to_child_fd;
291 if (to_child_fd == -1)
292 message (D_ERROR, background_process_error, "%s", _("Unknown error in child"));
294 /* Handle the call */
295 if (type == Return_Integer)
297 int result = 0;
299 if (!have_ctx)
300 switch (argc)
302 case 0:
303 result = routine.have_ctx0 (Background);
304 break;
305 case 1:
306 result = routine.have_ctx1 (Background, data[0]);
307 break;
308 case 2:
309 result = routine.have_ctx2 (Background, data[0], data[1]);
310 break;
311 case 3:
312 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
313 break;
314 case 4:
315 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
316 break;
317 default:
318 break;
320 else
321 switch (argc)
323 case 0:
324 result = routine.non_have_ctx0 (ctx, Background);
325 break;
326 case 1:
327 result = routine.non_have_ctx1 (ctx, Background, data[0]);
328 break;
329 case 2:
330 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
331 break;
332 case 3:
333 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
334 break;
335 case 4:
336 result =
337 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
338 break;
339 default:
340 break;
343 /* Send the result code and the value for shared variables */
344 ret = write (to_child_fd, &result, sizeof (result));
345 if (have_ctx && to_child_fd != -1)
346 ret = write (to_child_fd, ctx, sizeof (*ctx));
348 else if (type == Return_String)
350 int len;
351 char *resstr = NULL;
353 /* FIXME: string routines should also use the Foreground/Background
354 * parameter. Currently, this is not used here
356 switch (argc)
358 case 0:
359 resstr = routine.ret_str0 ();
360 break;
361 case 1:
362 resstr = routine.ret_str1 (data[0]);
363 break;
364 case 2:
365 resstr = routine.ret_str2 (data[0], data[1]);
366 break;
367 case 3:
368 resstr = routine.ret_str3 (data[0], data[1], data[2]);
369 break;
370 case 4:
371 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
372 break;
373 default:
374 g_assert_not_reached ();
376 if (resstr)
378 len = strlen (resstr);
379 ret = write (to_child_fd, &len, sizeof (len));
380 if (len != 0)
381 ret = write (to_child_fd, resstr, len);
382 g_free (resstr);
384 else
386 len = 0;
387 ret = write (to_child_fd, &len, sizeof (len));
390 for (i = 0; i < argc; i++)
391 g_free (data[i]);
393 repaint_screen ();
394 (void) ret;
395 return 0;
399 /* --------------------------------------------------------------------------------------------- */
400 /* }}} */
402 /* {{{ client RPC routines */
404 /* Sends the header for a call to a routine in the parent process. If the file
405 * operation context is not NULL, then it requests that the first parameter of
406 * the call be a file operation context.
409 static void
410 parent_call_header (void *routine, int argc, enum ReturnType type, file_op_context_t * ctx)
412 int have_ctx;
413 ssize_t ret;
415 have_ctx = (ctx != NULL);
417 ret = write (parent_fd, &routine, sizeof (routine));
418 ret = write (parent_fd, &argc, sizeof (argc));
419 ret = write (parent_fd, &type, sizeof (type));
420 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
422 if (have_ctx)
423 ret = write (parent_fd, ctx, sizeof (*ctx));
424 (void) ret;
427 /* --------------------------------------------------------------------------------------------- */
429 static int
430 parent_va_call (void *routine, gpointer data, int argc, va_list ap)
432 int i;
433 ssize_t ret;
434 file_op_context_t *ctx = (file_op_context_t *) data;
436 parent_call_header (routine, argc, Return_Integer, ctx);
437 for (i = 0; i < argc; i++)
439 int len;
440 void *value;
442 len = va_arg (ap, int);
443 value = va_arg (ap, void *);
444 ret = write (parent_fd, &len, sizeof (len));
445 ret = write (parent_fd, value, len);
448 ret = read (from_parent_fd, &i, sizeof (i));
449 if (ctx)
450 ret = read (from_parent_fd, ctx, sizeof (*ctx));
452 (void) ret;
453 return i;
456 /* --------------------------------------------------------------------------------------------- */
458 static char *
459 parent_va_call_string (void *routine, int argc, va_list ap)
461 char *str;
462 int i;
464 parent_call_header (routine, argc, Return_String, NULL);
465 for (i = 0; i < argc; i++)
467 int len;
468 void *value;
470 len = va_arg (ap, int);
471 value = va_arg (ap, void *);
472 if ((write (parent_fd, &len, sizeof (len)) != sizeof (len)) ||
473 (write (parent_fd, value, len) != len))
475 return NULL;
479 if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i))
480 return NULL;
481 if (!i)
482 return NULL;
483 str = g_malloc (i + 1);
484 if (read (from_parent_fd, str, i) != i)
486 g_free (str);
487 return NULL;
489 str[i] = 0;
490 return str;
493 /* --------------------------------------------------------------------------------------------- */
494 /*** public functions ****************************************************************************/
495 /* --------------------------------------------------------------------------------------------- */
497 void
498 unregister_task_running (pid_t pid, int fd)
500 destroy_task_and_return_fd (pid);
501 delete_select_channel (fd);
504 /* --------------------------------------------------------------------------------------------- */
506 void
507 unregister_task_with_pid (pid_t pid)
509 int fd = destroy_task_and_return_fd (pid);
510 if (fd != -1)
511 delete_select_channel (fd);
515 /* --------------------------------------------------------------------------------------------- */
517 * Try to make the Midnight Commander a background job
519 * Returns:
520 * 1 for parent
521 * 0 for child
522 * -1 on failure
525 do_background (file_op_context_t * ctx, char *info)
527 int comm[2]; /* control connection stream */
528 int back_comm[2]; /* back connection */
529 pid_t pid;
531 if (pipe (comm) == -1)
532 return -1;
534 if (pipe (back_comm) == -1)
535 return -1;
537 pid = fork ();
538 if (pid == -1)
540 int saved_errno = errno;
542 (void) close (comm[0]);
543 (void) close (comm[1]);
544 (void) close (back_comm[0]);
545 (void) close (back_comm[1]);
546 errno = saved_errno;
547 return -1;
550 if (pid == 0)
552 int nullfd;
554 parent_fd = comm[1];
555 from_parent_fd = back_comm[0];
557 mc_global.we_are_background = TRUE;
558 top_dlg = NULL;
560 /* Make stdin/stdout/stderr point somewhere */
561 close (STDIN_FILENO);
562 close (STDOUT_FILENO);
563 close (STDERR_FILENO);
565 nullfd = open ("/dev/null", O_RDWR);
566 if (nullfd != -1)
568 while (dup2 (nullfd, STDIN_FILENO) == -1 && errno == EINTR)
570 while (dup2 (nullfd, STDOUT_FILENO) == -1 && errno == EINTR)
572 while (dup2 (nullfd, STDERR_FILENO) == -1 && errno == EINTR)
576 return 0;
578 else
580 ctx->pid = pid;
581 register_task_running (ctx, pid, comm[0], back_comm[1], info);
582 return 1;
586 /* --------------------------------------------------------------------------------------------- */
589 parent_call (void *routine, file_op_context_t * ctx, int argc, ...)
591 int ret;
592 va_list ap;
594 va_start (ap, argc);
595 ret = parent_va_call (routine, (gpointer) ctx, argc, ap);
596 va_end (ap);
598 return ret;
601 /* --------------------------------------------------------------------------------------------- */
603 char *
604 parent_call_string (void *routine, int argc, ...)
606 va_list ap;
607 char *str;
609 va_start (ap, argc);
610 str = parent_va_call_string (routine, argc, ap);
611 va_end (ap);
613 return str;
616 /* --------------------------------------------------------------------------------------------- */
618 /* event callback */
619 gboolean
620 background_parent_call (const gchar * event_group_name, const gchar * event_name,
621 gpointer init_data, gpointer data)
623 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
625 (void) event_group_name;
626 (void) event_name;
627 (void) init_data;
629 event_data->ret.i =
630 parent_va_call (event_data->routine, event_data->ctx, event_data->argc, event_data->ap);
632 return TRUE;
635 /* --------------------------------------------------------------------------------------------- */
637 /* event callback */
638 gboolean
639 background_parent_call_string (const gchar * event_group_name, const gchar * event_name,
640 gpointer init_data, gpointer data)
642 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
644 (void) event_group_name;
645 (void) event_name;
646 (void) init_data;
648 event_data->ret.s =
649 parent_va_call_string (event_data->routine, event_data->argc, event_data->ap);
651 return TRUE;
654 /* --------------------------------------------------------------------------------------------- */