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. */
27 #ifdef WITH_BACKGROUND
35 #include <sys/types.h>
40 #include "background.h"
41 #include "tty.h" /* doupdate() */
42 #include "dialog.h" /* do_refresh() */
44 #include "fileopctx.h" /* FileOpContext */
45 #include "key.h" /* add_select_channel(), delete_select_channel() */
52 /* If true, this is a background process */
53 int we_are_background
= 0;
55 /* File descriptor for talking to our parent */
58 #define MAXCALLARGS 4 /* Number of arguments supported */
60 struct TaskList
*task_list
= NULL
;
62 static int background_attention (int fd
, void *closure
);
65 register_task_running (FileOpContext
*ctx
, pid_t pid
, int fd
, char *info
)
69 new = g_new (TaskList
, 1);
72 new->state
= Task_Running
;
73 new->next
= task_list
;
77 add_select_channel (fd
, background_attention
, ctx
);
81 unregister_task_running (pid_t pid
, int fd
)
83 TaskList
*p
= task_list
;
99 delete_select_channel (fd
);
103 * Try to make the Midnight Commander a background job
111 do_background (struct FileOpContext
*ctx
, char *info
)
113 int comm
[2]; /* control connection stream */
116 if (pipe (comm
) == -1)
119 if ((pid
= fork ()) == -1) {
120 int saved_errno
= errno
;
121 (void) close (comm
[0]);
122 (void) close (comm
[1]);
132 we_are_background
= 1;
135 /* Make stdin/stdout/stderr point somewhere */
140 if ((nullfd
= open ("/dev/null", O_RDWR
)) != -1) {
141 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
);
142 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
);
143 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
);
150 register_task_running (ctx
, pid
, comm
[0], info
);
155 /* {{{ Parent handlers */
157 /* Parent/child protocol
159 * the child (the background) process send the following:
160 * void *routine -- routine to be invoked in the parent
161 * int nargc -- number of arguments
162 * int type -- Return argument type.
164 * If the routine is zero, then it is a way to tell the parent
165 * that the process is dying.
167 * nargc arguments in the following format:
168 * int size of the coming block
169 * size bytes with the block
171 * Now, the parent loads all those and then invokes
172 * the routine with pointers to the information passed
173 * (we just support pointers).
175 * If the return type is integer:
177 * the parent then writes an int to the child with
178 * the return value from the routine and the values
179 * of any global variable that is modified in the parent
180 * currently: do_append and recursive_result.
182 * If the return type is a string:
184 * the parent writes the resulting string length
185 * if the result string was NULL or the empty string,
186 * then the length is zero.
187 * The parent then writes the string length and frees
191 * Receive requests from background process and invoke the
196 background_attention (int fd
, void *closure
)
201 int argc
, i
, result
, status
;
202 char *data
[MAXCALLARGS
];
204 enum ReturnType type
;
208 bytes
= read (fd
, &routine
, sizeof (routine
));
209 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
)) {
214 /* the process is still running, but it misbehaves - kill it */
215 kill (ctx
->pid
, SIGTERM
);
216 message (1, background_process_error
, _(" Unknown error in child "));
220 /* 0 means happy end */
221 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
224 message (1, background_process_error
, _(" Child died unexpectedly "));
229 read (fd
, &argc
, sizeof (argc
));
230 if (argc
> MAXCALLARGS
){
231 message (1, _(" Background protocol error "),
232 _(" Background process sent us a request for more arguments \n"
233 " than we can handle. \n"));
235 read (fd
, &type
, sizeof (type
));
236 read (fd
, &have_ctx
, sizeof (have_ctx
));
238 read (fd
, ctx
, sizeof (FileOpContext
));
240 for (i
= 0; i
< argc
; i
++){
243 read (fd
, &size
, sizeof (size
));
244 data
[i
] = g_malloc (size
+1);
245 read (fd
, data
[i
], size
);
247 data
[i
][size
] = 0; /* NULL terminate the blocks (they could be strings) */
250 /* Handle the call */
251 if (type
== Return_Integer
){
255 result
= (*(int (*)(int, char *))routine
)(Background
, data
[0]);
258 result
= (*(int (*)(int, char *, char *))routine
)
259 (Background
, data
[0], data
[1]);
262 result
= (*(int (*)(int, char *, char *, char *))routine
)
263 (Background
, data
[0], data
[1], data
[2]);
266 result
= (*(int (*)(int, char *, char *, char *, char *))routine
)
267 (Background
, data
[0], data
[1], data
[2], data
[3]);
273 result
= (*(int (*)(FileOpContext
*, int, char *))routine
)
274 (ctx
, Background
, data
[0]);
277 result
= (*(int (*)(FileOpContext
*, int, char *, char *))routine
)
278 (ctx
, Background
, data
[0], data
[1]);
281 result
= (*(int (*)(FileOpContext
*, int, char *, char *, char *))routine
)
282 (ctx
, Background
, data
[0], data
[1], data
[2]);
285 result
= (*(int (*)(FileOpContext
*, int, char *, char *, char *, char *))routine
)
286 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
290 /* Send the result code and the value for shared variables */
291 write (fd
, &result
, sizeof (int));
293 write (fd
, ctx
, sizeof (FileOpContext
));
294 } else if (type
== Return_String
) {
298 /* FIXME: string routines should also use the Foreground/Background
299 * parameter. Currently, this is not used here
303 resstr
= (*(char * (*)(char *))routine
)(data
[0]);
306 resstr
= (*(char * (*)(char *, char *))routine
)
307 (data
[0], data
[1]);
310 resstr
= (*(char * (*)(char *, char *, char *))routine
)
311 (data
[0], data
[1], data
[2]);
314 resstr
= (*(char * (*)(char *, char *, char *, char *))routine
)
315 (data
[0], data
[1], data
[2], data
[3]);
317 default: g_assert_not_reached();
320 len
= strlen (resstr
);
321 write (fd
, &len
, sizeof (len
));
323 write (fd
, resstr
, len
);
328 write (fd
, &len
, sizeof (len
));
331 for (i
= 0; i
< argc
; i
++)
343 /* {{{ client RPC routines */
345 /* Sends the header for a call to a routine in the parent process. If the file
346 * operation context is not NULL, then it requests that the first parameter of
347 * the call be a file operation context.
350 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
*ctx
)
354 have_ctx
= (ctx
!= NULL
);
356 write (parent_fd
, &routine
, sizeof (routine
));
357 write (parent_fd
, &argc
, sizeof (int));
358 write (parent_fd
, &type
, sizeof (type
));
359 write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
362 write (parent_fd
, ctx
, sizeof (FileOpContext
));
366 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
372 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
373 for (i
= 0; i
< argc
; i
++) {
377 len
= va_arg (ap
, int);
378 value
= va_arg (ap
, void *);
379 write (parent_fd
, &len
, sizeof (int));
380 write (parent_fd
, value
, len
);
382 read (parent_fd
, &i
, sizeof (int));
384 read (parent_fd
, ctx
, sizeof (FileOpContext
));
390 parent_call_string (void *routine
, int argc
, ...)
397 parent_call_header (routine
, argc
, Return_String
, NULL
);
398 for (i
= 0; i
< argc
; i
++){
402 len
= va_arg (ap
, int);
403 value
= va_arg (ap
, void *);
404 write (parent_fd
, &len
, sizeof (int));
405 write (parent_fd
, value
, len
);
407 read (parent_fd
, &i
, sizeof (int));
410 str
= g_malloc (i
+ 1);
411 read (parent_fd
, str
, i
);
416 #endif /* WITH_BACKGROUND */