mirror of
https://github.com/MidnightCommander/mc
synced 2025-01-18 09:19:24 +03:00
src/background.c: clean up and fix coding style.
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
parent
6d79aa7d5d
commit
3b8ee664f2
@ -106,15 +106,15 @@ static int
|
|||||||
destroy_task (pid_t pid)
|
destroy_task (pid_t pid)
|
||||||
{
|
{
|
||||||
TaskList *p = task_list;
|
TaskList *p = task_list;
|
||||||
TaskList *prev = 0;
|
TaskList *prev = NULL;
|
||||||
|
|
||||||
while (p)
|
while (p != NULL)
|
||||||
{
|
{
|
||||||
if (p->pid == pid)
|
if (p->pid == pid)
|
||||||
{
|
{
|
||||||
int fd = p->fd;
|
int fd = p->fd;
|
||||||
|
|
||||||
if (prev)
|
if (prev != NULL)
|
||||||
prev->next = p->next;
|
prev->next = p->next;
|
||||||
else
|
else
|
||||||
task_list = p->next;
|
task_list = p->next;
|
||||||
@ -127,7 +127,7 @@ destroy_task (pid_t pid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* pid not found */
|
/* pid not found */
|
||||||
return -1;
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
@ -224,7 +224,8 @@ background_attention (int fd, void *closure)
|
|||||||
if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
|
if (bytes == -1 || (size_t) bytes < (sizeof (routine)))
|
||||||
{
|
{
|
||||||
unregister_task_running (ctx->pid, fd);
|
unregister_task_running (ctx->pid, fd);
|
||||||
if (!waitpid (ctx->pid, &status, WNOHANG))
|
|
||||||
|
if (waitpid (ctx->pid, &status, WNOHANG) == 0)
|
||||||
{
|
{
|
||||||
/* the process is still running, but it misbehaves - kill it */
|
/* the process is still running, but it misbehaves - kill it */
|
||||||
kill (ctx->pid, SIGTERM);
|
kill (ctx->pid, SIGTERM);
|
||||||
@ -241,53 +242,41 @@ background_attention (int fd, void *closure)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
|
if (read (fd, &argc, sizeof (argc)) != sizeof (argc) ||
|
||||||
(read (fd, &type, sizeof (type)) != sizeof (type)) ||
|
read (fd, &type, sizeof (type)) != sizeof (type) ||
|
||||||
(read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
|
read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx))
|
||||||
{
|
|
||||||
return reading_failed (-1, data);
|
return reading_failed (-1, data);
|
||||||
}
|
|
||||||
|
|
||||||
if (argc > MAXCALLARGS)
|
if (argc > MAXCALLARGS)
|
||||||
{
|
|
||||||
message (D_ERROR, _("Background protocol error"), "%s",
|
message (D_ERROR, _("Background protocol error"), "%s",
|
||||||
_("Background process sent us a request for more arguments\n"
|
_("Background process sent us a request for more arguments\n"
|
||||||
"than we can handle."));
|
"than we can handle."));
|
||||||
}
|
|
||||||
|
|
||||||
if (have_ctx)
|
if (have_ctx != 0 && read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
|
||||||
{
|
return reading_failed (-1, data);
|
||||||
if (read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
|
|
||||||
{
|
|
||||||
return reading_failed (-1, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < argc; i++)
|
for (i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
if (read (fd, &size, sizeof (size)) != sizeof (size))
|
if (read (fd, &size, sizeof (size)) != sizeof (size))
|
||||||
{
|
|
||||||
return reading_failed (i - 1, data);
|
return reading_failed (i - 1, data);
|
||||||
}
|
|
||||||
data[i] = g_malloc (size + 1);
|
data[i] = g_malloc (size + 1);
|
||||||
|
|
||||||
if (read (fd, data[i], size) != size)
|
if (read (fd, data[i], size) != size)
|
||||||
{
|
|
||||||
return reading_failed (i, data);
|
return reading_failed (i, data);
|
||||||
}
|
|
||||||
data[i][size] = 0; /* NULL terminate the blocks (they could be strings) */
|
data[i][size] = '\0'; /* NULL terminate the blocks (they could be strings) */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find child task info by descriptor */
|
/* Find child task info by descriptor */
|
||||||
/* Find before call, because process can destroy self after */
|
/* Find before call, because process can destroy self after */
|
||||||
for (p = task_list; p; p = p->next)
|
for (p = task_list; p != NULL; p = p->next)
|
||||||
{
|
|
||||||
if (p->fd == fd)
|
if (p->fd == fd)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if (p)
|
if (p != NULL)
|
||||||
to_child_fd = p->to_child_fd;
|
to_child_fd = p->to_child_fd;
|
||||||
|
|
||||||
if (to_child_fd == -1)
|
if (to_child_fd == -1)
|
||||||
@ -298,7 +287,7 @@ background_attention (int fd, void *closure)
|
|||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
if (!have_ctx)
|
if (have_ctx == 0)
|
||||||
switch (argc)
|
switch (argc)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
@ -344,7 +333,7 @@ background_attention (int fd, void *closure)
|
|||||||
|
|
||||||
/* Send the result code and the value for shared variables */
|
/* Send the result code and the value for shared variables */
|
||||||
ret = write (to_child_fd, &result, sizeof (result));
|
ret = write (to_child_fd, &result, sizeof (result));
|
||||||
if (have_ctx && to_child_fd != -1)
|
if (have_ctx != 0 && to_child_fd != -1)
|
||||||
ret = write (to_child_fd, ctx, sizeof (*ctx));
|
ret = write (to_child_fd, ctx, sizeof (*ctx));
|
||||||
}
|
}
|
||||||
else if (type == Return_String)
|
else if (type == Return_String)
|
||||||
@ -375,7 +364,8 @@ background_attention (int fd, void *closure)
|
|||||||
default:
|
default:
|
||||||
g_assert_not_reached ();
|
g_assert_not_reached ();
|
||||||
}
|
}
|
||||||
if (resstr)
|
|
||||||
|
if (resstr != NULL)
|
||||||
{
|
{
|
||||||
len = strlen (resstr);
|
len = strlen (resstr);
|
||||||
ret = write (to_child_fd, &len, sizeof (len));
|
ret = write (to_child_fd, &len, sizeof (len));
|
||||||
@ -389,15 +379,17 @@ background_attention (int fd, void *closure)
|
|||||||
ret = write (to_child_fd, &len, sizeof (len));
|
ret = write (to_child_fd, &len, sizeof (len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < argc; i++)
|
for (i = 0; i < argc; i++)
|
||||||
g_free (data[i]);
|
g_free (data[i]);
|
||||||
|
|
||||||
repaint_screen ();
|
repaint_screen ();
|
||||||
|
|
||||||
(void) ret;
|
(void) ret;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
@ -414,15 +406,15 @@ parent_call_header (void *routine, int argc, enum ReturnType type, file_op_conte
|
|||||||
int have_ctx;
|
int have_ctx;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
have_ctx = (ctx != NULL);
|
have_ctx = ctx != NULL ? 1 : 0;
|
||||||
|
|
||||||
ret = write (parent_fd, &routine, sizeof (routine));
|
ret = write (parent_fd, &routine, sizeof (routine));
|
||||||
ret = write (parent_fd, &argc, sizeof (argc));
|
ret = write (parent_fd, &argc, sizeof (argc));
|
||||||
ret = write (parent_fd, &type, sizeof (type));
|
ret = write (parent_fd, &type, sizeof (type));
|
||||||
ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
|
ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
|
||||||
|
if (have_ctx != 0)
|
||||||
if (have_ctx)
|
|
||||||
ret = write (parent_fd, ctx, sizeof (*ctx));
|
ret = write (parent_fd, ctx, sizeof (*ctx));
|
||||||
|
|
||||||
(void) ret;
|
(void) ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -448,10 +440,11 @@ parent_va_call (void *routine, gpointer data, int argc, va_list ap)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret = read (from_parent_fd, &i, sizeof (i));
|
ret = read (from_parent_fd, &i, sizeof (i));
|
||||||
if (ctx)
|
if (ctx != NULL)
|
||||||
ret = read (from_parent_fd, ctx, sizeof (*ctx));
|
ret = read (from_parent_fd, ctx, sizeof (*ctx));
|
||||||
|
|
||||||
(void) ret;
|
(void) ret;
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,24 +464,21 @@ parent_va_call_string (void *routine, int argc, va_list ap)
|
|||||||
|
|
||||||
len = va_arg (ap, int);
|
len = va_arg (ap, int);
|
||||||
value = va_arg (ap, void *);
|
value = va_arg (ap, void *);
|
||||||
if ((write (parent_fd, &len, sizeof (len)) != sizeof (len)) ||
|
if (write (parent_fd, &len, sizeof (len)) != sizeof (len) ||
|
||||||
(write (parent_fd, value, len) != len))
|
write (parent_fd, value, len) != len)
|
||||||
{
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i))
|
if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i) || i == 0)
|
||||||
return NULL;
|
|
||||||
if (!i)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
str = g_malloc (i + 1);
|
str = g_malloc (i + 1);
|
||||||
if (read (from_parent_fd, str, i) != i)
|
if (read (from_parent_fd, str, i) != i)
|
||||||
{
|
{
|
||||||
g_free (str);
|
g_free (str);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
str[i] = 0;
|
str[i] = '\0';
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -508,12 +498,13 @@ unregister_task_running (pid_t pid, int fd)
|
|||||||
void
|
void
|
||||||
unregister_task_with_pid (pid_t pid)
|
unregister_task_with_pid (pid_t pid)
|
||||||
{
|
{
|
||||||
int fd = destroy_task (pid);
|
int fd;
|
||||||
|
|
||||||
|
fd = destroy_task (pid);
|
||||||
if (fd != -1)
|
if (fd != -1)
|
||||||
delete_select_channel (fd);
|
delete_select_channel (fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
/**
|
/**
|
||||||
* Try to make the Midnight Commander a background job
|
* Try to make the Midnight Commander a background job
|
||||||
@ -531,10 +522,10 @@ do_background (file_op_context_t * ctx, char *info)
|
|||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
if (pipe (comm) == -1)
|
if (pipe (comm) == -1)
|
||||||
return -1;
|
return (-1);
|
||||||
|
|
||||||
if (pipe (back_comm) == -1)
|
if (pipe (back_comm) == -1)
|
||||||
return -1;
|
return (-1);
|
||||||
|
|
||||||
pid = fork ();
|
pid = fork ();
|
||||||
if (pid == -1)
|
if (pid == -1)
|
||||||
@ -546,7 +537,8 @@ do_background (file_op_context_t * ctx, char *info)
|
|||||||
(void) close (back_comm[0]);
|
(void) close (back_comm[0]);
|
||||||
(void) close (back_comm[1]);
|
(void) close (back_comm[1]);
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
return -1;
|
|
||||||
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user