5 Copyright (C) 1996-2023
6 Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file background.c
30 * \brief Source: Background support
42 #include <sys/types.h>
44 #include <sys/wait.h> /* waitpid() */
46 #include "lib/global.h"
48 #include "lib/unixcompat.h"
49 #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
50 #include "lib/widget.h" /* message() */
51 #include "lib/event-types.h"
53 #include "filemanager/fileopctx.h" /* file_op_context_t */
55 #include "background.h"
57 /*** global variables ****************************************************************************/
59 #define MAXCALLARGS 4 /* Number of arguments supported */
61 /*** file scope macro definitions ****************************************************************/
63 /*** file scope type declarations ****************************************************************/
71 /*** forward declarations (file scope functions) *************************************************/
73 static int background_attention (int fd
, void *closure
);
75 /*** file scope variables ************************************************************************/
77 /* File descriptor for talking to our parent */
80 /* File descriptor for messages from our parent */
81 static int from_parent_fd
;
83 TaskList
*task_list
= NULL
;
85 /* --------------------------------------------------------------------------------------------- */
86 /*** file scope functions ************************************************************************/
87 /* --------------------------------------------------------------------------------------------- */
90 register_task_running (file_op_context_t
* ctx
, pid_t pid
, int fd
, int to_child
, char *info
)
94 new = g_new (TaskList
, 1);
97 new->state
= Task_Running
;
98 new->next
= task_list
;
100 new->to_child_fd
= to_child
;
103 add_select_channel (fd
, background_attention
, ctx
);
106 /* --------------------------------------------------------------------------------------------- */
109 destroy_task (pid_t pid
)
111 TaskList
*p
= task_list
;
112 TaskList
*prev
= NULL
;
121 prev
->next
= p
->next
;
136 /* --------------------------------------------------------------------------------------------- */
137 /* {{{ Parent handlers */
139 /* Parent/child protocol
141 * the child (the background) process send the following:
142 * void *routine -- routine to be invoked in the parent
143 * int nargc -- number of arguments
144 * int type -- Return argument type.
146 * If the routine is zero, then it is a way to tell the parent
147 * that the process is dying.
149 * nargc arguments in the following format:
150 * int size of the coming block
151 * size bytes with the block
153 * Now, the parent loads all those and then invokes
154 * the routine with pointers to the information passed
155 * (we just support pointers).
157 * If the return type is integer:
159 * the parent then writes an int to the child with
160 * the return value from the routine and the values
161 * of any global variable that is modified in the parent
162 * currently: do_append and recursive_result.
164 * If the return type is a string:
166 * the parent writes the resulting string length
167 * if the result string was NULL or the empty string,
168 * then the length is zero.
169 * The parent then writes the string length and frees
173 * Receive requests from background process and invoke the
178 reading_failed (int i
, char **data
)
182 message (D_ERROR
, _("Background protocol error"), "%s", _("Reading failed"));
186 /* --------------------------------------------------------------------------------------------- */
189 background_attention (int fd
, void *closure
)
191 file_op_context_t
*ctx
;
195 int (*have_ctx0
) (int);
196 int (*have_ctx1
) (int, char *);
197 int (*have_ctx2
) (int, char *, char *);
198 int (*have_ctx3
) (int, char *, char *, char *);
199 int (*have_ctx4
) (int, char *, char *, char *, char *);
201 int (*non_have_ctx0
) (file_op_context_t
*, int);
202 int (*non_have_ctx1
) (file_op_context_t
*, int, char *);
203 int (*non_have_ctx2
) (file_op_context_t
*, int, char *, char *);
204 int (*non_have_ctx3
) (file_op_context_t
*, int, char *, char *, char *);
205 int (*non_have_ctx4
) (file_op_context_t
*, int, char *, char *, char *, char *);
207 char *(*ret_str0
) (void);
208 char *(*ret_str1
) (char *);
209 char *(*ret_str2
) (char *, char *);
210 char *(*ret_str3
) (char *, char *, char *);
211 char *(*ret_str4
) (char *, char *, char *, char *);
217 char *data
[MAXCALLARGS
];
220 int to_child_fd
= -1;
221 enum ReturnType type
;
222 const char *background_process_error
= _("Background process error");
226 bytes
= read (fd
, &routine
.pointer
, sizeof (routine
));
227 if (bytes
== -1 || (size_t) bytes
< (sizeof (routine
)))
229 unregister_task_running (ctx
->pid
, fd
);
231 if (waitpid (ctx
->pid
, &status
, WNOHANG
) == 0)
233 /* the process is still running, but it misbehaves - kill it */
234 kill (ctx
->pid
, SIGTERM
);
235 message (D_ERROR
, background_process_error
, "%s", _("Unknown error in child"));
239 /* 0 means happy end */
240 if (WIFEXITED (status
) && (WEXITSTATUS (status
) == 0))
243 message (D_ERROR
, background_process_error
, "%s", _("Child died unexpectedly"));
248 if (read (fd
, &argc
, sizeof (argc
)) != sizeof (argc
) ||
249 read (fd
, &type
, sizeof (type
)) != sizeof (type
) ||
250 read (fd
, &have_ctx
, sizeof (have_ctx
)) != sizeof (have_ctx
))
251 return reading_failed (-1, data
);
253 if (argc
> MAXCALLARGS
)
254 message (D_ERROR
, _("Background protocol error"), "%s",
255 _("Background process sent us a request for more arguments\n"
256 "than we can handle."));
258 if (have_ctx
!= 0 && read (fd
, ctx
, sizeof (*ctx
)) != sizeof (*ctx
))
259 return reading_failed (-1, data
);
261 for (i
= 0; i
< argc
; i
++)
265 if (read (fd
, &size
, sizeof (size
)) != sizeof (size
))
266 return reading_failed (i
- 1, data
);
268 data
[i
] = g_malloc (size
+ 1);
270 if (read (fd
, data
[i
], size
) != size
)
271 return reading_failed (i
, data
);
273 data
[i
][size
] = '\0'; /* NULL terminate the blocks (they could be strings) */
276 /* Find child task info by descriptor */
277 /* Find before call, because process can destroy self after */
278 for (p
= task_list
; p
!= NULL
; p
= p
->next
)
283 to_child_fd
= p
->to_child_fd
;
285 if (to_child_fd
== -1)
286 message (D_ERROR
, background_process_error
, "%s", _("Unknown error in child"));
287 else if (type
== Return_Integer
)
289 /* Handle the call */
297 result
= routine
.have_ctx0 (Background
);
300 result
= routine
.have_ctx1 (Background
, data
[0]);
303 result
= routine
.have_ctx2 (Background
, data
[0], data
[1]);
306 result
= routine
.have_ctx3 (Background
, data
[0], data
[1], data
[2]);
309 result
= routine
.have_ctx4 (Background
, data
[0], data
[1], data
[2], data
[3]);
318 result
= routine
.non_have_ctx0 (ctx
, Background
);
321 result
= routine
.non_have_ctx1 (ctx
, Background
, data
[0]);
324 result
= routine
.non_have_ctx2 (ctx
, Background
, data
[0], data
[1]);
327 result
= routine
.non_have_ctx3 (ctx
, Background
, data
[0], data
[1], data
[2]);
331 routine
.non_have_ctx4 (ctx
, Background
, data
[0], data
[1], data
[2], data
[3]);
337 /* Send the result code and the value for shared variables */
338 ret
= write (to_child_fd
, &result
, sizeof (result
));
339 if (have_ctx
!= 0 && to_child_fd
!= -1)
340 ret
= write (to_child_fd
, ctx
, sizeof (*ctx
));
342 else if (type
== Return_String
)
347 /* FIXME: string routines should also use the Foreground/Background
348 * parameter. Currently, this is not used here
353 resstr
= routine
.ret_str0 ();
356 resstr
= routine
.ret_str1 (data
[0]);
359 resstr
= routine
.ret_str2 (data
[0], data
[1]);
362 resstr
= routine
.ret_str3 (data
[0], data
[1], data
[2]);
365 resstr
= routine
.ret_str4 (data
[0], data
[1], data
[2], data
[3]);
368 g_assert_not_reached ();
373 len
= strlen (resstr
);
374 ret
= write (to_child_fd
, &len
, sizeof (len
));
376 ret
= write (to_child_fd
, resstr
, len
);
382 ret
= write (to_child_fd
, &len
, sizeof (len
));
386 for (i
= 0; i
< argc
; i
++)
396 /* --------------------------------------------------------------------------------------------- */
399 /* {{{ client RPC routines */
401 /* Sends the header for a call to a routine in the parent process. If the file
402 * operation context is not NULL, then it requests that the first parameter of
403 * the call be a file operation context.
407 parent_call_header (void *routine
, int argc
, enum ReturnType type
, file_op_context_t
* ctx
)
412 have_ctx
= ctx
!= NULL
? 1 : 0;
414 ret
= write (parent_fd
, &routine
, sizeof (routine
));
415 ret
= write (parent_fd
, &argc
, sizeof (argc
));
416 ret
= write (parent_fd
, &type
, sizeof (type
));
417 ret
= write (parent_fd
, &have_ctx
, sizeof (have_ctx
));
419 ret
= write (parent_fd
, ctx
, sizeof (*ctx
));
424 /* --------------------------------------------------------------------------------------------- */
427 parent_va_call (void *routine
, gpointer data
, int argc
, va_list ap
)
431 file_op_context_t
*ctx
= (file_op_context_t
*) data
;
433 parent_call_header (routine
, argc
, Return_Integer
, ctx
);
434 for (i
= 0; i
< argc
; i
++)
439 len
= va_arg (ap
, int);
440 value
= va_arg (ap
, void *);
441 ret
= write (parent_fd
, &len
, sizeof (len
));
442 ret
= write (parent_fd
, value
, len
);
445 ret
= read (from_parent_fd
, &i
, sizeof (i
));
447 ret
= read (from_parent_fd
, ctx
, sizeof (*ctx
));
454 /* --------------------------------------------------------------------------------------------- */
457 parent_va_call_string (void *routine
, int argc
, va_list ap
)
462 parent_call_header (routine
, argc
, Return_String
, NULL
);
463 for (i
= 0; i
< argc
; i
++)
468 len
= va_arg (ap
, int);
469 value
= va_arg (ap
, void *);
470 if (write (parent_fd
, &len
, sizeof (len
)) != sizeof (len
) ||
471 write (parent_fd
, value
, len
) != len
)
475 if (read (from_parent_fd
, &i
, sizeof (i
)) != sizeof (i
) || i
== 0)
478 str
= g_malloc (i
+ 1);
479 if (read (from_parent_fd
, str
, i
) != i
)
488 /* --------------------------------------------------------------------------------------------- */
489 /*** public functions ****************************************************************************/
490 /* --------------------------------------------------------------------------------------------- */
493 unregister_task_running (pid_t pid
, int fd
)
496 delete_select_channel (fd
);
499 /* --------------------------------------------------------------------------------------------- */
502 unregister_task_with_pid (pid_t pid
)
506 fd
= destroy_task (pid
);
508 delete_select_channel (fd
);
511 /* --------------------------------------------------------------------------------------------- */
513 * Try to make the Midnight Commander a background job
521 do_background (file_op_context_t
* ctx
, char *info
)
523 int comm
[2]; /* control connection stream */
524 int back_comm
[2]; /* back connection */
527 if (pipe (comm
) == -1)
530 if (pipe (back_comm
) == -1)
532 (void) close (comm
[0]);
533 (void) close (comm
[1]);
541 int saved_errno
= errno
;
543 (void) close (comm
[0]);
544 (void) close (comm
[1]);
545 (void) close (back_comm
[0]);
546 (void) close (back_comm
[1]);
556 (void) close (comm
[0]);
559 (void) close (back_comm
[1]);
560 from_parent_fd
= back_comm
[0];
562 mc_global
.we_are_background
= TRUE
;
565 /* Make stdin/stdout/stderr point somewhere */
566 close (STDIN_FILENO
);
567 close (STDOUT_FILENO
);
568 close (STDERR_FILENO
);
570 nullfd
= open ("/dev/null", O_RDWR
);
573 while (dup2 (nullfd
, STDIN_FILENO
) == -1 && errno
== EINTR
)
575 while (dup2 (nullfd
, STDOUT_FILENO
) == -1 && errno
== EINTR
)
577 while (dup2 (nullfd
, STDERR_FILENO
) == -1 && errno
== EINTR
)
586 (void) close (comm
[1]);
587 (void) close (back_comm
[0]);
589 register_task_running (ctx
, pid
, comm
[0], back_comm
[1], info
);
594 /* --------------------------------------------------------------------------------------------- */
597 parent_call (void *routine
, file_op_context_t
* ctx
, int argc
, ...)
603 ret
= parent_va_call (routine
, (gpointer
) ctx
, argc
, ap
);
609 /* --------------------------------------------------------------------------------------------- */
612 parent_call_string (void *routine
, int argc
, ...)
618 str
= parent_va_call_string (routine
, argc
, ap
);
624 /* --------------------------------------------------------------------------------------------- */
628 background_parent_call (const gchar
* event_group_name
, const gchar
* event_name
,
629 gpointer init_data
, gpointer data
)
631 ev_background_parent_call_t
*event_data
= (ev_background_parent_call_t
*) data
;
633 (void) event_group_name
;
638 parent_va_call (event_data
->routine
, event_data
->ctx
, event_data
->argc
, event_data
->ap
);
643 /* --------------------------------------------------------------------------------------------- */
647 background_parent_call_string (const gchar
* event_group_name
, const gchar
* event_name
,
648 gpointer init_data
, gpointer data
)
650 ev_background_parent_call_t
*event_data
= (ev_background_parent_call_t
*) data
;
652 (void) event_group_name
;
657 parent_va_call_string (event_data
->routine
, event_data
->argc
, event_data
->ap
);
662 /* --------------------------------------------------------------------------------------------- */