Blame sysdeps/unix/sysv/linux/i386/smp.h

Packit 6c4009
/* Determine whether the host has multiple processors.  Linux version.
Packit 6c4009
   Copyright (C) 1996-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 Library General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2 of the
Packit 6c4009
   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
   Library General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Library General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/utsname.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
/* Test whether the machine has more than one processor.  This is not the
Packit 6c4009
   best test but good enough.  More complicated tests would require `malloc'
Packit 6c4009
   which is not available at that time.  */
Packit 6c4009
static inline int
Packit 6c4009
is_smp_system (void)
Packit 6c4009
{
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
    struct utsname uts;
Packit 6c4009
    char buf[512];
Packit 6c4009
  } u;
Packit 6c4009
  char *cp;
Packit 6c4009
Packit 6c4009
  /* Try reading the number using `sysctl' first.  */
Packit 6c4009
  if (__uname (&u.uts) == 0)
Packit 6c4009
    cp = u.uts.version;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* This was not successful.  Now try reading the /proc filesystem.  */
Packit 6c4009
      int fd = __open64_nocancel ("/proc/sys/kernel/version", O_RDONLY);
Packit 6c4009
      if (__builtin_expect (fd, 0) == -1
Packit 6c4009
	  || __read_nocancel (fd, u.buf, sizeof (u.buf)) <= 0)
Packit 6c4009
	/* This also didn't work.  We give up and say it's a UP machine.  */
Packit 6c4009
	u.buf[0] = '\0';
Packit 6c4009
Packit 6c4009
      __close_nocancel_nostatus (fd);
Packit 6c4009
      cp = u.buf;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return strstr (cp, "SMP") != NULL;
Packit 6c4009
}