Linux: Implement interfaces for memory protection keys
[glibc.git] / libio / iofopen.c
blob62d03329946660f737bc68569e80ec354dddc582
1 /* Copyright (C) 1993-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
27 #include "libioP.h"
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <shlib-compat.h>
33 _IO_FILE *
34 __fopen_maybe_mmap (_IO_FILE *fp)
36 #ifdef _G_HAVE_MMAP
37 if ((fp->_flags2 & _IO_FLAGS2_MMAP) && (fp->_flags & _IO_NO_WRITES))
39 /* Since this is read-only, we might be able to mmap the contents
40 directly. We delay the decision until the first read attempt by
41 giving it a jump table containing functions that choose mmap or
42 vanilla file operations and reset the jump table accordingly. */
44 if (fp->_mode <= 0)
45 _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps_maybe_mmap;
46 else
47 _IO_JUMPS_FILE_plus (fp) = &_IO_wfile_jumps_maybe_mmap;
48 fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_maybe_mmap;
50 #endif
51 return fp;
55 _IO_FILE *
56 __fopen_internal (const char *filename, const char *mode, int is32)
58 struct locked_FILE
60 struct _IO_FILE_plus fp;
61 #ifdef _IO_MTSAFE_IO
62 _IO_lock_t lock;
63 #endif
64 struct _IO_wide_data wd;
65 } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
67 if (new_f == NULL)
68 return NULL;
69 #ifdef _IO_MTSAFE_IO
70 new_f->fp.file._lock = &new_f->lock;
71 #endif
72 _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps);
73 _IO_JUMPS (&new_f->fp) = &_IO_file_jumps;
74 _IO_new_file_init_internal (&new_f->fp);
75 #if !_IO_UNIFIED_JUMPTABLES
76 new_f->fp.vtable = NULL;
77 #endif
78 if (_IO_file_fopen ((_IO_FILE *) new_f, filename, mode, is32) != NULL)
79 return __fopen_maybe_mmap (&new_f->fp.file);
81 _IO_un_link (&new_f->fp);
82 free (new_f);
83 return NULL;
86 _IO_FILE *
87 _IO_new_fopen (const char *filename, const char *mode)
89 return __fopen_internal (filename, mode, 1);
92 strong_alias (_IO_new_fopen, __new_fopen)
93 versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
94 versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);
96 # if !defined O_LARGEFILE || O_LARGEFILE == 0
97 weak_alias (_IO_new_fopen, _IO_fopen64)
98 weak_alias (_IO_new_fopen, fopen64)
99 # endif