Merge branch '54_dlg_mouse_handling'
[midnight-commander.git] / src / background.c
blob4bd2d2cc52512e8418d270b82462617d8453adf1
1 /* {{{ Copyright */
3 /* Background support.
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. */
23 /* }}} */
25 /** \file background.c
26 * \brief Source: Background support
29 #include <config.h>
31 #ifdef WITH_BACKGROUND
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h> /* waitpid() */
43 #include <unistd.h>
44 #include <fcntl.h>
46 #include "global.h"
47 #include "background.h"
48 #include "wtools.h"
49 #include "layout.h" /* repaint_screen() */
50 #include "fileopctx.h" /* FileOpContext */
51 #include "../src/tty/key.h" /* add_select_channel(), delete_select_channel() */
53 enum ReturnType {
54 Return_String,
55 Return_Integer
58 /* If true, this is a background process */
59 int we_are_background = 0;
61 /* File descriptor for talking to our parent */
62 static int parent_fd;
64 #define MAXCALLARGS 4 /* Number of arguments supported */
66 struct TaskList *task_list = NULL;
68 static int background_attention (int fd, void *closure);
70 static void
71 register_task_running (FileOpContext *ctx, pid_t pid, int fd, char *info)
73 TaskList *new;
75 new = g_new (TaskList, 1);
76 new->pid = pid;
77 new->info = info;
78 new->state = Task_Running;
79 new->next = task_list;
80 new->fd = fd;
81 task_list = new;
83 add_select_channel (fd, background_attention, ctx);
86 void
87 unregister_task_running (pid_t pid, int fd)
89 TaskList *p = task_list;
90 TaskList *prev = 0;
92 while (p){
93 if (p->pid == pid){
94 if (prev)
95 prev->next = p->next;
96 else
97 task_list = p->next;
98 g_free (p->info);
99 g_free (p);
100 break;
102 prev = p;
103 p = p->next;
105 delete_select_channel (fd);
109 * Try to make the Midnight Commander a background job
111 * Returns:
112 * 1 for parent
113 * 0 for child
114 * -1 on failure
117 do_background (struct FileOpContext *ctx, char *info)
119 int comm[2]; /* control connection stream */
120 pid_t pid;
122 if (pipe (comm) == -1)
123 return -1;
125 if ((pid = fork ()) == -1) {
126 int saved_errno = errno;
127 (void) close (comm[0]);
128 (void) close (comm[1]);
129 errno = saved_errno;
130 return -1;
133 if (pid == 0) {
134 int nullfd;
136 close (comm[0]);
137 parent_fd = comm[1];
138 we_are_background = 1;
139 current_dlg = NULL;
141 /* Make stdin/stdout/stderr point somewhere */
142 close (0);
143 close (1);
144 close (2);
146 if ((nullfd = open ("/dev/null", O_RDWR)) != -1) {
147 while (dup2 (nullfd, 0) == -1 && errno == EINTR);
148 while (dup2 (nullfd, 1) == -1 && errno == EINTR);
149 while (dup2 (nullfd, 2) == -1 && errno == EINTR);
152 return 0;
153 } else {
154 close (comm[1]);
155 ctx->pid = pid;
156 register_task_running (ctx, pid, comm[0], info);
157 return 1;
161 /* {{{ Parent handlers */
163 /* Parent/child protocol
165 * the child (the background) process send the following:
166 * void *routine -- routine to be invoked in the parent
167 * int nargc -- number of arguments
168 * int type -- Return argument type.
170 * If the routine is zero, then it is a way to tell the parent
171 * that the process is dying.
173 * nargc arguments in the following format:
174 * int size of the coming block
175 * size bytes with the block
177 * Now, the parent loads all those and then invokes
178 * the routine with pointers to the information passed
179 * (we just support pointers).
181 * If the return type is integer:
183 * the parent then writes an int to the child with
184 * the return value from the routine and the values
185 * of any global variable that is modified in the parent
186 * currently: do_append and recursive_result.
188 * If the return type is a string:
190 * the parent writes the resulting string length
191 * if the result string was NULL or the empty string,
192 * then the length is zero.
193 * The parent then writes the string length and frees
194 * the result string.
197 * Receive requests from background process and invoke the
198 * specified routine
201 static int
202 background_attention (int fd, void *closure)
204 FileOpContext *ctx;
205 int have_ctx;
206 union
208 int (*have_ctx1)(int, char *);
209 int (*have_ctx2)(int, char *, char *);
210 int (*have_ctx3)(int, char *, char *, char *);
211 int (*have_ctx4)(int, char *, char *, char *, char *);
213 int (*non_have_ctx1)(FileOpContext *, int, char *);
214 int (*non_have_ctx2)(FileOpContext *, int, char *, char *);
215 int (*non_have_ctx3)(FileOpContext *, int, char *, char *, char *);
216 int (*non_have_ctx4)(FileOpContext *, int, char *, char *, char *, char *);
218 char * (*ret_str1)(char *);
219 char * (*ret_str2)(char *, char *);
220 char * (*ret_str3)(char *, char *, char *);
221 char * (*ret_str4)(char *, char *, char *, char *);
223 void *pointer;
224 } routine;
225 /* void *routine;*/
226 int argc, i, result, status;
227 char *data [MAXCALLARGS];
228 ssize_t bytes;
229 enum ReturnType type;
231 ctx = closure;
233 bytes = read (fd, &routine.pointer, sizeof (routine));
234 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) {
235 const char *background_process_error = _(" Background process error ");
237 unregister_task_running (ctx->pid, fd);
238 if (!waitpid (ctx->pid, &status, WNOHANG)) {
239 /* the process is still running, but it misbehaves - kill it */
240 kill (ctx->pid, SIGTERM);
241 message (D_ERROR, background_process_error, _(" Unknown error in child "));
242 return 0;
245 /* 0 means happy end */
246 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
247 return 0;
249 message (D_ERROR, background_process_error, _(" Child died unexpectedly "));
251 return 0;
254 read (fd, &argc, sizeof (argc));
255 if (argc > MAXCALLARGS){
256 message (D_ERROR, _(" Background protocol error "),
257 _(" Background process sent us a request for more arguments \n"
258 " than we can handle. \n"));
260 read (fd, &type, sizeof (type));
261 read (fd, &have_ctx, sizeof (have_ctx));
262 if (have_ctx)
263 read (fd, ctx, sizeof (FileOpContext));
265 for (i = 0; i < argc; i++){
266 int size;
268 read (fd, &size, sizeof (size));
269 data [i] = g_malloc (size+1);
270 read (fd, data [i], size);
272 data [i][size] = 0; /* NULL terminate the blocks (they could be strings) */
275 /* Handle the call */
276 if (type == Return_Integer){
277 if (!have_ctx)
278 switch (argc){
279 case 1:
280 result = routine.have_ctx1 (Background, data [0]);
281 break;
282 case 2:
283 result = routine.have_ctx2 (Background, data [0], data [1]);
284 break;
285 case 3:
286 result = routine.have_ctx3 (Background, data [0], data [1], data [2]);
287 break;
288 case 4:
289 result = routine.have_ctx4 (Background, data [0], data [1], data [2], data [3]);
290 break;
292 else
293 switch (argc){
294 case 1:
295 result = routine.non_have_ctx1 (ctx, Background, data [0]);
296 break;
297 case 2:
298 result = routine.non_have_ctx2 (ctx, Background, data [0], data [1]);
299 break;
300 case 3:
301 result = routine.non_have_ctx3 (ctx, Background, data [0], data [1], data [2]);
302 break;
303 case 4:
304 result = routine.non_have_ctx4 (ctx, Background, data [0], data [1], data [2], data [3]);
305 break;
308 /* Send the result code and the value for shared variables */
309 write (fd, &result, sizeof (int));
310 if (have_ctx)
311 write (fd, ctx, sizeof (FileOpContext));
312 } else if (type == Return_String) {
313 int len;
314 char *resstr = NULL;
316 /* FIXME: string routines should also use the Foreground/Background
317 * parameter. Currently, this is not used here
319 switch (argc){
320 case 1:
321 resstr = routine.ret_str1 (data [0]);
322 break;
323 case 2:
324 resstr = routine.ret_str2 (data [0], data [1]);
325 break;
326 case 3:
327 resstr = routine.ret_str3 (data [0], data [1], data [2]);
328 break;
329 case 4:
330 resstr = routine.ret_str4 (data [0], data [1], data [2], data [3]);
331 break;
332 default: g_assert_not_reached();
334 if (resstr){
335 len = strlen (resstr);
336 write (fd, &len, sizeof (len));
337 if (len){
338 write (fd, resstr, len);
339 g_free (resstr);
341 } else {
342 len = 0;
343 write (fd, &len, sizeof (len));
346 for (i = 0; i < argc; i++)
347 g_free (data [i]);
349 repaint_screen ();
350 return 0;
354 /* }}} */
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.
362 static void
363 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
365 int have_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));
374 if (have_ctx)
375 write (parent_fd, ctx, sizeof (FileOpContext));
379 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
381 va_list ap;
382 int i;
384 va_start (ap, argc);
385 parent_call_header (routine, argc, Return_Integer, ctx);
386 for (i = 0; i < argc; i++) {
387 int len;
388 void *value;
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));
396 if (ctx)
397 read (parent_fd, ctx, sizeof (FileOpContext));
399 return i;
402 char *
403 parent_call_string (void *routine, int argc, ...)
405 va_list ap;
406 char *str;
407 int i;
409 va_start (ap, argc);
410 parent_call_header (routine, argc, Return_String, NULL);
411 for (i = 0; i < argc; i++){
412 int len;
413 void *value;
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));
421 if (!i)
422 return NULL;
423 str = g_malloc (i + 1);
424 read (parent_fd, str, i);
425 str [i] = 0;
426 return str;
429 #endif /* WITH_BACKGROUND */