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. */
25 /** \file background.c
26 * \brief Source: Background support
31 #ifdef WITH_BACKGROUND
40 #include <sys/types.h>
42 #include <sys/wait.h> /* waitpid() */
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 "filemanager/layout.h" /* repaint_screen() */
51 #include "filemanager/fileopctx.h" /* FileOpContext */
53 #include "background.h"
55 /*** global variables ****************************************************************************/
57 #define MAXCALLARGS 4 /* Number of arguments supported */
59 /* If true, this is a background process */
60 int we_are_background
= 0;
62 /*** file scope macro definitions ****************************************************************/
64 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
74 /* File descriptor for talking to our parent */
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 /* --------------------------------------------------------------------------------------------- */
88 register_task_running (FileOpContext
* ctx
, pid_t pid
, int fd
, int to_child
, char *info
)
92 new = g_new (TaskList
, 1);
95 new->state
= Task_Running
;
96 new->next
= task_list
;
98 new->to_child_fd
= to_child
;
101 add_select_channel (fd
, background_attention
, ctx
);
104 /* --------------------------------------------------------------------------------------------- */
107 destroy_task_and_return_fd (pid_t pid
)
109 TaskList
*p
= task_list
;
117 prev
->next
= p
->next
;
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
169 * Receive requests from background process and invoke the
174 background_attention (int fd
, void *closure
)
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 *);
201 int argc
, i
, result
, status
;
202 char *data
[MAXCALLARGS
];
205 int to_child_fd
= -1;
206 enum ReturnType type
;
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"));
224 /* 0 means happy end */
225 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
228 message (D_ERROR
, background_process_error
, _("Child died unexpectedly"));
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"));
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."));
250 if (read (fd
, ctx
, sizeof (FileOpContext
)) != sizeof (FileOpContext
))
252 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
257 for (i
= 0; i
< argc
; i
++)
261 if (read (fd
, &size
, sizeof (size
)) != sizeof (size
))
263 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
266 data
[i
] = g_malloc (size
+ 1);
267 if (read (fd
, data
[i
], size
) != size
)
269 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
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
)
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
)
296 result
= routine
.have_ctx0 (Background
);
299 result
= routine
.have_ctx1 (Background
, data
[0]);
302 result
= routine
.have_ctx2 (Background
, data
[0], data
[1]);
305 result
= routine
.have_ctx3 (Background
, data
[0], data
[1], data
[2]);
308 result
= routine
.have_ctx4 (Background
, data
[0], data
[1], data
[2], data
[3]);
315 result
= routine
.non_have_ctx0 (ctx
, Background
);
318 result
= routine
.non_have_ctx1 (ctx
, Background
, data
[0]);
321 result
= routine
.non_have_ctx2 (ctx
, Background
, data
[0], data
[1]);
324 result
= routine
.non_have_ctx3 (ctx
, Background
, data
[0], data
[1], data
[2]);
328 routine
.non_have_ctx4 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
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
)
342 /* FIXME: string routines should also use the Foreground/Background
343 * parameter. Currently, this is not used here
348 resstr
= routine
.ret_str0 ();
351 resstr
= routine
.ret_str1 (data
[0]);
354 resstr
= routine
.ret_str2 (data
[0], data
[1]);
357 resstr
= routine
.ret_str3 (data
[0], data
[1], data
[2]);
360 resstr
= routine
.ret_str4 (data
[0], data
[1], data
[2], data
[3]);
363 g_assert_not_reached ();
367 len
= strlen (resstr
);
368 ret
= write (to_child_fd
, &len
, sizeof (len
));
370 ret
= write (to_child_fd
, resstr
, len
);
376 ret
= write (to_child_fd
, &len
, sizeof (len
));
379 for (i
= 0; i
< argc
; i
++)
387 /* --------------------------------------------------------------------------------------------- */
390 /* {{{ client RPC routines */
392 /* Sends the header for a call to a routine in the parent process. If the file
393 * operation context is not NULL, then it requests that the first parameter of
394 * the call be a file operation context.
398 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
* ctx
)
403 have_ctx
= (ctx
!= NULL
);
405 ret
= write (parent_fd
, &routine
, sizeof (routine
));
406 ret
= write (parent_fd
, &argc
, sizeof (int));
407 ret
= write (parent_fd
, &type
, sizeof (type
));
408 ret
= write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
411 ret
= write (parent_fd
, ctx
, sizeof (FileOpContext
));
414 /* --------------------------------------------------------------------------------------------- */
415 /*** public functions ****************************************************************************/
416 /* --------------------------------------------------------------------------------------------- */
419 unregister_task_running (pid_t pid
, int fd
)
421 destroy_task_and_return_fd (pid
);
422 delete_select_channel (fd
);
425 /* --------------------------------------------------------------------------------------------- */
428 unregister_task_with_pid (pid_t pid
)
430 int fd
= destroy_task_and_return_fd (pid
);
432 delete_select_channel (fd
);
436 /* --------------------------------------------------------------------------------------------- */
438 * Try to make the Midnight Commander a background job
446 do_background (struct FileOpContext
*ctx
, char *info
)
448 int comm
[2]; /* control connection stream */
449 int back_comm
[2]; /* back connection */
452 if (pipe (comm
) == -1)
455 if (pipe (back_comm
) == -1)
461 int saved_errno
= errno
;
463 (void) close (comm
[0]);
464 (void) close (comm
[1]);
465 (void) close (back_comm
[0]);
466 (void) close (back_comm
[1]);
476 from_parent_fd
= back_comm
[0];
478 we_are_background
= 1;
481 /* Make stdin/stdout/stderr point somewhere */
486 nullfd
= open ("/dev/null", O_RDWR
);
489 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
)
491 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
)
493 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
)
502 register_task_running (ctx
, pid
, comm
[0], back_comm
[1], info
);
507 /* --------------------------------------------------------------------------------------------- */
510 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
517 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
518 for (i
= 0; i
< argc
; i
++)
523 len
= va_arg (ap
, int);
524 value
= va_arg (ap
, void *);
525 ret
= write (parent_fd
, &len
, sizeof (int));
526 ret
= write (parent_fd
, value
, len
);
529 ret
= read (from_parent_fd
, &i
, sizeof (int));
531 ret
= read (from_parent_fd
, ctx
, sizeof (FileOpContext
));
536 /* --------------------------------------------------------------------------------------------- */
539 parent_call_string (void *routine
, int argc
, ...)
546 parent_call_header (routine
, argc
, Return_String
, NULL
);
547 for (i
= 0; i
< argc
; i
++)
552 len
= va_arg (ap
, int);
553 value
= va_arg (ap
, void *);
554 if ((write (parent_fd
, &len
, sizeof (int)) != sizeof (int)) ||
555 (write (parent_fd
, value
, len
) != len
))
558 if (read (from_parent_fd
, &i
, sizeof (int)) != sizeof (int))
562 str
= g_malloc (i
+ 1);
563 if (read (from_parent_fd
, str
, i
) != i
)
572 /* --------------------------------------------------------------------------------------------- */
574 #endif /* WITH_BACKGROUND */