add test for strftime
[libc-test.git] / src / functional / ipc_shm.c
blob980743697a072e949cb9a734bf86048b3df6e1cb
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"
14 static const char path[] = ".";
15 static const int id = 'h';
17 #define T(f) do{ \
18 if ((f)+1 == 0) \
19 t_error("%s failed: %s\n", #f, strerror(errno)); \
20 }while(0)
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)
27 static void set()
29 time_t t;
30 key_t k;
31 int shmid;
32 struct shmid_ds shmid_ds;
33 void *p;
35 T(t = time(0));
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));
43 if (t_status)
44 exit(t_status);
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));
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);
69 /* test attach */
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");
80 T(shmdt(p));
83 static void get()
85 key_t k;
86 int shmid;
87 void *p;
89 T(k = ftok(path, id));
90 T(shmid = shmget(k, 0, 0));
92 errno = 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);
99 /* cleanup */
100 T(shmdt(p));
101 T(shmctl(shmid, IPC_RMID, 0));
104 int main(void)
106 int p;
107 int status;
109 set();
110 p = fork();
111 if (p == -1)
112 t_error("fork failed: %s\n", strerror(errno));
113 else if (p == 0)
114 get();
115 else {
116 T(waitpid(p, &status, 0));
117 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
118 t_error("child exit status: %d\n", status);
120 return t_status;