smp.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <sys/utsname.h>
  20. #include <not-cancel.h>
  21. /* Test whether the machine has more than one processor. This is not the
  22. best test but good enough. More complicated tests would require `malloc'
  23. which is not available at that time. */
  24. static inline int
  25. is_smp_system (void)
  26. {
  27. union
  28. {
  29. struct utsname uts;
  30. char buf[512];
  31. } u;
  32. char *cp;
  33. /* Try reading the number using `sysctl' first. */
  34. if (uname (&u.uts) == 0)
  35. cp = u.uts.version;
  36. else
  37. {
  38. /* This was not successful. Now try reading the /proc filesystem. */
  39. int fd = open_not_cancel_2 ("/proc/sys/kernel/version", O_RDONLY);
  40. if (__builtin_expect (fd, 0) == -1
  41. || read_not_cancel (fd, u.buf, sizeof (u.buf)) <= 0)
  42. /* This also didn't work. We give up and say it's a UP machine. */
  43. u.buf[0] = '\0';
  44. close_not_cancel_no_status (fd);
  45. cp = u.buf;
  46. }
  47. return strstr (cp, "SMP") != NULL;
  48. }