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