* sysdeps/unix/sysv/linux/Versions: Move sync_file_range to
[glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
blob0738501a5696efd650d6354ac7f43a60106d2307
1 /* Operating system specific code for generic dynamic loader functions. Linux.
2 Copyright (C) 2000,2001,2002,2004,2005,2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <string.h>
21 #include <fcntl.h>
22 #include <sys/utsname.h>
23 #include <kernel-features.h>
24 #include <dl-sysdep.h>
25 #include <stdint.h>
27 #ifndef MIN
28 # define MIN(a,b) (((a)<(b))?(a):(b))
29 #endif
31 #ifdef SHARED
32 /* This is the function used in the dynamic linker to print the fatal error
33 message. */
34 static inline void
35 __attribute__ ((__noreturn__))
36 dl_fatal (const char *str)
38 _dl_dprintf (2, str);
39 _exit (1);
41 #endif
43 static inline int __attribute__ ((always_inline))
44 _dl_discover_osversion (void)
46 #if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
47 if (GLRO(dl_sysinfo_map) != NULL)
49 /* If the kernel-supplied DSO contains a note indicating the kernel's
50 version, we don't need to call uname or parse any strings. */
52 static const struct
54 ElfW(Word) vendorlen;
55 ElfW(Word) datalen;
56 ElfW(Word) type;
57 char vendor[8];
58 } expected_note = { sizeof "Linux", sizeof (ElfW(Word)), 0, "Linux" };
59 const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
60 const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
61 for (uint_fast16_t i = 0; i < phnum; ++i)
62 if (phdr[i].p_type == PT_NOTE)
64 const ElfW(Addr) start = (phdr[i].p_vaddr
65 + GLRO(dl_sysinfo_map)->l_addr);
66 const struct
68 ElfW(Word) vendorlen;
69 ElfW(Word) datalen;
70 ElfW(Word) type;
71 } *note = (const void *) start;
72 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
74 if (!memcmp (note, &expected_note, sizeof expected_note))
75 return *(const ElfW(Word) *) ((const void *) note
76 + sizeof expected_note);
77 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
78 note = ((const void *) (note + 1)
79 + ROUND (note->vendorlen) + ROUND (note->datalen));
83 #endif
85 char bufmem[64];
86 char *buf = bufmem;
87 unsigned int version;
88 int parts;
89 char *cp;
90 struct utsname uts;
92 /* Try the uname system call. */
93 if (__uname (&uts))
95 /* This was not successful. Now try reading the /proc filesystem. */
96 int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);
97 if (fd < 0)
98 return -1;
99 ssize_t reslen = __read (fd, bufmem, sizeof (bufmem));
100 __close (fd);
101 if (reslen <= 0)
102 /* This also didn't work. We give up since we cannot
103 make sure the library can actually work. */
104 return -1;
105 buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
107 else
108 buf = uts.release;
110 /* Now convert it into a number. The string consists of at most
111 three parts. */
112 version = 0;
113 parts = 0;
114 cp = buf;
115 while ((*cp >= '0') && (*cp <= '9'))
117 unsigned int here = *cp++ - '0';
119 while ((*cp >= '0') && (*cp <= '9'))
121 here *= 10;
122 here += *cp++ - '0';
125 ++parts;
126 version <<= 8;
127 version |= here;
129 if (*cp++ != '.')
130 /* Another part following? */
131 break;
134 if (parts < 3)
135 version <<= 8 * (3 - parts);
137 return version;
140 #define DL_SYSDEP_OSCHECK(FATAL) \
141 do { \
142 /* Test whether the kernel is new enough. This test is only performed \
143 if the library is not compiled to run on all kernels. */ \
145 int version = _dl_discover_osversion (); \
146 if (__builtin_expect (version >= 0, 1)) \
148 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
149 || GLRO(dl_osversion) > version) \
150 GLRO(dl_osversion) = version; \
152 /* Now we can test with the required version. */ \
153 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
154 /* Not sufficent. */ \
155 FATAL ("FATAL: kernel too old\n"); \
157 else if (__LINUX_KERNEL_VERSION > 0) \
158 FATAL ("FATAL: cannot determine kernel version\n"); \
159 } while (0)
161 static inline uintptr_t __attribute__ ((always_inline))
162 _dl_setup_stack_chk_guard (void)
164 uintptr_t ret;
165 #ifdef ENABLE_STACKGUARD_RANDOMIZE
166 int fd = __open ("/dev/urandom", O_RDONLY);
167 if (fd >= 0)
169 ssize_t reslen = __read (fd, &ret, sizeof (ret));
170 __close (fd);
171 if (reslen == (ssize_t) sizeof (ret))
172 return ret;
174 #endif
175 ret = 0;
176 unsigned char *p = (unsigned char *) &ret;
177 p[sizeof (ret) - 1] = 255;
178 p[sizeof (ret) - 2] = '\n';
179 return ret;