mc/src/background.c

485 lines
12 KiB
C
Raw Normal View History

1998-02-27 07:54:42 +03:00
/* {{{ Copyright */
/* Background support.
Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
Free Software Foundation, Inc.
1998-02-27 07:54:42 +03:00
Written by: 1996 Miguel de Icaza
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1998-02-27 07:54:42 +03:00
/* }}} */
/** \file background.c
* \brief Source: Background support
*/
1998-02-27 07:54:42 +03:00
#include <config.h>
#ifdef WITH_BACKGROUND
2009-02-06 02:30:45 +03:00
#include <stdlib.h>
2005-02-08 12:04:03 +03:00
#include <errno.h>
#include <signal.h>
1998-02-27 07:54:42 +03:00
#include <stdarg.h>
2005-02-08 12:04:03 +03:00
#include <stdio.h>
#include <string.h>
1998-02-27 07:54:42 +03:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h> /* waitpid() */
2005-02-08 12:04:03 +03:00
#include <unistd.h>
2009-02-06 02:30:45 +03:00
#include <fcntl.h>
#include "global.h"
#include "background.h"
1998-02-27 07:54:42 +03:00
#include "wtools.h"
#include "layout.h" /* repaint_screen() */
#include "fileopctx.h" /* FileOpContext */
#include "../lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
1998-02-27 07:54:42 +03:00
enum ReturnType {
Return_String,
Return_Integer
};
1998-02-27 07:54:42 +03:00
/* If true, this is a background process */
int we_are_background = 0;
/* File descriptor for talking to our parent */
static int parent_fd;
/* File descriptor for messages from our parent */
static int from_parent_fd;
1998-02-27 07:54:42 +03:00
#define MAXCALLARGS 4 /* Number of arguments supported */
struct TaskList *task_list = NULL;
static int background_attention (int fd, void *closure);
1998-02-27 07:54:42 +03:00
static void
register_task_running (FileOpContext *ctx, pid_t pid, int fd, int to_child,
char *info)
1998-02-27 07:54:42 +03:00
{
TaskList *new;
new = g_new (TaskList, 1);
1998-02-27 07:54:42 +03:00
new->pid = pid;
new->info = info;
new->state = Task_Running;
new->next = task_list;
new->fd = fd;
new->to_child_fd = to_child;
1998-02-27 07:54:42 +03:00
task_list = new;
add_select_channel (fd, background_attention, ctx);
1998-02-27 07:54:42 +03:00
}
static int
destroy_task_and_return_fd (pid_t pid)
1998-02-27 07:54:42 +03:00
{
TaskList *p = task_list;
TaskList *prev = 0;
while (p){
if (p->pid == pid){
if (prev)
prev->next = p->next;
else
task_list = p->next;
g_free (p->info);
g_free (p);
return p->fd;
1998-02-27 07:54:42 +03:00
}
prev = p;
p = p->next;
}
/* pid not found */
return -1;
}
void
unregister_task_running (pid_t pid, int fd)
{
destroy_task_and_return_fd(pid);
1998-02-27 07:54:42 +03:00
delete_select_channel (fd);
}
void
unregister_task_with_pid (pid_t pid)
{
int fd = destroy_task_and_return_fd(pid);
if (fd != -1)
delete_select_channel (fd);
}
1998-02-27 07:54:42 +03:00
/*
* Try to make the Midnight Commander a background job
*
* Returns:
* 1 for parent
* 0 for child
* -1 on failure
*/
int
do_background (struct FileOpContext *ctx, char *info)
1998-02-27 07:54:42 +03:00
{
int comm[2]; /* control connection stream */
int back_comm[2]; /* back connection */
1999-03-26 23:37:48 +03:00
pid_t pid;
if (pipe (comm) == -1)
1998-02-27 07:54:42 +03:00
return -1;
if (pipe (back_comm) == -1)
return -1;
if ((pid = fork ()) == -1) {
int saved_errno = errno;
(void) close (comm[0]);
(void) close (comm[1]);
(void) close (back_comm[0]);
(void) close (back_comm[1]);
errno = saved_errno;
1998-02-27 07:54:42 +03:00
return -1;
}
if (pid == 0) {
1998-02-27 07:54:42 +03:00
int nullfd;
parent_fd = comm[1];
from_parent_fd = back_comm[0];
1998-02-27 07:54:42 +03:00
we_are_background = 1;
current_dlg = NULL;
1998-02-27 07:54:42 +03:00
/* Make stdin/stdout/stderr point somewhere */
close (0);
close (1);
close (2);
if ((nullfd = open ("/dev/null", O_RDWR)) != -1) {
while (dup2 (nullfd, 0) == -1 && errno == EINTR);
while (dup2 (nullfd, 1) == -1 && errno == EINTR);
while (dup2 (nullfd, 2) == -1 && errno == EINTR);
1998-02-27 07:54:42 +03:00
}
return 0;
} else {
ctx->pid = pid;
register_task_running (ctx, pid, comm[0], back_comm[1], info);
1998-02-27 07:54:42 +03:00
return 1;
}
}
/* {{{ Parent handlers */
/* Parent/child protocol
*
* the child (the background) process send the following:
* void *routine -- routine to be invoked in the parent
* int nargc -- number of arguments
* int type -- Return argument type.
*
* If the routine is zero, then it is a way to tell the parent
* that the process is dying.
*
* nargc arguments in the following format:
* int size of the coming block
* size bytes with the block
*
* Now, the parent loads all those and then invokes
* the routine with pointers to the information passed
* (we just support pointers).
*
* If the return type is integer:
*
* the parent then writes an int to the child with
* the return value from the routine and the values
* of any global variable that is modified in the parent
* currently: do_append and recursive_result.
*
* If the return type is a string:
*
2003-06-02 22:13:42 +04:00
* the parent writes the resulting string length
1998-02-27 07:54:42 +03:00
* if the result string was NULL or the empty string,
2003-06-02 22:13:42 +04:00
* then the length is zero.
* The parent then writes the string length and frees
1998-02-27 07:54:42 +03:00
* the result string.
*/
/*
* Receive requests from background process and invoke the
* specified routine
*/
static int
background_attention (int fd, void *closure)
1998-02-27 07:54:42 +03:00
{
FileOpContext *ctx;
int have_ctx;
union
{
int (*have_ctx0)(int);
int (*have_ctx1)(int, char *);
int (*have_ctx2)(int, char *, char *);
int (*have_ctx3)(int, char *, char *, char *);
int (*have_ctx4)(int, char *, char *, char *, char *);
int (*non_have_ctx0)(FileOpContext *, int);
int (*non_have_ctx1)(FileOpContext *, int, char *);
int (*non_have_ctx2)(FileOpContext *, int, char *, char *);
int (*non_have_ctx3)(FileOpContext *, int, char *, char *, char *);
int (*non_have_ctx4)(FileOpContext *, int, char *, char *, char *, char *);
char * (*ret_str0)();
char * (*ret_str1)(char *);
char * (*ret_str2)(char *, char *);
char * (*ret_str3)(char *, char *, char *);
char * (*ret_str4)(char *, char *, char *, char *);
void *pointer;
} routine;
/* void *routine;*/
1998-02-27 07:54:42 +03:00
int argc, i, result, status;
char *data [MAXCALLARGS];
ssize_t bytes;
struct TaskList *p;
int to_child_fd = -1;
1998-02-27 07:54:42 +03:00
enum ReturnType type;
ctx = closure;
bytes = read (fd, &routine.pointer, sizeof (routine));
if (bytes == -1 || (size_t) bytes < (sizeof (routine))) {
const char *background_process_error = _(" Background process error ");
unregister_task_running (ctx->pid, fd);
if (!waitpid (ctx->pid, &status, WNOHANG)) {
/* the process is still running, but it misbehaves - kill it */
kill (ctx->pid, SIGTERM);
message (D_ERROR, background_process_error, _(" Unknown error in child "));
return 0;
}
/* 0 means happy end */
if (WIFEXITED (status) && (WEXITSTATUS (status) == 0))
return 0;
message (D_ERROR, background_process_error, _(" Child died unexpectedly "));
1998-02-27 07:54:42 +03:00
return 0;
}
1998-02-27 07:54:42 +03:00
read (fd, &argc, sizeof (argc));
if (argc > MAXCALLARGS){
message (D_ERROR, _(" Background protocol error "),
_(" Background process sent us a request for more arguments \n"
" than we can handle. \n"));
1998-02-27 07:54:42 +03:00
}
read (fd, &type, sizeof (type));
read (fd, &have_ctx, sizeof (have_ctx));
if (have_ctx)
read (fd, ctx, sizeof (FileOpContext));
1998-02-27 07:54:42 +03:00
for (i = 0; i < argc; i++){
int size;
1998-02-27 07:54:42 +03:00
read (fd, &size, sizeof (size));
data [i] = g_malloc (size+1);
1998-02-27 07:54:42 +03:00
read (fd, data [i], size);
1998-02-27 07:54:42 +03:00
data [i][size] = 0; /* NULL terminate the blocks (they could be strings) */
}
/* Find child task info by descriptor */
/* Find before call, because process can destroy self after */
for (p = task_list; p; p = p->next) {
if (p->fd == fd)
break;
}
if (p) to_child_fd = p->to_child_fd;
if (to_child_fd == -1)
message (D_ERROR, _(" Background process error "), _(" Unknown error in child "));
1998-02-27 07:54:42 +03:00
/* Handle the call */
if (type == Return_Integer){
if (!have_ctx)
switch (argc){
case 0:
result = routine.have_ctx0 (Background);
break;
case 1:
result = routine.have_ctx1 (Background, data [0]);
break;
case 2:
result = routine.have_ctx2 (Background, data [0], data [1]);
break;
case 3:
result = routine.have_ctx3 (Background, data [0], data [1], data [2]);
break;
case 4:
result = routine.have_ctx4 (Background, data [0], data [1], data [2], data [3]);
break;
}
else
switch (argc){
case 0:
result = routine.non_have_ctx0 (ctx, Background);
break;
case 1:
result = routine.non_have_ctx1 (ctx, Background, data [0]);
break;
case 2:
result = routine.non_have_ctx2 (ctx, Background, data [0], data [1]);
break;
case 3:
result = routine.non_have_ctx3 (ctx, Background, data [0], data [1], data [2]);
break;
case 4:
result = routine.non_have_ctx4 (ctx, Background, data [0], data [1], data [2], data [3]);
break;
}
1998-02-27 07:54:42 +03:00
/* Send the result code and the value for shared variables */
write (to_child_fd, &result, sizeof (int));
if (have_ctx && to_child_fd != -1)
write (to_child_fd, ctx, sizeof (FileOpContext));
1998-02-27 07:54:42 +03:00
} else if (type == Return_String) {
int len;
char *resstr = NULL;
1998-02-27 07:54:42 +03:00
/* FIXME: string routines should also use the Foreground/Background
* parameter. Currently, this is not used here
*/
switch (argc){
case 0:
resstr = routine.ret_str0 ();
break;
1998-02-27 07:54:42 +03:00
case 1:
resstr = routine.ret_str1 (data [0]);
1998-02-27 07:54:42 +03:00
break;
case 2:
resstr = routine.ret_str2 (data [0], data [1]);
1998-02-27 07:54:42 +03:00
break;
case 3:
resstr = routine.ret_str3 (data [0], data [1], data [2]);
1998-02-27 07:54:42 +03:00
break;
case 4:
resstr = routine.ret_str4 (data [0], data [1], data [2], data [3]);
1998-02-27 07:54:42 +03:00
break;
default: g_assert_not_reached();
1998-02-27 07:54:42 +03:00
}
if (resstr){
len = strlen (resstr);
write (to_child_fd, &len, sizeof (len));
1998-02-27 07:54:42 +03:00
if (len){
write (to_child_fd, resstr, len);
g_free (resstr);
1998-02-27 07:54:42 +03:00
}
} else {
len = 0;
write (to_child_fd, &len, sizeof (len));
1998-02-27 07:54:42 +03:00
}
}
for (i = 0; i < argc; i++)
g_free (data [i]);
1998-02-27 07:54:42 +03:00
repaint_screen ();
1998-02-27 07:54:42 +03:00
return 0;
}
/* }}} */
/* {{{ client RPC routines */
/* Sends the header for a call to a routine in the parent process. If the file
* operation context is not NULL, then it requests that the first parameter of
* the call be a file operation context.
*/
1998-12-03 00:27:27 +03:00
static void
parent_call_header (void *routine, int argc, enum ReturnType type, FileOpContext *ctx)
1998-02-27 07:54:42 +03:00
{
int have_ctx;
have_ctx = (ctx != NULL);
1998-02-27 07:54:42 +03:00
write (parent_fd, &routine, sizeof (routine));
write (parent_fd, &argc, sizeof (int));
write (parent_fd, &type, sizeof (type));
write (parent_fd, &have_ctx, sizeof (have_ctx));
if (have_ctx)
write (parent_fd, ctx, sizeof (FileOpContext));
1998-02-27 07:54:42 +03:00
}
int
parent_call (void *routine, struct FileOpContext *ctx, int argc, ...)
1998-02-27 07:54:42 +03:00
{
va_list ap;
int i;
1998-02-27 07:54:42 +03:00
va_start (ap, argc);
parent_call_header (routine, argc, Return_Integer, ctx);
for (i = 0; i < argc; i++) {
1998-02-27 07:54:42 +03:00
int len;
void *value;
len = va_arg (ap, int);
value = va_arg (ap, void *);
write (parent_fd, &len, sizeof (int));
write (parent_fd, value, len);
}
read (from_parent_fd, &i, sizeof (int));
if (ctx)
read (from_parent_fd, ctx, sizeof (FileOpContext));
1998-02-27 07:54:42 +03:00
return i;
}
char *
1998-02-27 07:54:42 +03:00
parent_call_string (void *routine, int argc, ...)
{
va_list ap;
char *str;
int i;
va_start (ap, argc);
parent_call_header (routine, argc, Return_String, NULL);
1998-02-27 07:54:42 +03:00
for (i = 0; i < argc; i++){
int len;
void *value;
len = va_arg (ap, int);
value = va_arg (ap, void *);
write (parent_fd, &len, sizeof (int));
write (parent_fd, value, len);
}
read (from_parent_fd, &i, sizeof (int));
1998-02-27 07:54:42 +03:00
if (!i)
return NULL;
str = g_malloc (i + 1);
read (from_parent_fd, str, i);
1998-02-27 07:54:42 +03:00
str [i] = 0;
return str;
}
#endif /* WITH_BACKGROUND */