#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "readcmd.h" #include "jobs.h" extern int errno; struct cmdline *cmd; int pid_fils; int job_id = 0; list jobs; int prompting = 0; jmp_buf goto_prompt; char initcd[256], currentcd[256]; void handler_sigchld(int sig_num, siginfo_t *info) { cell *job = trouver(&jobs, info->si_pid); if (job && !job->state) { if (prompting) { printf("\n"); } printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd); supprimer(&jobs, job->pid); job_id--; if (prompting) { siglongjmp(goto_prompt, sig_num); } } } void handler_sigint(int sig_num) { printf("\n"); siglongjmp(goto_prompt, sig_num); } void handler_sigtstp(int sig_num) { if (!prompting) { kill(pid_fils, SIGTSTP); cell *job = ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 1); printf("[%d] %d suspended: %s\n", job_id, pid_fils, job->cmd); siglongjmp(goto_prompt, sig_num); } else { handler_sigint(sig_num); } } void attendre(int pid) { int wait_code; pid_t id = waitpid(pid, &wait_code, 0); // on attend la fin de l'exec du fils 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: %d's execution failed, (%d) %s\n", pid, wait_code, strerror(wait_code)); } } 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); // loop principal 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" kill(pid_fils, SIGCONT); continue; } else if (!strcmp(cmd->seq[0][0], "fg")) { // "fg" cell *job; if (cmd->seq[0][1] == NULL) { // no id fprintf(stderr, "ERROR: fg id error"); continue; } else { // id specified job = &(jobs[atoi(cmd->seq[0][1]) - 1]); } kill(job->pid, SIGCONT); printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd); supprimer(&job, job->pid); job_id--; attendre(job->pid); continue; } 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 action.sa_handler = SIG_DFL; sigaction(SIGTSTP, &action, NULL); // on default SIGTSTP sigaction(SIGCONT, &action, NULL); // on default SIGCONT if (cmd->backgrounded) { // background action.sa_handler = SIG_IGN; sigaction(SIGINT, &action, NULL); // on ignore SIGINT sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP } 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 ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 0); printf("[%d] %d\n", job_id, pid_fils); } else { // foreground attendre(pid_fils); } } } return EXIT_SUCCESS; }