Updated italian translation
[midnight-commander.git] / src / background.c
blob19eb5bb3b4b995c332256a1ca21c848c34d057f5
1 /* {{{ Copyright */
3 /* Background support.
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. */
22 /* }}} */
24 #include <config.h>
26 #ifdef WITH_BACKGROUND
28 #include <errno.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
38 #include "global.h"
39 #include "background.h"
40 #include "tty.h" /* doupdate() */
41 #include "dialog.h" /* do_refresh() */
42 #include "wtools.h"
43 #include "fileopctx.h" /* FileOpContext */
44 #include "key.h" /* add_select_channel(), delete_select_channel() */
46 enum ReturnType {
47 Return_String,
48 Return_Integer
51 /* If true, this is a background process */
52 int we_are_background = 0;
54 /* File descriptor for talking to our parent */
55 static int parent_fd;
57 #define MAXCALLARGS 4 /* Number of arguments supported */
59 struct TaskList *task_list = NULL;
61 static int background_attention (int fd, void *closure);
63 static void
64 register_task_running (FileOpContext *ctx, pid_t pid, int fd, char *info)
66 TaskList *new;
68 new = g_new (TaskList, 1);
69 new->pid = pid;
70 new->info = info;
71 new->state = Task_Running;
72 new->next = task_list;
73 new->fd = fd;
74 task_list = new;
76 add_select_channel (fd, background_attention, ctx);
79 void
80 unregister_task_running (pid_t pid, int fd)
82 TaskList *p = task_list;
83 TaskList *prev = 0;
85 while (p){
86 if (p->pid == pid){
87 if (prev)
88 prev->next = p->next;
89 else
90 task_list = p->next;
91 g_free (p->info);
92 g_free (p);
93 break;
95 prev = p;
96 p = p->next;
98 delete_select_channel (fd);
102 * Try to make the Midnight Commander a background job
104 * Returns:
105 * 1 for parent
106 * 0 for child
107 * -1 on failure
110 do_background (struct FileOpContext *ctx, char *info)
112 int comm[2]; /* control connection stream */
113 pid_t pid;
115 if (pipe (comm) == -1)
116 return -1;
118 if ((pid = fork ()) == -1) {
119 int saved_errno = errno;
120 (void) close (comm[0]);
121 (void) close (comm[1]);
122 errno = saved_errno;
123 return -1;
126 if (pid == 0) {
127 int nullfd;
129 close (comm[0]);
130 parent_fd = comm[1];
131 we_are_background = 1;
132 current_dlg = NULL;
134 /* Make stdin/stdout/stderr point somewhere */
135 close (0);
136 close (1);
137 close (2);
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);
145 return 0;
146 } else {
147 close (comm[1]);
148 ctx->pid = pid;
149 register_task_running (ctx, pid, comm[0], info);
150 return 1;
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
187 * the result string.
190 * Receive requests from background process and invoke the
191 * specified routine
194 static int
195 background_attention (int fd, void *closure)
197 FileOpContext *ctx;
198 int have_ctx;
199 void *routine;
200 int argc, i, result, status;
201 char *data [MAXCALLARGS];
202 ssize_t bytes;
203 enum ReturnType type;
205 ctx = closure;
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 "));
216 return 0;
219 /* 0 means happy end */
220 if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
221 return 0;
223 message (1, background_process_error, _(" Child died unexpectedly "));
225 return 0;
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));
236 if (have_ctx)
237 read (fd, ctx, sizeof (FileOpContext));
239 for (i = 0; i < argc; i++){
240 int size;
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){
251 if (!have_ctx)
252 switch (argc){
253 case 1:
254 result = (*(int (*)(int, char *))routine)(Background, data [0]);
255 break;
256 case 2:
257 result = (*(int (*)(int, char *, char *))routine)
258 (Background, data [0], data [1]);
259 break;
260 case 3:
261 result = (*(int (*)(int, char *, char *, char *))routine)
262 (Background, data [0], data [1], data [2]);
263 break;
264 case 4:
265 result = (*(int (*)(int, char *, char *, char *, char *))routine)
266 (Background, data [0], data [1], data [2], data [3]);
267 break;
269 else
270 switch (argc){
271 case 1:
272 result = (*(int (*)(FileOpContext *, int, char *))routine)
273 (ctx, Background, data [0]);
274 break;
275 case 2:
276 result = (*(int (*)(FileOpContext *, int, char *, char *))routine)
277 (ctx, Background, data [0], data [1]);
278 break;
279 case 3:
280 result = (*(int (*)(FileOpContext *, int, char *, char *, char *))routine)
281 (ctx, Background, data [0], data [1], data [2]);
282 break;
283 case 4:
284 result = (*(int (*)(FileOpContext *, int, char *, char *, char *, char *))routine)
285 (ctx, Background, data [0], data [1], data [2], data [3]);
286 break;
289 /* Send the result code and the value for shared variables */
290 write (fd, &result, sizeof (int));
291 if (have_ctx)
292 write (fd, ctx, sizeof (FileOpContext));
293 } else if (type == Return_String) {
294 int len;
295 char *resstr = NULL;
297 /* FIXME: string routines should also use the Foreground/Background
298 * parameter. Currently, this is not used here
300 switch (argc){
301 case 1:
302 resstr = (*(char * (*)(char *))routine)(data [0]);
303 break;
304 case 2:
305 resstr = (*(char * (*)(char *, char *))routine)
306 (data [0], data [1]);
307 break;
308 case 3:
309 resstr = (*(char * (*)(char *, char *, char *))routine)
310 (data [0], data [1], data [2]);
311 break;
312 case 4:
313 resstr = (*(char * (*)(char *, char *, char *, char *))routine)
314 (data [0], data [1], data [2], data [3]);
315 break;
316 default: g_assert_not_reached();
318 if (resstr){
319 len = strlen (resstr);
320 write (fd, &len, sizeof (len));
321 if (len){
322 write (fd, resstr, len);
323 g_free (resstr);
325 } else {
326 len = 0;
327 write (fd, &len, sizeof (len));
330 for (i = 0; i < argc; i++)
331 g_free (data [i]);
333 do_refresh ();
334 mc_refresh ();
335 doupdate ();
336 return 0;
340 /* }}} */
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.
348 static void
349 parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
351 int have_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));
360 if (have_ctx)
361 write (parent_fd, ctx, sizeof (FileOpContext));
365 parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
367 va_list ap;
368 int i;
370 va_start (ap, argc);
371 parent_call_header (routine, argc, Return_Integer, ctx);
372 for (i = 0; i < argc; i++) {
373 int len;
374 void *value;
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));
382 if (ctx)
383 read (parent_fd, ctx, sizeof (FileOpContext));
385 return i;
388 char *
389 parent_call_string (void *routine, int argc, ...)
391 va_list ap;
392 char *str;
393 int i;
395 va_start (ap, argc);
396 parent_call_header (routine, argc, Return_String, NULL);
397 for (i = 0; i < argc; i++){
398 int len;
399 void *value;
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));
407 if (!i)
408 return NULL;
409 str = g_malloc (i + 1);
410 read (parent_fd, str, i);
411 str [i] = 0;
412 return str;
415 #endif /* WITH_BACKGROUND */