Init/deinit clean up
[pantumic.git] / src / background.c
blob66fb1bc71aaa7c23bd6bcd8820ddfd31f7073b9b
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() */
50 #include "layout.h" /* repaint_screen() */
51 #include "fileopctx.h" /* FileOpContext */
52 #include "background.h"
54 /*** global variables ****************************************************************************/
56 #define MAXCALLARGS 4 /* Number of arguments supported */
58 /* If true, this is a background process */
59 int we_are_background = 0;
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 struct 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 (FileOpContext * 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 background_attention (int fd, void *closure)
175 FileOpContext *ctx;
176 int have_ctx;
177 union
179 int (*have_ctx0) (int);
180 int (*have_ctx1) (int, char *);
181 int (*have_ctx2) (int, char *, char *);
182 int (*have_ctx3) (int, char *, char *, char *);
183 int (*have_ctx4) (int, char *, char *, char *, char *);
185 int (*non_have_ctx0) (FileOpContext *, int);
186 int (*non_have_ctx1) (FileOpContext *, int, char *);
187 int (*non_have_ctx2) (FileOpContext *, int, char *, char *);
188 int (*non_have_ctx3) (FileOpContext *, int, char *, char *, char *);
189 int (*non_have_ctx4) (FileOpContext *, int, char *, char *, char *, char *);
191 char *(*ret_str0) ();
192 char *(*ret_str1) (char *);
193 char *(*ret_str2) (char *, char *);
194 char *(*ret_str3) (char *, char *, char *);
195 char *(*ret_str4) (char *, char *, char *, char *);
197 void *pointer;
198 } routine;
199 /* void *routine; */
200 int argc, i, result, status;
201 char *data[MAXCALLARGS];
202 ssize_t bytes, ret;
203 struct TaskList *p;
204 int to_child_fd = -1;
205 enum ReturnType type;
207 ctx = closure;
209 bytes = read (fd, &routine.pointer, sizeof (routine));
210 if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
212 const char *background_process_error = _("Background process error");
214 unregister_task_running (ctx->pid, fd);
215 if (!waitpid (ctx->pid, &status, WNOHANG))
217 /* the process is still running, but it misbehaves - kill it */
218 kill (ctx->pid, SIGTERM);
219 message (D_ERROR, background_process_error, _("Unknown error in child"));
220 return 0;
223 /* 0 means happy end */
224 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
225 return 0;
227 message (D_ERROR, background_process_error, _("Child died unexpectedly"));
229 return 0;
232 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
233 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
234 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
236 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
237 return 0;
240 if (argc > MAXCALLARGS)
242 message (D_ERROR, _("Background protocol error"),
243 _("Background process sent us a request for more arguments\n"
244 "than we can handle."));
247 if (have_ctx)
249 if (read (fd, ctx, sizeof (FileOpContext)) != sizeof (FileOpContext))
251 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
252 return 0;
256 for (i = 0; i < argc; i++)
258 int size;
260 if (read (fd, &size, sizeof (size)) != sizeof (size))
262 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
263 return 0;
265 data[i] = g_malloc (size + 1);
266 if (read (fd, data[i], size) != size)
268 message (D_ERROR, _("Background protocol error"), _("Reading failed"));
269 return 0;
271 data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
274 /* Find child task info by descriptor */
275 /* Find before call, because process can destroy self after */
276 for (p = task_list; p; p = p->next)
278 if (p->fd == fd)
279 break;
282 if (p)
283 to_child_fd = p->to_child_fd;
285 if (to_child_fd == -1)
286 message (D_ERROR, _("Background process error"), _("Unknown error in child"));
288 /* Handle the call */
289 if (type == Return_Integer)
291 if (!have_ctx)
292 switch (argc)
294 case 0:
295 result = routine.have_ctx0 (Background);
296 break;
297 case 1:
298 result = routine.have_ctx1 (Background, data[0]);
299 break;
300 case 2:
301 result = routine.have_ctx2 (Background, data[0], data[1]);
302 break;
303 case 3:
304 result = routine.have_ctx3 (Background, data[0], data[1], data[2]);
305 break;
306 case 4:
307 result = routine.have_ctx4 (Background, data[0], data[1], data[2], data[3]);
308 break;
310 else
311 switch (argc)
313 case 0:
314 result = routine.non_have_ctx0 (ctx, Background);
315 break;
316 case 1:
317 result = routine.non_have_ctx1 (ctx, Background, data[0]);
318 break;
319 case 2:
320 result = routine.non_have_ctx2 (ctx, Background, data[0], data[1]);
321 break;
322 case 3:
323 result = routine.non_have_ctx3 (ctx, Background, data[0], data[1], data[2]);
324 break;
325 case 4:
326 result =
327 routine.non_have_ctx4 (ctx, Background, data[0], data[1], data[2], data[3]);
328 break;
331 /* Send the result code and the value for shared variables */
332 ret = write (to_child_fd, &result, sizeof (int));
333 if (have_ctx && to_child_fd != -1)
334 ret = write (to_child_fd, ctx, sizeof (FileOpContext));
336 else if (type == Return_String)
338 int len;
339 char *resstr = NULL;
341 /* FIXME: string routines should also use the Foreground/Background
342 * parameter. Currently, this is not used here
344 switch (argc)
346 case 0:
347 resstr = routine.ret_str0 ();
348 break;
349 case 1:
350 resstr = routine.ret_str1 (data[0]);
351 break;
352 case 2:
353 resstr = routine.ret_str2 (data[0], data[1]);
354 break;
355 case 3:
356 resstr = routine.ret_str3 (data[0], data[1], data[2]);
357 break;
358 case 4:
359 resstr = routine.ret_str4 (data[0], data[1], data[2], data[3]);
360 break;
361 default:
362 g_assert_not_reached ();
364 if (resstr)
366 len = strlen (resstr);
367 ret = write (to_child_fd, &len, sizeof (len));
368 if (len != 0)
369 ret = write (to_child_fd, resstr, len);
370 g_free (resstr);
372 else
374 len = 0;
375 ret = write (to_child_fd, &len, sizeof (len));
378 for (i = 0; i < argc; i++)
379 g_free (data[i]);
381 repaint_screen ();
382 return 0;
386 /* --------------------------------------------------------------------------------------------- */
387 /* }}} */
389 /* {{{ client RPC routines */
391 /* Sends the header for a call to a routine in the parent process. If the file
392 * operation context is not NULL, then it requests that the first parameter of
393 * the call be a file operation context.
396 static void
397 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext * ctx)
399 int have_ctx;
400 ssize_t ret;
402 have_ctx = (ctx != NULL);
404 ret = write (parent_fd, &routine, sizeof (routine));
405 ret = write (parent_fd, &argc, sizeof (int));
406 ret = write (parent_fd, &type, sizeof (type));
407 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
409 if (have_ctx)
410 ret = write (parent_fd, ctx, sizeof (FileOpContext));
413 /* --------------------------------------------------------------------------------------------- */
414 /*** public functions ****************************************************************************/
415 /* --------------------------------------------------------------------------------------------- */
417 void
418 unregister_task_running (pid_t pid, int fd)
420 destroy_task_and_return_fd (pid);
421 delete_select_channel (fd);
424 /* --------------------------------------------------------------------------------------------- */
426 void
427 unregister_task_with_pid (pid_t pid)
429 int fd = destroy_task_and_return_fd (pid);
430 if (fd != -1)
431 delete_select_channel (fd);
435 /* --------------------------------------------------------------------------------------------- */
437 * Try to make the Midnight Commander a background job
439 * Returns:
440 * 1 for parent
441 * 0 for child
442 * -1 on failure
445 do_background (struct FileOpContext *ctx, char *info)
447 int comm[2]; /* control connection stream */
448 int back_comm[2]; /* back connection */
449 pid_t pid;
451 if (pipe (comm) == -1)
452 return -1;
454 if (pipe (back_comm) == -1)
455 return -1;
457 pid = fork ();
458 if (pid == -1)
460 int saved_errno = errno;
462 (void) close (comm[0]);
463 (void) close (comm[1]);
464 (void) close (back_comm[0]);
465 (void) close (back_comm[1]);
466 errno = saved_errno;
467 return -1;
470 if (pid == 0)
472 int nullfd;
474 parent_fd = comm[1];
475 from_parent_fd = back_comm[0];
477 we_are_background = 1;
478 top_dlg = NULL;
480 /* Make stdin/stdout/stderr point somewhere */
481 close (0);
482 close (1);
483 close (2);
485 nullfd = open ("/dev/null", O_RDWR);
486 if (nullfd != -1)
488 while (dup2 (nullfd, 0) == -1 && errno == EINTR)
490 while (dup2 (nullfd, 1) == -1 && errno == EINTR)
492 while (dup2 (nullfd, 2) == -1 && errno == EINTR)
496 return 0;
498 else
500 ctx->pid = pid;
501 register_task_running (ctx, pid, comm[0], back_comm[1], info);
502 return 1;
506 /* --------------------------------------------------------------------------------------------- */
509 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
511 va_list ap;
512 int i;
513 ssize_t ret;
515 va_start (ap, argc);
516 parent_call_header (routine, argc, Return_Integer, ctx);
517 for (i = 0; i < argc; i++)
519 int len;
520 void *value;
522 len = va_arg (ap, int);
523 value = va_arg (ap, void *);
524 ret = write (parent_fd, &len, sizeof (int));
525 ret = write (parent_fd, value, len);
528 ret = read (from_parent_fd, &i, sizeof (int));
529 if (ctx)
530 ret = read (from_parent_fd, ctx, sizeof (FileOpContext));
532 return i;
535 /* --------------------------------------------------------------------------------------------- */
537 char *
538 parent_call_string (void *routine, int argc, ...)
540 va_list ap;
541 char *str;
542 int i;
544 va_start (ap, argc);
545 parent_call_header (routine, argc, Return_String, NULL);
546 for (i = 0; i < argc; i++)
548 int len;
549 void *value;
551 len = va_arg (ap, int);
552 value = va_arg (ap, void *);
553 if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
554 (write (parent_fd, value, len) != len))
555 return NULL;
557 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
558 return NULL;
559 if (!i)
560 return NULL;
561 str = g_malloc (i + 1);
562 if (read (from_parent_fd, str, i) != i)
564 g_free (str);
565 return NULL;
567 str[i] = 0;
568 return str;
571 /* --------------------------------------------------------------------------------------------- */
573 #endif /* WITH_BACKGROUND */