1 /* Simple door test. */
14 #include <sys/types.h>
17 static char door_file
[] = "/tmp/vgtest_door_data.XXXXXX";
18 static volatile int exit_now
= 0;
20 static void child_handler(int sig
)
23 fprintf(stderr
, "Received premature SIGCHLD.\n");
30 static void server_procedure(void *cookie
, char *argp
, size_t arg_size
,
31 door_desc_t
*dp
, uint_t n_desc
)
33 char data
[] = "Hello from server";
38 if (arg_size
> INT_MAX
) {
39 fprintf(stderr
, "Value received from a client is too big.\n");
43 printf("SERVER: %.*s\n", (int)arg_size
, argp
);
47 /* Let server_main() know that we should exit. */
50 door_return(data
, strlen(data
) + 1, NULL
, 0);
52 /* Function door_return() should never return. */
53 perror("door_return");
57 static int server_main(void)
63 /* Make sure nothing else is attached. */
66 if ((did
= door_create(server_procedure
, (void*)&exit_now
, 0)) < 0) {
67 perror("door_create");
71 /* Attach to file system. */
72 if (fattach(did
, door_file
) < 0) {
74 snprintf(str
, sizeof(str
), "fattach %s", door_file
);
80 /* Poor man's termination. */
87 if (attached
&& unlink(door_file
)) {
89 snprintf(str
, sizeof(str
), "unlink %s", door_file
);
92 if (did
>= 0 && door_revoke(did
))
93 perror("door_revoke");
99 static int client_main(void)
105 struct door_info info
;
109 /* Open the door file. */
110 if ((did
= open(door_file
, O_RDWR
)) >= 0)
111 if (!door_info(did
, &info
))
118 snprintf(str
, sizeof(str
), "door_info %s", door_file
);
127 /* Set call parameters. */
128 snprintf(buf
, sizeof(buf
), "Hello from client");
129 params
.data_ptr
= buf
;
130 params
.data_size
= strlen(buf
) + 1;
131 params
.desc_ptr
= NULL
;
134 params
.rsize
= sizeof(buf
);
137 if (door_call(did
, ¶ms
)) {
145 /* Print a result of the call. */
146 printf("CLIENT: %.*s\n", (int)params
.rsize
, params
.rbuf
);
149 /* It's possible that the system allocated a new memory for rbuf. Unmap it
151 if (params
.rbuf
!= buf
)
152 if (munmap(params
.rbuf
, params
.rsize
) != 0) {
167 /* Establish handler for client error exit. */
168 sa
.sa_handler
= child_handler
;
169 sa
.sa_flags
= SA_NOCLDSTOP
;
170 if (sigemptyset(&sa
.sa_mask
)) {
171 perror("sigemptyset");
174 if (sigaction(SIGCHLD
, &sa
, NULL
)) {
179 door_fd
= mkstemp(door_file
);
193 return client_main();
195 int res
= server_main();
198 if (wait(NULL
) == pid
)
200 if (errno
!= EINTR
) {
204 } while (errno
== EINTR
);