1 /* Test cancellation of a door_return call. */ 2 3 #include <assert.h> 4 #include <door.h> 5 #include <errno.h> 6 #include <pthread.h> 7 #include <signal.h> 8 #include <stdlib.h> 9 #include <stdio.h> 10 #include <string.h> 11 #include <sys/lwp.h> 12 #include <unistd.h> 13 14 static volatile lwpid_t server_lwpid = 0; 15 16 static void server_procedure(void *cookie, char *argp, size_t arg_size, 17 door_desc_t *dp, uint_t n_desc) 18 { 19 assert(0); 20 } 21 22 static void *my_server_thread(void *arg) 23 { 24 server_lwpid = _lwp_self(); 25 door_return(NULL, 0, NULL, 0); 26 return NULL; 27 } 28 29 static void create_door_thread(door_info_t *info) 30 { 31 static int called = 0; 32 pthread_t thread; 33 int res; 34 35 /* Allow to create only one server door thread. */ 36 assert(!called); 37 called = 1; 38 39 res = pthread_create(&thread, NULL, my_server_thread, NULL); 40 if (res) { 41 errno = res; 42 perror("pthread_create"); 43 exit(1); 44 } 45 } 46 47 static void signal_handler(int signo, siginfo_t *info, void *uc) 48 { 49 const char str[] = "Signal caught.\n"; 50 size_t len = sizeof(str) - 1; 51 ssize_t res; 52 53 res = write(STDOUT_FILENO, str, len); 54 assert(res == len); 55 } 56 57 int main(void) 58 { 59 int res = 1; 60 int did = -1; 61 struct sigaction sa; 62 63 sa.sa_sigaction = signal_handler; 64 sa.sa_flags = SA_RESTART; 65 if (sigfillset(&sa.sa_mask)) { 66 perror("sigfillset"); 67 return 1; 68 } 69 if (sigaction(SIGINT, &sa, NULL)) { 70 perror("sigaction"); 71 return 1; 72 } 73 74 door_server_create(create_door_thread); 75 76 if ((did = door_create(server_procedure, NULL, 0)) < 0) { 77 perror("door_create"); 78 return 1; 79 } 80 81 /* Let the server thread to run. */ 82 sleep(2); 83 84 /* Send a signal to the server thread that should be already created and 85 blocked in door_return. */ 86 if (_lwp_kill(server_lwpid, SIGINT)) { 87 perror("_lwp_kill"); 88 goto out; 89 } 90 91 /* Let the other thread to run. */ 92 sleep(2); 93 94 res = 0; 95 96 out: 97 if (did >= 0 && door_revoke(did)) 98 perror("door_revoke"); 99 100 return res; 101 } 102 103