1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#include <sys/wait.h>
int main(void)
{
int pidfils,returncode;
bool bCommand=true;
char *pStrCommand;
char binarypath[255]="/usr/bin/";
pStrCommand=calloc(255,sizeof(char));
fprintf(stdout, "Programme de test pour vérifier qu'il soit possible d'exécuter des commandes"
" tant que \"exit\" ne soit pas encodé...\n");
while(bCommand==true)
{
fprintf(stderr,"[SHELL]>\t");
//fgets(pStrCommand,255,stdin); avec l'usage de execl il vaut mieux ne pas utiliser des fonctions de type "bufferisé"...
int octetslus=read(STDIN_FILENO,pStrCommand,255);
octetslus--;
*(pStrCommand+octetslus)='\0'; // on remplace le '\n' par '\0' sinon ça ne marchera pas avec execl...
pidfils=fork();
if(!pidfils) // FILS
{
fprintf(stderr,"\n[DEBUG] FILS...\n");
fprintf(stderr,"[FILS] pid %05d\n",pidfils);
fprintf(stderr,"[FILS] pStrCommand %s\n",pStrCommand);
if(strstr(pStrCommand,"exit")!=NULL) exit(1);
strcat(binarypath,pStrCommand);
fprintf(stderr,"[FILS] binarypath %s\n",binarypath);
// A partir d'ici le Code Segment du processus sera remplacé par "ps" (ou n'importe quel autre programme exécutable)
execl(binarypath,pStrCommand,NULL);
}
// PERE
int code;
returncode=wait(&code);
if(returncode!=-1)
{
fprintf(stderr,"[PROC DEATH] id: %05d\n",returncode);
if(code==256) bCommand=false; // exit(1) -> 256, exit(2) -> 512, ... exit(-1) -> 255, ... c'est un peu bizarre ^^
}
fprintf(stderr,"[PERE] bCommand %s\n",bCommand?"true":"false");
strcpy(binarypath,"/usr/bin/");
}
} |
Partager