Path: blob/master/drivers/unix/file_access_unix_pipe.cpp
21264 views
/**************************************************************************/1/* file_access_unix_pipe.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "file_access_unix_pipe.h"3132#if defined(UNIX_ENABLED)3334#include "core/os/os.h"35#include "core/string/print_string.h"3637#include <fcntl.h>38#include <sys/ioctl.h>39#include <sys/stat.h>40#include <sys/types.h>41#include <unistd.h>42#include <cerrno>43#include <csignal>4445#ifndef sighandler_t46typedef typeof(void(int)) *sighandler_t;47#endif4849Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd, bool p_blocking) {50// Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe.51_close();5253path_src = String();54unlink_on_close = false;55ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");56fd[0] = p_rfd;57fd[1] = p_wfd;5859if (!p_blocking) {60fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) | O_NONBLOCK);61fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL) | O_NONBLOCK);62}6364last_error = OK;65return OK;66}6768Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) {69_close();7071path_src = p_path;72ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");7374path = String("/tmp/") + p_path.replace("pipe://", "").replace_char('/', '_');75const CharString path_utf8 = path.utf8();7677struct stat st = {};78int err = stat(path_utf8.get_data(), &st);79if (err) {80if (mkfifo(path_utf8.get_data(), 0600) != 0) {81last_error = ERR_FILE_CANT_OPEN;82return last_error;83}84unlink_on_close = true;85} else {86ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file.");87}8889int f = ::open(path_utf8.get_data(), O_RDWR | O_CLOEXEC | O_NONBLOCK);90if (f < 0) {91switch (errno) {92case ENOENT: {93last_error = ERR_FILE_NOT_FOUND;94} break;95default: {96last_error = ERR_FILE_CANT_OPEN;97} break;98}99return last_error;100}101102// Set close on exec to avoid leaking it to subprocesses.103fd[0] = f;104fd[1] = f;105106last_error = OK;107return OK;108}109110void FileAccessUnixPipe::_close() {111if (fd[0] < 0) {112return;113}114115if (fd[1] != fd[0]) {116::close(fd[1]);117}118::close(fd[0]);119fd[0] = -1;120fd[1] = -1;121122if (unlink_on_close) {123::unlink(path.utf8().ptr());124}125unlink_on_close = false;126}127128bool FileAccessUnixPipe::is_open() const {129return (fd[0] >= 0 || fd[1] >= 0);130}131132String FileAccessUnixPipe::get_path() const {133return path_src;134}135136String FileAccessUnixPipe::get_path_absolute() const {137return path_src;138}139140uint64_t FileAccessUnixPipe::get_length() const {141ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");142143int buf_rem = 0;144ERR_FAIL_COND_V(ioctl(fd[0], FIONREAD, &buf_rem) != 0, 0);145return buf_rem;146}147148uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {149ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");150ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);151152ssize_t read = ::read(fd[0], p_dst, p_length);153if (read == -1) {154last_error = ERR_FILE_CANT_READ;155read = 0;156} else if (read != (ssize_t)p_length) {157last_error = ERR_FILE_CANT_READ;158} else {159last_error = OK;160}161return read;162}163164Error FileAccessUnixPipe::get_error() const {165return last_error;166}167168bool FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {169ERR_FAIL_COND_V_MSG(fd[1] < 0, false, "Pipe must be opened before use.");170ERR_FAIL_COND_V(!p_src && p_length > 0, false);171172sighandler_t sig_pipe = signal(SIGPIPE, SIG_IGN);173ssize_t ret = ::write(fd[1], p_src, p_length);174signal(SIGPIPE, sig_pipe);175176if (ret != (ssize_t)p_length) {177last_error = ERR_FILE_CANT_WRITE;178return false;179} else {180last_error = OK;181return true;182}183}184185void FileAccessUnixPipe::close() {186_close();187}188189FileAccessUnixPipe::~FileAccessUnixPipe() {190_close();191}192193#endif // UNIX_ENABLED194195196