/* Linux-specific functions to retrieve OS data.
- Copyright (C) 2009-2012 Free Software Foundation, Inc.
+ Copyright (C) 2009-2013 Free Software Foundation, Inc.
This file is part of GDB.
#include "linux-osdata.h"
#include <sys/types.h>
-#include <sys/stat.h>
-#include <dirent.h>
+#include <sys/sysinfo.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "gdb_assert.h"
#include "gdb_dirent.h"
+#include "gdb_stat.h"
+#include "filestuff.h"
/* Define PID_T to be a fixed size that is at least as large as pid_t,
so that reading pid values embedded in /proc works
sprintf (filename, "/proc/%lld/task/%lld/stat",
(PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid));
- f = fopen (filename, "r");
+ f = gdb_fopen_cloexec (filename, "r");
if (!f)
return -1;
command_from_pid (char *command, int maxlen, PID_T pid)
{
char *stat_path = xstrprintf ("/proc/%lld/stat", pid);
- FILE *fp = fopen (stat_path, "r");
+ FILE *fp = gdb_fopen_cloexec (stat_path, "r");
command[0] = '\0';
{
char *pathname = xstrprintf ("/proc/%lld/cmdline", pid);
char *commandline = NULL;
- FILE *f = fopen (pathname, "r");
+ FILE *f = gdb_fopen_cloexec (pathname, "r");
if (f)
{
return -1;
}
-/* Returns the number of CPU cores found on the system. */
-
-static int
-get_number_of_cpu_cores (void)
-{
- int cores = 0;
- FILE *f = fopen ("/proc/cpuinfo", "r");
-
- while (!feof (f))
- {
- char buf[512];
- char *p = fgets (buf, sizeof (buf), f);
-
- if (p && strncmp (buf, "processor", 9) == 0)
- ++cores;
- }
-
- fclose (f);
-
- return cores;
-}
-
/* Find the CPU cores used by process PID and return them in CORES.
- CORES points to an array of at least get_number_of_cpu_cores ()
+ CORES points to an array of at least sysconf(_SC_NPROCESSOR_ONLN)
elements. */
static int
dirp = opendir ("/proc");
if (dirp)
{
- const int num_cores = get_number_of_cpu_cores ();
+ const int num_cores = sysconf (_SC_NPROCESSORS_ONLN);
struct dirent *dp;
while ((dp = readdir (dirp)) != NULL)
continue;
fdname = xstrprintf ("%s/%s", pathname, dp2->d_name);
- rslt = readlink (fdname, buf, 1000);
+ rslt = readlink (fdname, buf, sizeof (buf) - 1);
if (rslt >= 0)
buf[rslt] = '\0';
else
return;
- fp = fopen (proc_file, "r");
+ fp = gdb_fopen_cloexec (proc_file, "r");
if (fp)
{
char buf[8192];
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n");
- fp = fopen ("/proc/sysvipc/shm", "r");
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
if (fp)
{
char buf[8192];
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n");
- fp = fopen ("/proc/sysvipc/sem", "r");
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
if (fp)
{
char buf[8192];
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n");
- fp = fopen ("/proc/sysvipc/msg", "r");
+ fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
if (fp)
{
char buf[8192];
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"modules\">\n");
- fp = fopen ("/proc/modules", "r");
+ fp = gdb_fopen_cloexec ("/proc/modules", "r");
if (fp)
{
char buf[8192];