Exemple #1
0
static int
get_cores_used_by_process (PID_T pid, int *cores)
{
  char taskdir[sizeof ("/proc/") + MAX_PID_T_STRLEN + sizeof ("/task") - 1];
  DIR *dir;
  struct dirent *dp;
  int task_count = 0;

  sprintf (taskdir, "/proc/%lld/task", pid);
  dir = opendir (taskdir);
  if (dir)
    {
      while ((dp = readdir (dir)) != NULL)
	{
	  PID_T tid;
	  int core;

	  if (!isdigit (dp->d_name[0])
	      || NAMELEN (dp) > MAX_PID_T_STRLEN)
	    continue;

	  sscanf (dp->d_name, "%lld", &tid);
	  core = linux_common_core_of_thread (ptid_build ((pid_t) pid,
							  (pid_t) tid, 0));

	  if (core >= 0)
	    {
	      ++cores[core];
	      ++task_count;
	    }
	}

      closedir (dir);
    }

  return task_count;
}
Exemple #2
0
static LONGEST
linux_xfer_osdata_fds (gdb_byte *readbuf,
		       ULONGEST offset, LONGEST len)
{
  /* We make the process list snapshot when the object starts to be read.  */
  static const char *buf;
  static LONGEST len_avail = -1;
  static struct buffer buffer;

  if (offset == 0)
    {
      DIR *dirp;

      if (len_avail != -1 && len_avail != 0)
	buffer_free (&buffer);
      len_avail = 0;
      buf = NULL;
      buffer_init (&buffer);
      buffer_grow_str (&buffer, "<osdata type=\"files\">\n");

      dirp = opendir ("/proc");
      if (dirp)
	{
	  struct dirent *dp;

	  while ((dp = readdir (dirp)) != NULL)
	    {
	      struct stat statbuf;
	      char procentry[sizeof ("/proc/4294967295")];

	      if (!isdigit (dp->d_name[0])
		  || NAMELEN (dp) > sizeof ("4294967295") - 1)
		continue;

	      sprintf (procentry, "/proc/%s", dp->d_name);
	      if (stat (procentry, &statbuf) == 0
		  && S_ISDIR (statbuf.st_mode))
		{
		  char *pathname;
		  DIR *dirp2;
		  PID_T pid;
		  char command[32];

		  pid = atoi (dp->d_name);
		  command_from_pid (command, sizeof (command), pid);

		  pathname = xstrprintf ("/proc/%s/fd", dp->d_name);
		  dirp2 = opendir (pathname);

		  if (dirp2)
		    {
		      struct dirent *dp2;

		      while ((dp2 = readdir (dirp2)) != NULL)
			{
			  char *fdname;
			  char buf[1000];
			  ssize_t rslt;

			  if (!isdigit (dp2->d_name[0]))
			    continue;

			  fdname = xstrprintf ("%s/%s", pathname, dp2->d_name);
			  rslt = readlink (fdname, buf, sizeof (buf) - 1);
			  if (rslt >= 0)
			    buf[rslt] = '\0';

			  buffer_xml_printf (
			    &buffer,
			    "<item>"
			    "<column name=\"pid\">%s</column>"
			    "<column name=\"command\">%s</column>"
			    "<column name=\"file descriptor\">%s</column>"
			    "<column name=\"name\">%s</column>"
			    "</item>",
			    dp->d_name,
			    command,
			    dp2->d_name,
			    (rslt >= 0 ? buf : dp2->d_name));
			}

		      closedir (dirp2);
		    }

		  xfree (pathname);
		}
	    }

	  closedir (dirp);
	}

      buffer_grow_str0 (&buffer, "</osdata>\n");
      buf = buffer_finish (&buffer);
      len_avail = strlen (buf);
    }

  if (offset >= len_avail)
    {
      /* Done.  Get rid of the buffer.  */
      buffer_free (&buffer);
      buf = NULL;
      len_avail = 0;
      return 0;
    }

  if (len > len_avail - offset)
    len = len_avail - offset;
  memcpy (readbuf, buf + offset, len);

  return len;
}
Exemple #3
0
static LONGEST
linux_xfer_osdata_threads (gdb_byte *readbuf,
			   ULONGEST offset, LONGEST len)
{
  /* We make the process list snapshot when the object starts to be read.  */
  static const char *buf;
  static LONGEST len_avail = -1;
  static struct buffer buffer;

  if (offset == 0)
    {
      DIR *dirp;

      if (len_avail != -1 && len_avail != 0)
	buffer_free (&buffer);
      len_avail = 0;
      buf = NULL;
      buffer_init (&buffer);
      buffer_grow_str (&buffer, "<osdata type=\"threads\">\n");

      dirp = opendir ("/proc");
      if (dirp)
	{
	  struct dirent *dp;

	  while ((dp = readdir (dirp)) != NULL)
	    {
	      struct stat statbuf;
	      char procentry[sizeof ("/proc/4294967295")];

	      if (!isdigit (dp->d_name[0])
		  || NAMELEN (dp) > sizeof ("4294967295") - 1)
		continue;

	      sprintf (procentry, "/proc/%s", dp->d_name);
	      if (stat (procentry, &statbuf) == 0
		  && S_ISDIR (statbuf.st_mode))
		{
		  DIR *dirp2;
		  char *pathname;
		  PID_T pid;
		  char command[32];

		  pathname = xstrprintf ("/proc/%s/task", dp->d_name);
		  
		  pid = atoi (dp->d_name);
		  command_from_pid (command, sizeof (command), pid);

		  dirp2 = opendir (pathname);

		  if (dirp2)
		    {
		      struct dirent *dp2;

		      while ((dp2 = readdir (dirp2)) != NULL)
			{
			  PID_T tid;
			  int core;

			  if (!isdigit (dp2->d_name[0])
			      || NAMELEN (dp2) > sizeof ("4294967295") - 1)
			    continue;

			  tid = atoi (dp2->d_name);
			  core = linux_common_core_of_thread (ptid_build (pid, tid, 0));

			  buffer_xml_printf (
			    &buffer,
			    "<item>"
			    "<column name=\"pid\">%lld</column>"
			    "<column name=\"command\">%s</column>"
			    "<column name=\"tid\">%lld</column>"
			    "<column name=\"core\">%d</column>"
			    "</item>",
			    pid,
			    command,
			    tid,
			    core);
			}

		      closedir (dirp2);
		    }

		  xfree (pathname);
		}
	    }

	  closedir (dirp);
	}

      buffer_grow_str0 (&buffer, "</osdata>\n");
      buf = buffer_finish (&buffer);
      len_avail = strlen (buf);
    }

  if (offset >= len_avail)
    {
      /* Done.  Get rid of the buffer.  */
      buffer_free (&buffer);
      buf = NULL;
      len_avail = 0;
      return 0;
    }

  if (len > len_avail - offset)
    len = len_avail - offset;
  memcpy (readbuf, buf + offset, len);

  return len;
}
Exemple #4
0
static LONGEST
linux_xfer_osdata_processgroups (gdb_byte *readbuf,
				 ULONGEST offset, LONGEST len)
{
  /* We make the process list snapshot when the object starts to be read.  */
  static const char *buf;
  static LONGEST len_avail = -1;
  static struct buffer buffer;

  if (offset == 0)
    {
      DIR *dirp;

      if (len_avail != -1 && len_avail != 0)
	buffer_free (&buffer);
      len_avail = 0;
      buf = NULL;
      buffer_init (&buffer);
      buffer_grow_str (&buffer, "<osdata type=\"process groups\">\n");

      dirp = opendir ("/proc");
      if (dirp)
	{
	  struct dirent *dp;
	  const size_t list_block_size = 512;
	  PID_T *process_list = (PID_T *) xmalloc (list_block_size * 2 * sizeof (PID_T));
	  size_t process_count = 0;
	  size_t i;

	  /* Build list consisting of PIDs followed by their
	     associated PGID.  */
	  while ((dp = readdir (dirp)) != NULL)
	    {
	      PID_T pid, pgid;

	      if (!isdigit (dp->d_name[0])
		  || NAMELEN (dp) > MAX_PID_T_STRLEN)
		continue;

	      sscanf (dp->d_name, "%lld", &pid);
	      pgid = getpgid (pid);

	      if (pgid > 0)
		{
		  process_list[2 * process_count] = pid;
		  process_list[2 * process_count + 1] = pgid;
		  ++process_count;

		  /* Increase the size of the list if necessary.  */
		  if (process_count % list_block_size == 0)
		    process_list = (PID_T *) xrealloc (
			process_list,
			(process_count + list_block_size)
			* 2 * sizeof (PID_T));
		}
	    }

	  closedir (dirp);

	  /* Sort the process list.  */
	  qsort (process_list, process_count, 2 * sizeof (PID_T),
		 compare_processes);

	  for (i = 0; i < process_count; ++i)
	    {
	      PID_T pid = process_list[2 * i];
	      PID_T pgid = process_list[2 * i + 1];
	      char leader_command[32];
	      char *command_line;

	      command_from_pid (leader_command, sizeof (leader_command), pgid);
	      command_line = commandline_from_pid (pid);

	      buffer_xml_printf (
		  &buffer,
		  "<item>"
		  "<column name=\"pgid\">%lld</column>"
		  "<column name=\"leader command\">%s</column>"
		  "<column name=\"pid\">%lld</column>"
		  "<column name=\"command line\">%s</column>"
		  "</item>",
		  pgid,
		  leader_command,
		  pid,
		  command_line ? command_line : "");

	      xfree (command_line);
	    }

	  xfree (process_list);
	}   

      buffer_grow_str0 (&buffer, "</osdata>\n");
      buf = buffer_finish (&buffer);
      len_avail = strlen (buf);
    }

  if (offset >= len_avail)
    {
      /* Done.  Get rid of the buffer.  */
      buffer_free (&buffer);
      buf = NULL;
      len_avail = 0;
      return 0;
    }

  if (len > len_avail - offset)
    len = len_avail - offset;
  memcpy (readbuf, buf + offset, len);

  return len;
}
Exemple #5
0
static LONGEST
linux_xfer_osdata_processes (gdb_byte *readbuf,
			     ULONGEST offset, LONGEST len)
{
  /* We make the process list snapshot when the object starts to be read.  */
  static const char *buf;
  static LONGEST len_avail = -1;
  static struct buffer buffer;

  if (offset == 0)
    {
      DIR *dirp;

      if (len_avail != -1 && len_avail != 0)
	buffer_free (&buffer);
      len_avail = 0;
      buf = NULL;
      buffer_init (&buffer);
      buffer_grow_str (&buffer, "<osdata type=\"processes\">\n");

      dirp = opendir ("/proc");
      if (dirp)
	{
	  const int num_cores = sysconf (_SC_NPROCESSORS_ONLN);
	  struct dirent *dp;

	  while ((dp = readdir (dirp)) != NULL)
	    {
	      PID_T pid;
	      uid_t owner;
	      char user[UT_NAMESIZE];
	      char *command_line;
	      int *cores;
	      int task_count;
	      char *cores_str;
	      int i;

	      if (!isdigit (dp->d_name[0])
		  || NAMELEN (dp) > MAX_PID_T_STRLEN)
		continue;

	      sscanf (dp->d_name, "%lld", &pid);
	      command_line = commandline_from_pid (pid);

	      if (get_process_owner (&owner, pid) == 0)
		user_from_uid (user, sizeof (user), owner);
	      else
		strcpy (user, "?");

	      /* Find CPU cores used by the process.  */
	      cores = (int *) xcalloc (num_cores, sizeof (int));
	      task_count = get_cores_used_by_process (pid, cores);
	      cores_str = (char *) xcalloc (task_count, sizeof ("4294967295") + 1);

	      for (i = 0; i < num_cores && task_count > 0; ++i)
		if (cores[i])
		  {
		    char core_str[sizeof ("4294967295")];

		    sprintf (core_str, "%d", i);
		    strcat (cores_str, core_str);

		    task_count -= cores[i];
		    if (task_count > 0)
		      strcat (cores_str, ",");
		  }

	      xfree (cores);
	      
	      buffer_xml_printf (
		  &buffer,
		  "<item>"
		  "<column name=\"pid\">%lld</column>"
		  "<column name=\"user\">%s</column>"
		  "<column name=\"command\">%s</column>"
		  "<column name=\"cores\">%s</column>"
		  "</item>",
		  pid,
		  user,
		  command_line ? command_line : "",
		  cores_str);

	      xfree (command_line);     
	      xfree (cores_str);
	    }
	  
	  closedir (dirp);
	}

      buffer_grow_str0 (&buffer, "</osdata>\n");
      buf = buffer_finish (&buffer);
      len_avail = strlen (buf);
    }

  if (offset >= len_avail)
    {
      /* Done.  Get rid of the buffer.  */
      buffer_free (&buffer);
      buf = NULL;
      len_avail = 0;
      return 0;
    }

  if (len > len_avail - offset)
    len = len_avail - offset;
  memcpy (readbuf, buf + offset, len);

  return len;
}
Exemple #6
0
long KRB5_CALLCONV
krb5int_open_plugin_dirs (const char * const *dirnames,
                          const char * const *filebases,
			  struct plugin_dir_handle *dirhandle,
                          struct errinfo *ep)
{
    long err = 0;
    struct plugin_file_handle **h = NULL;
    int count = 0;
    char **filenames = NULL;
    int i;

    if (!err) {
        err = krb5int_plugin_file_handle_array_init (&h);
    }
    
    if (!err && (filebases != NULL)) {
	err = krb5int_get_plugin_filenames (filebases, &filenames);
    }
    
    for (i = 0; !err && dirnames[i] != NULL; i++) {
	size_t dirnamelen = strlen (dirnames[i]) + 1; /* '/' */
        if (filenames != NULL) {
            /* load plugins with names from filenames from each directory */
            int j;
            
            for (j = 0; !err && filenames[j] != NULL; j++) {
                struct plugin_file_handle *handle = NULL;
		char *filepath = NULL;
		
		if (!err) {
		    filepath = malloc (dirnamelen + strlen (filenames[j]) + 1); /* NULL */
		    if (filepath == NULL) { 
			err = errno; 
		    } else {
			/*LINTED*/
			sprintf (filepath, "%s/%s", dirnames[i], filenames[j]);
		    }
		}
		
                if (krb5int_open_plugin (filepath, &handle, ep) == 0) {
                    err = krb5int_plugin_file_handle_array_add (&h, &count, handle);
                    if (!err) { handle = NULL; }  /* h takes ownership */
                }
                
		if (filepath != NULL) { free (filepath); }
		if (handle   != NULL) { krb5int_close_plugin (handle); }
            }
        } else {
            /* load all plugins in each directory */
#ifndef _WIN32
	    DIR *dir = opendir (dirnames[i]);
            
            while (dir != NULL && !err) {
                struct dirent *d = NULL;
                char *filepath = NULL;
                struct plugin_file_handle *handle = NULL;
                int len;
                
                d = readdir (dir);
                if (d == NULL) { break; }
                
                if ((strcmp (d->d_name, ".") == 0) || 
                    (strcmp (d->d_name, "..") == 0)) {
                    continue;
                }

		/* Solaris Kerberos: Only open files with a .so extension */
		len = NAMELEN (d);
		if (len < 3 || strcmp(".so", d->d_name + len - 3 ) != 0)
			continue;

		if (!err) {
		    filepath = malloc (dirnamelen + len + 1); /* NULL */
		    if (filepath == NULL) { 
			err = errno; 
		    } else {
			/*LINTED*/
			sprintf (filepath, "%s/%*s", dirnames[i], len, d->d_name);
		    }
		}
                
                if (!err) {            
                    if (krb5int_open_plugin (filepath, &handle, ep) == 0) {
                        err = krb5int_plugin_file_handle_array_add (&h, &count, handle);
                        if (!err) { handle = NULL; }  /* h takes ownership */
                    }
                }
                
                if (filepath  != NULL) { free (filepath); }
                if (handle    != NULL) { krb5int_close_plugin (handle); }
            }
            
            if (dir != NULL) { closedir (dir); }
#else
	    /* Until a Windows implementation of this code is implemented */
	    err = ENOENT;
#endif /* _WIN32 */
        }
    }
        
    if (err == ENOENT) {
        err = 0;  /* ran out of plugins -- do nothing */
    }
     
    if (!err) {
        dirhandle->files = h;
        h = NULL;  /* dirhandle->files takes ownership */
    }
    
    if (filenames != NULL) { krb5int_free_plugin_filenames (filenames); }
    if (h         != NULL) { krb5int_plugin_file_handle_array_free (h); }
    
    return err;
}
Exemple #7
0
long KRB5_CALLCONV
krb5int_open_plugin_dirs (const char * const *dirnames,
                          const char * const *filebases,
                          struct plugin_dir_handle *dirhandle,
                          struct errinfo *ep)
{
    long err = 0;
    struct plugin_file_handle **h = NULL;
    size_t count = 0;
    char **filenames = NULL;
    int i;

    if (!err) {
        err = krb5int_plugin_file_handle_array_init (&h);
    }

    if (!err && (filebases != NULL)) {
        err = krb5int_get_plugin_filenames (filebases, &filenames);
    }

    for (i = 0; !err && dirnames[i] != NULL; i++) {
        if (filenames != NULL) {
            /* load plugins with names from filenames from each directory */
            int j;

            for (j = 0; !err && filenames[j] != NULL; j++) {
                struct plugin_file_handle *handle = NULL;
                char *filepath = NULL;

                if (!err) {
                    if (asprintf(&filepath, "%s/%s", dirnames[i], filenames[j]) < 0) {
                        filepath = NULL;
                        err = ENOMEM;
                    }
                }

                if (krb5int_open_plugin (filepath, &handle, ep) == 0) {
                    err = krb5int_plugin_file_handle_array_add (&h, &count, handle);
                    if (!err) { handle = NULL; }  /* h takes ownership */
                }

                free(filepath);
                if (handle   != NULL) { krb5int_close_plugin (handle); }
            }
        } else {
            /* load all plugins in each directory */
            DIR *dir = opendir (dirnames[i]);

            while (dir != NULL && !err) {
                struct dirent *d = NULL;
                char *filepath = NULL;
                struct plugin_file_handle *handle = NULL;

                d = readdir (dir);
                if (d == NULL) { break; }

                if ((strcmp (d->d_name, ".") == 0) ||
                    (strcmp (d->d_name, "..") == 0)) {
                    continue;
                }

                if (!err) {
                    int len = NAMELEN (d);
                    if (asprintf(&filepath, "%s/%*s", dirnames[i], len, d->d_name) < 0) {
                        filepath = NULL;
                        err = ENOMEM;
                    }
                }

                if (!err) {
                    if (krb5int_open_plugin (filepath, &handle, ep) == 0) {
                        err = krb5int_plugin_file_handle_array_add (&h, &count, handle);
                        if (!err) { handle = NULL; }  /* h takes ownership */
                    }
                }

                free(filepath);
                if (handle    != NULL) { krb5int_close_plugin (handle); }
            }

            if (dir != NULL) { closedir (dir); }
        }
    }

    if (err == ENOENT) {
        err = 0;  /* ran out of plugins -- do nothing */
    }

    if (!err) {
        dirhandle->files = h;
        h = NULL;  /* dirhandle->files takes ownership */
    }

    if (filenames != NULL) { krb5int_free_plugin_filenames (filenames); }
    if (h         != NULL) { krb5int_plugin_file_handle_array_free (h); }

    return err;
}