1 /* 2 * Copyright (c) 2005, Bull S.A.. All rights reserved. 3 * Created by: Sebastien Decugis 4 * Copyright (c) 2013 Cyril Hrubis <chrubis (at) suse.cz> 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of version 2 of the GNU General Public License as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it would be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 * 14 * You should have received a copy of the GNU General Public License along 15 * with this program; if not, write the Free Software Foundation, Inc., 16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * This sample test aims to check the following assertions: 19 * 20 * If the signal action was set with the signal() function, getting it into oact 21 * then reinstalling it with act must be valid. 22 * 23 * The steps are: 24 * -> register a signal handler for SIGTTOU with signal(). 25 * -> check this signal handler works. 26 * -> change the signal handler with sigaction, saving old handler in oact. 27 * -> check the new signal handler works. 28 * -> set the old signal handler back 29 * -> check the old signal handler still works. 30 * 31 * The test fails if a signal handler does not work as expected. 32 */ 33 34 35 #include <stdarg.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <signal.h> 41 #include <errno.h> 42 43 #include "posixtest.h" 44 45 static volatile sig_atomic_t called = 1; 46 47 static void handler_1() 48 { 49 called++; 50 } 51 52 static void handler_2() 53 { 54 called--; 55 } 56 57 int main(void) 58 { 59 int ret; 60 61 struct sigaction sa, save; 62 63 if (signal(SIGTTOU, handler_1) == SIG_ERR) { 64 perror("Failed to register signal handler"); 65 return PTS_UNRESOLVED; 66 } 67 68 /* As whether signal handler is restored to default when executed 69 is implementation defined, we cannot check it was registered here. */ 70 71 /* Set the new signal handler with sigaction*/ 72 sa.sa_flags = 0; 73 74 sa.sa_handler = handler_2; 75 76 ret = sigemptyset(&sa.sa_mask); 77 78 if (ret != 0) { 79 perror("Failed to empty signal set"); 80 return PTS_UNRESOLVED; 81 } 82 83 ret = sigaction(SIGTTOU, &sa, &save); 84 85 if (ret != 0) { 86 perror("Failed to register signal handler"); 87 return PTS_UNRESOLVED; 88 } 89 90 /* Check the signal handler has been set up */ 91 ret = raise(SIGTTOU); 92 93 if (ret != 0) { 94 perror("Failed to raise signal"); 95 return PTS_UNRESOLVED; 96 } 97 98 if (called != 0) { 99 fprintf(stderr, "Handler was not executed\n"); 100 return PTS_FAIL; 101 } 102 103 /* Restore the first signal handler */ 104 ret = sigaction(SIGTTOU, &save, 0); 105 106 if (ret != 0) { 107 perror("Failed to restore signal handler"); 108 return PTS_UNRESOLVED; 109 } 110 111 /* Check the signal handler has been set up */ 112 ret = raise(SIGTTOU); 113 114 if (ret != 0) { 115 perror("Failed to raise signal"); 116 return PTS_UNRESOLVED; 117 } 118 119 if (called != 1) { 120 fprintf(stderr, "Handler was not executed\n"); 121 return PTS_FAIL; 122 } 123 124 printf("Test PASSED\n"); 125 return PTS_PASS; 126 } 127