4 Copyright (C) 1996 The Free Software Foundation
6 Written by: 1996 Miguel de Icaza
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #ifdef WITH_BACKGROUND
34 #include <sys/types.h>
39 #include "background.h"
40 #include "tty.h" /* doupdate() */
41 #include "dialog.h" /* do_refresh() */
43 #include "fileopctx.h" /* FileOpContext */
44 #include "key.h" /* add_select_channel(), delete_select_channel() */
51 /* If true, this is a background process */
52 int we_are_background
= 0;
54 /* File descriptor for talking to our parent */
57 #define MAXCALLARGS 4 /* Number of arguments supported */
59 struct TaskList
*task_list
= NULL
;
61 static int background_attention (int fd
, void *closure
);
64 register_task_running (FileOpContext
*ctx
, pid_t pid
, int fd
, char *info
)
68 new = g_new (TaskList
, 1);
71 new->state
= Task_Running
;
72 new->next
= task_list
;
76 add_select_channel (fd
, background_attention
, ctx
);
80 unregister_task_running (pid_t pid
, int fd
)
82 TaskList
*p
= task_list
;
98 delete_select_channel (fd
);
102 * Try to make the Midnight Commander a background job
110 do_background (struct FileOpContext
*ctx
, char *info
)
112 int comm
[2]; /* control connection stream */
115 if (pipe (comm
) == -1)
118 if ((pid
= fork ()) == -1) {
119 int saved_errno
= errno
;
120 (void) close (comm
[0]);
121 (void) close (comm
[1]);
131 we_are_background
= 1;
134 /* Make stdin/stdout/stderr point somewhere */
139 if ((nullfd
= open ("/dev/null", O_RDWR
)) != -1) {
140 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
);
141 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
);
142 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
);
149 register_task_running (ctx
, pid
, comm
[0], info
);
154 /* {{{ Parent handlers */
156 /* Parent/child protocol
158 * the child (the background) process send the following:
159 * void *routine -- routine to be invoked in the parent
160 * int nargc -- number of arguments
161 * int type -- Return argument type.
163 * If the routine is zero, then it is a way to tell the parent
164 * that the process is dying.
166 * nargc arguments in the following format:
167 * int size of the coming block
168 * size bytes with the block
170 * Now, the parent loads all those and then invokes
171 * the routine with pointers to the information passed
172 * (we just support pointers).
174 * If the return type is integer:
176 * the parent then writes an int to the child with
177 * the return value from the routine and the values
178 * of any global variable that is modified in the parent
179 * currently: do_append and recursive_result.
181 * If the return type is a string:
183 * the parent writes the resulting string length
184 * if the result string was NULL or the empty string,
185 * then the length is zero.
186 * The parent then writes the string length and frees
190 * Receive requests from background process and invoke the
195 background_attention (int fd
, void *closure
)
200 int argc
, i
, result
, status
;
201 char *data
[MAXCALLARGS
];
203 enum ReturnType type
;
207 bytes
= read (fd
, &routine
, sizeof (routine
));
208 if (bytes
== -1 || (size_t) bytes
< (sizeof (routine
))) {
209 const char *background_process_error
= _(" Background process error ");
211 unregister_task_running (ctx
->pid
, fd
);
212 if (!waitpid (ctx
->pid
, &status
, WNOHANG
)) {
213 /* the process is still running, but it misbehaves - kill it */
214 kill (ctx
->pid
, SIGTERM
);
215 message (1, background_process_error
, _(" Unknown error in child "));
219 /* 0 means happy end */
220 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
223 message (1, background_process_error
, _(" Child died unexpectedly "));
228 read (fd
, &argc
, sizeof (argc
));
229 if (argc
> MAXCALLARGS
){
230 message (1, _(" Background protocol error "),
231 _(" Background process sent us a request for more arguments \n"
232 " than we can handle. \n"));
234 read (fd
, &type
, sizeof (type
));
235 read (fd
, &have_ctx
, sizeof (have_ctx
));
237 read (fd
, ctx
, sizeof (FileOpContext
));
239 for (i
= 0; i
< argc
; i
++){
242 read (fd
, &size
, sizeof (size
));
243 data
[i
] = g_malloc (size
+1);
244 read (fd
, data
[i
], size
);
246 data
[i
][size
] = 0; /* NULL terminate the blocks (they could be strings) */
249 /* Handle the call */
250 if (type
== Return_Integer
){
254 result
= (*(int (*)(int, char *))routine
)(Background
, data
[0]);
257 result
= (*(int (*)(int, char *, char *))routine
)
258 (Background
, data
[0], data
[1]);
261 result
= (*(int (*)(int, char *, char *, char *))routine
)
262 (Background
, data
[0], data
[1], data
[2]);
265 result
= (*(int (*)(int, char *, char *, char *, char *))routine
)
266 (Background
, data
[0], data
[1], data
[2], data
[3]);
272 result
= (*(int (*)(FileOpContext
*, int, char *))routine
)
273 (ctx
, Background
, data
[0]);
276 result
= (*(int (*)(FileOpContext
*, int, char *, char *))routine
)
277 (ctx
, Background
, data
[0], data
[1]);
280 result
= (*(int (*)(FileOpContext
*, int, char *, char *, char *))routine
)
281 (ctx
, Background
, data
[0], data
[1], data
[2]);
284 result
= (*(int (*)(FileOpContext
*, int, char *, char *, char *, char *))routine
)
285 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
289 /* Send the result code and the value for shared variables */
290 write (fd
, &result
, sizeof (int));
292 write (fd
, ctx
, sizeof (FileOpContext
));
293 } else if (type
== Return_String
) {
297 /* FIXME: string routines should also use the Foreground/Background
298 * parameter. Currently, this is not used here
302 resstr
= (*(char * (*)(char *))routine
)(data
[0]);
305 resstr
= (*(char * (*)(char *, char *))routine
)
306 (data
[0], data
[1]);
309 resstr
= (*(char * (*)(char *, char *, char *))routine
)
310 (data
[0], data
[1], data
[2]);
313 resstr
= (*(char * (*)(char *, char *, char *, char *))routine
)
314 (data
[0], data
[1], data
[2], data
[3]);
316 default: g_assert_not_reached();
319 len
= strlen (resstr
);
320 write (fd
, &len
, sizeof (len
));
322 write (fd
, resstr
, len
);
327 write (fd
, &len
, sizeof (len
));
330 for (i
= 0; i
< argc
; i
++)
342 /* {{{ client RPC routines */
344 /* Sends the header for a call to a routine in the parent process. If the file
345 * operation context is not NULL, then it requests that the first parameter of
346 * the call be a file operation context.
349 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
*ctx
)
353 have_ctx
= (ctx
!= NULL
);
355 write (parent_fd
, &routine
, sizeof (routine
));
356 write (parent_fd
, &argc
, sizeof (int));
357 write (parent_fd
, &type
, sizeof (type
));
358 write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
361 write (parent_fd
, ctx
, sizeof (FileOpContext
));
365 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
371 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
372 for (i
= 0; i
< argc
; i
++) {
376 len
= va_arg (ap
, int);
377 value
= va_arg (ap
, void *);
378 write (parent_fd
, &len
, sizeof (int));
379 write (parent_fd
, value
, len
);
381 read (parent_fd
, &i
, sizeof (int));
383 read (parent_fd
, ctx
, sizeof (FileOpContext
));
389 parent_call_string (void *routine
, int argc
, ...)
396 parent_call_header (routine
, argc
, Return_String
, NULL
);
397 for (i
= 0; i
< argc
; i
++){
401 len
= va_arg (ap
, int);
402 value
= va_arg (ap
, void *);
403 write (parent_fd
, &len
, sizeof (int));
404 write (parent_fd
, value
, len
);
406 read (parent_fd
, &i
, sizeof (int));
409 str
= g_malloc (i
+ 1);
410 read (parent_fd
, str
, i
);
415 #endif /* WITH_BACKGROUND */