2021-04-20 16:12:44 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
2021-05-10 14:08:04 +00:00
|
|
|
#include <fcntl.h>
|
2021-04-20 16:12:44 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include "readcmd.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
extern int errno;
|
|
|
|
|
2021-04-21 12:15:14 +00:00
|
|
|
struct cmdline *cmd;
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-24 15:32:54 +00:00
|
|
|
int pid_fils, wait_code;
|
2021-04-21 14:18:53 +00:00
|
|
|
|
|
|
|
list jobs;
|
|
|
|
|
|
|
|
int prompting = 0;
|
|
|
|
jmp_buf goto_prompt;
|
|
|
|
|
|
|
|
char initcd[256], currentcd[256];
|
|
|
|
|
2021-05-10 14:08:04 +00:00
|
|
|
int file_in, file_out;
|
|
|
|
|
2021-04-24 15:32:54 +00:00
|
|
|
void handler_sigchld(int sig_num)
|
2021-04-20 16:50:27 +00:00
|
|
|
{
|
2021-04-24 15:32:54 +00:00
|
|
|
do
|
2021-04-21 14:18:53 +00:00
|
|
|
{
|
2021-04-24 15:32:54 +00:00
|
|
|
pid_fils = waitpid(-1, &wait_code, WNOHANG | WUNTRACED | WCONTINUED);
|
|
|
|
if ((pid_fils == -1) && (errno != ECHILD))
|
|
|
|
{ // wait fail ?
|
|
|
|
fprintf(stderr, "ERROR: waiting for child failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
exit(errno);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
2021-04-21 11:50:58 +00:00
|
|
|
|
2021-04-24 15:32:54 +00:00
|
|
|
cell *job = trouver(&jobs, pid_fils);
|
|
|
|
if (job != NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{
|
2021-04-24 15:32:54 +00:00
|
|
|
if (prompting)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
}
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-24 15:32:54 +00:00
|
|
|
if (WIFSTOPPED(wait_code))
|
|
|
|
{
|
2021-04-24 19:16:17 +00:00
|
|
|
printf("[%d] %d stopped: %s\n", job->id, job->pid, job->cmd);
|
2021-04-24 15:32:54 +00:00
|
|
|
}
|
|
|
|
else if (WIFCONTINUED(wait_code))
|
|
|
|
{
|
|
|
|
printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd);
|
2021-05-10 13:49:14 +00:00
|
|
|
if (!strcmp(cmd->seq[0][0], "fg"))
|
|
|
|
{
|
|
|
|
supprimer(&jobs, job->pid);
|
|
|
|
}
|
2021-04-24 15:32:54 +00:00
|
|
|
}
|
|
|
|
else if (WIFEXITED(wait_code))
|
|
|
|
{
|
2021-04-24 19:16:17 +00:00
|
|
|
printf("[%d] %d exited: %s\n", job->id, job->pid, job->cmd);
|
2021-04-24 15:32:54 +00:00
|
|
|
supprimer(&jobs, job->pid);
|
|
|
|
}
|
2021-04-24 19:16:17 +00:00
|
|
|
else if (wait_code == SIGKILL)
|
2021-04-24 17:53:12 +00:00
|
|
|
{
|
|
|
|
printf("[%d] %d killed: %s\n", job->id, job->pid, job->cmd);
|
|
|
|
supprimer(&jobs, job->pid);
|
|
|
|
}
|
2021-04-24 15:32:54 +00:00
|
|
|
}
|
|
|
|
} while (pid_fils > 0);
|
2021-04-24 19:16:17 +00:00
|
|
|
|
|
|
|
if (prompting)
|
|
|
|
{
|
|
|
|
siglongjmp(goto_prompt, sig_num);
|
|
|
|
}
|
2021-04-24 15:32:54 +00:00
|
|
|
}
|
2021-04-21 14:18:53 +00:00
|
|
|
|
|
|
|
void handler_sigint(int sig_num)
|
2021-04-20 19:42:13 +00:00
|
|
|
{
|
2021-04-20 20:19:58 +00:00
|
|
|
printf("\n");
|
2021-04-24 15:32:54 +00:00
|
|
|
if (!prompting)
|
|
|
|
{
|
|
|
|
kill(pid_fils, SIGKILL);
|
|
|
|
pause();
|
|
|
|
}
|
2021-04-21 14:18:53 +00:00
|
|
|
siglongjmp(goto_prompt, sig_num);
|
2021-04-20 19:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
void handler_sigtstp(int sig_num)
|
2021-04-21 11:50:58 +00:00
|
|
|
{
|
2021-04-24 15:32:54 +00:00
|
|
|
printf("\n");
|
2021-04-21 11:50:58 +00:00
|
|
|
if (!prompting)
|
|
|
|
{
|
2021-04-24 15:32:54 +00:00
|
|
|
ajouter(&jobs, pid_fils, *(cmd->seq));
|
|
|
|
kill(pid_fils, SIGSTOP);
|
|
|
|
pause();
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
2021-04-24 15:32:54 +00:00
|
|
|
siglongjmp(goto_prompt, sig_num);
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2021-04-20 16:50:27 +00:00
|
|
|
initialiser(&jobs);
|
2021-04-21 14:18:53 +00:00
|
|
|
getcwd(initcd, sizeof(initcd));
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
// gestion des signaux
|
2021-04-20 16:50:27 +00:00
|
|
|
struct sigaction action;
|
2021-04-20 20:19:58 +00:00
|
|
|
sigemptyset(&action.sa_mask);
|
2021-04-21 11:50:58 +00:00
|
|
|
action.sa_flags = SA_SIGINFO | SA_RESTART;
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = handler_sigchld;
|
2021-04-20 16:50:27 +00:00
|
|
|
sigaction(SIGCHLD, &action, NULL);
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = handler_sigint;
|
2021-04-20 20:19:58 +00:00
|
|
|
sigaction(SIGINT, &action, NULL);
|
2021-04-21 14:18:53 +00:00
|
|
|
action.sa_handler = handler_sigtstp;
|
2021-04-21 11:50:58 +00:00
|
|
|
sigaction(SIGTSTP, &action, NULL);
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-24 15:32:54 +00:00
|
|
|
// main loop
|
2021-04-20 16:50:27 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2021-04-21 14:18:53 +00:00
|
|
|
sigsetjmp(goto_prompt, 1);
|
2021-04-20 16:54:36 +00:00
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
prompting = 1;
|
2021-04-21 14:18:53 +00:00
|
|
|
getcwd(currentcd, sizeof(currentcd));
|
|
|
|
printf("%s >>> ", currentcd);
|
2021-04-21 12:15:14 +00:00
|
|
|
cmd = readcmd();
|
2021-04-20 16:50:27 +00:00
|
|
|
prompting = 0;
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 20:19:58 +00:00
|
|
|
if (cmd == NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // EOF
|
2021-04-20 20:19:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-10 14:09:34 +00:00
|
|
|
else if (cmd->err)
|
|
|
|
{ // error from readcmd
|
2021-05-10 14:10:18 +00:00
|
|
|
fprintf(stderr, "ERROR: readcmd failed, %s\n", cmd->err);
|
2021-05-10 14:09:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 20:19:58 +00:00
|
|
|
else if (cmd->seq[0] == NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // empty
|
2021-04-21 11:50:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "exit"))
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // "exit"
|
2021-04-20 16:50:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "cd"))
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // "cd"
|
2021-04-20 17:03:25 +00:00
|
|
|
int ret = 0;
|
2021-04-20 16:50:27 +00:00
|
|
|
if (cmd->seq[0][1] == NULL)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // no path
|
2021-04-20 17:03:25 +00:00
|
|
|
ret = chdir(initcd);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
else
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // with path
|
2021-04-20 17:03:25 +00:00
|
|
|
ret = chdir(cmd->seq[0][1]);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
2021-04-20 17:03:25 +00:00
|
|
|
if (ret)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // wrong path
|
2021-04-20 17:00:16 +00:00
|
|
|
fprintf(stderr, "ERROR: cd failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-24 15:32:54 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "jobs"))
|
|
|
|
{ // "jobs"
|
|
|
|
afficher(&jobs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "bg"))
|
|
|
|
{ // "bg"
|
|
|
|
cell *job;
|
|
|
|
if (cmd->seq[0][1] == NULL)
|
|
|
|
{ // no id
|
2021-04-24 17:53:12 +00:00
|
|
|
fprintf(stderr, "ERROR: id missing\n");
|
2021-04-24 15:32:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // id specified
|
|
|
|
job = trouver_id(&jobs, atoi(cmd->seq[0][1]));
|
|
|
|
}
|
|
|
|
kill(job->pid, SIGCONT);
|
|
|
|
pause();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "fg"))
|
|
|
|
{ // "fg"
|
|
|
|
cell *job;
|
|
|
|
if (cmd->seq[0][1] == NULL)
|
|
|
|
{ // no id
|
2021-04-24 17:53:12 +00:00
|
|
|
fprintf(stderr, "ERROR: id missing\n");
|
2021-04-24 15:32:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // id specified
|
|
|
|
job = trouver_id(&jobs, atoi(cmd->seq[0][1]));
|
2021-05-10 13:49:14 +00:00
|
|
|
pid_fils = job->pid;
|
2021-04-24 15:32:54 +00:00
|
|
|
}
|
2021-05-10 13:49:14 +00:00
|
|
|
kill(pid_fils, SIGCONT);
|
2021-04-24 15:32:54 +00:00
|
|
|
pause();
|
2021-05-10 13:49:14 +00:00
|
|
|
waitpid(pid_fils, NULL, 0);
|
2021-04-24 15:32:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-24 17:53:12 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "stop"))
|
|
|
|
{ // "stop"
|
|
|
|
cell *job;
|
|
|
|
if (cmd->seq[0][1] == NULL)
|
|
|
|
{ // no id
|
|
|
|
fprintf(stderr, "ERROR: id missing\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // id specified
|
|
|
|
job = trouver_id(&jobs, atoi(cmd->seq[0][1]));
|
|
|
|
}
|
|
|
|
kill(job->pid, SIGSTOP);
|
|
|
|
pause();
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-24 15:32:54 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "pid"))
|
|
|
|
{ // "pid"
|
|
|
|
printf("pid=%d\n", getpid());
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
pid_fils = fork();
|
|
|
|
if (pid_fils == -1)
|
|
|
|
{ // fork fail ?
|
2021-04-20 17:00:16 +00:00
|
|
|
fprintf(stderr, "ERROR: forking failed, (%d) %s\n", errno, strerror(errno));
|
2021-04-20 16:50:27 +00:00
|
|
|
exit(errno);
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 14:18:53 +00:00
|
|
|
if (pid_fils == 0)
|
2021-04-20 16:54:36 +00:00
|
|
|
{ // instructions du fils
|
2021-04-24 15:32:54 +00:00
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGTSTP, &action, NULL); // on igonre SIGTSTP
|
|
|
|
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
|
2021-04-21 14:18:53 +00:00
|
|
|
|
2021-05-10 14:08:04 +00:00
|
|
|
if (cmd->in)
|
|
|
|
{
|
|
|
|
file_in = open(cmd->in, O_RDONLY);
|
|
|
|
dup2(file_in, 0);
|
|
|
|
}
|
|
|
|
if (cmd->out)
|
|
|
|
{
|
|
|
|
file_out = open(cmd->out, O_CREAT | O_TRUNC | O_WRONLY, 0640);
|
|
|
|
dup2(file_out, 1);
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:03:49 +00:00
|
|
|
execvp(cmd->seq[0][0], cmd->seq[0]);
|
2021-05-10 14:08:04 +00:00
|
|
|
fprintf(stderr, "ERROR: execvp failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
exit(errno); // si execvp échoue on exit avec une erreur
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
2021-04-20 16:54:36 +00:00
|
|
|
else
|
|
|
|
{ // instructions du père
|
2021-04-20 16:50:27 +00:00
|
|
|
if (cmd->backgrounded)
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // background
|
2021-04-24 15:32:54 +00:00
|
|
|
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
|
|
|
|
printf("[%d] %d\n", id_fils, pid_fils);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
else
|
2021-04-21 14:18:53 +00:00
|
|
|
{ // foreground
|
2021-04-24 19:16:17 +00:00
|
|
|
pid_t id = waitpid(pid_fils, &wait_code, 0);
|
|
|
|
|
|
|
|
if (id == -1)
|
|
|
|
{ // wait fail ?
|
|
|
|
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno));
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_code)
|
|
|
|
{ // execvp fail ?
|
2021-05-10 14:08:04 +00:00
|
|
|
fprintf(stderr, "ERROR: child failed, (%d) %s\n", wait_code, strerror(wait_code));
|
2021-04-24 19:16:17 +00:00
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|