first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source3 / tests / sysv_ipc.c
1 /* this tests whether we can use a sysv shared memory segment
2    as needed for the sysv varient of FAST_SHARE_MODES */
3
4 #if defined(HAVE_UNISTD_H)
5 #include <unistd.h>
6 #endif
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/ipc.h>
10 #include <sys/shm.h>
11 #include <sys/sem.h>
12
13 #define KEY 0x963796
14 #define SEMKEY 0x963797
15 #define SIZE (32*1024)
16
17 #ifndef HAVE_UNION_SEMUN
18 union semun {
19         int val;
20         struct semid_ds *buf;
21         unsigned short *array;
22 };
23 #endif
24
25
26 main()
27 {
28         int id, sem_id;
29         int *buf;
30         int count=7;
31         union semun su;
32
33 #ifdef LINUX
34         if (sizeof(struct shmid_ds) == 52) {
35                 printf("WARNING: You probably have a broken set of glibc2 include files - disabling sysv shared memory\n");
36                 exit(1);
37         }
38 #endif
39
40
41         sem_id = semget(SEMKEY, 1, IPC_CREAT|IPC_EXCL|0600);
42
43         if (sem_id == -1) exit(1);
44
45         su.val = 1;
46         semctl(sem_id, 0, IPC_RMID, su);
47
48         id = shmget(KEY, 0, 0);
49         if (id != -1) {
50                 if (shmctl(id, IPC_RMID, 0) != 0) exit(1);
51         }
52
53         if (fork() == 0) {
54                 /* uggh - need locking */
55                 sleep(2);
56
57                 /* get an existing area */
58                 id = shmget(KEY, 0, 0);
59                 if (id == -1) exit(1);
60
61                 buf = (int *)shmat(id, 0, 0);
62                 if (buf == (int *)-1) exit(1);
63
64
65                 while (count-- && buf[6124] != 55732) sleep(1);
66
67                 if (count <= 0) exit(1);
68
69                 buf[1763] = 7268;
70                 exit(0);
71         }
72         
73         id = shmget(KEY, SIZE, IPC_CREAT | IPC_EXCL | 0600);
74         if (id == -1) exit(1);
75
76         buf = (int *)shmat(id, 0, 0);
77
78         if (buf == (int *)-1) exit(1);
79
80         buf[6124] = 55732;
81
82         while (count-- && buf[1763] != 7268) sleep(1);
83
84         shmctl(id, IPC_RMID, 0);
85
86         if (count <= 0) exit(1);
87         exit(0);
88 }