]>
Commit | Line | Data |
---|---|---|
683158e0 | 1 | /* Dynamic linker system dependencies for Linux. |
2b778ceb | 2 | Copyright (C) 1995-2021 Free Software Foundation, Inc. |
478b92f0 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
478b92f0 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
478b92f0 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
683158e0 RM |
18 | |
19 | /* Linux needs some special initialization, but otherwise uses | |
20 | the generic dynamic linker system interface code. */ | |
21 | ||
7c22c7ec UD |
22 | #include <string.h> |
23 | #include <fcntl.h> | |
683158e0 | 24 | #include <unistd.h> |
fe5f34e4 | 25 | #include <sys/param.h> |
7c22c7ec | 26 | #include <sys/utsname.h> |
a489c529 | 27 | #include <ldsodefs.h> |
329ea513 | 28 | #include <not-cancel.h> |
683158e0 | 29 | |
368a8dec UD |
30 | #ifdef SHARED |
31 | # define DL_SYSDEP_INIT frob_brk () | |
683158e0 RM |
32 | |
33 | static inline void | |
34 | frob_brk (void) | |
35 | { | |
683158e0 | 36 | __brk (0); /* Initialize the break. */ |
683158e0 RM |
37 | } |
38 | ||
368a8dec UD |
39 | # include <elf/dl-sysdep.c> |
40 | #endif | |
7c22c7ec UD |
41 | |
42 | ||
43 | int | |
44 | attribute_hidden | |
45 | _dl_discover_osversion (void) | |
46 | { | |
7775448e | 47 | #if defined NEED_DL_SYSINFO_DSO && defined SHARED |
7c22c7ec UD |
48 | if (GLRO(dl_sysinfo_map) != NULL) |
49 | { | |
50 | /* If the kernel-supplied DSO contains a note indicating the kernel's | |
51 | version, we don't need to call uname or parse any strings. */ | |
52 | ||
53 | static const struct | |
54 | { | |
55 | ElfW(Nhdr) hdr; | |
56 | char vendor[8]; | |
57 | } expected_note = { { sizeof "Linux", sizeof (ElfW(Word)), 0 }, "Linux" }; | |
58 | const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr; | |
59 | const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum; | |
60 | for (uint_fast16_t i = 0; i < phnum; ++i) | |
61 | if (phdr[i].p_type == PT_NOTE) | |
62 | { | |
63 | const ElfW(Addr) start = (phdr[i].p_vaddr | |
64 | + GLRO(dl_sysinfo_map)->l_addr); | |
65 | const ElfW(Nhdr) *note = (const void *) start; | |
66 | while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz) | |
67 | { | |
68 | if (!memcmp (note, &expected_note, sizeof expected_note)) | |
69 | return *(const ElfW(Word) *) ((const void *) note | |
70 | + sizeof expected_note); | |
71 | #define ROUND(len) (((len) + sizeof note->n_type - 1) & -sizeof note->n_type) | |
72 | note = ((const void *) (note + 1) | |
73 | + ROUND (note->n_namesz) + ROUND (note->n_descsz)); | |
74 | #undef ROUND | |
75 | } | |
76 | } | |
77 | } | |
78 | #endif | |
79 | ||
80 | char bufmem[64]; | |
81 | char *buf = bufmem; | |
82 | unsigned int version; | |
83 | int parts; | |
84 | char *cp; | |
85 | struct utsname uts; | |
86 | ||
87 | /* Try the uname system call. */ | |
88 | if (__uname (&uts)) | |
89 | { | |
90 | /* This was not successful. Now try reading the /proc filesystem. */ | |
329ea513 | 91 | int fd = __open64_nocancel ("/proc/sys/kernel/osrelease", O_RDONLY); |
7c22c7ec UD |
92 | if (fd < 0) |
93 | return -1; | |
329ea513 ZW |
94 | ssize_t reslen = __read_nocancel (fd, bufmem, sizeof (bufmem)); |
95 | __close_nocancel (fd); | |
7c22c7ec UD |
96 | if (reslen <= 0) |
97 | /* This also didn't work. We give up since we cannot | |
98 | make sure the library can actually work. */ | |
99 | return -1; | |
100 | buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0'; | |
101 | } | |
102 | else | |
103 | buf = uts.release; | |
104 | ||
105 | /* Now convert it into a number. The string consists of at most | |
106 | three parts. */ | |
107 | version = 0; | |
108 | parts = 0; | |
109 | cp = buf; | |
110 | while ((*cp >= '0') && (*cp <= '9')) | |
111 | { | |
112 | unsigned int here = *cp++ - '0'; | |
113 | ||
114 | while ((*cp >= '0') && (*cp <= '9')) | |
115 | { | |
116 | here *= 10; | |
117 | here += *cp++ - '0'; | |
118 | } | |
119 | ||
120 | ++parts; | |
121 | version <<= 8; | |
122 | version |= here; | |
123 | ||
124 | if (*cp++ != '.' || parts == 3) | |
125 | /* Another part following? */ | |
126 | break; | |
127 | } | |
128 | ||
129 | if (parts < 3) | |
130 | version <<= 8 * (3 - parts); | |
131 | ||
132 | return version; | |
133 | } |