Home | History | Annotate | Download | only in sigaction
      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 SA_SIGINFO is set in sa_flags and Real Time Signals extension is supported,
     21 * sa_sigaction is used as the signal handling function.
     22 *
     23 * The steps are:
     24 * -> test for RTS extension
     25 * -> register a handler for SIGTTIN with SA_SIGINFO, and a known function
     26 *   as sa_sigaction
     27 * -> raise SIGTTIN, and check the function has been called.
     28 *
     29 * The test fails if the function is not called
     30 */
     31 
     32 
     33 #include <pthread.h>
     34 #include <stdarg.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <unistd.h>
     39 #include <signal.h>
     40 #include <errno.h>
     41 #include "posixtest.h"
     42 
     43 #define WRITE(str) write(STDOUT_FILENO, str, sizeof(str) - 1)
     44 
     45 static volatile sig_atomic_t called = 0;
     46 
     47 static void handler(int sig, siginfo_t *info, void *context)
     48 {
     49 	(void) sig;
     50 	(void) context;
     51 
     52 	if (info->si_signo != SIGTTIN) {
     53 		WRITE("Wrong signal generated?\n");
     54 		_exit(PTS_FAIL);
     55 	}
     56 
     57 	called = 1;
     58 }
     59 
     60 int main(void)
     61 {
     62 	int ret;
     63 	long rts;
     64 
     65 	struct sigaction sa;
     66 
     67 	/* Test the RTS extension */
     68 	rts = sysconf(_SC_REALTIME_SIGNALS);
     69 
     70 	if (rts < 0L) {
     71 		fprintf(stderr, "This test needs the RTS extension");
     72 		return PTS_UNTESTED;
     73 	}
     74 
     75 	/* Set the signal handler */
     76 	sa.sa_flags = SA_SIGINFO;
     77 	sa.sa_sigaction = handler;
     78 
     79 	ret = sigemptyset(&sa.sa_mask);
     80 
     81 	if (ret != 0) {
     82 		perror("Failed to empty signal set");
     83 		return PTS_UNRESOLVED;
     84 	}
     85 
     86 	/* Install the signal handler for SIGTTIN */
     87 	ret = sigaction(SIGTTIN, &sa, 0);
     88 
     89 	if (ret != 0) {
     90 		perror("Failed to set signal handler");
     91 		return PTS_UNTESTED;
     92 	}
     93 
     94 	if (called) {
     95 		fprintf(stderr,
     96 			"The signal handler has been called before signal was raised");
     97 		return PTS_FAIL;
     98 	}
     99 
    100 	ret = raise(SIGTTIN);
    101 
    102 	if (ret != 0) {
    103 		perror("Failed to raise SIGTTIN");
    104 		return PTS_UNRESOLVED;
    105 	}
    106 
    107 	if (!called) {
    108 		fprintf(stderr, "The sa_handler was not called");
    109 		return PTS_FAIL;
    110 	}
    111 
    112 	printf("Test PASSED\n");
    113 
    114 	return PTS_PASS;
    115 }
    116