blob: 62242f11f2c7fd141121b921ce95fcf075b5b592 [file] [log] [blame]
sewardjb4112022007-11-09 22:49:28 +00001
2/* Do stupid things with semaphores, and check that Thrcheck doesn't
3 fall over and does report errors appropriately. If nothing else
4 this just checks that the relevant functions are getting
5 intercepted. */
6
7/* This is pretty lame, because making the sem_ functions fail is
8 difficult. Not sure it's really worth having. */
sewardj862ed5e2007-11-11 05:52:36 +00009#include <unistd.h>
sewardjb4112022007-11-09 22:49:28 +000010#include <stdio.h>
11#include <stdlib.h>
12#include <assert.h>
13#include <pthread.h>
mjw0c66d2e2015-09-04 09:41:42 +000014#include "safe-semaphore.h"
sewardjb4112022007-11-09 22:49:28 +000015#include <string.h>
sewardj862ed5e2007-11-11 05:52:36 +000016void start_watchdog ( void );
sewardjb4112022007-11-09 22:49:28 +000017int main ( void )
18{
bartf976f6c2011-04-03 17:42:19 +000019 int r __attribute__((unused));
sewardjb4112022007-11-09 22:49:28 +000020 sem_t s1;
sewardj862ed5e2007-11-11 05:52:36 +000021 start_watchdog();
sewardjb4112022007-11-09 22:49:28 +000022 /* Do sem_init with huge initial count */
23 r= sem_init(&s1, 0, ~0);
24
25 /* initialise properly */
26 r= sem_init(&s1, 0, 0);
27
28 /* in glibc, sem_destroy is a no-op; making it fail is
29 impossible. */
30
31 /* Do 'wait' on a bogus semaphore. This should fail, but on glibc
32 it succeeds. */
33 memset(&s1, 0x55, sizeof(s1));
34 r= sem_wait(&s1); /* assert(r != 0); */
sewardj8eb8bab2015-07-21 14:44:28 +000035#if defined(VGO_solaris)
36 assert(r != 0);
37#endif
sewardjb4112022007-11-09 22:49:28 +000038
sewardje3b57aa2008-01-18 07:42:01 +000039 /* this only fails with glibc 2.7 and later. */
40 r= sem_post(&s1);
sewardjb4112022007-11-09 22:49:28 +000041
42 sem_destroy(&s1);
43
44 return 0;
45}
sewardj862ed5e2007-11-11 05:52:36 +000046
47void* watchdog ( void* v )
48{
49 sleep(10);
50 fprintf(stderr, "watchdog timer expired - not a good sign\n");
51 exit(0);
52}
53
54void start_watchdog ( void )
55{
56 pthread_t t;
57 int r;
58 r= pthread_create(&t, NULL, watchdog, NULL);
59 assert(!r);
60}