Ticket #1395 (Copying to fish is broken)
[midnight-commander.git] / src / background.c
blob9eadd29f24cee68d8761d2526aa1cd6d937d1be8
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 "tty.h" /* doupdate() */
49 #include "dialog.h" /* do_refresh() */
50 #include "wtools.h"
51 #include "fileopctx.h" /* FileOpContext */
52 #include "key.h" /* add_select_channel(), delete_select_channel() */
54 enum ReturnType {
55 Return_String,
56 Return_Integer
59 /* If true, this is a background process */
60 int we_are_background = 0;
62 /* File descriptor for talking to our parent */
63 static int parent_fd;
65 #define MAXCALLARGS 4 /* Number of arguments supported */
67 struct TaskList *task_list = NULL;
69 static int background_attention (int fd, void *closure);
71 static void
72 register_task_running (FileOpContext *ctx, pid_t pid, int fd, char *info)
74 TaskList *new;
76 new = g_new (TaskList, 1);
77 new->pid = pid;
78 new->info = info;
79 new->state = Task_Running;
80 new->next = task_list;
81 new->fd = fd;
82 task_list = new;
84 add_select_channel (fd, background_attention, ctx);
87 void
88 unregister_task_running (pid_t pid, int fd)
90 TaskList *p = task_list;
91 TaskList *prev = 0;
93 while (p){
94 if (p->pid == pid){
95 if (prev)
96 prev->next = p->next;
97 else
98 task_list = p->next;
99 g_free (p->info);
100 g_free (p);
101 break;
103 prev = p;
104 p = p->next;
106 delete_select_channel (fd);
110 * Try to make the Midnight Commander a background job
112 * Returns:
113 * 1 for parent
114 * 0 for child
115 * -1 on failure
118 do_background (struct FileOpContext *ctx, char *info)
120 int comm[2]; /* control connection stream */
121 pid_t pid;
123 if (pipe (comm) == -1)
124 return -1;
126 if ((pid = fork ()) == -1) {
127 int saved_errno = errno;
128 (void) close (comm[0]);
129 (void) close (comm[1]);
130 errno = saved_errno;
131 return -1;
134 if (pid == 0) {
135 int nullfd;
137 close (comm[0]);
138 parent_fd = comm[1];
139 we_are_background = 1;
140 current_dlg = NULL;
142 /* Make stdin/stdout/stderr point somewhere */
143 close (0);
144 close (1);
145 close (2);
147 if ((nullfd = open ("/dev/null", O_RDWR)) != -1) {
148 while (dup2 (nullfd, 0) == -1 && errno == EINTR);
149 while (dup2 (nullfd, 1) == -1 && errno == EINTR);
150 while (dup2 (nullfd, 2) == -1 && errno == EINTR);
153 return 0;
154 } else {
155 close (comm[1]);
156 ctx->pid = pid;
157 register_task_running (ctx, pid, comm[0], info);
158 return 1;
162 /* {{{ Parent handlers */
164 /* Parent/child protocol
166 * the child (the background) process send the following:
167 * void *routine -- routine to be invoked in the parent
168 * int nargc -- number of arguments
169 * int type -- Return argument type.
171 * If the routine is zero, then it is a way to tell the parent
172 * that the process is dying.
174 * nargc arguments in the following format:
175 * int size of the coming block
176 * size bytes with the block
178 * Now, the parent loads all those and then invokes
179 * the routine with pointers to the information passed
180 * (we just support pointers).
182 * If the return type is integer:
184 * the parent then writes an int to the child with
185 * the return value from the routine and the values
186 * of any global variable that is modified in the parent
187 * currently: do_append and recursive_result.
189 * If the return type is a string:
191 * the parent writes the resulting string length
192 * if the result string was NULL or the empty string,
193 * then the length is zero.
194 * The parent then writes the string length and frees
195 * the result string.
198 * Receive requests from background process and invoke the
199 * specified routine
202 static int
203 background_attention (int fd, void *closure)
205 FileOpContext *ctx;
206 int have_ctx;
207 union
209 int (*have_ctx1)(int, char *);
210 int (*have_ctx2)(int, char *, char *);
211 int (*have_ctx3)(int, char *, char *, char *);
212 int (*have_ctx4)(int, char *, char *, char *, char *);
214 int (*non_have_ctx1)(FileOpContext *, int, char *);
215 int (*non_have_ctx2)(FileOpContext *, int, char *, char *);
216 int (*non_have_ctx3)(FileOpContext *, int, char *, char *, char *);
217 int (*non_have_ctx4)(FileOpContext *, int, char *, char *, char *, char *);
219 char * (*ret_str1)(char *);
220 char * (*ret_str2)(char *, char *);
221 char * (*ret_str3)(char *, char *, char *);
222 char * (*ret_str4)(char *, char *, char *, char *);
224 void *pointer;
225 } routine;
226 /* void *routine;*/
227 int argc, i, result, status;
228 char *data [MAXCALLARGS];
229 ssize_t bytes;
230 enum ReturnType type;
232 ctx = closure;
234 bytes = read (fd, &routine.pointer, sizeof (routine));
235 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) {
236 const char *background_process_error = _(" Background process error ");
238 unregister_task_running (ctx->pid, fd);
239 if (!waitpid (ctx->pid, &status, WNOHANG)) {
240 /* the process is still running, but it misbehaves - kill it */
241 kill (ctx->pid, SIGTERM);
242 message (D_ERROR, background_process_error, _(" Unknown error in child "));
243 return 0;
246 /* 0 means happy end */
247 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
248 return 0;
250 message (D_ERROR, background_process_error, _(" Child died unexpectedly "));
252 return 0;
255 read (fd, &argc, sizeof (argc));
256 if (argc > MAXCALLARGS){
257 message (D_ERROR, _(" Background protocol error "),
258 _(" Background process sent us a request for more arguments \n"
259 " than we can handle. \n"));
261 read (fd, &type, sizeof (type));
262 read (fd, &have_ctx, sizeof (have_ctx));
263 if (have_ctx)
264 read (fd, ctx, sizeof (FileOpContext));
266 for (i = 0; i < argc; i++){
267 int size;
269 read (fd, &size, sizeof (size));
270 data [i] = g_malloc (size+1);
271 read (fd, data [i], size);
273 data [i][size] = 0; /* NULL terminate the blocks (they could be strings) */
276 /* Handle the call */
277 if (type == Return_Integer){
278 if (!have_ctx)
279 switch (argc){
280 case 1:
281 result = routine.have_ctx1 (Background, data [0]);
282 break;
283 case 2:
284 result = routine.have_ctx2 (Background, data [0], data [1]);
285 break;
286 case 3:
287 result = routine.have_ctx3 (Background, data [0], data [1], data [2]);
288 break;
289 case 4:
290 result = routine.have_ctx4 (Background, data [0], data [1], data [2], data [3]);
291 break;
293 else
294 switch (argc){
295 case 1:
296 result = routine.non_have_ctx1 (ctx, Background, data [0]);
297 break;
298 case 2:
299 result = routine.non_have_ctx2 (ctx, Background, data [0], data [1]);
300 break;
301 case 3:
302 result = routine.non_have_ctx3 (ctx, Background, data [0], data [1], data [2]);
303 break;
304 case 4:
305 result = routine.non_have_ctx4 (ctx, Background, data [0], data [1], data [2], data [3]);
306 break;
309 /* Send the result code and the value for shared variables */
310 write (fd, &result, sizeof (int));
311 if (have_ctx)
312 write (fd, ctx, sizeof (FileOpContext));
313 } else if (type == Return_String) {
314 int len;
315 char *resstr = NULL;
317 /* FIXME: string routines should also use the Foreground/Background
318 * parameter. Currently, this is not used here
320 switch (argc){
321 case 1:
322 resstr = routine.ret_str1 (data [0]);
323 break;
324 case 2:
325 resstr = routine.ret_str2 (data [0], data [1]);
326 break;
327 case 3:
328 resstr = routine.ret_str3 (data [0], data [1], data [2]);
329 break;
330 case 4:
331 resstr = routine.ret_str4 (data [0], data [1], data [2], data [3]);
332 break;
333 default: g_assert_not_reached();
335 if (resstr){
336 len = strlen (resstr);
337 write (fd, &len, sizeof (len));
338 if (len){
339 write (fd, resstr, len);
340 g_free (resstr);
342 } else {
343 len = 0;
344 write (fd, &len, sizeof (len));
347 for (i = 0; i < argc; i++)
348 g_free (data [i]);
350 do_refresh ();
351 mc_refresh ();
352 doupdate ();
353 return 0;
357 /* }}} */
359 /* {{{ client RPC routines */
361 /* Sends the header for a call to a routine in the parent process. If the file
362 * operation context is not NULL, then it requests that the first parameter of
363 * the call be a file operation context.
365 static void
366 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
368 int have_ctx;
370 have_ctx = (ctx != NULL);
372 write (parent_fd, &routine, sizeof (routine));
373 write (parent_fd, &argc, sizeof (int));
374 write (parent_fd, &type, sizeof (type));
375 write (parent_fd, &have_ctx, sizeof (have_ctx));
377 if (have_ctx)
378 write (parent_fd, ctx, sizeof (FileOpContext));
382 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
384 va_list ap;
385 int i;
387 va_start (ap, argc);
388 parent_call_header (routine, argc, Return_Integer, ctx);
389 for (i = 0; i < argc; i++) {
390 int len;
391 void *value;
393 len = va_arg (ap, int);
394 value = va_arg (ap, void *);
395 write (parent_fd, &len, sizeof (int));
396 write (parent_fd, value, len);
398 read (parent_fd, &i, sizeof (int));
399 if (ctx)
400 read (parent_fd, ctx, sizeof (FileOpContext));
402 return i;
405 char *
406 parent_call_string (void *routine, int argc, ...)
408 va_list ap;
409 char *str;
410 int i;
412 va_start (ap, argc);
413 parent_call_header (routine, argc, Return_String, NULL);
414 for (i = 0; i < argc; i++){
415 int len;
416 void *value;
418 len = va_arg (ap, int);
419 value = va_arg (ap, void *);
420 write (parent_fd, &len, sizeof (int));
421 write (parent_fd, value, len);
423 read (parent_fd, &i, sizeof (int));
424 if (!i)
425 return NULL;
426 str = g_malloc (i + 1);
427 read (parent_fd, str, i);
428 str [i] = 0;
429 return str;
432 #endif /* WITH_BACKGROUND */