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 "background.h"
49 #include "layout.h" /* repaint_screen() */
50 #include "fileopctx.h" /* FileOpContext */
51 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
59 /* If true, this is a background process */
60 int we_are_background
= 0;
62 /* File descriptor for talking to our parent */
65 /* File descriptor for messages from our parent */
66 static int from_parent_fd
;
68 #define MAXCALLARGS 4 /* Number of arguments supported */
70 struct TaskList
*task_list
= NULL
;
72 static int background_attention (int fd
, void *closure
);
75 register_task_running (FileOpContext
* ctx
, pid_t pid
, int fd
, int to_child
, char *info
)
79 new = g_new (TaskList
, 1);
82 new->state
= Task_Running
;
83 new->next
= task_list
;
85 new->to_child_fd
= to_child
;
88 add_select_channel (fd
, background_attention
, ctx
);
92 destroy_task_and_return_fd (pid_t pid
)
94 TaskList
*p
= task_list
;
102 prev
->next
= p
->next
;
118 unregister_task_running (pid_t pid
, int fd
)
120 destroy_task_and_return_fd (pid
);
121 delete_select_channel (fd
);
125 unregister_task_with_pid (pid_t pid
)
127 int fd
= destroy_task_and_return_fd (pid
);
129 delete_select_channel (fd
);
133 * Try to make the Midnight Commander a background job
141 do_background (struct FileOpContext
*ctx
, char *info
)
143 int comm
[2]; /* control connection stream */
144 int back_comm
[2]; /* back connection */
147 if (pipe (comm
) == -1)
150 if (pipe (back_comm
) == -1)
153 if ((pid
= fork ()) == -1)
155 int saved_errno
= errno
;
156 (void) close (comm
[0]);
157 (void) close (comm
[1]);
158 (void) close (back_comm
[0]);
159 (void) close (back_comm
[1]);
169 from_parent_fd
= back_comm
[0];
171 we_are_background
= 1;
174 /* Make stdin/stdout/stderr point somewhere */
179 if ((nullfd
= open ("/dev/null", O_RDWR
)) != -1)
181 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
);
182 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
);
183 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
);
191 register_task_running (ctx
, pid
, comm
[0], back_comm
[1], info
);
196 /* {{{ Parent handlers */
198 /* Parent/child protocol
200 * the child (the background) process send the following:
201 * void *routine -- routine to be invoked in the parent
202 * int nargc -- number of arguments
203 * int type -- Return argument type.
205 * If the routine is zero, then it is a way to tell the parent
206 * that the process is dying.
208 * nargc arguments in the following format:
209 * int size of the coming block
210 * size bytes with the block
212 * Now, the parent loads all those and then invokes
213 * the routine with pointers to the information passed
214 * (we just support pointers).
216 * If the return type is integer:
218 * the parent then writes an int to the child with
219 * the return value from the routine and the values
220 * of any global variable that is modified in the parent
221 * currently: do_append and recursive_result.
223 * If the return type is a string:
225 * the parent writes the resulting string length
226 * if the result string was NULL or the empty string,
227 * then the length is zero.
228 * The parent then writes the string length and frees
232 * Receive requests from background process and invoke the
237 background_attention (int fd
, void *closure
)
243 int (*have_ctx0
) (int);
244 int (*have_ctx1
) (int, char *);
245 int (*have_ctx2
) (int, char *, char *);
246 int (*have_ctx3
) (int, char *, char *, char *);
247 int (*have_ctx4
) (int, char *, char *, char *, char *);
249 int (*non_have_ctx0
) (FileOpContext
*, int);
250 int (*non_have_ctx1
) (FileOpContext
*, int, char *);
251 int (*non_have_ctx2
) (FileOpContext
*, int, char *, char *);
252 int (*non_have_ctx3
) (FileOpContext
*, int, char *, char *, char *);
253 int (*non_have_ctx4
) (FileOpContext
*, int, char *, char *, char *, char *);
255 char *(*ret_str0
) ();
256 char *(*ret_str1
) (char *);
257 char *(*ret_str2
) (char *, char *);
258 char *(*ret_str3
) (char *, char *, char *);
259 char *(*ret_str4
) (char *, char *, char *, char *);
264 int argc
, i
, result
, status
;
265 char *data
[MAXCALLARGS
];
268 int to_child_fd
= -1;
269 enum ReturnType type
;
273 bytes
= read (fd
, &routine
.pointer
, sizeof (routine
));
274 if (bytes
== -1 || (size_t) bytes
< (sizeof (routine
)))
276 const char *background_process_error
= _(" Background process error ");
278 unregister_task_running (ctx
->pid
, fd
);
279 if (!waitpid (ctx
->pid
, &status
, WNOHANG
))
281 /* the process is still running, but it misbehaves - kill it */
282 kill (ctx
->pid
, SIGTERM
);
283 message (D_ERROR
, background_process_error
, _(" Unknown error in child "));
287 /* 0 means happy end */
288 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
291 message (D_ERROR
, background_process_error
, _(" Child died unexpectedly "));
296 if ((read (fd
, &argc
, sizeof (argc
)) != sizeof (argc
)) ||
297 (read (fd
, &type
, sizeof (type
)) != sizeof (type
)) ||
298 (read (fd
, &have_ctx
, sizeof (have_ctx
)) != sizeof (have_ctx
)))
300 message (D_ERROR
, _(" Background protocol error "), _("Reading failed"));
304 if (argc
> MAXCALLARGS
)
306 message (D_ERROR
, _(" Background protocol error "),
307 _(" Background process sent us a request for more arguments \n"
308 " than we can handle. \n"));
313 if (read (fd
, ctx
, sizeof (FileOpContext
)) != sizeof (FileOpContext
))
315 message (D_ERROR
, _(" Background protocol error "), _("Reading failed"));
320 for (i
= 0; i
< argc
; i
++)
324 if (read (fd
, &size
, sizeof (size
)) != sizeof (size
))
326 message (D_ERROR
, _(" Background protocol error "), _("Reading failed"));
329 data
[i
] = g_malloc (size
+ 1);
330 if (read (fd
, data
[i
], size
) != size
)
332 message (D_ERROR
, _(" Background protocol error "), _("Reading failed"));
335 data
[i
][size
] = 0; /* NULL terminate the blocks (they could be strings) */
338 /* Find child task info by descriptor */
339 /* Find before call, because process can destroy self after */
340 for (p
= task_list
; p
; p
= p
->next
)
347 to_child_fd
= p
->to_child_fd
;
349 if (to_child_fd
== -1)
350 message (D_ERROR
, _(" Background process error "), _(" Unknown error in child "));
352 /* Handle the call */
353 if (type
== Return_Integer
)
359 result
= routine
.have_ctx0 (Background
);
362 result
= routine
.have_ctx1 (Background
, data
[0]);
365 result
= routine
.have_ctx2 (Background
, data
[0], data
[1]);
368 result
= routine
.have_ctx3 (Background
, data
[0], data
[1], data
[2]);
371 result
= routine
.have_ctx4 (Background
, data
[0], data
[1], data
[2], data
[3]);
378 result
= routine
.non_have_ctx0 (ctx
, Background
);
381 result
= routine
.non_have_ctx1 (ctx
, Background
, data
[0]);
384 result
= routine
.non_have_ctx2 (ctx
, Background
, data
[0], data
[1]);
387 result
= routine
.non_have_ctx3 (ctx
, Background
, data
[0], data
[1], data
[2]);
391 routine
.non_have_ctx4 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
395 /* Send the result code and the value for shared variables */
396 ret
= write (to_child_fd
, &result
, sizeof (int));
397 if (have_ctx
&& to_child_fd
!= -1)
398 ret
= write (to_child_fd
, ctx
, sizeof (FileOpContext
));
400 else if (type
== Return_String
)
405 /* FIXME: string routines should also use the Foreground/Background
406 * parameter. Currently, this is not used here
411 resstr
= routine
.ret_str0 ();
414 resstr
= routine
.ret_str1 (data
[0]);
417 resstr
= routine
.ret_str2 (data
[0], data
[1]);
420 resstr
= routine
.ret_str3 (data
[0], data
[1], data
[2]);
423 resstr
= routine
.ret_str4 (data
[0], data
[1], data
[2], data
[3]);
426 g_assert_not_reached ();
430 len
= strlen (resstr
);
431 ret
= write (to_child_fd
, &len
, sizeof (len
));
434 write (to_child_fd
, resstr
, len
);
441 ret
= write (to_child_fd
, &len
, sizeof (len
));
444 for (i
= 0; i
< argc
; i
++)
454 /* {{{ client RPC routines */
456 /* Sends the header for a call to a routine in the parent process. If the file
457 * operation context is not NULL, then it requests that the first parameter of
458 * the call be a file operation context.
461 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
* ctx
)
466 have_ctx
= (ctx
!= NULL
);
468 ret
= write (parent_fd
, &routine
, sizeof (routine
));
469 ret
= write (parent_fd
, &argc
, sizeof (int));
470 ret
= write (parent_fd
, &type
, sizeof (type
));
471 ret
= write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
474 ret
= write (parent_fd
, ctx
, sizeof (FileOpContext
));
478 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
485 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
486 for (i
= 0; i
< argc
; i
++)
491 len
= va_arg (ap
, int);
492 value
= va_arg (ap
, void *);
493 ret
= write (parent_fd
, &len
, sizeof (int));
494 ret
= write (parent_fd
, value
, len
);
497 ret
= read (from_parent_fd
, &i
, sizeof (int));
499 ret
= read (from_parent_fd
, ctx
, sizeof (FileOpContext
));
505 parent_call_string (void *routine
, int argc
, ...)
512 parent_call_header (routine
, argc
, Return_String
, NULL
);
513 for (i
= 0; i
< argc
; i
++)
518 len
= va_arg (ap
, int);
519 value
= va_arg (ap
, void *);
520 if ((write (parent_fd
, &len
, sizeof (int)) != sizeof (int)) ||
521 (write (parent_fd
, value
, len
) != len
))
524 if (read (from_parent_fd
, &i
, sizeof (int)) != sizeof (int))
528 str
= g_malloc (i
+ 1);
529 if (read (from_parent_fd
, str
, i
) != i
)
538 #endif /* WITH_BACKGROUND */