2017-08-14 18:19:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 Collabora, Ltd.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2017-08-14 18:19:42 +02:00
|
|
|
#include <errno.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <fcntl.h>
|
2017-08-14 18:19:42 +02:00
|
|
|
#include <stdlib.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2018-06-30 03:55:33 +02:00
|
|
|
#include <sys/stat.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2018-07-21 21:39:00 +02:00
|
|
|
#include <wlr/config.h>
|
2017-11-30 15:00:36 +01:00
|
|
|
#include "util/os-compatibility.h"
|
2017-08-14 18:19:42 +02:00
|
|
|
|
2017-11-30 15:00:36 +01:00
|
|
|
int os_fd_set_cloexec(int fd) {
|
2018-07-21 12:55:54 +02:00
|
|
|
if (fd == -1) {
|
2017-08-14 18:19:42 +02:00
|
|
|
return -1;
|
2018-07-21 12:55:54 +02:00
|
|
|
}
|
2017-08-14 18:19:42 +02:00
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
long flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags == -1) {
|
2017-08-14 18:19:42 +02:00
|
|
|
return -1;
|
2018-07-21 12:55:54 +02:00
|
|
|
}
|
2017-08-14 18:19:42 +02:00
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
|
2017-08-14 18:19:42 +02:00
|
|
|
return -1;
|
2018-07-21 12:55:54 +02:00
|
|
|
}
|
2017-08-14 18:19:42 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-30 15:00:36 +01:00
|
|
|
int set_cloexec_or_close(int fd) {
|
2017-08-14 18:19:42 +02:00
|
|
|
if (os_fd_set_cloexec(fd) != 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
int create_tmpfile_cloexec(char *tmpname) {
|
2017-08-14 18:19:42 +02:00
|
|
|
int fd;
|
2018-06-30 03:55:33 +02:00
|
|
|
mode_t prev_umask = umask(0066);
|
2017-08-14 18:19:42 +02:00
|
|
|
#ifdef HAVE_MKOSTEMP
|
|
|
|
fd = mkostemp(tmpname, O_CLOEXEC);
|
2018-07-21 12:55:54 +02:00
|
|
|
if (fd >= 0) {
|
2017-08-14 18:19:42 +02:00
|
|
|
unlink(tmpname);
|
2018-07-21 12:55:54 +02:00
|
|
|
}
|
2017-08-14 18:19:42 +02:00
|
|
|
#else
|
|
|
|
fd = mkstemp(tmpname);
|
|
|
|
if (fd >= 0) {
|
|
|
|
fd = set_cloexec_or_close(fd);
|
|
|
|
unlink(tmpname);
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-30 03:55:33 +02:00
|
|
|
umask(prev_umask);
|
2017-08-14 18:19:42 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new, unique, anonymous file of the given size, and
|
|
|
|
* return the file descriptor for it. The file descriptor is set
|
|
|
|
* CLOEXEC. The file is immediately suitable for mmap()'ing
|
|
|
|
* the given size at offset zero.
|
|
|
|
*
|
|
|
|
* The file should not have a permanent backing store like a disk,
|
|
|
|
* but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
|
|
|
|
*
|
|
|
|
* The file name is deleted from the file system.
|
|
|
|
*
|
|
|
|
* The file is suitable for buffer sharing between processes by
|
|
|
|
* transmitting the file descriptor over Unix sockets using the
|
|
|
|
* SCM_RIGHTS methods.
|
|
|
|
*
|
|
|
|
* If the C library implements posix_fallocate(), it is used to
|
|
|
|
* guarantee that disk space is available for the file at the
|
|
|
|
* given size. If disk space is insufficient, errno is set to ENOSPC.
|
|
|
|
* If posix_fallocate() is not supported, program may receive
|
|
|
|
* SIGBUS on accessing mmap()'ed file contents instead.
|
|
|
|
*/
|
2017-11-30 15:00:36 +01:00
|
|
|
int os_create_anonymous_file(off_t size) {
|
2017-08-14 18:19:42 +02:00
|
|
|
static const char template[] = "/wlroots-shared-XXXXXX";
|
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
const char *path = getenv("XDG_RUNTIME_DIR");
|
2017-08-14 18:19:42 +02:00
|
|
|
if (!path) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
char *name = malloc(strlen(path) + sizeof(template));
|
|
|
|
if (!name) {
|
2017-08-14 18:19:42 +02:00
|
|
|
return -1;
|
2018-07-21 12:55:54 +02:00
|
|
|
}
|
2017-08-14 18:19:42 +02:00
|
|
|
|
|
|
|
strcpy(name, path);
|
|
|
|
strcat(name, template);
|
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
int fd = create_tmpfile_cloexec(name);
|
2017-08-14 18:19:42 +02:00
|
|
|
free(name);
|
2018-07-21 12:55:54 +02:00
|
|
|
if (fd < 0) {
|
2017-08-14 18:19:42 +02:00
|
|
|
return -1;
|
2018-07-21 12:55:54 +02:00
|
|
|
}
|
2017-08-14 18:19:42 +02:00
|
|
|
|
2018-07-21 12:55:54 +02:00
|
|
|
#ifdef WLR_HAS_POSIX_FALLOCATE
|
|
|
|
int ret;
|
2017-08-14 18:19:42 +02:00
|
|
|
do {
|
|
|
|
ret = posix_fallocate(fd, 0, size);
|
|
|
|
} while (ret == EINTR);
|
|
|
|
if (ret != 0) {
|
|
|
|
close(fd);
|
|
|
|
errno = ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2018-07-21 12:55:54 +02:00
|
|
|
int ret;
|
2017-08-14 18:19:42 +02:00
|
|
|
do {
|
|
|
|
ret = ftruncate(fd, size);
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
if (ret < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|