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);
32 void Door_int_SIGCHLDHandler(int signum);
35 tHandler gDoor_Handler = {
41 char *gsDoor_Password = "";
42 volatile int giDoor_ChildTerminated;
45 int Door_InitHandler(void)
47 signal(SIGCHLD, Door_int_SIGCHLDHandler);
53 int Door_CanDispense(int User, int Item)
56 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
59 if( Item != 0 ) return -1;
61 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
64 printf("Door_CanDispense: User %i not in door\n", User);
70 printf("Door_CanDispense: User %i can open the door\n", User);
77 * \brief Actually do a dispense from the coke machine
79 int Door_DoDispense(int User, int Item)
91 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
95 if( Item != 0 ) return -1;
97 // Check if user is in door
98 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
101 printf("Door_CanDispense: User %i not in door\n", User);
107 // Create stdin/stdout
108 if( pipe(stdin_pair) || pipe(stdout_pair) )
114 parent_pid = getpid();
126 // Close write end of stdin, and set it to #0
127 close(stdin_pair[1]); dup2(stdin_pair[0], 0);
128 // Close read end of stdout, and set it to #1
129 close(stdout_pair[0]); dup2(stdout_pair[1], 1);
131 execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
132 kill(parent_pid, SIGCHLD);
137 child_stdin = fdopen(stdin_pair[1], "w");
138 close(stdin_pair[0]); // child stdin read
139 close(stdout_pair[1]); // child stdout write
143 child_stdin = popen("llogin door -w -", "w");
144 if( !child_stdin || child_stdin == (void*)-1 ) {
146 printf("Door_DoDispense: llogin failure\n");
152 if(fread(buf, 1, 512, child_stdin) == 0) return -1;
155 if( fputs(gsDoor_Password, child_stdin) <= 0 ) return -1;
156 fputs("\n", child_stdin);
160 printf("Door_DoDispense: Door unlock\n");
163 // ATH1 - Unlock door
164 if( fputs("ATH1\n", child_stdin) <= 0) return -1;
166 // Wait before re-locking
167 sleep(DOOR_UNLOCKED_DELAY);
171 printf("Door_DoDispense: Door re-lock\n");
175 if( fputs("ATH0\n", child_stdin) <= 0 ) return -1;
179 close(stdin_pair[1]); // child stdin write
180 close(stdout_pair[0]); // child stdout read
186 printf("Door_DoDispense: User %i opened door\n", User);
192 void Door_int_SIGCHLDHandler(int signum)
194 signum = 0; // Snut up
195 giDoor_ChildTerminated = 1;