X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fhandler_door.c;h=b2c6c2bb0d2960c475183ef9cc75c526605245d5;hb=27821403db9a9488cc508899af92f90c9bbade34;hp=1f959fdc8f8220673a1cd57b259cfbdbd20a34ff;hpb=d0ec658923323d126ffbe20e0c7a439affc5a6ea;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_door.c b/src/server/handler_door.c index 1f959fd..b2c6c2b 100644 --- a/src/server/handler_door.c +++ b/src/server/handler_door.c @@ -17,9 +17,8 @@ #include #include #include -#if !USE_POPEN -# include -#endif +#include +#include #define DOOR_UNLOCKED_DELAY 10 // 10 seconds before it re-locks @@ -38,10 +37,19 @@ tHandler gDoor_Handler = { Door_DoDispense }; char *gsDoor_Password = ""; +volatile int giDoor_ChildStatus; // == CODE === +void Door_SIGCHLDHandler(int signum) +{ + signum = 0; + giDoor_ChildStatus ++; + printf("SIGCHLD: giDoor_ChildStatus = %i \n", giDoor_ChildStatus); +} + int Door_InitHandler(void) -{ +{ + signal(SIGCHLD, Door_SIGCHLDHandler); return 0; } @@ -80,6 +88,7 @@ int Door_DoDispense(int User, int Item) int stdin_pair[2]; int stdout_pair[2]; pid_t childPid; + pid_t parentPid; #if DEBUG printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item); @@ -104,6 +113,8 @@ int Door_DoDispense(int User, int Item) return -1; } + giDoor_ChildStatus = 0; // Set child status to zero + parentPid = getpid(); childPid = fork(); if( childPid < 0 ) @@ -120,7 +131,8 @@ int Door_DoDispense(int User, int Item) // Close read end of stdout, and set it to #1 close(stdout_pair[0]); dup2(stdout_pair[1], 1); - execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL); + //execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL); + execl("/usr/bin/llogin", "llogin" "door" "-w-", NULL); perror("execl"); exit(-1); } @@ -129,17 +141,30 @@ int Door_DoDispense(int User, int Item) close(stdin_pair[0]); // child stdin read close(stdout_pair[1]); // child stdout write - if(read(stdout_pair[1], buf, 512) < 0) { + { + int len; + if( giDoor_ChildStatus || (len = read(stdout_pair[0], buf, 512)) < 0) + { + #if DEBUG + int child_exit; + waitpid(childPid, &child_exit, 0); + printf("Door_DoDispense: fread fail (child status %i)\n", child_exit); + #endif + return -1; + } + buf[len] = '\0'; + #if DEBUG - printf("Door_DoDispense: fread fail\n"); + printf("Door_DoDispense: buf = %i '%s'\n", len, buf); #endif - return -1; } // Send password - if( fputs(gsDoor_Password, child_stdin) <= 0 ) { + if( giDoor_ChildStatus || fputs(gsDoor_Password, child_stdin) <= 0 ) { #if DEBUG - printf("Door_DoDispense: fputs password\n"); + int child_exit; + waitpid(childPid, &child_exit, 0); + printf("Door_DoDispense: fputs password fail (child status %i)\n", child_exit); #endif return -1; } @@ -151,7 +176,7 @@ int Door_DoDispense(int User, int Item) #endif // ATH1 - Unlock door - if( fputs("ATH1\n", child_stdin) <= 0) { + if( giDoor_ChildStatus || fputs("ATH1\n", child_stdin) <= 0) { #if DEBUG printf("Door_DoDispense: fputs unlock\n"); #endif @@ -166,8 +191,8 @@ int Door_DoDispense(int User, int Item) printf("Door_DoDispense: Door re-lock\n"); #endif - // Re-lock the door - if( fputs("ATH0\n", child_stdin) <= 0 ) { + // Re-lock the door (and quit llogin) + if( giDoor_ChildStatus || fputs("ATH0\n\x1D", child_stdin) == 0 ) { #if DEBUG printf("Door_DoDispense: fputs lock\n"); #endif @@ -182,6 +207,8 @@ int Door_DoDispense(int User, int Item) printf("Door_DoDispense: User %i opened door\n", User); #endif + kill(childPid, SIGKILL); + return 0; }