5751002 [rkeene@sledge /home/rkeene/devel/old/kblockd]$ cat -n kblockd.c
  1 /*
  2    Lock keyboard when the device connecting pins 5 and 8 is removed,
  3    and unlock it when it is replaced.
  4      --RKeene [07/17/99:04:00]
  5 
  6 
  7    Flags:
  8 
  9     -DUSE_SVGA          To use SVGAlib to clear the screen when locking
 10     -DKBD_BASE=0x60     To change where the keyboard is.
 11     -DISABLE_ON_INT To disable on the keyboard on SIGINT
 12     -DONT_LOG       To disable logging
 13     -DONT_DISABLE   Do not disable the keyboard.
 14     -DONT_DETACH    To not detach from the terminal.
 15     -DEXEC_DIS=program  Run program on disabilitation of keyboard
 16     -DEXEC_ENA=program  "             " rehabilitation "         "
 17     -DONT_FORK
 18 
 19 */
 20 
 21 #ifdef GO32
 22 #include <inlines/pc.h>
 23 #undef LINUX
 24 #endif /* GO32 */
 25 
 26 #ifdef LINUX
 27 #include <asm/io.h>
 28 #include <syslog.h>
 29 #endif /* Linux */
 30 #include <unistd.h>
 31 #include <stdio.h>
 32 #include <string.h>
 33 #include <signal.h>
 34 #ifdef USE_SVGA
 35 #include <vga.h>
 36 #endif /* USE_SVGA */
 37 #if (defined EXEC_DIS) || (defined EXEC_ENA)
 38 #include <stdlib.h>
 39 #endif
 40 
 41 #ifndef KBDBASE
 42 #define KBDBASE 0x60
 43 #endif
 44 int COMBASE=0x2f8;      /* /dev/ttyS1 */
 45 int VGA_MODE;
 46 
 47 
 48 #ifdef GO32
 49 #define ONT_LOG
 50 
 51 void outb(unsigned char value,int address) {
 52   outportb(value,address);
 53   return;
 54 }
 55 unsigned char inb(int address) {
 56   return(inportb(address));
 57 }
 58 
 59 unsigned char ioperm(unsigned long from, unsigned long num, int turn_on) {
 60   return(0);
 61 }
 62 #define LINUX
 63 #endif /* GO32 */
 64 
 65 #ifndef LINUX
 66 #define ONT_LOG
 67 /*
 68   These routines MIGHT work, but I dont know.
 69 */
 70 void outb(unsigned char value, int address) {
 71   __asm__ __volatile__("
 72     movb %%al, %0
 73     outb %1, %%al"
 74     :
 75     : "g" (value), "g" (address)
 76   );
 77   return;
 78 }
 79 
 80 unsigned char inb(int address) {
 81   unsigned char value;
 82   __asm__ __volatile__("
 83     inb %0"
 84   : "=g" (value)
 85   : "g" (address)
 86   );
 87   return(value);
 88 }
 89 
 90 int ioperm(unsigned long from, unsigned long num, int turn_on) {
 91   return(0);
 92 }
 93 #endif /* !LINUX */
 94 
 95 
 96 #ifndef ONT_LOG
 97 void writelog(char message[128]) {
 98   openlog("kblockd", LOG_PID, LOG_DAEMON);
 99   syslog(LOG_INFO, message);
100   closelog();
101 }
102 #endif
103 
104 
105 void disablekbd(int log) {
106 #ifndef ONT_LOG
107   if (log==1) { writelog("Device removed, disabling keyboard"); }
108 #endif
109 #ifdef EXEC_DIS
110   system(EXEC_DIS);
111 #endif
112 #ifndef ONT_DISABLE
113   outb(0xa7,KBDBASE+4);
114   outb(0xad,KBDBASE+4);
115   outb(0xaf,KBDBASE+4);
116 #endif
117 #ifdef USE_SVGA
118   VGA_MODE=vga_getcurrentmode();
119   vga_setmode(1);
120 #endif
121 }
122 
123 
124 void enablekbd(int log) {
125 #ifndef ONT_LOG
126   if (log==1) { writelog("Device detected, enabling keyboard"); }
127 #endif
128 #ifndef ONT_DISABLE
129   outb(0xa8,KBDBASE+4);
130   outb(0xae,KBDBASE+4);
131   outb(0xb0,KBDBASE+4);
132   outb(0xf4,KBDBASE);
133 #endif
134 #ifdef USE_SVGA
135   vga_setmode(VGA_MODE);
136 #endif
137 #ifdef EXEC_ENA
138   system(EXEC_ENA);
139 #endif
140   return;
141 }
142 
143 /*
144   1=offline, 0=online
145 */
146 int checkplug(void) {
147   int i;
148   outb(1,COMBASE+4);
149   for (i=0;i<4;i++) { inb(COMBASE+6); }
150   if (inb(COMBASE+6)!=128) { return 1; }
151   return 0;
152 }
153 /*
154   1=locked, 0=free
155 */
156 int checkkbdlock(void) {
157   int stats;
158   outb(0xa9,0x64);
159   stats=inb(0x60);
160   if (stats==1) { return 1; }
161   return 0;
162 }
163 
164 void handleINT(void) {
165 #ifndef ONT_LOG
166   writelog("SIGINT recieved, terminating.");
167 #endif
168 #ifndef ISABLE_ON_INT
169   enablekbd(0);
170 #else
171   disablekbd(0);
172 #endif
173   exit(1);
174 }
175 
176 void handleKill(void) {
177 #ifndef ONT_LOG
178   writelog("Kill message recieved, ignored.");
179 #endif
180   signal(SIGINT,(void *)handleINT);
181   signal(SIGHUP,(void *)handleKill);
182   signal(SIGTERM,(void *)handleKill);  
183 }
184 
185 int main(int argc, char *argv[])
186 {
187   int status=2,tmpstatus,log=0;
188   if (getuid()!=0) { printf("You must be root to run this program.\n"); return 1; }
189 #ifndef ONT_FORK
190   if (fork()!=0) { return(0); }
191 #endif
192 #ifndef USE_SVGA
193 #ifndef ONT_DETACH
194   setsid();
195   fclose(stdin);
196   fclose(stdout);
197   fclose(stderr);
198 #endif
199 #else
200   if (vga_init()!=0) { printf("Unable to intialize SVGAlib, terminating.\n"); exit(1); }
201   VGA_MODE=vga_getcurrentmode();
202 #endif
203   chdir("/tmp");
204   signal(SIGINT,(void *)handleINT);
205   signal(SIGHUP,(void *)handleKill);
206   signal(SIGTERM,(void *)handleKill);
207   signal(SIGSEGV,(void *)handleINT);
208   ioperm(KBDBASE,1,1);
209 #ifndef ONT_LOG
210   writelog("Starting keyboard locking daemon.");
211 #endif
212   ioperm(KBDBASE+4,1,1);
213   if (argc!=1) { 
214     if (strncmp(argv[1],"1",1)==0) { COMBASE=0x3f8; }
215   }
216   ioperm(COMBASE,16,1);
217   while(1) {
218     tmpstatus=checkplug();
219     if ((status!=tmpstatus)) { 
220       status=tmpstatus;
221       if (tmpstatus==1) { disablekbd(log); }
222       if (tmpstatus==0) { enablekbd(log); }
223       log=1;
224     }
225     usleep(1000);
226   }
227   return(0);
228 }
5751003 [rkeene@sledge /home/rkeene/devel/old/kblockd]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 1999-08-25 21:42:36