Beispiel #1
0
static void add_to_omit_list(char* input_arg)
{
  static size_t omit_size = 0;
  char* omit_str;
  char* endptr;
  pid_t omit_pid;
  
  omit_str = strtok(input_arg, ",");
  while (omit_str)
    {
      omit_pid = (pid_t)strtoul(omit_str, &endptr, 10);
      
      if (*endptr == '\0')
	{
	  if (omit_count == omit_size)
	    omitted_procs = xrealloc(omitted_procs, __grow(omit_size) * sizeof(*omitted_procs));
	  omitted_procs[omit_count++] = omit_pid;
	}
      else
	{
	  fprintf(stderr, _("%s: Illegal omit PID value: %s!\n"), *argv, omit_str);
	  exit(EXIT_FAILURE);
	}
      
      omit_str = strtok(NULL, ",");
    }
}
Beispiel #2
0
static char* pid_link(pid_t pid, const char* base_name)
{
  size_t path_alloc_size = 0;
  ssize_t len = 0;
  char* result = NULL;
  char link[PROCPATHLEN];
  
  snprintf(link, sizeof(link) / sizeof(char), "/proc/%d/%s", pid, base_name);
  
  while ((size_t)len == path_alloc_size)
    {
      result = xrealloc(result, __grow(path_alloc_size));
      len = readlink(link, result, path_alloc_size - 1);
      if (len < 0)
	{
	  len = 0;
	  break;
	}
    }
  
  result[len] = '\0';
  return result;
}
Beispiel #3
0
static void select_procs(void)
{
  static size_t size = 0;
  PROCTAB* ptp;
  proc_t* task;
  size_t match;
  char* cmd_arg0;
  char* cmd_arg0_base;
  char* cmd_arg1;
  char* cmd_arg1_base;
  char* program_base;
  char* root_link;
  char* exe_link;
  char* exe_link_base;
  
  program_base = basename(program);  /* get the input base name */
  ptp = openproc(PROC_FILLCOM | PROC_FILLSTAT);
  
  while ((task = readproc(ptp, NULL)))
    {
      if (epidof_root)
	{
	  /* get the /proc/<pid>/root symlink value */
	  root_link = pid_link(task->XXXID, "root");
	  match = !strcmp(epidof_root, root_link);
	  xfree(root_link);
	  
	  if (!match) /* root check failed */
	    {
	      freeproc(task);
	      continue;
	    }
	}
      
      if (!is_omitted(task->XXXID) && task->cmdline)
	{
	  cmd_arg0 = task->cmdline[0];
	  
	  /* processes starting with '-' are login shells */
	  if (*cmd_arg0 == '-')
	    cmd_arg0++;
	  
	  cmd_arg0_base = basename(cmd_arg0);           /* get the argv0 basename */
	  exe_link      = pid_link(task->XXXID, "exe");  /* get the /proc/<pid>/exe symlink value */
	  exe_link_base = basename(exe_link);           /* get the exe_link basename */
	  match = 0;
	  
#define __test(p, c)  (!strcmp(p, c##_base) || !strcmp(p, c) || !strcmp(p##_base, c))
	  
	  if (__test(program, cmd_arg0) || __test(program, exe_link))
	    match = 1;
	  else if (opt_scripts_too && task->cmdline[1])
	    {
	      cmd_arg1 = task->cmdline[1];
	      cmd_arg1_base = basename(cmd_arg1);
	      
	      /* if script, then task->cmd = argv1, otherwise task->cmd = argv0 */
	      if (task->cmd &&
		  !strncmp(task->cmd, cmd_arg1_base, strlen(task->cmd)) &&
		  __test(program, cmd_arg1))
		match = 1;
	    }
	  
#undef __test
	  
	  xfree(exe_link);
	  
	  if (match && environment_test(task->XXXID, *argv))
	    {
	      if (proc_count == size)
		procs = xrealloc(procs, __grow(size) * sizeof(*procs));
	      procs[proc_count++] = task->XXXID;
	    }
	}
      freeproc(task);
    }
  closeproc(ptp);
}
Beispiel #4
0
inline static int __ensureCapacity(struct MutableString *const self, UInteger minCapacity) {
	if ( minCapacity > self->capacity )
		return __grow(self, minCapacity);
	return 0;
}