]> pd.if.org Git - mmurtl/blob - msamples/service/service.c
autocommit for file dated 1995-01-21 07:59:46
[mmurtl] / msamples / service / service.c
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
8    4) Exit\r
9 */\r
10 \r
11 #include <stdio.h>\r
12 #include "\OSSource\MKernel.h"\r
13 #include "\OSSource\MJob.h"\r
14 #include "\OSSource\MVid.h"\r
15 \r
16 #define ErcOK         0\r
17 #define ErcOpCancel   4\r
18 #define ErcNoSuchSvc  30\r
19 #define ErcBadSvcCode 32\r
20 \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
26 \r
27 void main(void)\r
28 {\r
29 unsigned long OSError, ErrorToUser, keycode;\r
30 long *pDataRet;\r
31 \r
32   OSError = AllocExch(&MainExch);         /* get an exchange */\r
33 \r
34   if (OSError)                      /* look for a kernel error */\r
35     ExitJob(OSError);\r
36 \r
37   OSError = RegisterSvc("NUMBERS ", MainExch);\r
38 \r
39   if (OSError)                      /* look for a system error */\r
40     ExitJob(OSError);\r
41 \r
42   SetNormVid(WHITE|BGBLACK);\r
43   ClrScr();\r
44 \r
45   printf("NUMBERS Service Installed.\r\n");\r
46   printf("ANY valid keystroke will terminate the service.\r\n");\r
47 \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
50   if (OSError)\r
51           printf("Error on Keyboard Request:\r\n", OSError);\r
52 \r
53   while (1)             /* WHILE forever (almost...) */\r
54   {\r
55 \r
56         /* Now we wait for a client or for a keystroke to come back */\r
57 \r
58     OSError = WaitMsg(MainExch, Message); /* Exch & pointer */\r
59 \r
60     if (!OSError)\r
61     {\r
62 \r
63           if (Message[0] == rqHndl)  /* it was a keystroke and NOT a client */\r
64           {\r
65                 UnRegisterSvc("NUMBERS ");\r
66                 while (!CheckMsg(MainExch, Message))\r
67                 {\r
68             pRqBlk = Message[0];\r
69                         Respond(pRqBlk, ErcNoSuchSvc);\r
70                 }\r
71                 DeAllocExch(MainExch);\r
72                 ExitJob(ErcOpCancel);\r
73           }\r
74 \r
75       pRqBlk = Message[0];   /* First DWORD contains ptr to RqBlk */\r
76 \r
77           if (pRqBlk->ServiceCode == 0)          /* Abort request from OS */\r
78           ErrorToUser = ErcOK;\r
79 \r
80       else if (pRqBlk->ServiceCode == 1)     /* User Asking for Number */\r
81       {\r
82                  pDataRet = pRqBlk->pData1;\r
83          *pDataRet = NextNumber++;       /* Give them a number */\r
84          ErrorToUser = ErcOK;                 /* Respond with No error */\r
85 \r
86                 printf("NUMBERS Service gave out number: %d.\r\n", NextNumber-1);\r
87 \r
88       }\r
89       else ErrorToUser = ErcBadSvcCode;       /* Unknown Service code! */\r
90 \r
91       OSError = Respond(pRqBlk, ErrorToUser); /* Respond to Request */\r
92 \r
93     }\r
94   }  /* Loop while(1) */\r
95 }\r