lib/widget/input_complete.c: minor refactoring and optimization.
[midnight-commander.git] / src / background.c
blob756944098537c477da442e7ef4cfe5f5eff04560
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 #include <stdlib.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/wait.h> /* waitpid() */
46 #include <unistd.h>
47 #include <fcntl.h>
49 #include "lib/global.h"
50 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
51 #include "lib/widget.h" /* message() */
52 #include "lib/event-types.h"
54 #include "filemanager/fileopctx.h" /* FileOpContext */
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 /*** file scope variables ************************************************************************/
74 /* File descriptor for talking to our parent */
75 static int parent_fd;
77 /* File descriptor for messages from our parent */
78 static int from_parent_fd;
80 struct TaskList *task_list = NULL;
82 static int background_attention (int fd, void *closure);
84 /*** file scope functions ************************************************************************/
85 /* --------------------------------------------------------------------------------------------- */
87 static void
88 register_task_running (FileOpContext * ctx, pid_t pid, int fd, int to_child, char *info)
90 TaskList *new;
92 new = g_new (TaskList, 1);
93 new->pid = pid;
94 new->info = info;
95 new->state = Task_Running;
96 new->next = task_list;
97 new->fd = fd;
98 new->to_child_fd = to_child;
99 task_list = new;
101 add_select_channel (fd, background_attention, ctx);
104 /* --------------------------------------------------------------------------------------------- */
106 static int
107 destroy_task_and_return_fd (pid_t pid)
109 TaskList *p = task_list;
110 TaskList *prev = 0;
112 while (p)
114 if (p->pid == pid)
116 if (prev)
117 prev->next = p->next;
118 else
119 task_list = p->next;
120 g_free (p->info);
121 g_free (p);
122 return p->fd;
124 prev = p;
125 p = p->next;
128 /* pid not found */
129 return -1;
132 /* --------------------------------------------------------------------------------------------- */
133 /* {{{ Parent handlers */
135 /* Parent/child protocol
137 * the child (the background) process send the following:
138 * void *routine -- routine to be invoked in the parent
139 * int nargc -- number of arguments
140 * int type -- Return argument type.
142 * If the routine is zero, then it is a way to tell the parent
143 * that the process is dying.
145 * nargc arguments in the following format:
146 * int size of the coming block
147 * size bytes with the block
149 * Now, the parent loads all those and then invokes
150 * the routine with pointers to the information passed
151 * (we just support pointers).
153 * If the return type is integer:
155 * the parent then writes an int to the child with
156 * the return value from the routine and the values
157 * of any global variable that is modified in the parent
158 * currently: do_append and recursive_result.
160 * If the return type is a string:
162 * the parent writes the resulting string length
163 * if the result string was NULL or the empty string,
164 * then the length is zero.
165 * The parent then writes the string length and frees
166 * the result string.
169 * Receive requests from background process and invoke the
170 * specified routine
173 static int
174 background_attention (int fd, void *closure)
176 FileOpContext *ctx;
177 int have_ctx;
178 union
180 int (*have_ctx0) (int);
181 int (*have_ctx1) (int, char *);
182 int (*have_ctx2) (int, char *, char *);
183 int (*have_ctx3) (int, char *, char *, char *);
184 int (*have_ctx4) (int, char *, char *, char *, char *);
186 int (*non_have_ctx0) (FileOpContext *, int);
187 int (*non_have_ctx1) (FileOpContext *, int, char *);
188 int (*non_have_ctx2) (FileOpContext *, int, char *, char *);
189 int (*non_have_ctx3) (FileOpContext *, int, char *, char *, char *);
190 int (*non_have_ctx4) (FileOpContext *, int, char *, char *, char *, char *);
192 char *(*ret_str0) ();
193 char *(*ret_str1) (char *);
194 char *(*ret_str2) (char *, char *);
195 char *(*ret_str3) (char *, char *, char *);
196 char *(*ret_str4) (char *, char *, char *, char *);
198 void *pointer;
199 } routine;
200 /* void *routine; */
201 int argc, i, result, status;
202 char *data[MAXCALLARGS];
203 ssize_t bytes, ret;
204 struct TaskList *p;
205 int to_child_fd = -1;
206 enum ReturnType type;
208 ctx = closure;
210 bytes = read (fd, &routine.pointer, sizeof (routine));
211 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
213 const char *background_process_error = _("Background process error");
215 unregister_task_running (ctx->pid, fd);
216 if (!waitpid (ctx->pid, &status, WNOHANG))
218 /* the process is still running, but it misbehaves - kill it */
219 kill (ctx->pid, SIGTERM);
220 message (D_ERROR, background_process_error, _("Unknown error in child"));
221 return 0;
224 /* 0 means happy end */
225 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
226 return 0;
228 message (D_ERROR, background_process_error, _("Child died unexpectedly"));
230 return 0;
233 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
234 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
235 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
237 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
238 return 0;
241 if (argc > MAXCALLARGS)
243 message (D_ERROR, _("Background protocol error"),
244 _("Background process sent us a request for more arguments\n"
245 "than we can handle."));
248 if (have_ctx)
250 if (read (fd, ctx, sizeof (FileOpContext)) != sizeof (FileOpContext))
252 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
253 return 0;
257 for (i = 0; i < argc; i++)
259 int size;
261 if (read (fd, &size, sizeof (size)) != sizeof (size))
263 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
264 return 0;
266 data[i] = g_malloc (size + 1);
267 if (read (fd, data[i], size) != size)
269 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
270 return 0;
272 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
275 /* Find child task info by descriptor */
276 /* Find before call, because process can destroy self after */
277 for (p = task_list; p; p = p->next)
279 if (p->fd == fd)
280 break;
283 if (p)
284 to_child_fd = p->to_child_fd;
286 if (to_child_fd == -1)
287 message (D_ERROR, _("Background process error"), _("Unknown error in child"));
289 /* Handle the call */
290 if (type == Return_Integer)
292 if (!have_ctx)
293 switch (argc)
295 case 0:
296 result = routine.have_ctx0 (Background);
297 break;
298 case 1:
299 result = routine.have_ctx1 (Background, data[0]);
300 break;
301 case 2:
302 result = routine.have_ctx2 (Background, data[0], data[1]);
303 break;
304 case 3:
305 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
306 break;
307 case 4:
308 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
309 break;
311 else
312 switch (argc)
314 case 0:
315 result = routine.non_have_ctx0 (ctx, Background);
316 break;
317 case 1:
318 result = routine.non_have_ctx1 (ctx, Background, data[0]);
319 break;
320 case 2:
321 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
322 break;
323 case 3:
324 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
325 break;
326 case 4:
327 result =
328 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
329 break;
332 /* Send the result code and the value for shared variables */
333 ret = write (to_child_fd, &result, sizeof (int));
334 if (have_ctx && to_child_fd != -1)
335 ret = write (to_child_fd, ctx, sizeof (FileOpContext));
337 else if (type == Return_String)
339 int len;
340 char *resstr = NULL;
342 /* FIXME: string routines should also use the Foreground/Background
343 * parameter. Currently, this is not used here
345 switch (argc)
347 case 0:
348 resstr = routine.ret_str0 ();
349 break;
350 case 1:
351 resstr = routine.ret_str1 (data[0]);
352 break;
353 case 2:
354 resstr = routine.ret_str2 (data[0], data[1]);
355 break;
356 case 3:
357 resstr = routine.ret_str3 (data[0], data[1], data[2]);
358 break;
359 case 4:
360 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
361 break;
362 default:
363 g_assert_not_reached ();
365 if (resstr)
367 len = strlen (resstr);
368 ret = write (to_child_fd, &len, sizeof (len));
369 if (len != 0)
370 ret = write (to_child_fd, resstr, len);
371 g_free (resstr);
373 else
375 len = 0;
376 ret = write (to_child_fd, &len, sizeof (len));
379 for (i = 0; i < argc; i++)
380 g_free (data[i]);
382 repaint_screen ();
383 (void) ret;
384 return 0;
388 /* --------------------------------------------------------------------------------------------- */
389 /* }}} */
391 /* {{{ client RPC routines */
393 /* Sends the header for a call to a routine in the parent process. If the file
394 * operation context is not NULL, then it requests that the first parameter of
395 * the call be a file operation context.
398 static void
399 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext * ctx)
401 int have_ctx;
402 ssize_t ret;
404 have_ctx = (ctx != NULL);
406 ret = write (parent_fd, &routine, sizeof (routine));
407 ret = write (parent_fd, &argc, sizeof (int));
408 ret = write (parent_fd, &type, sizeof (type));
409 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
411 if (have_ctx)
412 ret = write (parent_fd, ctx, sizeof (FileOpContext));
413 (void) ret;
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 (void) ret;
442 return i;
445 /* --------------------------------------------------------------------------------------------- */
447 static char *
448 parent_va_call_string (void *routine, int argc, va_list ap)
450 char *str;
451 int i;
453 parent_call_header (routine, argc, Return_String, NULL);
454 for (i = 0; i < argc; i++)
456 int len;
457 void *value;
459 len = va_arg (ap, int);
460 value = va_arg (ap, void *);
461 if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
462 (write (parent_fd, value, len) != len))
464 return NULL;
468 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
469 return NULL;
470 if (!i)
471 return NULL;
472 str = g_malloc (i + 1);
473 if (read (from_parent_fd, str, i) != i)
475 g_free (str);
476 return NULL;
478 str[i] = 0;
479 return str;
482 /* --------------------------------------------------------------------------------------------- */
483 /*** public functions ****************************************************************************/
484 /* --------------------------------------------------------------------------------------------- */
486 void
487 unregister_task_running (pid_t pid, int fd)
489 destroy_task_and_return_fd (pid);
490 delete_select_channel (fd);
493 /* --------------------------------------------------------------------------------------------- */
495 void
496 unregister_task_with_pid (pid_t pid)
498 int fd = destroy_task_and_return_fd (pid);
499 if (fd != -1)
500 delete_select_channel (fd);
504 /* --------------------------------------------------------------------------------------------- */
506 * Try to make the Midnight Commander a background job
508 * Returns:
509 * 1 for parent
510 * 0 for child
511 * -1 on failure
514 do_background (struct FileOpContext *ctx, char *info)
516 int comm[2]; /* control connection stream */
517 int back_comm[2]; /* back connection */
518 pid_t pid;
520 if (pipe (comm) == -1)
521 return -1;
523 if (pipe (back_comm) == -1)
524 return -1;
526 pid = fork ();
527 if (pid == -1)
529 int saved_errno = errno;
531 (void) close (comm[0]);
532 (void) close (comm[1]);
533 (void) close (back_comm[0]);
534 (void) close (back_comm[1]);
535 errno = saved_errno;
536 return -1;
539 if (pid == 0)
541 int nullfd;
543 parent_fd = comm[1];
544 from_parent_fd = back_comm[0];
546 mc_global.we_are_background = TRUE;
547 top_dlg = NULL;
549 /* Make stdin/stdout/stderr point somewhere */
550 close (0);
551 close (1);
552 close (2);
554 nullfd = open ("/dev/null", O_RDWR);
555 if (nullfd != -1)
557 while (dup2 (nullfd, 0) == -1 && errno == EINTR)
559 while (dup2 (nullfd, 1) == -1 && errno == EINTR)
561 while (dup2 (nullfd, 2) == -1 && errno == EINTR)
565 return 0;
567 else
569 ctx->pid = pid;
570 register_task_running (ctx, pid, comm[0], back_comm[1], info);
571 return 1;
575 /* --------------------------------------------------------------------------------------------- */
578 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
580 int ret;
581 va_list ap;
583 va_start (ap, argc);
584 ret = parent_va_call (routine, (gpointer) ctx, argc, ap);
585 va_end (ap);
587 return ret;
590 /* --------------------------------------------------------------------------------------------- */
592 char *
593 parent_call_string (void *routine, int argc, ...)
595 va_list ap;
596 char *str;
598 va_start (ap, argc);
599 str = parent_va_call_string (routine, argc, ap);
600 va_end (ap);
602 return str;
605 /* --------------------------------------------------------------------------------------------- */
607 /* event callback */
608 gboolean
609 background_parent_call (const gchar * event_group_name, const gchar * event_name,
610 gpointer init_data, gpointer data)
612 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
614 (void) event_group_name;
615 (void) event_name;
616 (void) init_data;
618 event_data->ret.i =
619 parent_va_call (event_data->routine, event_data->ctx, event_data->argc, event_data->ap);
621 return TRUE;
624 /* --------------------------------------------------------------------------------------------- */
626 /* event callback */
627 gboolean
628 background_parent_call_string (const gchar * event_group_name, const gchar * event_name,
629 gpointer init_data, gpointer data)
631 ev_background_parent_call_t *event_data = (ev_background_parent_call_t *) data;
633 (void) event_group_name;
634 (void) event_name;
635 (void) init_data;
637 event_data->ret.s =
638 parent_va_call_string (event_data->routine, event_data->argc, event_data->ap);
640 return TRUE;
643 /* --------------------------------------------------------------------------------------------- */