Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2016, Cyril Bur, IBM Corp.
0004  *
0005  * Sending one self a signal should always get delivered.
0006  */
0007 
0008 #include <signal.h>
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <sys/types.h>
0013 #include <sys/wait.h>
0014 #include <unistd.h>
0015 
0016 #include <altivec.h>
0017 
0018 #include "utils.h"
0019 
0020 #define MAX_ATTEMPT 500000
0021 #define TIMEOUT 5
0022 
0023 extern long signal_self(pid_t pid, int sig);
0024 
0025 static sig_atomic_t signaled;
0026 static sig_atomic_t fail;
0027 
0028 static void signal_handler(int sig)
0029 {
0030     if (sig == SIGUSR1)
0031         signaled = 1;
0032     else
0033         fail = 1;
0034 }
0035 
0036 static int test_signal()
0037 {
0038     int i;
0039     struct sigaction act;
0040     pid_t ppid = getpid();
0041     pid_t pid;
0042 
0043     act.sa_handler = signal_handler;
0044     act.sa_flags = 0;
0045     sigemptyset(&act.sa_mask);
0046     if (sigaction(SIGUSR1, &act, NULL) < 0) {
0047         perror("sigaction SIGUSR1");
0048         exit(1);
0049     }
0050     if (sigaction(SIGALRM, &act, NULL) < 0) {
0051         perror("sigaction SIGALRM");
0052         exit(1);
0053     }
0054 
0055     /* Don't do this for MAX_ATTEMPT, its simply too long */
0056     for(i  = 0; i < 1000; i++) {
0057         pid = fork();
0058         if (pid == -1) {
0059             perror("fork");
0060             exit(1);
0061         }
0062         if (pid == 0) {
0063             signal_self(ppid, SIGUSR1);
0064             exit(1);
0065         } else {
0066             alarm(0); /* Disable any pending */
0067             alarm(2);
0068             while (!signaled && !fail)
0069                 asm volatile("": : :"memory");
0070             if (!signaled) {
0071                 fprintf(stderr, "Didn't get signal from child\n");
0072                 FAIL_IF(1); /* For the line number */
0073             }
0074             /* Otherwise we'll loop too fast and fork() will eventually fail */
0075             waitpid(pid, NULL, 0);
0076         }
0077     }
0078 
0079     for (i = 0; i < MAX_ATTEMPT; i++) {
0080         long rc;
0081 
0082         alarm(0); /* Disable any pending */
0083         signaled = 0;
0084         alarm(TIMEOUT);
0085         rc = signal_self(ppid, SIGUSR1);
0086         if (rc) {
0087             fprintf(stderr, "(%d) Fail reason: %d rc=0x%lx",
0088                     i, fail, rc);
0089             FAIL_IF(1); /* For the line number */
0090         }
0091         while (!signaled && !fail)
0092             asm volatile("": : :"memory");
0093         if (!signaled) {
0094             fprintf(stderr, "(%d) Fail reason: %d rc=0x%lx",
0095                     i, fail, rc);
0096             FAIL_IF(1); /* For the line number */
0097         }
0098     }
0099 
0100     return 0;
0101 }
0102 
0103 int main(void)
0104 {
0105     test_harness_set_timeout(300);
0106     return test_harness(test_signal, "signal");
0107 }