1 /* Super Simple System Service.
\r
2 This is expanded from the sample in the Systems Programming
\r
3 chapter to show how to properly deinstall a system service.
\r
4 The steps to deinstall are:
\r
5 1) UnRegister the Service
\r
6 2) Serve all remaining requests at service exchange
\r
7 3) Deallocate all resources
\r
12 #include "\OSSource\MKernel.h"
\r
13 #include "\OSSource\MJob.h"
\r
14 #include "\OSSource\MVid.h"
\r
17 #define ErcOpCancel 4
\r
18 #define ErcNoSuchSvc 30
\r
19 #define ErcBadSvcCode 32
\r
21 struct RqBlkType *pRqBlk; /* A pointer to a Reqeust Block */
\r
22 unsigned long NextNumber = 0; /* The number to return */
\r
23 unsigned long MainExch; /* Where we wait for Requests */
\r
24 unsigned long Message[2]; /* The Message with the Request */
\r
25 long rqHndl; /* Used for keyboard request */
\r
29 unsigned long OSError, ErrorToUser, keycode;
\r
32 OSError = AllocExch(&MainExch); /* get an exchange */
\r
34 if (OSError) /* look for a kernel error */
\r
37 OSError = RegisterSvc("NUMBERS ", MainExch);
\r
39 if (OSError) /* look for a system error */
\r
42 SetNormVid(WHITE|BGBLACK);
\r
45 printf("NUMBERS Service Installed.\r\n");
\r
46 printf("ANY valid keystroke will terminate the service.\r\n");
\r
48 OSError = Request("KEYBOARD", 1, MainExch, &rqHndl, 0, &keycode,
\r
49 4, 0, 0, 1, 0, 0); /* 1 in dData0 = WAIT for key */
\r
51 printf("Error on Keyboard Request:\r\n", OSError);
\r
53 while (1) /* WHILE forever (almost...) */
\r
56 /* Now we wait for a client or for a keystroke to come back */
\r
58 OSError = WaitMsg(MainExch, Message); /* Exch & pointer */
\r
63 if (Message[0] == rqHndl) /* it was a keystroke and NOT a client */
\r
65 UnRegisterSvc("NUMBERS ");
\r
66 while (!CheckMsg(MainExch, Message))
\r
68 pRqBlk = Message[0];
\r
69 Respond(pRqBlk, ErcNoSuchSvc);
\r
71 DeAllocExch(MainExch);
\r
72 ExitJob(ErcOpCancel);
\r
75 pRqBlk = Message[0]; /* First DWORD contains ptr to RqBlk */
\r
77 if (pRqBlk->ServiceCode == 0) /* Abort request from OS */
\r
78 ErrorToUser = ErcOK;
\r
80 else if (pRqBlk->ServiceCode == 1) /* User Asking for Number */
\r
82 pDataRet = pRqBlk->pData1;
\r
83 *pDataRet = NextNumber++; /* Give them a number */
\r
84 ErrorToUser = ErcOK; /* Respond with No error */
\r
86 printf("NUMBERS Service gave out number: %d.\r\n", NextNumber-1);
\r
89 else ErrorToUser = ErcBadSvcCode; /* Unknown Service code! */
\r
91 OSError = Respond(pRqBlk, ErrorToUser); /* Respond to Request */
\r
94 } /* Loop while(1) */
\r