3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_doror.c - Door Relay code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
23 #define DOOR_UNLOCKED_DELAY 10 // 10 seconds before it re-locks
28 int Door_InitHandler();
29 int Door_CanDispense(int User, int Item);
30 int Door_DoDispense(int User, int Item);
33 tHandler gDoor_Handler = {
39 char *gsDoor_Password = "";
40 volatile int giDoor_ChildStatus;
43 void Door_SIGCHLDHandler(int signum)
46 giDoor_ChildStatus ++;
47 printf("SIGCHLD: giDoor_ChildStatus = %i \n", giDoor_ChildStatus);
50 int Door_InitHandler(void)
52 signal(SIGCHLD, Door_SIGCHLDHandler);
58 int Door_CanDispense(int User, int Item)
61 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
64 if( Item != 0 ) return -1;
66 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
69 printf("Door_CanDispense: User %i not in door\n", User);
75 printf("Door_CanDispense: User %i can open the door\n", User);
82 * \brief Actually do a dispense from the coke machine
84 int Door_DoDispense(int User, int Item)
94 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
98 if( Item != 0 ) return -1;
100 // Check if user is in door
101 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
104 printf("Door_CanDispense: User %i not in door\n", User);
109 // Create stdin/stdout
110 if( pipe(stdin_pair) || pipe(stdout_pair) )
116 giDoor_ChildStatus = 0; // Set child status to zero
117 parentPid = getpid();
129 // Close write end of stdin, and set it to #0
130 close(stdin_pair[1]); dup2(stdin_pair[0], 0);
131 // Close read end of stdout, and set it to #1
132 close(stdout_pair[0]); dup2(stdout_pair[1], 1);
134 //execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
135 execl("/usr/bin/llogin", "llogin" "door" "-w-", NULL);
140 child_stdin = fdopen(stdin_pair[1], "w");
141 close(stdin_pair[0]); // child stdin read
142 close(stdout_pair[1]); // child stdout write
146 if( giDoor_ChildStatus || (len = read(stdout_pair[0], buf, 512)) < 0)
150 waitpid(childPid, &child_exit, 0);
151 printf("Door_DoDispense: fread fail (child status %i)\n", child_exit);
158 printf("Door_DoDispense: buf = %i '%s'\n", len, buf);
163 if( giDoor_ChildStatus || fputs(gsDoor_Password, child_stdin) <= 0 ) {
166 waitpid(childPid, &child_exit, 0);
167 printf("Door_DoDispense: fputs password fail (child status %i)\n", child_exit);
171 fputs("\n", child_stdin);
175 printf("Door_DoDispense: Door unlock\n");
178 // ATH1 - Unlock door
179 if( giDoor_ChildStatus || fputs("ATH1\n", child_stdin) <= 0) {
181 printf("Door_DoDispense: fputs unlock\n");
186 // Wait before re-locking
187 sleep(DOOR_UNLOCKED_DELAY);
191 printf("Door_DoDispense: Door re-lock\n");
194 // Re-lock the door (and quit llogin)
195 if( giDoor_ChildStatus || fputs("ATH0\n\x1D", child_stdin) == 0 ) {
197 printf("Door_DoDispense: fputs lock\n");
203 close(stdin_pair[1]); // child stdin write
204 close(stdout_pair[0]); // child stdout read
207 printf("Door_DoDispense: User %i opened door\n", User);
210 kill(childPid, SIGKILL);