Changes into src directory:
[midnight-commander.git] / src / background.c
blobb1b83ffe140ba00eb583114593c0c8459e275d0f
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 "background.h"
48 #include "wtools.h"
49 #include "layout.h" /* repaint_screen() */
50 #include "fileopctx.h" /* FileOpContext */
51 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
53 enum ReturnType {
54 Return_String,
55 Return_Integer
58 /* If true, this is a background process */
59 int we_are_background = 0;
61 /* File descriptor for talking to our parent */
62 static int parent_fd;
64 /* File descriptor for messages from our parent */
65 static int from_parent_fd;
67 #define MAXCALLARGS 4 /* Number of arguments supported */
69 struct TaskList *task_list = NULL;
71 static int background_attention (int fd, void *closure);
73 static void
74 register_task_running (FileOpContext *ctx, pid_t pid, int fd, int to_child,
75 char *info)
77 TaskList *new;
79 new = g_new (TaskList, 1);
80 new->pid = pid;
81 new->info = info;
82 new->state = Task_Running;
83 new->next = task_list;
84 new->fd = fd;
85 new->to_child_fd = to_child;
86 task_list = new;
88 add_select_channel (fd, background_attention, ctx);
91 static int
92 destroy_task_and_return_fd (pid_t pid)
94 TaskList *p = task_list;
95 TaskList *prev = 0;
97 while (p){
98 if (p->pid == pid){
99 if (prev)
100 prev->next = p->next;
101 else
102 task_list = p->next;
103 g_free (p->info);
104 g_free (p);
105 return p->fd;
107 prev = p;
108 p = p->next;
111 /* pid not found */
112 return -1;
115 void
116 unregister_task_running (pid_t pid, int fd)
118 destroy_task_and_return_fd(pid);
119 delete_select_channel (fd);
122 void
123 unregister_task_with_pid (pid_t pid)
125 int fd = destroy_task_and_return_fd(pid);
126 if (fd != -1)
127 delete_select_channel (fd);
131 * Try to make the Midnight Commander a background job
133 * Returns:
134 * 1 for parent
135 * 0 for child
136 * -1 on failure
139 do_background (struct FileOpContext *ctx, char *info)
141 int comm[2]; /* control connection stream */
142 int back_comm[2]; /* back connection */
143 pid_t pid;
145 if (pipe (comm) == -1)
146 return -1;
148 if (pipe (back_comm) == -1)
149 return -1;
151 if ((pid = fork ()) == -1) {
152 int saved_errno = errno;
153 (void) close (comm[0]);
154 (void) close (comm[1]);
155 (void) close (back_comm[0]);
156 (void) close (back_comm[1]);
157 errno = saved_errno;
158 return -1;
161 if (pid == 0) {
162 int nullfd;
164 parent_fd = comm[1];
165 from_parent_fd = back_comm[0];
167 we_are_background = 1;
168 current_dlg = NULL;
170 /* Make stdin/stdout/stderr point somewhere */
171 close (0);
172 close (1);
173 close (2);
175 if ((nullfd = open ("/dev/null", O_RDWR)) != -1) {
176 while (dup2 (nullfd, 0) == -1 && errno == EINTR);
177 while (dup2 (nullfd, 1) == -1 && errno == EINTR);
178 while (dup2 (nullfd, 2) == -1 && errno == EINTR);
181 return 0;
182 } else {
183 ctx->pid = pid;
184 register_task_running (ctx, pid, comm[0], back_comm[1], info);
185 return 1;
189 /* {{{ Parent handlers */
191 /* Parent/child protocol
193 * the child (the background) process send the following:
194 * void *routine -- routine to be invoked in the parent
195 * int nargc -- number of arguments
196 * int type -- Return argument type.
198 * If the routine is zero, then it is a way to tell the parent
199 * that the process is dying.
201 * nargc arguments in the following format:
202 * int size of the coming block
203 * size bytes with the block
205 * Now, the parent loads all those and then invokes
206 * the routine with pointers to the information passed
207 * (we just support pointers).
209 * If the return type is integer:
211 * the parent then writes an int to the child with
212 * the return value from the routine and the values
213 * of any global variable that is modified in the parent
214 * currently: do_append and recursive_result.
216 * If the return type is a string:
218 * the parent writes the resulting string length
219 * if the result string was NULL or the empty string,
220 * then the length is zero.
221 * The parent then writes the string length and frees
222 * the result string.
225 * Receive requests from background process and invoke the
226 * specified routine
229 static int
230 background_attention (int fd, void *closure)
232 FileOpContext *ctx;
233 int have_ctx;
234 union
236 int (*have_ctx0)(int);
237 int (*have_ctx1)(int, char *);
238 int (*have_ctx2)(int, char *, char *);
239 int (*have_ctx3)(int, char *, char *, char *);
240 int (*have_ctx4)(int, char *, char *, char *, char *);
242 int (*non_have_ctx0)(FileOpContext *, int);
243 int (*non_have_ctx1)(FileOpContext *, int, char *);
244 int (*non_have_ctx2)(FileOpContext *, int, char *, char *);
245 int (*non_have_ctx3)(FileOpContext *, int, char *, char *, char *);
246 int (*non_have_ctx4)(FileOpContext *, int, char *, char *, char *, char *);
248 char * (*ret_str0)();
249 char * (*ret_str1)(char *);
250 char * (*ret_str2)(char *, char *);
251 char * (*ret_str3)(char *, char *, char *);
252 char * (*ret_str4)(char *, char *, char *, char *);
254 void *pointer;
255 } routine;
256 /* void *routine;*/
257 int argc, i, result, status;
258 char *data [MAXCALLARGS];
259 ssize_t bytes, ret;
260 struct TaskList *p;
261 int to_child_fd = -1;
262 enum ReturnType type;
264 ctx = closure;
266 bytes = read (fd, &routine.pointer, sizeof (routine));
267 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) {
268 const char *background_process_error = _(" Background process error ");
270 unregister_task_running (ctx->pid, fd);
271 if (!waitpid (ctx->pid, &status, WNOHANG)) {
272 /* the process is still running, but it misbehaves - kill it */
273 kill (ctx->pid, SIGTERM);
274 message (D_ERROR, background_process_error, _(" Unknown error in child "));
275 return 0;
278 /* 0 means happy end */
279 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
280 return 0;
282 message (D_ERROR, background_process_error, _(" Child died unexpectedly "));
284 return 0;
287 if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
288 (read (fd, &type, sizeof (type)) != sizeof (type)) ||
289 (read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
291 message (D_ERROR, _(" Background protocol error "),_("Reading failed"));
292 return 0;
295 if (argc > MAXCALLARGS){
296 message (D_ERROR, _(" Background protocol error "),
297 _(" Background process sent us a request for more arguments \n"
298 " than we can handle. \n"));
301 if (have_ctx)
303 if (read (fd, ctx, sizeof (FileOpContext)) != sizeof (FileOpContext))
305 message (D_ERROR, _(" Background protocol error "),_("Reading failed"));
306 return 0;
310 for (i = 0; i < argc; i++){
311 int size;
313 if (read (fd, &size, sizeof (size)) != sizeof (size)) {
314 message (D_ERROR, _(" Background protocol error "),_("Reading failed"));
315 return 0;
317 data [i] = g_malloc (size+1);
318 if (read (fd, data [i], size) != size) {
319 message (D_ERROR, _(" Background protocol error "),_("Reading failed"));
320 return 0;
322 data [i][size] = 0; /* NULL terminate the blocks (they could be strings) */
325 /* Find child task info by descriptor */
326 /* Find before call, because process can destroy self after */
327 for (p = task_list; p; p = p->next) {
328 if (p->fd == fd)
329 break;
332 if (p) to_child_fd = p->to_child_fd;
334 if (to_child_fd == -1)
335 message (D_ERROR, _(" Background process error "), _(" Unknown error in child "));
337 /* Handle the call */
338 if (type == Return_Integer){
339 if (!have_ctx)
340 switch (argc){
341 case 0:
342 result = routine.have_ctx0 (Background);
343 break;
344 case 1:
345 result = routine.have_ctx1 (Background, data [0]);
346 break;
347 case 2:
348 result = routine.have_ctx2 (Background, data [0], data [1]);
349 break;
350 case 3:
351 result = routine.have_ctx3 (Background, data [0], data [1], data [2]);
352 break;
353 case 4:
354 result = routine.have_ctx4 (Background, data [0], data [1], data [2], data [3]);
355 break;
357 else
358 switch (argc){
359 case 0:
360 result = routine.non_have_ctx0 (ctx, Background);
361 break;
362 case 1:
363 result = routine.non_have_ctx1 (ctx, Background, data [0]);
364 break;
365 case 2:
366 result = routine.non_have_ctx2 (ctx, Background, data [0], data [1]);
367 break;
368 case 3:
369 result = routine.non_have_ctx3 (ctx, Background, data [0], data [1], data [2]);
370 break;
371 case 4:
372 result = routine.non_have_ctx4 (ctx, Background, data [0], data [1], data [2], data [3]);
373 break;
376 /* Send the result code and the value for shared variables */
377 ret = write (to_child_fd, &result, sizeof (int));
378 if (have_ctx && to_child_fd != -1)
379 ret = write (to_child_fd, ctx, sizeof (FileOpContext));
380 } else if (type == Return_String) {
381 int len;
382 char *resstr = NULL;
384 /* FIXME: string routines should also use the Foreground/Background
385 * parameter. Currently, this is not used here
387 switch (argc){
388 case 0:
389 resstr = routine.ret_str0 ();
390 break;
391 case 1:
392 resstr = routine.ret_str1 (data [0]);
393 break;
394 case 2:
395 resstr = routine.ret_str2 (data [0], data [1]);
396 break;
397 case 3:
398 resstr = routine.ret_str3 (data [0], data [1], data [2]);
399 break;
400 case 4:
401 resstr = routine.ret_str4 (data [0], data [1], data [2], data [3]);
402 break;
403 default: g_assert_not_reached();
405 if (resstr){
406 len = strlen (resstr);
407 ret = write (to_child_fd, &len, sizeof (len));
408 if (len){
409 write (to_child_fd, resstr, len);
411 g_free (resstr);
412 } else {
413 len = 0;
414 ret = write (to_child_fd, &len, sizeof (len));
417 for (i = 0; i < argc; i++)
418 g_free (data [i]);
420 repaint_screen ();
421 return 0;
425 /* }}} */
427 /* {{{ client RPC routines */
429 /* Sends the header for a call to a routine in the parent process. If the file
430 * operation context is not NULL, then it requests that the first parameter of
431 * the call be a file operation context.
433 static void
434 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
436 int have_ctx;
437 ssize_t ret;
439 have_ctx = (ctx != NULL);
441 ret = write (parent_fd, &routine, sizeof (routine));
442 ret = write (parent_fd, &argc, sizeof (int));
443 ret = write (parent_fd, &type, sizeof (type));
444 ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
446 if (have_ctx)
447 ret = write (parent_fd, ctx, sizeof (FileOpContext));
451 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
453 va_list ap;
454 int i;
455 ssize_t ret;
457 va_start (ap, argc);
458 parent_call_header (routine, argc, Return_Integer, ctx);
459 for (i = 0; i < argc; i++) {
460 int len;
461 void *value;
463 len = va_arg (ap, int);
464 value = va_arg (ap, void *);
465 ret = write (parent_fd, &len, sizeof (int));
466 ret = write (parent_fd, value, len);
469 ret = read (from_parent_fd, &i, sizeof (int));
470 if (ctx)
471 ret = read (from_parent_fd, ctx, sizeof (FileOpContext));
473 return i;
476 char *
477 parent_call_string (void *routine, int argc, ...)
479 va_list ap;
480 char *str;
481 int i;
483 va_start (ap, argc);
484 parent_call_header (routine, argc, Return_String, NULL);
485 for (i = 0; i < argc; i++){
486 int len;
487 void *value;
489 len = va_arg (ap, int);
490 value = va_arg (ap, void *);
491 if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
492 (write (parent_fd, value, len) != len))
493 return NULL;
495 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
496 return NULL;
497 if (!i)
498 return NULL;
499 str = g_malloc (i + 1);
500 if (read (from_parent_fd, str, i) != i)
502 g_free(str);
503 return NULL;
505 str [i] = 0;
506 return str;
509 #endif /* WITH_BACKGROUND */