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>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include "readcmd.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
|
|
|
|
int prompting = 0;
|
|
|
|
int job_id = 1;
|
|
|
|
list jobs;
|
2021-04-21 11:50:58 +00:00
|
|
|
pid_t pidFils;
|
2021-04-21 12:15:14 +00:00
|
|
|
struct cmdline *cmd;
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 20:19:58 +00:00
|
|
|
jmp_buf start;
|
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
void handler_print(int signal_num, siginfo_t *info)
|
|
|
|
{
|
2021-04-21 13:12:19 +00:00
|
|
|
cell* job = trouver(&jobs, info->si_pid);
|
|
|
|
if (job)
|
|
|
|
{ // pid dans la lite des proccess en fond
|
|
|
|
if (job->state)
|
|
|
|
{
|
|
|
|
job->state = 0;
|
|
|
|
siglongjmp(start, signal_num);
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
if (prompting)
|
2021-04-21 13:12:19 +00:00
|
|
|
{ // si il n'y a pas d'execution de proccess actuellement
|
2021-04-20 16:50:27 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2021-04-21 11:50:58 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
printf("[%d] (%d) done: %s\n", job->id, job->pid, job->cmd);
|
|
|
|
supprimer(&jobs, job->pid);
|
2021-04-20 16:50:27 +00:00
|
|
|
job_id--;
|
2021-04-21 11:50:58 +00:00
|
|
|
|
|
|
|
if (prompting)
|
|
|
|
{ // on réaffiche un prompt si besoin
|
|
|
|
siglongjmp(start, signal_num);
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
void handler_restart(int signal_num)
|
2021-04-20 19:42:13 +00:00
|
|
|
{
|
2021-04-20 20:19:58 +00:00
|
|
|
printf("\n");
|
|
|
|
siglongjmp(start, signal_num);
|
2021-04-20 19:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
void handler_stop(int signal_num)
|
2021-04-21 11:50:58 +00:00
|
|
|
{
|
|
|
|
if (!prompting)
|
|
|
|
{
|
|
|
|
kill(pidFils, SIGTSTP);
|
2021-04-21 13:12:19 +00:00
|
|
|
ajouter(&jobs, pidFils, job_id++, *(cmd->seq), 1);
|
|
|
|
printf("[%d] (%d) suspended: %s\n", job_id, pidFils, *(cmd->seq));
|
2021-04-21 11:50:58 +00:00
|
|
|
siglongjmp(start, signal_num);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-21 13:12:19 +00:00
|
|
|
handler_restart(signal_num);
|
2021-04-21 11:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
extern int errno;
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
initialiser(&jobs);
|
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-20 16:50:27 +00:00
|
|
|
action.sa_handler = handler_print;
|
|
|
|
sigaction(SIGCHLD, &action, NULL);
|
2021-04-20 20:19:58 +00:00
|
|
|
action.sa_handler = handler_restart;
|
|
|
|
sigaction(SIGINT, &action, NULL);
|
2021-04-21 11:50:58 +00:00
|
|
|
action.sa_handler = handler_stop;
|
|
|
|
sigaction(SIGTSTP, &action, NULL);
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
// variables pour afficher le path
|
2021-04-20 16:50:27 +00:00
|
|
|
char initcd[256], currentcd[256];
|
|
|
|
getcwd(initcd, 256);
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 16:54:36 +00:00
|
|
|
// loop principal
|
2021-04-20 16:50:27 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2021-04-20 20:19:58 +00:00
|
|
|
if (sigsetjmp(start, 1) == 2)
|
|
|
|
{ // si on provient du jump
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
getcwd(currentcd, 256);
|
|
|
|
printf("%s >>> ", currentcd);
|
2021-04-20 16:54:36 +00:00
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
prompting = 1;
|
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)
|
|
|
|
{ // on quitte le shell, EOF
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cmd->seq[0] == NULL)
|
2021-04-20 16:50:27 +00:00
|
|
|
{ // ligne vide, on skip
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-21 11:50:58 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "bg"))
|
|
|
|
{
|
|
|
|
kill(pidFils, SIGCONT);
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "exit"))
|
2021-04-20 20:19:58 +00:00
|
|
|
{
|
2021-04-20 16:50:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "cd"))
|
|
|
|
{ // 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 11:50:58 +00:00
|
|
|
{ // vide, sans path
|
2021-04-20 17:03:25 +00:00
|
|
|
ret = chdir(initcd);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // avec un 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-20 17:00:16 +00:00
|
|
|
{ // si le path n'existe pas
|
|
|
|
fprintf(stderr, "ERROR: cd failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
}
|
2021-04-20 16:50:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "jobs"))
|
2021-04-20 16:54:36 +00:00
|
|
|
{ // on affiche tous les process de fond
|
2021-04-20 16:50:27 +00:00
|
|
|
afficher(&jobs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-21 11:50:58 +00:00
|
|
|
pidFils = fork();
|
2021-04-20 16:12:44 +00:00
|
|
|
|
2021-04-20 16:50:27 +00:00
|
|
|
if (pidFils == -1)
|
2021-04-20 16:54:36 +00:00
|
|
|
{ // si le fork échoue
|
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-20 16:50:27 +00:00
|
|
|
if (pidFils == 0)
|
2021-04-20 16:54:36 +00:00
|
|
|
{ // instructions du fils
|
2021-04-21 11:50:58 +00:00
|
|
|
if (cmd->backgrounded)
|
|
|
|
{
|
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
|
|
|
|
}
|
2021-04-20 17:03:49 +00:00
|
|
|
execvp(cmd->seq[0][0], cmd->seq[0]);
|
2021-04-20 16:50:27 +00:00
|
|
|
exit(errno); // si execlp échoue on exit avec une erreur
|
|
|
|
}
|
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-20 17:06:59 +00:00
|
|
|
{ // on sauvegarde le pid, si background
|
2021-04-21 13:12:19 +00:00
|
|
|
ajouter(&jobs, pidFils, job_id++, *(cmd->seq), 0);
|
|
|
|
printf("[%d] %d\n", job_id, pidFils);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
else
|
2021-04-20 17:06:59 +00:00
|
|
|
{ // on attend le fils, si foreground
|
2021-04-20 16:50:27 +00:00
|
|
|
int codeTerm;
|
|
|
|
pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils
|
|
|
|
|
|
|
|
if (idFils == -1)
|
|
|
|
{ // si le wait fail
|
2021-04-21 11:50:58 +00:00
|
|
|
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", codeTerm, errno, strerror(errno));
|
|
|
|
exit(errno);
|
2021-04-20 16:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (codeTerm)
|
2021-04-20 16:54:36 +00:00
|
|
|
{ // si l'exec a échoué
|
2021-04-20 16:50:27 +00:00
|
|
|
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pidFils, codeTerm, strerror(codeTerm));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
liberer(&jobs);
|
2021-04-20 16:50:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
2021-04-20 16:12:44 +00:00
|
|
|
}
|