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)
48 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)
95 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
99 if( Item != 0 ) return -1;
101 // Check if user is in door
102 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
105 printf("Door_CanDispense: User %i not in door\n", User);
110 // Create stdin/stdout
111 if( pipe(stdin_pair) || pipe(stdout_pair) )
117 giDoor_ChildStatus = 0; // Set child status to zero
118 parentPid = getpid();
131 // Close write end of stdin, and set it to #0
132 close(stdin_pair[1]); dup2(stdin_pair[0], 0);
133 // Close read end of stdout, and set it to #1
134 close(stdout_pair[0]); dup2(stdout_pair[1], 1);
136 execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
141 child_stdin = fdopen(stdin_pair[1], "w");
142 close(stdin_pair[0]); // child stdin read
143 close(stdout_pair[1]); // child stdout write
145 if( giDoor_ChildStatus || read(stdout_pair[0], buf, 512) < 0) {
147 printf("Door_DoDispense: fread fail\n");
153 if( giDoor_ChildStatus || fputs(gsDoor_Password, child_stdin) <= 0 ) {
155 printf("Door_DoDispense: fputs password\n");
159 fputs("\n", child_stdin);
163 printf("Door_DoDispense: Door unlock\n");
166 // ATH1 - Unlock door
167 if( giDoor_ChildStatus || fputs("ATH1\n", child_stdin) <= 0) {
169 printf("Door_DoDispense: fputs unlock\n");
174 // Wait before re-locking
175 sleep(DOOR_UNLOCKED_DELAY);
179 printf("Door_DoDispense: Door re-lock\n");
183 if( giDoor_ChildStatus || fputs("ATH0\n", child_stdin) == 0 ) {
185 printf("Door_DoDispense: fputs lock\n");
191 close(stdin_pair[1]); // child stdin write
192 close(stdout_pair[0]); // child stdout read
195 printf("Door_DoDispense: User %i opened door\n", User);