Exemple #1
0
static int spawn_worker(char *cmd) 
{
	/* params struct for both workers */
	struct worker_params params;
	/* children's pids */
	pid_t c1, c2;
	int status, ret = 0;

	trim(cmd);
	params.cmd = cmd;

	if(open_pipe(params.pipe) == -1) {
		(void) fprintf(stderr, "%s: Could not create pipe\n", pgname);
		return -1;
	}

	/* We flush all our standard fd's so we'll have them empty in the workers */
	fflush(stdin);
	fflush(stdout);
	fflush(stderr);

	/* Fork execute worker */
	if((c1 = fork_function(execute, &params)) == -1) {
		(void) fprintf(stderr, "%s: Could not spawn execute worker\n", pgname);
		close_pipe(params.pipe, channel_all);
		return -1;
	}

	/* Fork format worker */
	if((c2 = fork_function(format, &params)) == -1) {
		(void) fprintf(stderr, "%s: Could not spawn format worker\n", pgname);
		/* Wait for child 1 */
		if(wait_for_child(c1) == -1) {
			(void) fprintf(stderr, "%s: Error waiting for execute worker to finish\n", pgname);
		}
		close_pipe(params.pipe, channel_all);
		return -1;
	}

	/* We need to close the pipe in parent, so that the format worker will quit working when execute's output has finished */
	close_pipe(params.pipe, channel_all);
	
	if((status = wait_for_child(c1)) != 0) {
		(void) fprintf(stderr, "%s: Execute worker returned %d\n", pgname, status);
		/* not neccessarily an error. If there was a typo in cmd don't quit the whole programm */
	//	ret = -1;
	}

	if((status = wait_for_child(c2)) != 0) {
		(void) fprintf(stderr, "%s: Format worker returned %d\n", pgname, status);
	//	ret = -1;
	}
	
	return ret;
}
Exemple #2
0
static void		pipe_exec(char **command, t_env *e, int fork)
{
	static char	*builtins[] = { BUILTINS };
	static void	(*function[])(char **, t_env *) = { FUNCTION };
	char		*path;
	int			i;

	i = 0;
	while (builtins[i])
	{
		if (ft_strcmp(*command, builtins[i]) == 0)
		{
			function[i](command, e);
			return ;
		}
		i++;
	}
	if (!fork)
		pipe_fork(command, e);
	else
	{
		path = fork_function(command, e);
		if (path)
			free(path);
	}
}