// "owner" creates/frees the bufs, clients just open existing ones Extnbuf * _extnbuf_new(const char *base, int id, Eina_Bool sys, int num, int w, int h, Eina_Bool owner) { Extnbuf *b; char file[PATH_MAX]; mode_t mode = S_IRUSR; int prot = PROT_READ; int page_size; Eina_Tmpstr *tmp = NULL; page_size = eina_cpu_page_size(); b = calloc(1, sizeof(Extnbuf)); b->fd = -1; b->lockfd = -1; b->addr = MAP_FAILED; b->w = w; b->h = h; b->stride = w * 4; b->size = page_size * (((b->stride * b->h) + (page_size - 1)) / page_size); b->am_owner = owner; snprintf(file, sizeof(file), "/%s-%i.%i", base, id, num); b->file = eina_stringshare_add(file); if (!b->file) goto err; if (sys) mode |= S_IRGRP | S_IROTH; if (owner) { mode |= S_IWUSR; prot |= PROT_WRITE; } if (b->am_owner) { b->lockfd = eina_file_mkstemp("ee-lock-XXXXXX", &tmp); if (b->lockfd < 0) goto err; b->lock = eina_stringshare_add(file); if (!b->lock) goto err; b->fd = shm_open(b->file, O_RDWR | O_CREAT | O_EXCL, mode); if (b->fd < 0) goto err; if (ftruncate(b->fd, b->size) < 0) goto err; } else { b->fd = shm_open(b->file, O_RDONLY, mode); if (b->fd < 0) goto err; } b->addr = mmap(NULL, b->size, prot, MAP_SHARED, b->fd, 0); if (b->addr == MAP_FAILED) goto err; return b; err: if (tmp) eina_tmpstr_del(tmp); _extnbuf_free(b); return NULL; }
// "owner" creates/frees the bufs, clients just open existing ones Extnbuf * _extnbuf_new(const char *base, int id, Eina_Bool sys, int num, int w, int h, Eina_Bool owner) { Extnbuf *b; char file[PATH_MAX]; mode_t mode = S_IRUSR | S_IWUSR; b = calloc(1, sizeof(Extnbuf)); b->fd = -1; b->lockfd = -1; b->addr = MAP_FAILED; b->w = w; b->h = h; b->stride = w * 4; b->size = 4096 * (((b->stride * b->h) + (4096 - 1)) / 4096); b->am_owner = owner; snprintf(file, sizeof(file), "/%s-%i.%i", base, id, num); b->file = eina_stringshare_add(file); if (!b->file) goto err; if (sys) mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; if (b->am_owner) { const char *s; s = getenv("XDG_RUNTIME_DIR"); if (!s) s = getenv("TMPDIR"); if (!s) s = "/tmp"; snprintf(file, sizeof(file), "%s/ee-lock-XXXXXX", s); b->lockfd = mkstemp(file); if (b->lockfd < 0) goto err; b->lock = eina_stringshare_add(file); if (!b->lock) goto err; b->fd = shm_open(b->file, O_RDWR | O_CREAT | O_EXCL, mode); if (b->fd < 0) goto err; if (ftruncate(b->fd, b->size) < 0) goto err; } else { b->fd = shm_open(b->file, O_RDWR, mode); if (b->fd < 0) goto err; } b->addr = mmap(NULL, b->size, PROT_READ | PROT_WRITE, MAP_SHARED, b->fd, 0); if (b->addr == MAP_FAILED) goto err; return b; err: _extnbuf_free(b); return NULL; }