back to looking like the GNU tutorial
This commit is contained in:
parent
43f1295324
commit
62cdc4ca84
12 changed files with 52 additions and 42 deletions
|
|
@ -57,6 +57,7 @@ exec/process_redirect.c\
|
|||
exec/process_reset.c\
|
||||
exec/process_setexec.c\
|
||||
exec/process_setgroup.c\
|
||||
exec/process_setsig.c\
|
||||
exec/set_exitstatus.c\
|
||||
glob/dir_glob.c\
|
||||
glob/expand_brace.c\
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ struct s_process
|
|||
pid_t pid;
|
||||
int fdin;
|
||||
int fdout;
|
||||
int toclose;
|
||||
int status;
|
||||
t_flag attributes;
|
||||
};
|
||||
|
|
@ -83,8 +84,9 @@ int exec_command(t_btree **ast);
|
|||
|
||||
int launch_process(t_process *p);
|
||||
int process_setexec(t_type type, t_process *p);
|
||||
int process_setgroup(t_process *p);
|
||||
int process_setgroup(t_process *p, pid_t pid);
|
||||
int process_redirect(t_process *p);
|
||||
void process_setsig(void);
|
||||
void process_free(void *content, size_t content_size);
|
||||
void process_reset(void);
|
||||
|
||||
|
|
|
|||
|
|
@ -25,11 +25,13 @@ int exec_pipe(t_btree **ast)
|
|||
DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]);
|
||||
p->fdout = fds[PIPE_WRITE];
|
||||
start = IS_PIPESTART(p->attributes);
|
||||
p->toclose = fds[PIPE_READ];
|
||||
|
||||
p->attributes &= ~PROCESS_PIPEEND;
|
||||
ft_exec(&(*ast)->left);
|
||||
p->attributes &= ~PROCESS_PIPESTART;
|
||||
|
||||
p->toclose = STDIN;
|
||||
close(fds[PIPE_WRITE]);
|
||||
p->fdout = STDOUT;
|
||||
p->fdin = fds[PIPE_READ];
|
||||
|
|
|
|||
|
|
@ -39,19 +39,17 @@ int launch_process(t_process *p)
|
|||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
process_setgroup(p);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGQUIT, SIG_DFL);
|
||||
signal(SIGTSTP, SIG_DFL);
|
||||
signal(SIGTTIN, sigttin_handler);
|
||||
signal(SIGTTOU, sigttou_handler);
|
||||
signal(SIGCHLD, SIG_DFL);
|
||||
process_setgroup(p, 0);
|
||||
process_setsig();
|
||||
process_redirect(p);
|
||||
(*p->execf)(p->path, p->av, data_singleton()->env);
|
||||
exit(42);
|
||||
}
|
||||
else if (pid > 0)
|
||||
{
|
||||
p->pid = pid;
|
||||
process_setgroup(p, pid);
|
||||
}
|
||||
else if (pid == -1)
|
||||
perror("fork");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,15 +14,15 @@
|
|||
|
||||
int process_redirect(t_process *p)
|
||||
{
|
||||
if (p->toclose != STDIN)
|
||||
close(p->toclose);
|
||||
if (p->fdin != STDIN)
|
||||
{
|
||||
/* DG("redirect STDIN to %i", p->fdin); */
|
||||
dup2(p->fdin, STDIN);
|
||||
close(p->fdin);
|
||||
}
|
||||
if (p->fdout != STDOUT)
|
||||
{
|
||||
/* DG("redirect STDOUT to %i", p->fdout); */
|
||||
dup2(p->fdout, STDOUT);
|
||||
close(p->fdout);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,22 +13,22 @@
|
|||
#include "job_control.h"
|
||||
#include "exec.h"
|
||||
|
||||
int process_setgroup(t_process *p)
|
||||
int process_setgroup(t_process *p, pid_t pid)
|
||||
{
|
||||
t_job *job;
|
||||
int pid;
|
||||
t_job *j;
|
||||
|
||||
(void)p;
|
||||
job = &data_singleton()->exec.job;
|
||||
pid = getpid();
|
||||
if (job->pgid == 0)
|
||||
job->pgid = pid;
|
||||
if (setpgid(pid, job->pgid))
|
||||
DG("setpgid(%i, %i) failed", pid, job->pgid);
|
||||
if (JOB_IS_FG(job->attributes))
|
||||
if (!SHELL_HAS_JOBC(data_singleton()->opts))
|
||||
return (0);
|
||||
j = &data_singleton()->exec.job;
|
||||
if (!j->pgid)
|
||||
j->pgid = pid ? pid : getpid();
|
||||
DG("in pid %i gonna setpgid(%i, %i)", getpid(), pid, j->pgid);
|
||||
setpgid(pid, j->pgid);
|
||||
if (pid == 0 && JOB_IS_FG(j->attributes))
|
||||
{
|
||||
signal(SIGTTOU, SIG_IGN);
|
||||
tcsetpgrp(STDIN, job->pgid);
|
||||
tcsetpgrp(STDIN, j->pgid);
|
||||
signal(SIGTTOU, SIG_DFL);
|
||||
}
|
||||
return (0);
|
||||
|
|
|
|||
11
42sh/src/exec/process_setsig.c
Normal file
11
42sh/src/exec/process_setsig.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include "exec.h"
|
||||
|
||||
void process_setsig(void)
|
||||
{
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGQUIT, SIG_DFL);
|
||||
signal(SIGTSTP, SIG_DFL);
|
||||
signal(SIGTTIN, SIG_DFL);
|
||||
signal(SIGTTOU, SIG_DFL);
|
||||
signal(SIGCHLD, SIG_DFL);
|
||||
}
|
||||
|
|
@ -12,10 +12,10 @@
|
|||
|
||||
#include "job_control.h"
|
||||
|
||||
int put_job_in_background(t_job *job, int cont)
|
||||
int put_job_in_background(t_job *j, int cont)
|
||||
{
|
||||
if (cont)
|
||||
if (kill(-job->pgid, SIGCONT) < 0)
|
||||
perror("kill (SIGCONT)");
|
||||
if (kill(-j->pgid, SIGCONT) < 0)
|
||||
DG("kill (SIGCONT) malfunction");
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,27 +12,24 @@
|
|||
|
||||
#include "job_control.h"
|
||||
|
||||
int put_job_in_foreground(t_job *job, int cont)
|
||||
int put_job_in_foreground(t_job *j, int cont)
|
||||
{
|
||||
t_jobc *jobc;
|
||||
|
||||
jobc = &data_singleton()->jobc;
|
||||
tcsetpgrp (STDIN, j->pgid);
|
||||
if (cont)
|
||||
{
|
||||
signal(SIGTTOU, SIG_IGN);
|
||||
if (tcsetpgrp(STDIN, job->pgid) == -1)
|
||||
return (1);
|
||||
signal(SIGTTOU, sigttou_handler);
|
||||
tcsetattr(STDIN, TCSANOW, &job->tmodes);
|
||||
if (kill(-job->pgid, SIGCONT) < 0)
|
||||
perror("kill (SIGCONT)");
|
||||
tcsetattr(STDIN, TCSADRAIN, &j->tmodes);
|
||||
if (kill(-j->pgid, SIGCONT) < 0)
|
||||
DG("kill (SIGCONT) malfunction");
|
||||
}
|
||||
job_wait(job->id);
|
||||
job_remove(job->id);
|
||||
signal(SIGTTOU, SIG_IGN);
|
||||
job_wait(j->id);
|
||||
job_remove(j->id);
|
||||
|
||||
tcsetpgrp(STDIN, jobc->shell_pgid);
|
||||
signal(SIGTTOU, sigttou_handler);
|
||||
tcgetattr(STDIN, &job->tmodes);
|
||||
|
||||
tcgetattr(STDIN, &j->tmodes);
|
||||
tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,5 +15,5 @@
|
|||
void sigttin_handler(int signo)
|
||||
{
|
||||
(void)signo;
|
||||
DG("got SIGTTIN");
|
||||
DG("got SIGTTIN, pid=%i, pgid=%i", getpid(), getpgrp());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,5 +15,5 @@
|
|||
void sigttou_handler(int signo)
|
||||
{
|
||||
(void)signo;
|
||||
DG("got SIGTTOU");
|
||||
DG("got SIGTTOU, pid=%i, pgid=%i", getpid(), getpgid(getpid()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,8 @@ void shell_init(int ac, char **av)
|
|||
signal(SIGINT, sigint_handler);
|
||||
signal(SIGQUIT, SIG_IGN);
|
||||
signal(SIGTSTP, sigtstp_handler);
|
||||
/* signal(SIGTSTP, SIG_IGN); */
|
||||
signal(SIGTTIN, sigttin_handler);
|
||||
signal(SIGTTOU, sigttou_handler);
|
||||
signal(SIGTTIN, SIG_IGN);
|
||||
signal(SIGTTOU, SIG_IGN);
|
||||
signal(SIGCHLD, sigchld_handler);
|
||||
*shell_pgid = getpid();
|
||||
if (setpgid(*shell_pgid, *shell_pgid))
|
||||
|
|
|
|||
Loading…
Reference in a new issue