5 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
7 The Free Software Foundation, Inc.
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/>.
30 /** \file background.c
31 * \brief Source: Background support
36 #ifdef WITH_BACKGROUND
45 #include <sys/types.h>
47 #include <sys/wait.h> /* waitpid() */
51 #include "lib/global.h"
52 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
53 #include "lib/widget.h" /* message() */
54 #include "lib/event-types.h"
56 #include "filemanager/fileopctx.h" /* FileOpContext */
58 #include "background.h"
60 /*** global variables ****************************************************************************/
62 #define MAXCALLARGS 4 /* Number of arguments supported */
64 /*** file scope macro definitions ****************************************************************/
66 /*** file scope type declarations ****************************************************************/
74 /*** file scope variables ************************************************************************/
76 /* File descriptor for talking to our parent */
79 /* File descriptor for messages from our parent */
80 static int from_parent_fd
;
82 struct TaskList
*task_list
= NULL
;
84 static int background_attention (int fd
, void *closure
);
86 /*** file scope functions ************************************************************************/
87 /* --------------------------------------------------------------------------------------------- */
90 register_task_running (FileOpContext
* ctx
, pid_t pid
, int fd
, int to_child
, char *info
)
94 new = g_new (TaskList
, 1);
97 new->state
= Task_Running
;
98 new->next
= task_list
;
100 new->to_child_fd
= to_child
;
103 add_select_channel (fd
, background_attention
, ctx
);
106 /* --------------------------------------------------------------------------------------------- */
109 destroy_task_and_return_fd (pid_t pid
)
111 TaskList
*p
= task_list
;
119 prev
->next
= p
->next
;
134 /* --------------------------------------------------------------------------------------------- */
135 /* {{{ Parent handlers */
137 /* Parent/child protocol
139 * the child (the background) process send the following:
140 * void *routine -- routine to be invoked in the parent
141 * int nargc -- number of arguments
142 * int type -- Return argument type.
144 * If the routine is zero, then it is a way to tell the parent
145 * that the process is dying.
147 * nargc arguments in the following format:
148 * int size of the coming block
149 * size bytes with the block
151 * Now, the parent loads all those and then invokes
152 * the routine with pointers to the information passed
153 * (we just support pointers).
155 * If the return type is integer:
157 * the parent then writes an int to the child with
158 * the return value from the routine and the values
159 * of any global variable that is modified in the parent
160 * currently: do_append and recursive_result.
162 * If the return type is a string:
164 * the parent writes the resulting string length
165 * if the result string was NULL or the empty string,
166 * then the length is zero.
167 * The parent then writes the string length and frees
171 * Receive requests from background process and invoke the
176 background_attention (int fd
, void *closure
)
182 int (*have_ctx0
) (int);
183 int (*have_ctx1
) (int, char *);
184 int (*have_ctx2
) (int, char *, char *);
185 int (*have_ctx3
) (int, char *, char *, char *);
186 int (*have_ctx4
) (int, char *, char *, char *, char *);
188 int (*non_have_ctx0
) (FileOpContext
*, int);
189 int (*non_have_ctx1
) (FileOpContext
*, int, char *);
190 int (*non_have_ctx2
) (FileOpContext
*, int, char *, char *);
191 int (*non_have_ctx3
) (FileOpContext
*, int, char *, char *, char *);
192 int (*non_have_ctx4
) (FileOpContext
*, int, char *, char *, char *, char *);
194 char *(*ret_str0
) ();
195 char *(*ret_str1
) (char *);
196 char *(*ret_str2
) (char *, char *);
197 char *(*ret_str3
) (char *, char *, char *);
198 char *(*ret_str4
) (char *, char *, char *, char *);
203 int argc
, i
, result
, status
;
204 char *data
[MAXCALLARGS
];
207 int to_child_fd
= -1;
208 enum ReturnType type
;
212 bytes
= read (fd
, &routine
.pointer
, sizeof (routine
));
213 if (bytes
== -1 || (size_t) bytes
< (sizeof (routine
)))
215 const char *background_process_error
= _("Background process error");
217 unregister_task_running (ctx
->pid
, fd
);
218 if (!waitpid (ctx
->pid
, &status
, WNOHANG
))
220 /* the process is still running, but it misbehaves - kill it */
221 kill (ctx
->pid
, SIGTERM
);
222 message (D_ERROR
, background_process_error
, _("Unknown error in child"));
226 /* 0 means happy end */
227 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
230 message (D_ERROR
, background_process_error
, _("Child died unexpectedly"));
235 if ((read (fd
, &argc
, sizeof (argc
)) != sizeof (argc
)) ||
236 (read (fd
, &type
, sizeof (type
)) != sizeof (type
)) ||
237 (read (fd
, &have_ctx
, sizeof (have_ctx
)) != sizeof (have_ctx
)))
239 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
243 if (argc
> MAXCALLARGS
)
245 message (D_ERROR
, _("Background protocol error"),
246 _("Background process sent us a request for more arguments\n"
247 "than we can handle."));
252 if (read (fd
, ctx
, sizeof (FileOpContext
)) != sizeof (FileOpContext
))
254 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
259 for (i
= 0; i
< argc
; i
++)
263 if (read (fd
, &size
, sizeof (size
)) != sizeof (size
))
265 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
268 data
[i
] = g_malloc (size
+ 1);
269 if (read (fd
, data
[i
], size
) != size
)
271 message (D_ERROR
, _("Background protocol error"), _("Reading failed"));
274 data
[i
][size
] = 0; /* NULL terminate the blocks (they could be strings) */
277 /* Find child task info by descriptor */
278 /* Find before call, because process can destroy self after */
279 for (p
= task_list
; p
; p
= p
->next
)
286 to_child_fd
= p
->to_child_fd
;
288 if (to_child_fd
== -1)
289 message (D_ERROR
, _("Background process error"), _("Unknown error in child"));
291 /* Handle the call */
292 if (type
== Return_Integer
)
298 result
= routine
.have_ctx0 (Background
);
301 result
= routine
.have_ctx1 (Background
, data
[0]);
304 result
= routine
.have_ctx2 (Background
, data
[0], data
[1]);
307 result
= routine
.have_ctx3 (Background
, data
[0], data
[1], data
[2]);
310 result
= routine
.have_ctx4 (Background
, data
[0], data
[1], data
[2], data
[3]);
317 result
= routine
.non_have_ctx0 (ctx
, Background
);
320 result
= routine
.non_have_ctx1 (ctx
, Background
, data
[0]);
323 result
= routine
.non_have_ctx2 (ctx
, Background
, data
[0], data
[1]);
326 result
= routine
.non_have_ctx3 (ctx
, Background
, data
[0], data
[1], data
[2]);
330 routine
.non_have_ctx4 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
334 /* Send the result code and the value for shared variables */
335 ret
= write (to_child_fd
, &result
, sizeof (int));
336 if (have_ctx
&& to_child_fd
!= -1)
337 ret
= write (to_child_fd
, ctx
, sizeof (FileOpContext
));
339 else if (type
== Return_String
)
344 /* FIXME: string routines should also use the Foreground/Background
345 * parameter. Currently, this is not used here
350 resstr
= routine
.ret_str0 ();
353 resstr
= routine
.ret_str1 (data
[0]);
356 resstr
= routine
.ret_str2 (data
[0], data
[1]);
359 resstr
= routine
.ret_str3 (data
[0], data
[1], data
[2]);
362 resstr
= routine
.ret_str4 (data
[0], data
[1], data
[2], data
[3]);
365 g_assert_not_reached ();
369 len
= strlen (resstr
);
370 ret
= write (to_child_fd
, &len
, sizeof (len
));
372 ret
= write (to_child_fd
, resstr
, len
);
378 ret
= write (to_child_fd
, &len
, sizeof (len
));
381 for (i
= 0; i
< argc
; i
++)
389 /* --------------------------------------------------------------------------------------------- */
392 /* {{{ client RPC routines */
394 /* Sends the header for a call to a routine in the parent process. If the file
395 * operation context is not NULL, then it requests that the first parameter of
396 * the call be a file operation context.
400 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
* ctx
)
405 have_ctx
= (ctx
!= NULL
);
407 ret
= write (parent_fd
, &routine
, sizeof (routine
));
408 ret
= write (parent_fd
, &argc
, sizeof (int));
409 ret
= write (parent_fd
, &type
, sizeof (type
));
410 ret
= write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
413 ret
= write (parent_fd
, ctx
, sizeof (FileOpContext
));
416 /* --------------------------------------------------------------------------------------------- */
419 parent_va_call (void *routine
, gpointer data
, int argc
, va_list ap
)
423 struct FileOpContext
*ctx
= (struct FileOpContext
*) data
;
425 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
426 for (i
= 0; i
< argc
; i
++)
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));
439 ret
= read (from_parent_fd
, ctx
, sizeof (FileOpContext
));
444 /* --------------------------------------------------------------------------------------------- */
447 parent_va_call_string (void *routine
, int argc
, va_list ap
)
452 parent_call_header (routine
, argc
, Return_String
, NULL
);
453 for (i
= 0; i
< argc
; i
++)
458 len
= va_arg (ap
, int);
459 value
= va_arg (ap
, void *);
460 if ((write (parent_fd
, &len
, sizeof (int)) != sizeof (int)) ||
461 (write (parent_fd
, value
, len
) != len
))
467 if (read (from_parent_fd
, &i
, sizeof (int)) != sizeof (int))
471 str
= g_malloc (i
+ 1);
472 if (read (from_parent_fd
, str
, i
) != i
)
481 /* --------------------------------------------------------------------------------------------- */
482 /*** public functions ****************************************************************************/
483 /* --------------------------------------------------------------------------------------------- */
486 unregister_task_running (pid_t pid
, int fd
)
488 destroy_task_and_return_fd (pid
);
489 delete_select_channel (fd
);
492 /* --------------------------------------------------------------------------------------------- */
495 unregister_task_with_pid (pid_t pid
)
497 int fd
= destroy_task_and_return_fd (pid
);
499 delete_select_channel (fd
);
503 /* --------------------------------------------------------------------------------------------- */
505 * Try to make the Midnight Commander a background job
513 do_background (struct FileOpContext
*ctx
, char *info
)
515 int comm
[2]; /* control connection stream */
516 int back_comm
[2]; /* back connection */
519 if (pipe (comm
) == -1)
522 if (pipe (back_comm
) == -1)
528 int saved_errno
= errno
;
530 (void) close (comm
[0]);
531 (void) close (comm
[1]);
532 (void) close (back_comm
[0]);
533 (void) close (back_comm
[1]);
543 from_parent_fd
= back_comm
[0];
545 mc_global
.we_are_background
= 1;
548 /* Make stdin/stdout/stderr point somewhere */
553 nullfd
= open ("/dev/null", O_RDWR
);
556 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
)
558 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
)
560 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
)
569 register_task_running (ctx
, pid
, comm
[0], back_comm
[1], info
);
574 /* --------------------------------------------------------------------------------------------- */
577 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
583 ret
= parent_va_call (routine
, (gpointer
) ctx
, argc
, ap
);
589 /* --------------------------------------------------------------------------------------------- */
592 parent_call_string (void *routine
, int argc
, ...)
598 str
= parent_va_call_string (routine
, argc
, ap
);
604 /* --------------------------------------------------------------------------------------------- */
608 background_parent_call (const gchar
* event_group_name
, const gchar
* event_name
,
609 gpointer init_data
, gpointer data
)
611 ev_background_parent_call_t
*event_data
= (ev_background_parent_call_t
*) data
;
613 (void) event_group_name
;
618 parent_va_call (event_data
->routine
, event_data
->ctx
, event_data
->argc
, event_data
->ap
);
623 /* --------------------------------------------------------------------------------------------- */
627 background_parent_call_string (const gchar
* event_group_name
, const gchar
* event_name
,
628 gpointer init_data
, gpointer data
)
630 ev_background_parent_call_t
*event_data
= (ev_background_parent_call_t
*) data
;
632 (void) event_group_name
;
637 parent_va_call_string (event_data
->routine
, event_data
->argc
, event_data
->ap
);
642 /* --------------------------------------------------------------------------------------------- */
644 #endif /* WITH_BACKGROUND */