| 1 |
|
|---|
| 2 |
|
|---|
| 3 | /*******************************************************************************
|
|---|
| 4 | * Header Files *
|
|---|
| 5 | *******************************************************************************/
|
|---|
| 6 | #define _BSD_SOURCE
|
|---|
| 7 | #define _GNU_SOURCE
|
|---|
| 8 | #include <sys/time.h>
|
|---|
| 9 | #include <sys/stat.h>
|
|---|
| 10 | #include <fcntl.h>
|
|---|
| 11 | #include <time.h>
|
|---|
| 12 | #include <stdio.h>
|
|---|
| 13 | #include <string.h>
|
|---|
| 14 | #include <errno.h>
|
|---|
| 15 | #include <pthread.h>
|
|---|
| 16 | #include <signal.h>
|
|---|
| 17 | #include <unistd.h>
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | /*******************************************************************************
|
|---|
| 21 | * Global Variables *
|
|---|
| 22 | *******************************************************************************/
|
|---|
| 23 | /** The number of signals. */
|
|---|
| 24 | static volatile long g_cSigs = 0;
|
|---|
| 25 | /** Number of signals received on threads other than the main one. */
|
|---|
| 26 | static volatile long g_cSigsOther = 0;
|
|---|
| 27 | /** Whether to shutdown or not. */
|
|---|
| 28 | static volatile int g_fShutdown = 0;
|
|---|
| 29 | /** The handle of the main thread. */
|
|---|
| 30 | static pthread_t g_hMainThread;
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | static void SigHandler(int iSig)
|
|---|
| 34 | {
|
|---|
| 35 | g_cSigs++;
|
|---|
| 36 | if (pthread_self() != g_hMainThread)
|
|---|
| 37 | g_cSigsOther++;
|
|---|
| 38 |
|
|---|
| 39 | (void)iSig;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | static void NanoSleep(unsigned long cNanoSecs)
|
|---|
| 44 | {
|
|---|
| 45 | struct timespec Ts;
|
|---|
| 46 | Ts.tv_sec = 0;
|
|---|
| 47 | Ts.tv_nsec = cNanoSecs;
|
|---|
| 48 | nanosleep(&Ts, NULL);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | static void *ThreadProc(void *pvIgnored)
|
|---|
| 53 | {
|
|---|
| 54 | int volatile i = 0;
|
|---|
| 55 | while (!g_fShutdown)
|
|---|
| 56 | {
|
|---|
| 57 | // NanoSleep(850);
|
|---|
| 58 | if (g_fShutdown)
|
|---|
| 59 | break;
|
|---|
| 60 |
|
|---|
| 61 | pthread_kill(g_hMainThread, SIGALRM);
|
|---|
| 62 | for (i = 6666; i > 0; i--)
|
|---|
| 63 | /* nothing */;
|
|---|
| 64 | }
|
|---|
| 65 | return NULL;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | int main(int argc, char **argv)
|
|---|
| 70 | {
|
|---|
| 71 | void (*rcSig)(int);
|
|---|
| 72 | pthread_t hThread;
|
|---|
| 73 | char szName[1024];
|
|---|
| 74 | int i;
|
|---|
| 75 | int rc;
|
|---|
| 76 |
|
|---|
| 77 | /*
|
|---|
| 78 | * Set up the signal handlers.
|
|---|
| 79 | */
|
|---|
| 80 | rcSig = bsd_signal(SIGALRM, SigHandler);
|
|---|
| 81 | if (rcSig != SIG_ERR)
|
|---|
| 82 | rcSig = bsd_signal(SIGCHLD, SigHandler);
|
|---|
| 83 | if (rcSig == SIG_ERR)
|
|---|
| 84 | {
|
|---|
| 85 | fprintf(stderr, "bsd_signal failed: %s\n", strerror(errno));
|
|---|
| 86 | return 1;
|
|---|
| 87 | }
|
|---|
| 88 | if (argc == 2) /* testing... */
|
|---|
| 89 | {
|
|---|
| 90 | siginterrupt(SIGALRM, 1);
|
|---|
| 91 | siginterrupt(SIGCHLD, 1);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /*
|
|---|
| 95 | * Kick off a thread that will signal us like there was no tomorrow.
|
|---|
| 96 | */
|
|---|
| 97 | g_hMainThread = pthread_self();
|
|---|
| 98 | rc = pthread_create(&hThread, NULL, ThreadProc, NULL);
|
|---|
| 99 | if (rc != 0)
|
|---|
| 100 | {
|
|---|
| 101 | fprintf(stderr, "pthread_create failed: %s\n", strerror(rc));
|
|---|
| 102 | return 1;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /*
|
|---|
| 106 | * Do path related stuff.
|
|---|
| 107 | */
|
|---|
| 108 | snprintf(szName, sizeof(szName), "%s-test2", argv[0]);
|
|---|
| 109 | for (i = 0; i < 100*1000*1000; i++)
|
|---|
| 110 | {
|
|---|
| 111 | struct stat St;
|
|---|
| 112 | int fd;
|
|---|
| 113 |
|
|---|
| 114 | rc = stat(argv[0], &St);
|
|---|
| 115 | if (rc == 0 || errno != EINTR)
|
|---|
| 116 | rc = stat(szName, &St);
|
|---|
| 117 | if (errno == EINTR && rc != 0)
|
|---|
| 118 | {
|
|---|
| 119 | printf("iteration %d: stat: %u\n", i, errno);
|
|---|
| 120 | break;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | fd = open(szName, O_CREAT | O_RDWR, 0666);
|
|---|
| 124 | if (errno == EINTR && fd < 0)
|
|---|
| 125 | {
|
|---|
| 126 | printf("iteration %d: open: %u\n", i, errno);
|
|---|
| 127 | break;
|
|---|
| 128 | }
|
|---|
| 129 | close(fd);
|
|---|
| 130 | rc = unlink(szName);
|
|---|
| 131 | if (errno == EINTR && rc != 0)
|
|---|
| 132 | {
|
|---|
| 133 | printf("iteration %d: unlink: %u\n", i, errno);
|
|---|
| 134 | break;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /* Show progress info */
|
|---|
| 138 | if ((i % 100000) == 0)
|
|---|
| 139 | {
|
|---|
| 140 | printf(".");
|
|---|
| 141 | if ((i % 1000000) == 0)
|
|---|
| 142 | printf("[%d/%ld/%ld]\n", i, g_cSigs, g_cSigsOther);
|
|---|
| 143 | fflush(stdout);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | g_fShutdown = 1;
|
|---|
| 148 | if (rc)
|
|---|
| 149 | printf("No EINTR in %d iterations - system is working nicely!\n", i);
|
|---|
| 150 | NanoSleep(10000000);
|
|---|
| 151 |
|
|---|
| 152 | return rc ? 1 : 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|