Blame sysdeps/unix/sysv/linux/dl-sysdep.c

Packit 6c4009
/* Dynamic linker system dependencies for Linux.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Linux needs some special initialization, but otherwise uses
Packit 6c4009
   the generic dynamic linker system interface code.  */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <sys/utsname.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
# define DL_SYSDEP_INIT frob_brk ()
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
frob_brk (void)
Packit 6c4009
{
Packit 6c4009
  __brk (0);			/* Initialize the break.  */
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# include <elf/dl-sysdep.c>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
attribute_hidden
Packit 6c4009
_dl_discover_osversion (void)
Packit 6c4009
{
Packit 6c4009
#if defined NEED_DL_SYSINFO_DSO && defined SHARED
Packit 6c4009
  if (GLRO(dl_sysinfo_map) != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* If the kernel-supplied DSO contains a note indicating the kernel's
Packit 6c4009
	 version, we don't need to call uname or parse any strings.  */
Packit 6c4009
Packit 6c4009
      static const struct
Packit 6c4009
      {
Packit 6c4009
	ElfW(Nhdr) hdr;
Packit 6c4009
	char vendor[8];
Packit 6c4009
      } expected_note = { { sizeof "Linux", sizeof (ElfW(Word)), 0 }, "Linux" };
Packit 6c4009
      const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
Packit 6c4009
      const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
Packit 6c4009
      for (uint_fast16_t i = 0; i < phnum; ++i)
Packit 6c4009
	if (phdr[i].p_type == PT_NOTE)
Packit 6c4009
	  {
Packit 6c4009
	    const ElfW(Addr) start = (phdr[i].p_vaddr
Packit 6c4009
				      + GLRO(dl_sysinfo_map)->l_addr);
Packit 6c4009
	    const ElfW(Nhdr) *note = (const void *) start;
Packit 6c4009
	    while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
Packit 6c4009
	      {
Packit 6c4009
		if (!memcmp (note, &expected_note, sizeof expected_note))
Packit 6c4009
		  return *(const ElfW(Word) *) ((const void *) note
Packit 6c4009
						+ sizeof expected_note);
Packit 6c4009
#define ROUND(len) (((len) + sizeof note->n_type - 1) & -sizeof note->n_type)
Packit 6c4009
		note = ((const void *) (note + 1)
Packit 6c4009
			+ ROUND (note->n_namesz) + ROUND (note->n_descsz));
Packit 6c4009
#undef ROUND
Packit 6c4009
	      }
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  char bufmem[64];
Packit 6c4009
  char *buf = bufmem;
Packit 6c4009
  unsigned int version;
Packit 6c4009
  int parts;
Packit 6c4009
  char *cp;
Packit 6c4009
  struct utsname uts;
Packit 6c4009
Packit 6c4009
  /* Try the uname system call.  */
Packit 6c4009
  if (__uname (&uts))
Packit 6c4009
    {
Packit 6c4009
      /* This was not successful.  Now try reading the /proc filesystem.  */
Packit 6c4009
      int fd = __open64_nocancel ("/proc/sys/kernel/osrelease", O_RDONLY);
Packit 6c4009
      if (fd < 0)
Packit 6c4009
	return -1;
Packit 6c4009
      ssize_t reslen = __read_nocancel (fd, bufmem, sizeof (bufmem));
Packit 6c4009
      __close_nocancel (fd);
Packit 6c4009
      if (reslen <= 0)
Packit 6c4009
	/* This also didn't work.  We give up since we cannot
Packit 6c4009
	   make sure the library can actually work.  */
Packit 6c4009
	return -1;
Packit 6c4009
      buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    buf = uts.release;
Packit 6c4009
Packit 6c4009
  /* Now convert it into a number.  The string consists of at most
Packit 6c4009
     three parts.  */
Packit 6c4009
  version = 0;
Packit 6c4009
  parts = 0;
Packit 6c4009
  cp = buf;
Packit 6c4009
  while ((*cp >= '0') && (*cp <= '9'))
Packit 6c4009
    {
Packit 6c4009
      unsigned int here = *cp++ - '0';
Packit 6c4009
Packit 6c4009
      while ((*cp >= '0') && (*cp <= '9'))
Packit 6c4009
	{
Packit 6c4009
	  here *= 10;
Packit 6c4009
	  here += *cp++ - '0';
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      ++parts;
Packit 6c4009
      version <<= 8;
Packit 6c4009
      version |= here;
Packit 6c4009
Packit 6c4009
      if (*cp++ != '.' || parts == 3)
Packit 6c4009
	/* Another part following?  */
Packit 6c4009
	break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (parts < 3)
Packit 6c4009
    version <<= 8 * (3 - parts);
Packit 6c4009
Packit 6c4009
  return version;
Packit 6c4009
}