Not only comment it out but removing it
[midnight-commander.git] / src / background.c
blob7d733dd6ea1c52a84b2bbd40cb29c60e8f9da650
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 #include <config.h>
27 #ifdef WITH_BACKGROUND
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
39 #include "global.h"
40 #include "background.h"
41 #include "tty.h" /* doupdate() */
42 #include "dialog.h" /* do_refresh() */
43 #include "wtools.h"
44 #include "fileopctx.h" /* FileOpContext */
45 #include "key.h" /* add_select_channel(), delete_select_channel() */
47 enum ReturnType {
48 Return_String,
49 Return_Integer
52 /* If true, this is a background process */
53 int we_are_background = 0;
55 /* File descriptor for talking to our parent */
56 static int parent_fd;
58 #define MAXCALLARGS 4 /* Number of arguments supported */
60 struct TaskList *task_list = NULL;
62 static int background_attention (int fd, void *closure);
64 static void
65 register_task_running (FileOpContext *ctx, pid_t pid, int fd, char *info)
67 TaskList *new;
69 new = g_new (TaskList, 1);
70 new->pid = pid;
71 new->info = info;
72 new->state = Task_Running;
73 new->next = task_list;
74 new->fd = fd;
75 task_list = new;
77 add_select_channel (fd, background_attention, ctx);
80 void
81 unregister_task_running (pid_t pid, int fd)
83 TaskList *p = task_list;
84 TaskList *prev = 0;
86 while (p){
87 if (p->pid == pid){
88 if (prev)
89 prev->next = p->next;
90 else
91 task_list = p->next;
92 g_free (p->info);
93 g_free (p);
94 break;
96 prev = p;
97 p = p->next;
99 delete_select_channel (fd);
103 * Try to make the Midnight Commander a background job
105 * Returns:
106 * 1 for parent
107 * 0 for child
108 * -1 on failure
111 do_background (struct FileOpContext *ctx, char *info)
113 int comm[2]; /* control connection stream */
114 pid_t pid;
116 if (pipe (comm) == -1)
117 return -1;
119 if ((pid = fork ()) == -1) {
120 int saved_errno = errno;
121 (void) close (comm[0]);
122 (void) close (comm[1]);
123 errno = saved_errno;
124 return -1;
127 if (pid == 0) {
128 int nullfd;
130 close (comm[0]);
131 parent_fd = comm[1];
132 we_are_background = 1;
133 current_dlg = NULL;
135 /* Make stdin/stdout/stderr point somewhere */
136 close (0);
137 close (1);
138 close (2);
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);
146 return 0;
147 } else {
148 close (comm[1]);
149 ctx->pid = pid;
150 register_task_running (ctx, pid, comm[0], info);
151 return 1;
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
188 * the result string.
191 * Receive requests from background process and invoke the
192 * specified routine
195 static int
196 background_attention (int fd, void *closure)
198 FileOpContext *ctx;
199 int have_ctx;
200 void *routine;
201 int argc, i, result, status;
202 char *data [MAXCALLARGS];
203 ssize_t bytes;
204 enum ReturnType type;
206 ctx = closure;
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 "));
217 return 0;
220 /* 0 means happy end */
221 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
222 return 0;
224 message (1, background_process_error, _(" Child died unexpectedly "));
226 return 0;
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));
237 if (have_ctx)
238 read (fd, ctx, sizeof (FileOpContext));
240 for (i = 0; i < argc; i++){
241 int size;
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){
252 if (!have_ctx)
253 switch (argc){
254 case 1:
255 result = (*(int (*)(int, char *))routine)(Background, data [0]);
256 break;
257 case 2:
258 result = (*(int (*)(int, char *, char *))routine)
259 (Background, data [0], data [1]);
260 break;
261 case 3:
262 result = (*(int (*)(int, char *, char *, char *))routine)
263 (Background, data [0], data [1], data [2]);
264 break;
265 case 4:
266 result = (*(int (*)(int, char *, char *, char *, char *))routine)
267 (Background, data [0], data [1], data [2], data [3]);
268 break;
270 else
271 switch (argc){
272 case 1:
273 result = (*(int (*)(FileOpContext *, int, char *))routine)
274 (ctx, Background, data [0]);
275 break;
276 case 2:
277 result = (*(int (*)(FileOpContext *, int, char *, char *))routine)
278 (ctx, Background, data [0], data [1]);
279 break;
280 case 3:
281 result = (*(int (*)(FileOpContext *, int, char *, char *, char *))routine)
282 (ctx, Background, data [0], data [1], data [2]);
283 break;
284 case 4:
285 result = (*(int (*)(FileOpContext *, int, char *, char *, char *, char *))routine)
286 (ctx, Background, data [0], data [1], data [2], data [3]);
287 break;
290 /* Send the result code and the value for shared variables */
291 write (fd, &result, sizeof (int));
292 if (have_ctx)
293 write (fd, ctx, sizeof (FileOpContext));
294 } else if (type == Return_String) {
295 int len;
296 char *resstr = NULL;
298 /* FIXME: string routines should also use the Foreground/Background
299 * parameter. Currently, this is not used here
301 switch (argc){
302 case 1:
303 resstr = (*(char * (*)(char *))routine)(data [0]);
304 break;
305 case 2:
306 resstr = (*(char * (*)(char *, char *))routine)
307 (data [0], data [1]);
308 break;
309 case 3:
310 resstr = (*(char * (*)(char *, char *, char *))routine)
311 (data [0], data [1], data [2]);
312 break;
313 case 4:
314 resstr = (*(char * (*)(char *, char *, char *, char *))routine)
315 (data [0], data [1], data [2], data [3]);
316 break;
317 default: g_assert_not_reached();
319 if (resstr){
320 len = strlen (resstr);
321 write (fd, &len, sizeof (len));
322 if (len){
323 write (fd, resstr, len);
324 g_free (resstr);
326 } else {
327 len = 0;
328 write (fd, &len, sizeof (len));
331 for (i = 0; i < argc; i++)
332 g_free (data [i]);
334 do_refresh ();
335 mc_refresh ();
336 doupdate ();
337 return 0;
341 /* }}} */
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.
349 static void
350 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
352 int have_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));
361 if (have_ctx)
362 write (parent_fd, ctx, sizeof (FileOpContext));
366 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
368 va_list ap;
369 int i;
371 va_start (ap, argc);
372 parent_call_header (routine, argc, Return_Integer, ctx);
373 for (i = 0; i < argc; i++) {
374 int len;
375 void *value;
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));
383 if (ctx)
384 read (parent_fd, ctx, sizeof (FileOpContext));
386 return i;
389 char *
390 parent_call_string (void *routine, int argc, ...)
392 va_list ap;
393 char *str;
394 int i;
396 va_start (ap, argc);
397 parent_call_header (routine, argc, Return_String, NULL);
398 for (i = 0; i < argc; i++){
399 int len;
400 void *value;
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));
408 if (!i)
409 return NULL;
410 str = g_malloc (i + 1);
411 read (parent_fd, str, i);
412 str [i] = 0;
413 return str;
416 #endif /* WITH_BACKGROUND */