smp.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Determine whether the host has multiple processors. Linux version.
  2. Copyright (C) 1996, 2002, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include <sys/utsname.h>
  19. #include <not-cancel.h>
  20. /* Test whether the machine has more than one processor. This is not the
  21. best test but good enough. More complicated tests would require `malloc'
  22. which is not available at that time. */
  23. static inline int
  24. is_smp_system (void)
  25. {
  26. union
  27. {
  28. struct utsname uts;
  29. char buf[512];
  30. } u;
  31. char *cp;
  32. /* Try reading the number using `sysctl' first. */
  33. if (uname (&u.uts) == 0)
  34. cp = u.uts.version;
  35. else
  36. {
  37. /* This was not successful. Now try reading the /proc filesystem. */
  38. int fd = open_not_cancel_2 ("/proc/sys/kernel/version", O_RDONLY);
  39. if (__builtin_expect (fd, 0) == -1
  40. || read_not_cancel (fd, u.buf, sizeof (u.buf)) <= 0)
  41. /* This also didn't work. We give up and say it's a UP machine. */
  42. u.buf[0] = '\0';
  43. close_not_cancel_no_status (fd);
  44. cp = u.buf;
  45. }
  46. return strstr (cp, "SMP") != NULL;
  47. }