1/* posix.c -- POSIX file I/O routines for the backtrace library.
2   Copyright (C) 2012-2015 Free Software Foundation, Inc.
3   Written by Ian Lance Taylor, Google.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9    (1) Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
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
15    distribution.
16
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
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY 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
44#ifndef O_BINARY
45#define O_BINARY 0
46#endif
47
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
58int
59backtrace_open (const char *filename, backtrace_error_callback error_callback,
60		void *data, int *does_not_exist)
61{
62  int descriptor;
63
64  if (does_not_exist != NULL)
65    *does_not_exist = 0;
66
67  descriptor = open (filename, O_RDONLY | O_BINARY | O_CLOEXEC);
68  if (descriptor < 0)
69    {
70      if (does_not_exist != NULL && errno == ENOENT)
71	*does_not_exist = 1;
72      else
73	error_callback (data, filename, errno);
74      return -1;
75    }
76
77#ifdef HAVE_FCNTL
78  /* Set FD_CLOEXEC just in case the kernel does not support
79     O_CLOEXEC. It doesn't matter if this fails for some reason.
80     FIXME: At some point it should be safe to only do this if
81     O_CLOEXEC == 0.  */
82  fcntl (descriptor, F_SETFD, FD_CLOEXEC);
83#endif
84
85  return descriptor;
86}
87
88/* Close DESCRIPTOR.  */
89
90int
91backtrace_close (int descriptor, backtrace_error_callback error_callback,
92		 void *data)
93{
94  if (close (descriptor) < 0)
95    {
96      error_callback (data, "close", errno);
97      return 0;
98    }
99  return 1;
100}
101