1 | /* $Id: TstProcessReadWrite.c,v 1.2 2000-12-11 06:53:56 bird Exp $
|
---|
2 | *
|
---|
3 | * Test program for the ProcessReadWrite API.
|
---|
4 | * The testprogram will spawn a child process which it will interact with.
|
---|
5 | * The interaction is as follows:
|
---|
6 | * - Parent reads dataarea in the datasegemnt and compare it with is's
|
---|
7 | * own duplicate area.
|
---|
8 | * - Parent writes some text data to the child process.
|
---|
9 | * - Parent process reads the text data back again and displays it.
|
---|
10 | *
|
---|
11 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
12 | *
|
---|
13 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
14 | *
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*******************************************************************************
|
---|
18 | * Defined Constants And Macros *
|
---|
19 | *******************************************************************************/
|
---|
20 | #define INCL_BASE
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <os2.h>
|
---|
26 |
|
---|
27 | #include <string.h>
|
---|
28 | #include <stdio.h>
|
---|
29 |
|
---|
30 | #include <win32k.h>
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Global Variables *
|
---|
34 | *******************************************************************************/
|
---|
35 | /* The data we read from the child process */
|
---|
36 | static char szDataRead[] =
|
---|
37 | "* Test program for the ProcessReadWrite API. \n"
|
---|
38 | "* The testprogram will spawn a child process which it will interact with. \n"
|
---|
39 | "* The interaction is as follows: \n"
|
---|
40 | "* - Parent reads dataarea in the datasegemnt and compare it with is's \n"
|
---|
41 | "* own duplicate area. \n"
|
---|
42 | "* - Parent writes some text data to the child process. \n"
|
---|
43 | "* - Parent process reads the text data back again and displays it. \n";
|
---|
44 |
|
---|
45 | /* The data we write to the child process (overwrites szDataRead) */
|
---|
46 | static char szDataWrite[] =
|
---|
47 | " write data - write data - write data - write data - write data - write data \n"
|
---|
48 | " write data - write data - write data - write data - write data - write data \n"
|
---|
49 | " write data - write data - write data - write data - write data - write data \n"
|
---|
50 | " write data - write data - write data - write data - write data - write data \n"
|
---|
51 | " write data - write data - write data - write data - write data - write data \n"
|
---|
52 | " write data - write data - write data - write data - write data - write data \n";
|
---|
53 |
|
---|
54 | /* Buffer used to read into */
|
---|
55 | static char szReadBuffer[1024];
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | *
|
---|
60 | *
|
---|
61 | * @returns
|
---|
62 | * @param
|
---|
63 | * @equiv
|
---|
64 | * @time
|
---|
65 | * @sketch
|
---|
66 | * @status
|
---|
67 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
68 | * @remark
|
---|
69 | */
|
---|
70 | int main(int argc, char **argv)
|
---|
71 | {
|
---|
72 | RESULTCODES resc;
|
---|
73 | APIRET rc;
|
---|
74 | int rcTest;
|
---|
75 | char szArg[1024];
|
---|
76 |
|
---|
77 | /* Check if we're the child process */
|
---|
78 | if (argc == 2 && strcmp(argv[1], "2ndProcess") == 0)
|
---|
79 | {
|
---|
80 | DosSleep(-1);
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* initiate Win32k */
|
---|
85 | rc = libWin32kInit();
|
---|
86 | if (rc != NO_ERROR)
|
---|
87 | {
|
---|
88 | printf("libWin32kInit failed with rc=%d\n", rc);
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Start child process */
|
---|
93 | szArg[sprintf(szArg, "%s 2ndProcess", argv[0]) - 11] = '\0';
|
---|
94 | rc = DosExecPgm(NULL, 0, EXEC_ASYNC, szArg, NULL, &resc, szArg);
|
---|
95 | if (rc != NO_ERROR)
|
---|
96 | {
|
---|
97 | printf("Failed to spawn child process. DosExecPgm failed with rc=%d\n", rc);
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 | printf("Created child process with PID = %d\n", resc.codeTerminate);
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Use the API.
|
---|
104 | */
|
---|
105 |
|
---|
106 | /* 1. Read data block from child process. */
|
---|
107 | rcTest = W32kProcessReadWrite(resc.codeTerminate, sizeof(szDataRead), szDataRead, szReadBuffer, TRUE);
|
---|
108 | if (rcTest == NO_ERROR)
|
---|
109 | {
|
---|
110 | rcTest = memcmp(szDataRead, szReadBuffer, sizeof(szDataRead));
|
---|
111 | if (rcTest == 0)
|
---|
112 | {
|
---|
113 | printf("Read successfull:\n%.*s\n\n", sizeof(szDataRead), szReadBuffer);
|
---|
114 |
|
---|
115 | /* 2. Write datablock to childprocess */
|
---|
116 | rcTest = W32kProcessReadWrite(resc.codeTerminate, sizeof(szDataWrite), szDataWrite, szDataRead, FALSE);
|
---|
117 | if (rcTest == NO_ERROR)
|
---|
118 | {
|
---|
119 | printf("Wrote successfully to child\n");
|
---|
120 | /* 2b. Read it back again */
|
---|
121 | rcTest = W32kProcessReadWrite(resc.codeTerminate, sizeof(szDataWrite), szDataRead, szReadBuffer, TRUE);
|
---|
122 | if (rcTest == NO_ERROR)
|
---|
123 | {
|
---|
124 | rcTest = memcmp(szDataWrite, szReadBuffer, sizeof(szDataWrite));
|
---|
125 | if (rcTest == 0)
|
---|
126 | {
|
---|
127 | printf("Read successfull the data which we wrote:\n%.*s\n\n", sizeof(szDataWrite), szReadBuffer);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | printf("The data read back was not equal!\n");
|
---|
131 | }
|
---|
132 | else
|
---|
133 | printf("Read back failed with rc=%d\n", rcTest);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | printf("Failed to write data to process. rc=%d\n", rc);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | printf("The data read was not equal!\n");
|
---|
140 | }
|
---|
141 | else
|
---|
142 | printf("First read failed with rc=%d\n", rcTest);
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Negative test... Read data at invalid addresses.
|
---|
146 | */
|
---|
147 | if (rcTest == NO_ERROR)
|
---|
148 | {
|
---|
149 | rcTest = W32kProcessReadWrite(resc.codeTerminate, 10, (PVOID)0xffffff00, szReadBuffer, TRUE);
|
---|
150 | if (rcTest != NO_ERROR)
|
---|
151 | {
|
---|
152 | rcTest = W32kProcessReadWrite(resc.codeTerminate, 10, (PVOID)0x00000100, szReadBuffer, TRUE);
|
---|
153 | if (rcTest != NO_ERROR)
|
---|
154 | {
|
---|
155 | rcTest = W32kProcessReadWrite(resc.codeTerminate, 10, (PVOID)0x001f000, szReadBuffer, TRUE); /* shouldn't be anything here! check map file... */
|
---|
156 | if (rcTest != NO_ERROR)
|
---|
157 | {
|
---|
158 |
|
---|
159 | }
|
---|
160 | else
|
---|
161 | printf("Error - negative test: read at 0x001f000\n");
|
---|
162 | }
|
---|
163 | else
|
---|
164 | printf("Error - negative test: read at 0x00000100\n");
|
---|
165 | }
|
---|
166 | else
|
---|
167 | printf("Error - negative test: read at 0xffffff00\n");
|
---|
168 |
|
---|
169 | rcTest = (rcTest == 0) ? -1 : 0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * Test result.
|
---|
174 | */
|
---|
175 | if (rcTest == 0)
|
---|
176 | {
|
---|
177 | printf(" ... Test ended successfully! ...\n");
|
---|
178 | }
|
---|
179 | else
|
---|
180 | printf(" !!!!!!! Test failed (%3d) !!!!!!!\n", rcTest);
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Cleanup.
|
---|
184 | */
|
---|
185 | /* Kill child. */
|
---|
186 | rc = DosKillProcess(DKP_PROCESS,resc.codeTerminate);
|
---|
187 | if (rc != NO_ERROR)
|
---|
188 | printf("Failed to kill child! rc=%d\n", rc);
|
---|
189 |
|
---|
190 | /* Terminate Win32k library. */
|
---|
191 | libWin32kTerm();
|
---|
192 |
|
---|
193 | return rcTest;
|
---|
194 | }
|
---|