|
Szabolcs Nagy |
d7d397 |
#ifndef _XOPEN_SOURCE
|
|
Szabolcs Nagy |
d7d397 |
#define _XOPEN_SOURCE 700
|
|
Szabolcs Nagy |
d7d397 |
#endif
|
|
Szabolcs Nagy |
d7d397 |
#include <errno.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <stdlib.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <string.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <time.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <sys/types.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <sys/shm.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <sys/wait.h>
|
|
Szabolcs Nagy |
d7d397 |
#include <unistd.h>
|
|
Szabolcs Nagy |
d7d397 |
#include "test.h"
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
static const char path[] = ".";
|
|
Szabolcs Nagy |
a5138a |
static const int id = 'h';
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
#define T(f) do{ \
|
|
Szabolcs Nagy |
d7d397 |
if ((f)+1 == 0) \
|
|
Szabolcs Nagy |
cfa23c |
t_error("%s failed: %s\n", #f, strerror(errno)); \
|
|
Szabolcs Nagy |
d7d397 |
}while(0)
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
#define EQ(a,b,fmt) do{ \
|
|
Szabolcs Nagy |
d7d397 |
if ((a) != (b)) \
|
|
Szabolcs Nagy |
cfa23c |
t_error("%s == %s failed: " fmt "\n", #a, #b, a, b); \
|
|
Szabolcs Nagy |
d7d397 |
}while(0)
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
static void set()
|
|
Szabolcs Nagy |
d7d397 |
{
|
|
Szabolcs Nagy |
d7d397 |
time_t t;
|
|
Szabolcs Nagy |
d7d397 |
key_t k;
|
|
Szabolcs Nagy |
d7d397 |
int shmid;
|
|
Szabolcs Nagy |
d7d397 |
struct shmid_ds shmid_ds;
|
|
Szabolcs Nagy |
d7d397 |
void *p;
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
T(t = time(0));
|
|
Szabolcs Nagy |
d7d397 |
T(k = ftok(path, id));
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
/* make sure we get a clean shared memory id */
|
|
Szabolcs Nagy |
d7d397 |
T(shmid = shmget(k, 100, IPC_CREAT|0666));
|
|
Szabolcs Nagy |
d7d397 |
T(shmctl(shmid, IPC_RMID, 0));
|
|
Szabolcs Nagy |
d7d397 |
T(shmid = shmget(k, 100, IPC_CREAT|IPC_EXCL|0666));
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
cfa23c |
if (t_status)
|
|
Szabolcs Nagy |
cfa23c |
exit(t_status);
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
/* check IPC_EXCL */
|
|
Szabolcs Nagy |
d7d397 |
errno = 0;
|
|
Szabolcs Nagy |
d7d397 |
if (shmget(k, 100, IPC_CREAT|IPC_EXCL|0666) != -1 || errno != EEXIST)
|
|
Szabolcs Nagy |
cfa23c |
t_error("shmget(IPC_CREAT|IPC_EXCL) should have failed with EEXIST, got %s\n", strerror(errno));
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
/* check if shmget initilaized the msshmid_ds structure correctly */
|
|
Szabolcs Nagy |
d7d397 |
T(shmctl(shmid, IPC_STAT, &shmid_ds));
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_perm.cuid, geteuid(), "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_perm.uid, geteuid(), "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_perm.cgid, getegid(), "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_perm.gid, getegid(), "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_perm.mode & 0x1ff, 0666, "got %o, want %o");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_segsz, 100, "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_lpid, 0, "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_cpid, getpid(), "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ((int)shmid_ds.shm_nattch, 0, "got %d, want %d");
|
|
Szabolcs Nagy |
d8e961 |
EQ((long long)shmid_ds.shm_atime, 0, "got %lld, want %d");
|
|
Szabolcs Nagy |
d8e961 |
EQ((long long)shmid_ds.shm_dtime, 0, "got %lld, want %d");
|
|
Szabolcs Nagy |
d7d397 |
if (shmid_ds.shm_ctime < t)
|
|
Szabolcs Nagy |
d8e961 |
t_error("shmid_ds.shm_ctime >= t failed: got %lld, want >= %lld\n", (long long)shmid_ds.shm_ctime, (long long)t);
|
|
Szabolcs Nagy |
d5fe2f |
if (shmid_ds.shm_ctime > t+5)
|
|
Szabolcs Nagy |
d8e961 |
t_error("shmid_ds.shm_ctime <= t+5 failed: got %lld, want <= %lld\n", (long long)shmid_ds.shm_ctime, (long long)t+5);
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
/* test attach */
|
|
Szabolcs Nagy |
d7d397 |
if ((p=shmat(shmid, 0, 0)) == 0)
|
|
Szabolcs Nagy |
cfa23c |
t_error("shmat failed: %s\n", strerror(errno));
|
|
Szabolcs Nagy |
d7d397 |
T(shmctl(shmid, IPC_STAT, &shmid_ds));
|
|
Szabolcs Nagy |
d7d397 |
EQ((int)shmid_ds.shm_nattch, 1, "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
EQ(shmid_ds.shm_lpid, getpid(), "got %d, want %d");
|
|
Szabolcs Nagy |
d7d397 |
if (shmid_ds.shm_atime < t)
|
|
Szabolcs Nagy |
d8e961 |
t_error("shm_atime is %lld want >= %lld\n", (long long)shmid_ds.shm_atime, (long long)t);
|
|
Szabolcs Nagy |
d5fe2f |
if (shmid_ds.shm_atime > t+5)
|
|
Szabolcs Nagy |
d8e961 |
t_error("shm_atime is %lld want <= %lld\n", (long long)shmid_ds.shm_atime, (long long)t+5);
|
|
Szabolcs Nagy |
d7d397 |
strcpy(p, "test data");
|
|
Szabolcs Nagy |
d7d397 |
T(shmdt(p));
|
|
Szabolcs Nagy |
d7d397 |
}
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
static void get()
|
|
Szabolcs Nagy |
d7d397 |
{
|
|
Szabolcs Nagy |
d7d397 |
key_t k;
|
|
Szabolcs Nagy |
d7d397 |
int shmid;
|
|
Szabolcs Nagy |
d7d397 |
void *p;
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
T(k = ftok(path, id));
|
|
Szabolcs Nagy |
d7d397 |
T(shmid = shmget(k, 0, 0));
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
errno = 0;
|
|
Szabolcs Nagy |
d7d397 |
if ((p=shmat(shmid, 0, SHM_RDONLY)) == 0)
|
|
Szabolcs Nagy |
cfa23c |
t_error("shmat failed: %s\n", strerror(errno));
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
if (strcmp(p, "test data") != 0)
|
|
Szabolcs Nagy |
cfa23c |
t_error("reading shared mem failed: got \"%.100s\" want \"test data\"\n", p);
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
/* cleanup */
|
|
Szabolcs Nagy |
d7d397 |
T(shmdt(p));
|
|
Szabolcs Nagy |
d7d397 |
T(shmctl(shmid, IPC_RMID, 0));
|
|
Szabolcs Nagy |
d7d397 |
}
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
int main(void)
|
|
Szabolcs Nagy |
d7d397 |
{
|
|
Szabolcs Nagy |
d7d397 |
int p;
|
|
Szabolcs Nagy |
d7d397 |
int status;
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
d7d397 |
set();
|
|
Szabolcs Nagy |
d7d397 |
p = fork();
|
|
Szabolcs Nagy |
d7d397 |
if (p == -1)
|
|
Szabolcs Nagy |
cfa23c |
t_error("fork failed: %s\n", strerror(errno));
|
|
Szabolcs Nagy |
d7d397 |
else if (p == 0)
|
|
Szabolcs Nagy |
d7d397 |
get();
|
|
Szabolcs Nagy |
d7d397 |
else {
|
|
Szabolcs Nagy |
d7d397 |
T(waitpid(p, &status, 0));
|
|
Szabolcs Nagy |
d7d397 |
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
Szabolcs Nagy |
cfa23c |
t_error("child exit status: %d\n", status);
|
|
Szabolcs Nagy |
d7d397 |
}
|
|
Szabolcs Nagy |
cfa23c |
return t_status;
|
|
Szabolcs Nagy |
d7d397 |
}
|