Minor O_CLOEXEC optimization, "regression" fix
[deliverable/binutils-gdb.git] / gdb / common / linux-osdata.c
index 6488963c0fcea7c854a103a89b78531684cb6743..9723839525de9c8f28f50a189cf9cb985c33a1f3 100644 (file)
@@ -1,6 +1,6 @@
 /* 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.
 
@@ -26,8 +26,7 @@
 #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>
@@ -44,6 +43,8 @@
 #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
@@ -76,7 +77,7 @@ linux_common_core_of_thread (ptid_t ptid)
 
   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;
 
@@ -125,7 +126,7 @@ static void
 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';
  
@@ -165,7 +166,7 @@ commandline_from_pid (PID_T pid)
 {
   char *pathname = xstrprintf ("/proc/%lld/cmdline", pid);
   char *commandline = NULL;
-  FILE *f = fopen (pathname, "r");
+  FILE *f = gdb_fopen_cloexec (pathname, "r");
 
   if (f)
     {
@@ -254,30 +255,8 @@ get_process_owner (uid_t *owner, PID_T pid)
     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
@@ -341,7 +320,7 @@ linux_xfer_osdata_processes (gdb_byte *readbuf,
       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)
@@ -759,7 +738,7 @@ linux_xfer_osdata_fds (gdb_byte *readbuf,
                            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';
 
@@ -882,7 +861,7 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer)
   else
     return;
 
-  fp = fopen (proc_file, "r");
+  fp = gdb_fopen_cloexec (proc_file, "r");
   if (fp)
     {
       char buf[8192];
@@ -1110,7 +1089,7 @@ linux_xfer_osdata_shm (gdb_byte *readbuf,
       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];
@@ -1238,7 +1217,7 @@ linux_xfer_osdata_sem (gdb_byte *readbuf,
       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];
@@ -1350,7 +1329,7 @@ linux_xfer_osdata_msg (gdb_byte *readbuf,
       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];
@@ -1476,7 +1455,7 @@ linux_xfer_osdata_modules (gdb_byte *readbuf,
       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];
This page took 0.026428 seconds and 4 git commands to generate.