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() */
49 #include "lib/event-types.h"
51 #include "filemanager/fileopctx.h" /* FileOpContext */
53 #include "background.h"
55 /*** global variables ****************************************************************************/
57 #define MAXCALLARGS 4 /* Number of arguments supported */
59 /*** file scope macro definitions ****************************************************************/
61 /*** file scope type declarations ****************************************************************/
69 /*** file scope variables ************************************************************************/
71 /* File descriptor for talking to our parent */
74 /* File descriptor for messages from our parent */
75 static int from_parent_fd
;
77 struct TaskList
*task_list
= NULL
;
79 static int background_attention (int fd
, void *closure
);
81 /*** file scope functions ************************************************************************/
82 /* --------------------------------------------------------------------------------------------- */
85 register_task_running (FileOpContext
* ctx
, pid_t pid
, int fd
, int to_child
, char *info
)
89 new = g_new (TaskList
, 1);
92 new->state
= Task_Running
;
93 new->next
= task_list
;
95 new->to_child_fd
= to_child
;
98 add_select_channel (fd
, background_attention
, ctx
);
101 /* --------------------------------------------------------------------------------------------- */
104 destroy_task_and_return_fd (pid_t pid
)
106 TaskList
*p
= task_list
;
114 prev
->next
= p
->next
;
129 /* --------------------------------------------------------------------------------------------- */
130 /* {{{ Parent handlers */
132 /* Parent/child protocol
134 * the child (the background) process send the following:
135 * void *routine -- routine to be invoked in the parent
136 * int nargc -- number of arguments
137 * int type -- Return argument type.
139 * If the routine is zero, then it is a way to tell the parent
140 * that the process is dying.
142 * nargc arguments in the following format:
143 * int size of the coming block
144 * size bytes with the block
146 * Now, the parent loads all those and then invokes
147 * the routine with pointers to the information passed
148 * (we just support pointers).
150 * If the return type is integer:
152 * the parent then writes an int to the child with
153 * the return value from the routine and the values
154 * of any global variable that is modified in the parent
155 * currently: do_append and recursive_result.
157 * If the return type is a string:
159 * the parent writes the resulting string length
160 * if the result string was NULL or the empty string,
161 * then the length is zero.
162 * The parent then writes the string length and frees
166 * Receive requests from background process and invoke the
171 background_attention (int fd
, void *closure
)
177 int (*have_ctx0
) (int);
178 int (*have_ctx1
) (int, char *);
179 int (*have_ctx2
) (int, char *, char *);
180 int (*have_ctx3
) (int, char *, char *, char *);
181 int (*have_ctx4
) (int, char *, char *, char *, char *);
183 int (*non_have_ctx0
) (FileOpContext
*, int);
184 int (*non_have_ctx1
) (FileOpContext
*, int, char *);
185 int (*non_have_ctx2
) (FileOpContext
*, int, char *, char *);
186 int (*non_have_ctx3
) (FileOpContext
*, int, char *, char *, char *);
187 int (*non_have_ctx4
) (FileOpContext
*, int, char *, char *, char *, char *);
189 char *(*ret_str0
) ();
190 char *(*ret_str1
) (char *);
191 char *(*ret_str2
) (char *, char *);
192 char *(*ret_str3
) (char *, char *, char *);
193 char *(*ret_str4
) (char *, char *, char *, char *);
198 int argc
, i
, result
, status
;
199 char *data
[MAXCALLARGS
];
202 int to_child_fd
= -1;
203 enum ReturnType type
;
207 bytes
= read (fd
, &routine
.pointer
, sizeof (routine
));
208 if (bytes
== -1 || (size_t) bytes
< (sizeof (routine
)))
210 const char *background_process_error
= _("Background process error");
212 unregister_task_running (ctx
->pid
, fd
);
213 if (!waitpid (ctx
->pid
, &status
, WNOHANG
))
215 /* the process is still running, but it misbehaves - kill it */
216 kill (ctx
->pid
, SIGTERM
);
217 message (D_ERROR
, background_process_error
, _("Unknown error in child"));
221 /* 0 means happy end */
222 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
225 message (D_ERROR
, background_process_error
, _("Child died unexpectedly"));
230 if ((read (fd
, &argc
, sizeof (argc
)) != sizeof (argc
)) ||
231 (read (fd
, &type
, sizeof (type
)) != sizeof (type
)) ||
232 (read (fd
, &have_ctx
, sizeof (have_ctx
)) != sizeof (have_ctx
)))
234 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
238 if (argc
> MAXCALLARGS
)
240 message (D_ERROR
, _("Background protocol error"),
241 _("Background process sent us a request for more arguments\n"
242 "than we can handle."));
247 if (read (fd
, ctx
, sizeof (FileOpContext
)) != sizeof (FileOpContext
))
249 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
254 for (i
= 0; i
< argc
; i
++)
258 if (read (fd
, &size
, sizeof (size
)) != sizeof (size
))
260 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
263 data
[i
] = g_malloc (size
+ 1);
264 if (read (fd
, data
[i
], size
) != size
)
266 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
269 data
[i
][size
] = 0; /* NULL terminate the blocks (they could be strings) */
272 /* Find child task info by descriptor */
273 /* Find before call, because process can destroy self after */
274 for (p
= task_list
; p
; p
= p
->next
)
281 to_child_fd
= p
->to_child_fd
;
283 if (to_child_fd
== -1)
284 message (D_ERROR
, _("Background process error"), _("Unknown error in child"));
286 /* Handle the call */
287 if (type
== Return_Integer
)
293 result
= routine
.have_ctx0 (Background
);
296 result
= routine
.have_ctx1 (Background
, data
[0]);
299 result
= routine
.have_ctx2 (Background
, data
[0], data
[1]);
302 result
= routine
.have_ctx3 (Background
, data
[0], data
[1], data
[2]);
305 result
= routine
.have_ctx4 (Background
, data
[0], data
[1], data
[2], data
[3]);
312 result
= routine
.non_have_ctx0 (ctx
, Background
);
315 result
= routine
.non_have_ctx1 (ctx
, Background
, data
[0]);
318 result
= routine
.non_have_ctx2 (ctx
, Background
, data
[0], data
[1]);
321 result
= routine
.non_have_ctx3 (ctx
, Background
, data
[0], data
[1], data
[2]);
325 routine
.non_have_ctx4 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
329 /* Send the result code and the value for shared variables */
330 ret
= write (to_child_fd
, &result
, sizeof (int));
331 if (have_ctx
&& to_child_fd
!= -1)
332 ret
= write (to_child_fd
, ctx
, sizeof (FileOpContext
));
334 else if (type
== Return_String
)
339 /* FIXME: string routines should also use the Foreground/Background
340 * parameter. Currently, this is not used here
345 resstr
= routine
.ret_str0 ();
348 resstr
= routine
.ret_str1 (data
[0]);
351 resstr
= routine
.ret_str2 (data
[0], data
[1]);
354 resstr
= routine
.ret_str3 (data
[0], data
[1], data
[2]);
357 resstr
= routine
.ret_str4 (data
[0], data
[1], data
[2], data
[3]);
360 g_assert_not_reached ();
364 len
= strlen (resstr
);
365 ret
= write (to_child_fd
, &len
, sizeof (len
));
367 ret
= write (to_child_fd
, resstr
, len
);
373 ret
= write (to_child_fd
, &len
, sizeof (len
));
376 for (i
= 0; i
< argc
; i
++)
384 /* --------------------------------------------------------------------------------------------- */
387 /* {{{ client RPC routines */
389 /* Sends the header for a call to a routine in the parent process. If the file
390 * operation context is not NULL, then it requests that the first parameter of
391 * the call be a file operation context.
395 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
* ctx
)
400 have_ctx
= (ctx
!= NULL
);
402 ret
= write (parent_fd
, &routine
, sizeof (routine
));
403 ret
= write (parent_fd
, &argc
, sizeof (int));
404 ret
= write (parent_fd
, &type
, sizeof (type
));
405 ret
= write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
408 ret
= write (parent_fd
, ctx
, sizeof (FileOpContext
));
411 /* --------------------------------------------------------------------------------------------- */
414 parent_va_call (void *routine
, gpointer data
, int argc
, va_list ap
)
418 struct FileOpContext
*ctx
= (struct FileOpContext
*) data
;
420 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
421 for (i
= 0; i
< argc
; i
++)
426 len
= va_arg (ap
, int);
427 value
= va_arg (ap
, void *);
428 ret
= write (parent_fd
, &len
, sizeof (int));
429 ret
= write (parent_fd
, value
, len
);
432 ret
= read (from_parent_fd
, &i
, sizeof (int));
434 ret
= read (from_parent_fd
, ctx
, sizeof (FileOpContext
));
439 /* --------------------------------------------------------------------------------------------- */
442 parent_va_call_string (void *routine
, int argc
, va_list ap
)
447 parent_call_header (routine
, argc
, Return_String
, NULL
);
448 for (i
= 0; i
< argc
; i
++)
453 len
= va_arg (ap
, int);
454 value
= va_arg (ap
, void *);
455 if ((write (parent_fd
, &len
, sizeof (int)) != sizeof (int)) ||
456 (write (parent_fd
, value
, len
) != len
))
462 if (read (from_parent_fd
, &i
, sizeof (int)) != sizeof (int))
466 str
= g_malloc (i
+ 1);
467 if (read (from_parent_fd
, str
, i
) != i
)
476 /* --------------------------------------------------------------------------------------------- */
477 /*** public functions ****************************************************************************/
478 /* --------------------------------------------------------------------------------------------- */
481 unregister_task_running (pid_t pid
, int fd
)
483 destroy_task_and_return_fd (pid
);
484 delete_select_channel (fd
);
487 /* --------------------------------------------------------------------------------------------- */
490 unregister_task_with_pid (pid_t pid
)
492 int fd
= destroy_task_and_return_fd (pid
);
494 delete_select_channel (fd
);
498 /* --------------------------------------------------------------------------------------------- */
500 * Try to make the Midnight Commander a background job
508 do_background (struct FileOpContext
*ctx
, char *info
)
510 int comm
[2]; /* control connection stream */
511 int back_comm
[2]; /* back connection */
514 if (pipe (comm
) == -1)
517 if (pipe (back_comm
) == -1)
523 int saved_errno
= errno
;
525 (void) close (comm
[0]);
526 (void) close (comm
[1]);
527 (void) close (back_comm
[0]);
528 (void) close (back_comm
[1]);
538 from_parent_fd
= back_comm
[0];
540 mc_global
.we_are_background
= 1;
543 /* Make stdin/stdout/stderr point somewhere */
548 nullfd
= open ("/dev/null", O_RDWR
);
551 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
)
553 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
)
555 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
)
564 register_task_running (ctx
, pid
, comm
[0], back_comm
[1], info
);
569 /* --------------------------------------------------------------------------------------------- */
572 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
578 ret
= parent_va_call (routine
, (gpointer
) ctx
, argc
, ap
);
584 /* --------------------------------------------------------------------------------------------- */
587 parent_call_string (void *routine
, int argc
, ...)
593 str
= parent_va_call_string (routine
, argc
, ap
);
599 /* --------------------------------------------------------------------------------------------- */
603 background_parent_call (const gchar
* event_group_name
, const gchar
* event_name
,
604 gpointer init_data
, gpointer data
)
606 ev_background_parent_call_t
*event_data
= (ev_background_parent_call_t
*) data
;
608 (void) event_group_name
;
613 parent_va_call (event_data
->routine
, event_data
->ctx
, event_data
->argc
, event_data
->ap
);
618 /* --------------------------------------------------------------------------------------------- */
622 background_parent_call_string (const gchar
* event_group_name
, const gchar
* event_name
,
623 gpointer init_data
, gpointer data
)
625 ev_background_parent_call_t
*event_data
= (ev_background_parent_call_t
*) data
;
627 (void) event_group_name
;
632 parent_va_call_string (event_data
->routine
, event_data
->argc
, event_data
->ap
);
637 /* --------------------------------------------------------------------------------------------- */
639 #endif /* WITH_BACKGROUND */