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.
24 #define DOOR_UNLOCKED_DELAY 10 // 10 seconds before it re-locks
29 int Door_InitHandler();
30 int Door_CanDispense(int User, int Item);
31 int Door_DoDispense(int User, int Item);
34 tHandler gDoor_Handler = {
40 char *gsDoor_Password = "";
41 volatile int giDoor_ChildStatus;
44 void Door_SIGCHLDHandler(int signum)
47 giDoor_ChildStatus ++;
48 printf("SIGCHLD: giDoor_ChildStatus = %i \n", giDoor_ChildStatus);
51 int Door_InitHandler(void)
53 signal(SIGCHLD, Door_SIGCHLDHandler);
59 int Door_CanDispense(int User, int Item)
62 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
65 if( Item != 0 ) return -1;
67 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
70 printf("Door_CanDispense: User %i not in door\n", User);
76 printf("Door_CanDispense: User %i can open the door\n", User);
83 * \brief Actually do a dispense from the coke machine
85 int Door_DoDispense(int User, int Item)
98 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
102 if( Item != 0 ) return -1;
104 // Check if user is in door
105 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
108 printf("Door_CanDispense: User %i not in door\n", User);
113 giDoor_ChildStatus = 0; // Set child status to zero
114 parentPid = getpid();
116 // Create stdin/stdout
117 if( pipe(stdin_pair) || pipe(stdout_pair) )
125 childPid = forkpty(&child_stdin_fd, NULL, NULL, NULL);
138 // Close write end of stdin, and set it to #0
139 close(stdin_pair[1]); dup2(stdin_pair[0], 0);
140 // Close read end of stdout, and set it to #1
141 close(stdout_pair[0]); dup2(stdout_pair[1], 1);
144 //execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
145 execl("/usr/bin/llogin", "llogin", "door", "-w-", NULL);
151 child_stdin = fdopen(stdin_pair[1], "w");
152 close(stdin_pair[0]); // child stdin read
153 close(stdout_pair[1]); // child stdout write
155 child_stdin = fdopen(child_stdin_fd, "w");
162 if( giDoor_ChildStatus || (len = read(stdout_pair[0], buf, sizeof buf)) < 0)
164 if( giDoor_ChildStatus || (len = read(child_stdin_fd, buf, sizeof buf)) < 0)
169 waitpid(childPid, &child_exit, 0);
170 printf("Door_DoDispense: fread fail (child status %i)\n", child_exit);
177 printf("Door_DoDispense: buf = %i '%s'\n", len, buf);
182 if( giDoor_ChildStatus || fputs(gsDoor_Password, child_stdin) <= 0 ) {
185 waitpid(childPid, &child_exit, 0);
186 printf("Door_DoDispense: fputs password fail (child status %i)\n", child_exit);
190 fputs("\n", child_stdin);
194 printf("Door_DoDispense: Door unlock\n");
197 // ATH1 - Unlock door
198 if( giDoor_ChildStatus || fputs("ATH1\n", child_stdin) <= 0) {
200 printf("Door_DoDispense: fputs unlock\n");
205 // Wait before re-locking
206 sleep(DOOR_UNLOCKED_DELAY);
210 printf("Door_DoDispense: Door re-lock\n");
213 // Re-lock the door (and quit llogin)
214 if( giDoor_ChildStatus || fputs("ATH0\n\x1D", child_stdin) == 0 ) {
216 printf("Door_DoDispense: fputs lock\n");
223 close(stdin_pair[1]); // child stdin write
224 close(stdout_pair[0]); // child stdout read
226 close(child_stdin_fd);
230 printf("Door_DoDispense: User %i opened door\n", User);
233 kill(childPid, SIGKILL);