Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 1 | /* posix.c -- POSIX file I/O routines for the backtrace library. |
Thomas Koenig | b18a97e | 2021-09-13 19:49:49 +0200 | [diff] [blame] | 2 | Copyright (C) 2012-2021 Free Software Foundation, Inc. |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 3 | Written by Ian Lance Taylor, Google. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are |
| 7 | met: |
| 8 | |
| 9 | (1) Redistributions of source code must retain the above copyright |
Carlos Liam | 84ebf63 | 2016-09-11 15:44:07 +0200 | [diff] [blame] | 10 | notice, this list of conditions and the following disclaimer. |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 11 | |
| 12 | (2) Redistributions in binary form must reproduce the above copyright |
| 13 | notice, this list of conditions and the following disclaimer in |
| 14 | the documentation and/or other materials provided with the |
Carlos Liam | 84ebf63 | 2016-09-11 15:44:07 +0200 | [diff] [blame] | 15 | distribution. |
| 16 | |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 17 | (3) The name of the author may not be used to |
| 18 | endorse or promote products derived from this software without |
| 19 | specific prior written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 22 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 25 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 29 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 30 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | POSSIBILITY OF SUCH DAMAGE. */ |
| 32 | |
| 33 | #include "config.h" |
| 34 | |
| 35 | #include <errno.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include "backtrace.h" |
| 42 | #include "internal.h" |
| 43 | |
Ian Lance Taylor | 3319ef1 | 2012-09-18 18:06:28 +0000 | [diff] [blame] | 44 | #ifndef O_BINARY |
| 45 | #define O_BINARY 0 |
| 46 | #endif |
| 47 | |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 48 | #ifndef O_CLOEXEC |
| 49 | #define O_CLOEXEC 0 |
| 50 | #endif |
| 51 | |
| 52 | #ifndef FD_CLOEXEC |
| 53 | #define FD_CLOEXEC 1 |
| 54 | #endif |
| 55 | |
| 56 | /* Open a file for reading. */ |
| 57 | |
| 58 | int |
| 59 | backtrace_open (const char *filename, backtrace_error_callback error_callback, |
Ian Lance Taylor | 73f4149 | 2012-10-26 20:08:29 +0000 | [diff] [blame] | 60 | void *data, int *does_not_exist) |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 61 | { |
| 62 | int descriptor; |
| 63 | |
Ian Lance Taylor | 73f4149 | 2012-10-26 20:08:29 +0000 | [diff] [blame] | 64 | if (does_not_exist != NULL) |
| 65 | *does_not_exist = 0; |
| 66 | |
Ian Lance Taylor | 750cdaf | 2015-09-17 17:08:04 +0000 | [diff] [blame] | 67 | descriptor = open (filename, (int) (O_RDONLY | O_BINARY | O_CLOEXEC)); |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 68 | if (descriptor < 0) |
| 69 | { |
Ian Lance Taylor | 702adbb | 2020-05-13 10:18:45 -0700 | [diff] [blame] | 70 | /* If DOES_NOT_EXIST is not NULL, then don't call ERROR_CALLBACK |
| 71 | if the file does not exist. We treat lacking permission to |
| 72 | open the file as the file not existing; this case arises when |
| 73 | running the libgo syscall package tests as root. */ |
| 74 | if (does_not_exist != NULL && (errno == ENOENT || errno == EACCES)) |
Ian Lance Taylor | 73f4149 | 2012-10-26 20:08:29 +0000 | [diff] [blame] | 75 | *does_not_exist = 1; |
| 76 | else |
| 77 | error_callback (data, filename, errno); |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
Ian Lance Taylor | 3319ef1 | 2012-09-18 18:06:28 +0000 | [diff] [blame] | 81 | #ifdef HAVE_FCNTL |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 82 | /* Set FD_CLOEXEC just in case the kernel does not support |
| 83 | O_CLOEXEC. It doesn't matter if this fails for some reason. |
| 84 | FIXME: At some point it should be safe to only do this if |
| 85 | O_CLOEXEC == 0. */ |
| 86 | fcntl (descriptor, F_SETFD, FD_CLOEXEC); |
Ian Lance Taylor | 3319ef1 | 2012-09-18 18:06:28 +0000 | [diff] [blame] | 87 | #endif |
Ian Lance Taylor | eff02e4 | 2012-09-17 16:38:38 +0000 | [diff] [blame] | 88 | |
| 89 | return descriptor; |
| 90 | } |
| 91 | |
| 92 | /* Close DESCRIPTOR. */ |
| 93 | |
| 94 | int |
| 95 | backtrace_close (int descriptor, backtrace_error_callback error_callback, |
| 96 | void *data) |
| 97 | { |
| 98 | if (close (descriptor) < 0) |
| 99 | { |
| 100 | error_callback (data, "close", errno); |
| 101 | return 0; |
| 102 | } |
| 103 | return 1; |
| 104 | } |