src/background.c: clean up and fix coding style.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2018-10-11 14:33:28 +03:00
parent 6d79aa7d5d
commit 3b8ee664f2
1 changed files with 41 additions and 49 deletions

View File

@ -106,15 +106,15 @@ static int
destroy_task (pid_t pid)
{
TaskList *p = task_list;
TaskList *prev = 0;
TaskList *prev = NULL;
while (p)
while (p != NULL)
{
if (p->pid == pid)
{
int fd = p->fd;
if (prev)
if (prev != NULL)
prev->next = p->next;
else
task_list = p->next;
@ -127,7 +127,7 @@ destroy_task (pid_t pid)
}
/* 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)))
{
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 */
kill (ctx->pid, SIGTERM);
@ -241,53 +242,41 @@ background_attention (int fd, void *closure)
return 0;
}
if ((read (fd, &argc, sizeof (argc)) != sizeof (argc)) ||
(read (fd, &type, sizeof (type)) != sizeof (type)) ||
(read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx)))
{
if (read (fd, &argc, sizeof (argc)) != sizeof (argc) ||
read (fd, &type, sizeof (type)) != sizeof (type) ||
read (fd, &have_ctx, sizeof (have_ctx)) != sizeof (have_ctx))
return reading_failed (-1, data);
}
if (argc > MAXCALLARGS)
{
message (D_ERROR, _("Background protocol error"), "%s",
_("Background process sent us a request for more arguments\n"
"than we can handle."));
}
if (have_ctx)
{
if (read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
{
return reading_failed (-1, data);
}
}
if (have_ctx != 0 && read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
return reading_failed (-1, data);
for (i = 0; i < argc; i++)
{
int size;
if (read (fd, &size, sizeof (size)) != sizeof (size))
{
return reading_failed (i - 1, data);
}
data[i] = g_malloc (size + 1);
if (read (fd, data[i], size) != size)
{
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 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)
break;
}
if (p)
if (p != NULL)
to_child_fd = p->to_child_fd;
if (to_child_fd == -1)
@ -298,7 +287,7 @@ background_attention (int fd, void *closure)
{
int result = 0;
if (!have_ctx)
if (have_ctx == 0)
switch (argc)
{
case 0:
@ -344,7 +333,7 @@ background_attention (int fd, void *closure)
/* Send the result code and the value for shared variables */
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));
}
else if (type == Return_String)
@ -375,7 +364,8 @@ background_attention (int fd, void *closure)
default:
g_assert_not_reached ();
}
if (resstr)
if (resstr != NULL)
{
len = strlen (resstr);
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));
}
}
for (i = 0; i < argc; i++)
g_free (data[i]);
repaint_screen ();
(void) ret;
return 0;
}
/* --------------------------------------------------------------------------------------------- */
/* }}} */
@ -414,15 +406,15 @@ parent_call_header (void *routine, int argc, enum ReturnType type, file_op_conte
int have_ctx;
ssize_t ret;
have_ctx = (ctx != NULL);
have_ctx = ctx != NULL ? 1 : 0;
ret = write (parent_fd, &routine, sizeof (routine));
ret = write (parent_fd, &argc, sizeof (argc));
ret = write (parent_fd, &type, sizeof (type));
ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
if (have_ctx)
if (have_ctx != 0)
ret = write (parent_fd, ctx, sizeof (*ctx));
(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));
if (ctx)
if (ctx != NULL)
ret = read (from_parent_fd, ctx, sizeof (*ctx));
(void) ret;
return i;
}
@ -471,24 +464,21 @@ parent_va_call_string (void *routine, int argc, va_list ap)
len = va_arg (ap, int);
value = va_arg (ap, void *);
if ((write (parent_fd, &len, sizeof (len)) != sizeof (len)) ||
(write (parent_fd, value, len) != len))
{
if (write (parent_fd, &len, sizeof (len)) != sizeof (len) ||
write (parent_fd, value, len) != len)
return NULL;
}
}
if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i))
return NULL;
if (!i)
if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i) || i == 0)
return NULL;
str = g_malloc (i + 1);
if (read (from_parent_fd, str, i) != i)
{
g_free (str);
return NULL;
}
str[i] = 0;
str[i] = '\0';
return str;
}
@ -508,12 +498,13 @@ unregister_task_running (pid_t pid, int fd)
void
unregister_task_with_pid (pid_t pid)
{
int fd = destroy_task (pid);
int fd;
fd = destroy_task (pid);
if (fd != -1)
delete_select_channel (fd);
}
/* --------------------------------------------------------------------------------------------- */
/**
* 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;
if (pipe (comm) == -1)
return -1;
return (-1);
if (pipe (back_comm) == -1)
return -1;
return (-1);
pid = fork ();
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[1]);
errno = saved_errno;
return -1;
return (-1);
}
if (pid == 0)