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);
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");
85 T(k = ftok(path, id));
86 T(shmid = shmget(k, 0, 0));
89 if ((p=shmat(shmid, 0, SHM_RDONLY)) == 0)
90 t_error("shmat failed: %s\n", strerror(errno));
92 if (strcmp(p, "test data") != 0)
93 t_error("reading shared mem failed: got \"%.100s\" want \"test data\"\n", p);
97 T(shmctl(shmid, IPC_RMID, 0));
108 t_error("fork failed: %s\n", strerror(errno));
112 T(waitpid(p, &status, 0));
113 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
114 t_error("child exit status: %d\n", status);