2 #define _XOPEN_SOURCE 700
14 static const char path
[] = ".";
15 static const int id
= 'h';
19 t_error("%s failed: %s\n", #f, strerror(errno)); \
22 #define EQ(a,b,fmt) do{ \
24 t_error("%s == %s failed: " fmt "\n", #a, #b, a, b); \
32 struct shmid_ds shmid_ds
;
36 T(k
= ftok(path
, id
));
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));
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
));
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 if (shmid_ds
.shm_ctime
> t
+5)
67 t_error("shmid_ds.shm_ctime <= t+5 failed: got %ld, want <= %ld\n", (long)shmid_ds
.shm_ctime
, (long)t
+5);
70 if ((p
=shmat(shmid
, 0, 0)) == 0)
71 t_error("shmat failed: %s\n", strerror(errno
));
72 T(shmctl(shmid
, IPC_STAT
, &shmid_ds
));
73 EQ((int)shmid_ds
.shm_nattch
, 1, "got %d, want %d");
74 EQ(shmid_ds
.shm_lpid
, getpid(), "got %d, want %d");
75 if (shmid_ds
.shm_atime
< t
)
76 t_error("shm_atime is %ld want >= %ld\n", (long)shmid_ds
.shm_atime
, (long)t
);
77 if (shmid_ds
.shm_atime
> t
+5)
78 t_error("shm_atime is %ld want <= %ld\n", (long)shmid_ds
.shm_atime
, (long)t
+5);
79 strcpy(p
, "test data");
89 T(k
= ftok(path
, id
));
90 T(shmid
= shmget(k
, 0, 0));
93 if ((p
=shmat(shmid
, 0, SHM_RDONLY
)) == 0)
94 t_error("shmat failed: %s\n", strerror(errno
));
96 if (strcmp(p
, "test data") != 0)
97 t_error("reading shared mem failed: got \"%.100s\" want \"test data\"\n", p
);
101 T(shmctl(shmid
, IPC_RMID
, 0));
112 t_error("fork failed: %s\n", strerror(errno
));
116 T(waitpid(p
, &status
, 0));
117 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
118 t_error("child exit status: %d\n", status
);