/* posix.c -- POSIX file I/O routines for the backtrace library.1Copyright (C) 2012-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#include "config.h"3334#include <errno.h>35#include <sys/types.h>36#include <sys/stat.h>37#include <fcntl.h>38#include <unistd.h>3940#include "backtrace.h"41#include "internal.h"4243#ifndef O_BINARY44#define O_BINARY 045#endif4647#ifndef O_CLOEXEC48#define O_CLOEXEC 049#endif5051#ifndef FD_CLOEXEC52#define FD_CLOEXEC 153#endif5455/* Open a file for reading. */5657int58backtrace_open (const char *filename, backtrace_error_callback error_callback,59void *data, int *does_not_exist)60{61int descriptor;6263if (does_not_exist != NULL)64*does_not_exist = 0;6566descriptor = open (filename, (int) (O_RDONLY | O_BINARY | O_CLOEXEC));67if (descriptor < 0)68{69/* If DOES_NOT_EXIST is not NULL, then don't call ERROR_CALLBACK70if the file does not exist. We treat lacking permission to71open the file as the file not existing; this case arises when72running the libgo syscall package tests as root. */73if (does_not_exist != NULL && (errno == ENOENT || errno == EACCES))74*does_not_exist = 1;75else76error_callback (data, filename, errno);77return -1;78}7980#ifdef HAVE_FCNTL81/* Set FD_CLOEXEC just in case the kernel does not support82O_CLOEXEC. It doesn't matter if this fails for some reason.83FIXME: At some point it should be safe to only do this if84O_CLOEXEC == 0. */85fcntl (descriptor, F_SETFD, FD_CLOEXEC);86#endif8788return descriptor;89}9091/* Close DESCRIPTOR. */9293int94backtrace_close (int descriptor, backtrace_error_callback error_callback,95void *data)96{97if (close (descriptor) < 0)98{99error_callback (data, "close", errno);100return 0;101}102return 1;103}104105106