2c87e2c5ac41d9913ef8122248747f2f534a5683
[libc-test] / src / functional / ipc_shm.c
1 #ifndef _XOPEN_SOURCE
2 #define _XOPEN_SOURCE 700
3 #endif
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <sys/types.h>
9 #include <sys/shm.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12 #include "test.h"
13
14 static const char path[] = ".";
15 static const int id = 'h';
16
17 #define T(f) do{ \
18         if ((f)+1 == 0) \
19                 t_error("%s failed: %s\n", #f, strerror(errno)); \
20 }while(0)
21
22 #define EQ(a,b,fmt) do{ \
23         if ((a) != (b)) \
24                 t_error("%s == %s failed: " fmt "\n", #a, #b, a, b); \
25 }while(0)
26
27 static void set()
28 {
29         time_t t;
30         key_t k;
31         int shmid;
32         struct shmid_ds shmid_ds;
33         void *p;
34
35         T(t = time(0));
36         T(k = ftok(path, id));
37
38         /* make sure we get a clean shared memory id */
39         T(shmid = shmget(k, 100, IPC_CREAT|0666));
40         T(shmctl(shmid, IPC_RMID, 0));
41         T(shmid = shmget(k, 100, IPC_CREAT|IPC_EXCL|0666));
42
43         if (t_status)
44                 exit(t_status);
45
46         /* check IPC_EXCL */
47         errno = 0;
48         if (shmget(k, 100, IPC_CREAT|IPC_EXCL|0666) != -1 || errno != EEXIST)
49                 t_error("shmget(IPC_CREAT|IPC_EXCL) should have failed with EEXIST, got %s\n", strerror(errno));
50
51         /* check if shmget initilaized the msshmid_ds structure correctly */
52         T(shmctl(shmid, IPC_STAT, &shmid_ds));
53         EQ(shmid_ds.shm_perm.cuid, geteuid(), "got %d, want %d");
54         EQ(shmid_ds.shm_perm.uid, geteuid(), "got %d, want %d");
55         EQ(shmid_ds.shm_perm.cgid, getegid(), "got %d, want %d");
56         EQ(shmid_ds.shm_perm.gid, getegid(), "got %d, want %d");
57         EQ(shmid_ds.shm_perm.mode & 0x1ff, 0666, "got %o, want %o");
58         EQ(shmid_ds.shm_segsz, 100, "got %d, want %d");
59         EQ(shmid_ds.shm_lpid, 0, "got %d, want %d");
60         EQ(shmid_ds.shm_cpid, getpid(), "got %d, want %d");
61         EQ((int)shmid_ds.shm_nattch, 0, "got %d, want %d");
62         EQ((long)shmid_ds.shm_atime, 0, "got %ld, want %d");
63         EQ((long)shmid_ds.shm_dtime, 0, "got %ld, want %d");
64         if (shmid_ds.shm_ctime < t)
65                 t_error("shmid_ds.shm_ctime >= t failed: got %ld, want %ld\n", (long)shmid_ds.shm_ctime, (long)t);
66
67         /* test attach */
68         if ((p=shmat(shmid, 0, 0)) == 0)
69                 t_error("shmat failed: %s\n", strerror(errno));
70         T(shmctl(shmid, IPC_STAT, &shmid_ds));
71         EQ((int)shmid_ds.shm_nattch, 1, "got %d, want %d");
72         EQ(shmid_ds.shm_lpid, getpid(), "got %d, want %d");
73         if (shmid_ds.shm_atime < t)
74                 t_error("shm_atime is %ld want >= %ld\n", (long)shmid_ds.shm_atime, (long)t);
75         strcpy(p, "test data");
76         T(shmdt(p));
77 }
78
79 static void get()
80 {
81         key_t k;
82         int shmid;
83         void *p;
84
85         T(k = ftok(path, id));
86         T(shmid = shmget(k, 0, 0));
87
88         errno = 0;
89         if ((p=shmat(shmid, 0, SHM_RDONLY)) == 0)
90                 t_error("shmat failed: %s\n", strerror(errno));
91
92         if (strcmp(p, "test data") != 0)
93                 t_error("reading shared mem failed: got \"%.100s\" want \"test data\"\n", p);
94
95         /* cleanup */
96         T(shmdt(p));
97         T(shmctl(shmid, IPC_RMID, 0));
98 }
99
100 int main(void)
101 {
102         int p;
103         int status;
104
105         set();
106         p = fork();
107         if (p == -1)
108                 t_error("fork failed: %s\n", strerror(errno));
109         else if (p == 0)
110                 get();
111         else {
112                 T(waitpid(p, &status, 0));
113                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
114                         t_error("child exit status: %d\n", status);
115         }
116         return t_status;
117 }