posix.c 2.8 KB

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