Data-Buffer-Shared

 view release on metacpan or  search on metacpan

buf_generic.h  view on Meta::CPAN


/* ---- mmap create/open ---- */

/* Race-safe create-or-attach with restrictive perms + symlink refusal.
 * The backing file is created 0600 by default (see file_mode); a local peer
 * cannot pre-create it as a symlink (O_NOFOLLOW) nor open a wider-permissioned
 * copy.  Sets *created=1 iff this call won the O_EXCL race and made the file. */
static int buf_secure_open(const char *path, mode_t file_mode, int *created, char *errbuf) {
    for (int attempt = 0; attempt < 100; attempt++) {
        int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, file_mode);
        if (fd >= 0) { (void)fchmod(fd, file_mode); *created = 1; return fd; }  /* exact mode: umask narrowed the create */
        if (errno != EEXIST) {
            snprintf(errbuf, BUF_ERR_BUFLEN, "create(%s): %s", path, strerror(errno));
            return -1;
        }
        fd = open(path, O_RDWR | O_NOFOLLOW | O_CLOEXEC);
        if (fd >= 0) { *created = 0; return fd; }
        if (errno == ENOENT) continue;   /* creator unlinked between our two opens; retry */
        snprintf(errbuf, BUF_ERR_BUFLEN, "open(%s): %s", path, strerror(errno));
        return -1;
    }



( run in 0.622 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )