projet-systeme-exploitation.../à zipper/Q7.c

259 lines
6.7 KiB
C
Raw Normal View History

2021-04-24 15:32:54 +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"
extern int errno;
struct cmdline *cmd;
2021-05-10 13:49:14 +00:00
int pid_fils, wait_code;
2021-04-24 15:32:54 +00:00
list jobs;
int prompting = 0;
jmp_buf goto_prompt;
char initcd[256], currentcd[256];
2021-05-10 13:49:14 +00:00
void handler_sigchld(int sig_num)
2021-04-24 15:32:54 +00:00
{
2021-05-10 13:49:14 +00:00
do
2021-04-24 15:32:54 +00:00
{
2021-05-10 13:49:14 +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-24 15:32:54 +00:00
}
2021-05-10 13:49:14 +00:00
cell *job = trouver(&jobs, pid_fils);
if (job != NULL)
2021-04-24 15:32:54 +00:00
{
2021-05-10 13:49:14 +00:00
if (prompting)
{
printf("\n");
}
if (WIFSTOPPED(wait_code))
{
printf("[%d] %d stopped: %s\n", job->id, job->pid, job->cmd);
}
else if (WIFCONTINUED(wait_code))
{
printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd);
if (!strcmp(cmd->seq[0][0], "fg"))
{
supprimer(&jobs, job->pid);
}
}
else if (WIFEXITED(wait_code))
{
printf("[%d] %d exited: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
}
else if (wait_code == SIGKILL)
{
printf("[%d] %d killed: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
}
2021-04-24 15:32:54 +00:00
}
2021-05-10 13:49:14 +00:00
} while (pid_fils > 0);
if (prompting)
{
siglongjmp(goto_prompt, sig_num);
2021-04-24 15:32:54 +00:00
}
}
void handler_sigint(int sig_num)
{
printf("\n");
2021-05-10 13:49:14 +00:00
if (!prompting)
{
kill(pid_fils, SIGKILL);
pause();
}
2021-04-24 15:32:54 +00:00
siglongjmp(goto_prompt, sig_num);
}
void handler_sigtstp(int sig_num)
{
2021-05-10 13:49:14 +00:00
printf("\n");
2021-04-24 15:32:54 +00:00
if (!prompting)
{
2021-05-10 13:49:14 +00:00
ajouter(&jobs, pid_fils, *(cmd->seq));
kill(pid_fils, SIGSTOP);
pause();
2021-04-24 15:32:54 +00:00
}
2021-05-10 13:49:14 +00:00
siglongjmp(goto_prompt, sig_num);
2021-04-24 15:32:54 +00:00
}
int main(int argc, char *argv[])
{
initialiser(&jobs);
getcwd(initcd, sizeof(initcd));
// gestion des signaux
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO | SA_RESTART;
action.sa_handler = handler_sigchld;
sigaction(SIGCHLD, &action, NULL);
action.sa_handler = handler_sigint;
sigaction(SIGINT, &action, NULL);
action.sa_handler = handler_sigtstp;
sigaction(SIGTSTP, &action, NULL);
2021-05-10 13:49:14 +00:00
// main loop
2021-04-24 15:32:54 +00:00
while (1)
{
sigsetjmp(goto_prompt, 1);
prompting = 1;
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
cmd = readcmd();
prompting = 0;
if (cmd == NULL)
{ // EOF
break;
}
else if (cmd->seq[0] == NULL)
{ // empty
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit"))
{ // "exit"
break;
}
else if (!strcmp(cmd->seq[0][0], "cd"))
{ // "cd"
int ret = 0;
if (cmd->seq[0][1] == NULL)
{ // no path
ret = chdir(initcd);
}
else
{ // with path
ret = chdir(cmd->seq[0][1]);
}
if (ret)
{ // wrong path
fprintf(stderr, "ERROR: cd failed, (%d) %s\n", errno, strerror(errno));
}
continue;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{ // "jobs"
afficher(&jobs);
continue;
}
else if (!strcmp(cmd->seq[0][0], "bg"))
{ // "bg"
2021-05-10 13:49:14 +00:00
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, SIGCONT);
pause();
2021-04-24 15:32:54 +00:00
continue;
}
else if (!strcmp(cmd->seq[0][0], "fg"))
{ // "fg"
cell *job;
if (cmd->seq[0][1] == NULL)
{ // no id
2021-05-10 13:49:14 +00:00
fprintf(stderr, "ERROR: id missing\n");
2021-04-24 15:32:54 +00:00
continue;
}
else
{ // id specified
2021-05-10 13:49:14 +00:00
job = trouver_id(&jobs, atoi(cmd->seq[0][1]));
pid_fils = job->pid;
2021-04-24 15:32:54 +00:00
}
2021-05-10 13:49:14 +00:00
kill(pid_fils, SIGCONT);
pause();
waitpid(pid_fils, NULL, 0);
2021-04-24 15:32:54 +00:00
continue;
}
2021-05-10 13:49:14 +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;
}
else if (!strcmp(cmd->seq[0][0], "pid"))
{ // "pid"
printf("pid=%d\n", getpid());
continue;
}
2021-04-24 15:32:54 +00:00
pid_fils = fork();
if (pid_fils == -1)
{ // fork fail ?
fprintf(stderr, "ERROR: forking failed, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
if (pid_fils == 0)
{ // instructions du fils
2021-05-10 13:49:14 +00:00
action.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &action, NULL); // on igonre SIGTSTP
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
2021-04-24 15:32:54 +00:00
execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
else
{ // instructions du père
if (cmd->backgrounded)
{ // background
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
printf("[%d] %d\n", id_fils, pid_fils);
}
else
{ // foreground
2021-05-10 13:49:14 +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 ?
fprintf(stderr, "ERROR: command failed, (%d) %s\n", wait_code, strerror(wait_code));
}
2021-04-24 15:32:54 +00:00
}
}
}
return EXIT_SUCCESS;
2021-05-10 13:49:14 +00:00
}