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
39 #include <sys/types.h>
44 #include "background.h"
45 #include "tty.h" /* doupdate() */
46 #include "dialog.h" /* do_refresh() */
48 #include "fileopctx.h" /* FileOpContext */
49 #include "key.h" /* add_select_channel(), delete_select_channel() */
56 /* If true, this is a background process */
57 int we_are_background
= 0;
59 /* File descriptor for talking to our parent */
62 #define MAXCALLARGS 4 /* Number of arguments supported */
64 struct TaskList
*task_list
= NULL
;
66 static int background_attention (int fd
, void *closure
);
69 register_task_running (FileOpContext
*ctx
, pid_t pid
, int fd
, char *info
)
73 new = g_new (TaskList
, 1);
76 new->state
= Task_Running
;
77 new->next
= task_list
;
81 add_select_channel (fd
, background_attention
, ctx
);
85 unregister_task_running (pid_t pid
, int fd
)
87 TaskList
*p
= task_list
;
103 delete_select_channel (fd
);
107 * Try to make the Midnight Commander a background job
115 do_background (struct FileOpContext
*ctx
, char *info
)
117 int comm
[2]; /* control connection stream */
120 if (pipe (comm
) == -1)
123 if ((pid
= fork ()) == -1) {
124 int saved_errno
= errno
;
125 (void) close (comm
[0]);
126 (void) close (comm
[1]);
136 we_are_background
= 1;
139 /* Make stdin/stdout/stderr point somewhere */
144 if ((nullfd
= open ("/dev/null", O_RDWR
)) != -1) {
145 while (dup2 (nullfd
, 0) == -1 && errno
== EINTR
);
146 while (dup2 (nullfd
, 1) == -1 && errno
== EINTR
);
147 while (dup2 (nullfd
, 2) == -1 && errno
== EINTR
);
154 register_task_running (ctx
, pid
, comm
[0], info
);
159 /* {{{ Parent handlers */
161 /* Parent/child protocol
163 * the child (the background) process send the following:
164 * void *routine -- routine to be invoked in the parent
165 * int nargc -- number of arguments
166 * int type -- Return argument type.
168 * If the routine is zero, then it is a way to tell the parent
169 * that the process is dying.
171 * nargc arguments in the following format:
172 * int size of the coming block
173 * size bytes with the block
175 * Now, the parent loads all those and then invokes
176 * the routine with pointers to the information passed
177 * (we just support pointers).
179 * If the return type is integer:
181 * the parent then writes an int to the child with
182 * the return value from the routine and the values
183 * of any global variable that is modified in the parent
184 * currently: do_append and recursive_result.
186 * If the return type is a string:
188 * the parent writes the resulting string length
189 * if the result string was NULL or the empty string,
190 * then the length is zero.
191 * The parent then writes the string length and frees
195 * Receive requests from background process and invoke the
200 background_attention (int fd
, void *closure
)
206 int (*have_ctx1
)(int, char *);
207 int (*have_ctx2
)(int, char *, char *);
208 int (*have_ctx3
)(int, char *, char *, char *);
209 int (*have_ctx4
)(int, char *, char *, char *, char *);
211 int (*non_have_ctx1
)(FileOpContext
*, int, char *);
212 int (*non_have_ctx2
)(FileOpContext
*, int, char *, char *);
213 int (*non_have_ctx3
)(FileOpContext
*, int, char *, char *, char *);
214 int (*non_have_ctx4
)(FileOpContext
*, int, char *, char *, char *, char *);
216 char * (*ret_str1
)(char *);
217 char * (*ret_str2
)(char *, char *);
218 char * (*ret_str3
)(char *, char *, char *);
219 char * (*ret_str4
)(char *, char *, char *, char *);
224 int argc
, i
, result
, status
;
225 char *data
[MAXCALLARGS
];
227 enum ReturnType type
;
231 bytes
= read (fd
, &routine
.pointer
, sizeof (routine
));
232 if (bytes
== -1 || (size_t) bytes
< (sizeof (routine
))) {
233 const char *background_process_error
= _(" Background process error ");
235 unregister_task_running (ctx
->pid
, fd
);
236 if (!waitpid (ctx
->pid
, &status
, WNOHANG
)) {
237 /* the process is still running, but it misbehaves - kill it */
238 kill (ctx
->pid
, SIGTERM
);
239 message (D_ERROR
, background_process_error
, _(" Unknown error in child "));
243 /* 0 means happy end */
244 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
247 message (D_ERROR
, background_process_error
, _(" Child died unexpectedly "));
252 read (fd
, &argc
, sizeof (argc
));
253 if (argc
> MAXCALLARGS
){
254 message (D_ERROR
, _(" Background protocol error "),
255 _(" Background process sent us a request for more arguments \n"
256 " than we can handle. \n"));
258 read (fd
, &type
, sizeof (type
));
259 read (fd
, &have_ctx
, sizeof (have_ctx
));
261 read (fd
, ctx
, sizeof (FileOpContext
));
263 for (i
= 0; i
< argc
; i
++){
266 read (fd
, &size
, sizeof (size
));
267 data
[i
] = g_malloc (size
+1);
268 read (fd
, data
[i
], size
);
270 data
[i
][size
] = 0; /* NULL terminate the blocks (they could be strings) */
273 /* Handle the call */
274 if (type
== Return_Integer
){
278 result
= routine
.have_ctx1 (Background
, data
[0]);
281 result
= routine
.have_ctx2 (Background
, data
[0], data
[1]);
284 result
= routine
.have_ctx3 (Background
, data
[0], data
[1], data
[2]);
287 result
= routine
.have_ctx4 (Background
, data
[0], data
[1], data
[2], data
[3]);
293 result
= routine
.non_have_ctx1 (ctx
, Background
, data
[0]);
296 result
= routine
.non_have_ctx2 (ctx
, Background
, data
[0], data
[1]);
299 result
= routine
.non_have_ctx3 (ctx
, Background
, data
[0], data
[1], data
[2]);
302 result
= routine
.non_have_ctx4 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
306 /* Send the result code and the value for shared variables */
307 write (fd
, &result
, sizeof (int));
309 write (fd
, ctx
, sizeof (FileOpContext
));
310 } else if (type
== Return_String
) {
314 /* FIXME: string routines should also use the Foreground/Background
315 * parameter. Currently, this is not used here
319 resstr
= routine
.ret_str1 (data
[0]);
322 resstr
= routine
.ret_str2 (data
[0], data
[1]);
325 resstr
= routine
.ret_str3 (data
[0], data
[1], data
[2]);
328 resstr
= routine
.ret_str4 (data
[0], data
[1], data
[2], data
[3]);
330 default: g_assert_not_reached();
333 len
= strlen (resstr
);
334 write (fd
, &len
, sizeof (len
));
336 write (fd
, resstr
, len
);
341 write (fd
, &len
, sizeof (len
));
344 for (i
= 0; i
< argc
; i
++)
356 /* {{{ client RPC routines */
358 /* Sends the header for a call to a routine in the parent process. If the file
359 * operation context is not NULL, then it requests that the first parameter of
360 * the call be a file operation context.
363 parent_call_header (void *routine
, int argc
, enum ReturnType type
, FileOpContext
*ctx
)
367 have_ctx
= (ctx
!= NULL
);
369 write (parent_fd
, &routine
, sizeof (routine
));
370 write (parent_fd
, &argc
, sizeof (int));
371 write (parent_fd
, &type
, sizeof (type
));
372 write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
375 write (parent_fd
, ctx
, sizeof (FileOpContext
));
379 parent_call (void *routine
, struct FileOpContext
*ctx
, int argc
, ...)
385 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
386 for (i
= 0; i
< argc
; i
++) {
390 len
= va_arg (ap
, int);
391 value
= va_arg (ap
, void *);
392 write (parent_fd
, &len
, sizeof (int));
393 write (parent_fd
, value
, len
);
395 read (parent_fd
, &i
, sizeof (int));
397 read (parent_fd
, ctx
, sizeof (FileOpContext
));
403 parent_call_string (void *routine
, int argc
, ...)
410 parent_call_header (routine
, argc
, Return_String
, NULL
);
411 for (i
= 0; i
< argc
; i
++){
415 len
= va_arg (ap
, int);
416 value
= va_arg (ap
, void *);
417 write (parent_fd
, &len
, sizeof (int));
418 write (parent_fd
, value
, len
);
420 read (parent_fd
, &i
, sizeof (int));
423 str
= g_malloc (i
+ 1);
424 read (parent_fd
, str
, i
);
429 #endif /* WITH_BACKGROUND */