~emersion/mrsh

940df206f3525df1a50fde17ff47815bdf92d8fe — emersion 3 years ago 56de570
shell: improve error handling, fix process_poll
5 files changed, 11 insertions(+), 5 deletions(-)

M include/shell.h
M shell/process.c
M shell/task.c
M shell/task_list.c
M shell/task_process.c
M include/shell.h => include/shell.h +1 -0
@@ 8,6 8,7 @@ struct context {

struct process {
	pid_t pid;
	bool finished;
	int stat;
};


M shell/process.c => shell/process.c +3 -1
@@ 8,11 8,12 @@ static struct mrsh_array running_processes = {0};
void process_init(struct process *proc, pid_t pid) {
	mrsh_array_add(&running_processes, proc);
	proc->pid = pid;
	proc->finished = false;
	proc->stat = 0;
}

int process_poll(struct process *proc) {
	if (proc->stat == 0) {
	if (!proc->finished) {
		return -1;
	}
	return WEXITSTATUS(proc->stat);


@@ 36,6 37,7 @@ void process_notify(pid_t pid, int stat) {
	for (size_t i = 0; i < running_processes.len; ++i) {
		struct process *proc = running_processes.data[i];
		if (proc->pid == pid) {
			proc->finished = true;
			proc->stat = stat;
			process_remove(proc);
			break;

M shell/task.c => shell/task.c +3 -0
@@ 1,6 1,8 @@
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "shell.h"



@@ 38,6 40,7 @@ int task_run(struct task *task, struct context *ctx) {
			if (errno == EINTR) {
				continue;
			}
			fprintf(stderr, "failed to waitpid(): %s\n", strerror(errno));
			return -1;
		}


M shell/task_list.c => shell/task_list.c +2 -2
@@ 18,9 18,9 @@ static int task_list_poll(struct task *task, struct context *ctx) {

	int ret = 0;
	while (tl->current < tl->children.len) {
		struct task *current = tl->children.data[tl->current];
		struct task *child = tl->children.data[tl->current];

		ret = task_poll(current, ctx);
		ret = task_poll(child, ctx);
		if (ret < 0) {
			return ret;
		}

M shell/task_process.c => shell/task_process.c +2 -2
@@ 42,8 42,8 @@ static bool task_process_start(struct task *task, struct context *ctx) {

	pid_t pid = fork();
	if (pid < 0) {
		fprintf(stderr, "failed to fork()\n");
		return NULL;
		fprintf(stderr, "failed to fork(): %s\n", strerror(errno));
		return false;
	} else if (pid == 0) {
		int argc = 1 + sc->arguments.len;
		char *argv[argc + 1];