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