testsh2.sh: change output to new cksum util
[minix.git] / test / safemap / grantor.c
blobe36fda12c2582935ac78beffd67998f7d1c08899
1 #include "inc.h"
3 char buf_buf[BUF_SIZE + CLICK_SIZE];
5 int fid_send, fid_get;
7 /* SEF functions and variables. */
8 static void sef_local_startup(void);
10 /*===========================================================================*
11 * main *
12 *===========================================================================*/
13 int main(int argc, char **argv)
15 endpoint_t ep_self, ep_requestor, ep_child;
16 cp_grant_id_t gid;
17 int i, r, pid;
18 char *buf;
19 int status;
21 /* SEF local startup. */
22 env_setargs(argc, argv);
23 sef_local_startup();
25 /* Prepare work. */
26 buf = (char*) CLICK_CEIL(buf_buf);
27 fid_send = open(FIFO_GRANTOR, O_WRONLY);
28 fid_get = open(FIFO_REQUESTOR, O_RDONLY);
29 if(fid_get < 0 || fid_send < 0) {
30 printf("GRANTOR: can't open fifo files.\n");
31 return 1;
34 /* Get the requestor's endpoint. */
35 read(fid_get, &ep_requestor, sizeof(ep_requestor));
36 dprint("GRANTOR: getting requestor's endpoint: %d\n", ep_requestor);
38 /* Grant. */
39 gid = cpf_grant_direct(ep_requestor, (long)buf, BUF_SIZE,
40 CPF_READ | CPF_WRITE | CPF_MAP);
41 ep_self = getprocnr();
42 dprint("GRANTOR: sending my endpoint %d and gid %d\n", ep_self, gid);
43 write(fid_send, &ep_self, sizeof(ep_self));
44 write(fid_send, &gid, sizeof(gid));
46 /* Test MAP. */
47 buf[0] = BUF_START_GRANTOR;
48 FIFO_NOTIFY(fid_send);
49 FIFO_WAIT(fid_get);
50 CHECK_TEST("GRANTOR", buf[0], BUF_START_REQUESTOR, "MAP");
52 /* Test UNMAP. */
53 buf[0] = BUF_START_GRANTOR;
54 FIFO_NOTIFY(fid_send);
55 FIFO_WAIT(fid_get);
56 CHECK_TEST("GRANTOR", buf[0], BUF_START_GRANTOR, "UNMAP");
58 /* Test REVOKE. */
59 r = sys_saferevmap_gid(gid);
60 if(r != OK) {
61 printf("GRANTOR: error in sys_saferevmap_gid: %d\n", r);
62 return 1;
64 buf[0] = BUF_START_GRANTOR+1;
65 FIFO_NOTIFY(fid_send);
66 FIFO_WAIT(fid_get);
67 CHECK_TEST("GRANTOR", buf[0], BUF_START_GRANTOR+1, "REVOKE");
69 /* Test SMAP_COW. */
70 FIFO_NOTIFY(fid_send);
71 FIFO_WAIT(fid_get);
72 buf[0] = BUF_START_GRANTOR;
73 FIFO_NOTIFY(fid_send);
74 FIFO_WAIT(fid_get);
75 CHECK_TEST("GRANTOR", buf[0], BUF_START_GRANTOR, "SMAP_COW");
77 /* Test COW_SMAP. */
78 r = sys_saferevmap_gid(gid);
79 if(r != OK) {
80 printf("GRANTOR: error in sys_saferevmap_gid: %d\n", r);
81 return 1;
83 buf[0] = BUF_START_GRANTOR+1;
84 pid = fork();
85 if(pid < 0) {
86 printf("GRANTOR: error in fork.\n");
87 return 1;
89 if(pid == 0) {
90 buf[0] = BUF_START_GRANTOR+2;
91 exit(0);
93 FIFO_NOTIFY(fid_send);
94 FIFO_WAIT(fid_get);
95 ep_child = getnprocnr(pid);
96 if ((r = sys_privctl(ep_child, SYS_PRIV_SET_USER, NULL)) != OK) {
97 printf("GRANTOR: unable to set privileges: %d\n", r);
98 return 1;
100 if ((r = sys_privctl(ep_child, SYS_PRIV_ALLOW, NULL)) != OK) {
101 printf("GRANTOR: child process can't run: %d\n", r);
102 return 1;
104 wait(&status);
105 FIFO_NOTIFY(fid_send);
106 CHECK_TEST("GRANTOR", buf[0], BUF_START_GRANTOR+1, "COW_SMAP");
108 /* Test COW_SMAP2 (with COW safecopy). */
109 r = sys_saferevmap_gid(gid);
110 if(r != OK) {
111 printf("GRANTOR: error in sys_saferevmap_gid: %d\n", r);
112 return 1;
114 FIFO_NOTIFY(fid_send);
115 FIFO_WAIT(fid_get);
116 CHECK_TEST("GRANTOR", buf[0], BUF_START_REQUESTOR, "COW_SMAP2");
118 return 0;
121 /*===========================================================================*
122 * sef_local_startup *
123 *===========================================================================*/
124 static void sef_local_startup()
126 /* Let SEF perform startup. */
127 sef_startup();