annotate libbacktrace/posix.c @ 131:84e7813d76e9

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