+/* Super Simple System Service.\r
+ This is expanded from the sample in the Systems Programming\r
+ chapter to show how to properly deinstall a system service.\r
+ The steps to deinstall are:\r
+ 1) UnRegister the Service\r
+ 2) Serve all remaining requests at service exchange\r
+ 3) Deallocate all resources\r
+ 4) Exit\r
+*/\r
+\r
+#include <stdio.h>\r
+#include "\OSSource\MKernel.h"\r
+#include "\OSSource\MJob.h"\r
+#include "\OSSource\MVid.h"\r
+\r
+#define ErcOK 0\r
+#define ErcOpCancel 4\r
+#define ErcNoSuchSvc 30\r
+#define ErcBadSvcCode 32\r
+\r
+struct RqBlkType *pRqBlk; /* A pointer to a Reqeust Block */\r
+unsigned long NextNumber = 0; /* The number to return */\r
+unsigned long MainExch; /* Where we wait for Requests */\r
+unsigned long Message[2]; /* The Message with the Request */\r
+long rqHndl; /* Used for keyboard request */\r
+\r
+void main(void)\r
+{\r
+unsigned long OSError, ErrorToUser, keycode;\r
+long *pDataRet;\r
+\r
+ OSError = AllocExch(&MainExch); /* get an exchange */\r
+\r
+ if (OSError) /* look for a kernel error */\r
+ ExitJob(OSError);\r
+\r
+ OSError = RegisterSvc("NUMBERS ", MainExch);\r
+\r
+ if (OSError) /* look for a system error */\r
+ ExitJob(OSError);\r
+\r
+ SetNormVid(WHITE|BGBLACK);\r
+ ClrScr();\r
+\r
+ printf("NUMBERS Service Installed.\r\n");\r
+ printf("ANY valid keystroke will terminate the service.\r\n");\r
+\r
+ OSError = Request("KEYBOARD", 1, MainExch, &rqHndl, 0, &keycode,\r
+ 4, 0, 0, 1, 0, 0); /* 1 in dData0 = WAIT for key */\r
+ if (OSError)\r
+ printf("Error on Keyboard Request:\r\n", OSError);\r
+\r
+ while (1) /* WHILE forever (almost...) */\r
+ {\r
+\r
+ /* Now we wait for a client or for a keystroke to come back */\r
+\r
+ OSError = WaitMsg(MainExch, Message); /* Exch & pointer */\r
+\r
+ if (!OSError)\r
+ {\r
+\r
+ if (Message[0] == rqHndl) /* it was a keystroke and NOT a client */\r
+ {\r
+ UnRegisterSvc("NUMBERS ");\r
+ while (!CheckMsg(MainExch, Message))\r
+ {\r
+ pRqBlk = Message[0];\r
+ Respond(pRqBlk, ErcNoSuchSvc);\r
+ }\r
+ DeAllocExch(MainExch);\r
+ ExitJob(ErcOpCancel);\r
+ }\r
+\r
+ pRqBlk = Message[0]; /* First DWORD contains ptr to RqBlk */\r
+\r
+ if (pRqBlk->ServiceCode == 0) /* Abort request from OS */\r
+ ErrorToUser = ErcOK;\r
+\r
+ else if (pRqBlk->ServiceCode == 1) /* User Asking for Number */\r
+ {\r
+ pDataRet = pRqBlk->pData1;\r
+ *pDataRet = NextNumber++; /* Give them a number */\r
+ ErrorToUser = ErcOK; /* Respond with No error */\r
+\r
+ printf("NUMBERS Service gave out number: %d.\r\n", NextNumber-1);\r
+\r
+ }\r
+ else ErrorToUser = ErcBadSvcCode; /* Unknown Service code! */\r
+\r
+ OSError = Respond(pRqBlk, ErrorToUser); /* Respond to Request */\r
+\r
+ }\r
+ } /* Loop while(1) */\r
+}\r