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