Updated to fedora-glibc-20041217T0906
[kopensolaris-gnu/glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
1 /* Operating system specific code  for generic dynamic loader functions.
2    Copyright (C) 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
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
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.
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
13    Lesser General Public License for more details.
14
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.  */
19
20 #include <string.h>
21 #include <fcntl.h>
22 #include <sys/sysctl.h>
23 #include <sys/utsname.h>
24 #include "kernel-features.h"
25
26 #ifndef MIN
27 # define MIN(a,b) (((a)<(b))?(a):(b))
28 #endif
29
30 #ifdef SHARED
31 /* This is the function used in the dynamic linker to print the fatal error
32    message.  */
33 static inline void
34 __attribute__ ((__noreturn__))
35 dl_fatal (const char *str)
36 {
37   _dl_dprintf (2, str);
38   _exit (1);
39 }
40 #endif
41
42
43 #define DL_SYSDEP_OSCHECK(FATAL) \
44   do {                                                                        \
45     /* Test whether the kernel is new enough.  This test is only              \
46        performed if the library is not compiled to run on all                 \
47        kernels.  */                                                           \
48     if (__LINUX_KERNEL_VERSION > 0)                                           \
49       {                                                                       \
50         char bufmem[64];                                                      \
51         char *buf = bufmem;                                                   \
52         unsigned int version;                                                 \
53         int parts;                                                            \
54         char *cp;                                                             \
55         struct utsname uts;                                                   \
56                                                                               \
57         /* Try the uname syscall */                                           \
58         if (__uname (&uts))                                                   \
59           {                                                                   \
60             /* This was not successful.  Now try reading the /proc            \
61                filesystem.  */                                                \
62             ssize_t reslen;                                                   \
63             int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);         \
64             if (fd == -1                                                      \
65                 || (reslen = __read (fd, bufmem, sizeof (bufmem))) <= 0)      \
66               /* This also didn't work.  We give up since we cannot           \
67                  make sure the library can actually work.  */                 \
68               FATAL ("FATAL: cannot determine kernel version\n");             \
69             __close (fd);                                                     \
70             buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';          \
71           }                                                                   \
72         else                                                                  \
73           buf = uts.release;                                                  \
74                                                                               \
75         /* Now convert it into a number.  The string consists of at most      \
76            three parts.  */                                                   \
77         version = 0;                                                          \
78         parts = 0;                                                            \
79         cp = buf;                                                             \
80         while ((*cp >= '0') && (*cp <= '9'))                                  \
81           {                                                                   \
82             unsigned int here = *cp++ - '0';                                  \
83                                                                               \
84             while ((*cp >= '0') && (*cp <= '9'))                              \
85               {                                                               \
86                 here *= 10;                                                   \
87                 here += *cp++ - '0';                                          \
88               }                                                               \
89                                                                               \
90             ++parts;                                                          \
91             version <<= 8;                                                    \
92             version |= here;                                                  \
93                                                                               \
94             if (*cp++ != '.')                                                 \
95               /* Another part following?  */                                  \
96               break;                                                          \
97           }                                                                   \
98                                                                               \
99         if (parts < 3)                                                        \
100           version <<= 8 * (3 - parts);                                        \
101                                                                               \
102         /* Now we can test with the required version.  */                     \
103         if (version < __LINUX_KERNEL_VERSION)                                 \
104           /* Not sufficent.  */                                               \
105           FATAL ("FATAL: kernel too old\n");                                  \
106                                                                               \
107         GLRO(dl_osversion) = version;                                         \
108       }                                                                       \
109   } while (0)