Esempio n. 1
0
static void		manage_u(t_all *all, char ***e_b, char ***e_d, char ***arg_d)
{
	*e_b = str_array_dup(all->envp, 0);
	if (!ft_strcmp(all->ch_arg[2], "PATH"))
		all->path = NULL;
	if (all->my_envp)
		free_string_array(&all->envp);
	all->envp = str_array_dup(*e_d, 0);
	all->env_list = store_env(*e_d);
	all->cmd = ft_strdup(all->ch_arg[3]);
	all->ch_arg = str_array_dup(*arg_d, 0);
	manage_choice(all);
	if (all->my_envp)
		free_string_array(&all->envp);
	all->envp = str_array_dup(*e_b, 0);
	all->env_list = store_env(all->envp);
	all->path = get_path(all->env_list);
}
void script_exec(const char* path, char** envp)
{
    pid_t pid = fork();

    if(0 != pid)
    {
        store_env(pid, envp);

        // parent
        struct sigaction sa;
        sa.sa_handler = &script_done_handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
        if (sigaction(SIGCHLD, &sa, 0) == -1) 
        {
            perror(0);
            exit(1);
        }
    }
    else
    {
        char* args[] = { NULL, NULL };
        args[0] = (char*)path;

        LOG_DEBUG(Scene, "Executing %s", path);

        /* Create a new SID for the child process */  
        pid_t sid = setsid();  
        if (sid < 0)    
        {  
            exit(EXIT_FAILURE);  
        }  
    
        int fd = open("/dev/null",O_RDWR, 0);  
    
        if (fd != -1)  
        {  
            dup2 (fd, STDIN_FILENO);  
            dup2 (fd, STDOUT_FILENO);  
            dup2 (fd, STDERR_FILENO);  
      
            if (fd > 2)  
            {  
                close (fd);  
            }  
        }  
      
        /*resettign File Creation Mask */  
        umask(027); 

        execve(path, args, envp);
        perror("execle");
        exit(1);
    }
}