Rizin
unix-like reverse engineering framework and cli tools
darwin.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2010 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <stdio.h>
5 
6 int pids_cmdline(int pid, char *cmdline) {
7  int fd;
8  sprintf(cmdline, "/proc/%d/cmdline", pid);
9  fd = open(cmdline, O_RDONLY);
10  cmdline[0] = '\0';
11  if (fd != -1) {
12  rz_xread(fd, cmdline, 1024);
13  cmdline[1024] = '\0';
14  close(fd);
15  }
16 
17  return 0;
18 }
19 
20 // XXX
21 int pids_sons_of_r(int pid, int recursive, int limit) {
22  int p, mola, tmp, n = 0;
23  FILE *fd;
24  char tmp3[8];
25  char buf[128];
26  char tmp2[1024];
27  struct dirent *file;
28  DIR *dh;
29 
30  if (pid < 1)
31  return false;
32  dh = opendir("/proc/");
33  if (!dh) {
34  return false;
35  }
36 
37  while ((file = (struct dirent *)readdir(dh))) {
38  p = atoi(file->d_name);
39  if (p) {
40  sprintf(buf, "/proc/%s/stat", file->d_name);
41  fd = fopen(buf, "r");
42  if (fd) {
43  mola = 0;
44  fscanf(fd, "%d %s %s %d",
45  &tmp, tmp2, tmp3, &mola);
46  if (mola == pid) {
47  pids_cmdline(p, tmp2);
48  // for(i=0; i<recursive*2;i++)
49  // printf(" ");
50  cons_printf(" `- %d : %s (%s)\n",
51  p, tmp2, (tmp3[0] == 'S') ? "sleeping" : (tmp3[0] == 'T') ? "stopped"
52  : "running");
53  n++;
54  if (recursive < limit) {
55  n += pids_sons_of_r(p, recursive + 1, limit);
56  }
57  }
58  }
59  fclose(fd);
60  }
61  }
62  closedir(dh);
63  return n;
64 }
static static fork const void static count close
Definition: sflib.h:33
voidpf void * buf
Definition: ioapi.h:138
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
int pids_sons_of_r(int pid, int recursive, int limit)
Definition: darwin.c:21
int pids_cmdline(int pid, char *cmdline)
Definition: darwin.c:6
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path static newfd static getpgrp static euid const sigset_t static mask const char static len const gid_t static list const char const char static newpath const char static library readdir
Definition: sflib.h:120
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
int n
Definition: mipsasm.c:19
string FILE
Definition: benchmark.py:21
#define rz_xread(fd, buf, count)
Definition: rz_types.h:643
#define O_RDONLY
Definition: sftypes.h:486
Definition: sftypes.h:48
Definition: gzappend.c:170
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int file
Definition: z80asm.c:58