shmpipe: Simplify handling of random paths

This commit is contained in:
Olivier Crête 2010-01-28 11:57:34 +02:00
parent 23414310a6
commit 8f8b50a88e

View File

@ -224,7 +224,6 @@ sp_open_shm (char *path, int id, int writer, mode_t perms, size_t size)
ShmArea *area = spalloc_new (ShmArea); ShmArea *area = spalloc_new (ShmArea);
char tmppath[PATH_MAX]; char tmppath[PATH_MAX];
int flags; int flags;
int has_path = (path != NULL) ? 1 : 0;
int prot; int prot;
memset (area, 0, sizeof (ShmArea)); memset (area, 0, sizeof (ShmArea));
@ -240,25 +239,24 @@ sp_open_shm (char *path, int id, int writer, mode_t perms, size_t size)
flags = O_RDONLY; flags = O_RDONLY;
area->shm_fd = -1; area->shm_fd = -1;
if (!path)
path = tmppath;
if (path) {
area->shm_fd = shm_open (path, flags, perms);
} else {
do { do {
if (path == tmppath) {
path = tmppath;
snprintf (tmppath, PATH_MAX, "/%X%X%X%X%X.shmpipe", snprintf (tmppath, PATH_MAX, "/%X%X%X%X%X.shmpipe",
rand (), rand (), rand (), rand (), rand ()); rand (), rand (), rand (), rand (), rand ());
area->shm_fd = shm_open (tmppath, flags, perms);
} while (area->shm_fd < 0 && errno == EEXIST);
} }
area->shm_fd = shm_open (path, flags, perms);
} while (path == tmppath && area->shm_fd < 0 && errno == EEXIST);
if (area->shm_fd < 0) { if (area->shm_fd < 0) {
RETURN_ERROR ("shm_open failed on %s (%d): %s\n", path, errno, RETURN_ERROR ("shm_open failed on %s (%d): %s\n",
strerror (errno)); path ? path : tmppath, errno, strerror (errno));
} }
if (!has_path) if (!path)
area->shm_area_name = strdup (path); area->shm_area_name = strdup (tmppath);
if (writer) { if (writer) {
if (ftruncate (area->shm_fd, size)) { if (ftruncate (area->shm_fd, size)) {