2 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
36 static regex_t pat_vmsize
, pat_ppid
, pat_state
, pat_uid
;
38 static void __attribute__((constructor
)) measure_init() {
39 regcomp(&pat_vmsize
, "VmSize:[ \t]*([0-9]*) kB", REG_EXTENDED
);
40 regcomp(&pat_uid
, "Uid:[ \t]*([0-9]*).*", REG_EXTENDED
);
41 regcomp(&pat_ppid
, "PPid:[ \t]*([0-9]+)", REG_EXTENDED
);
42 regcomp(&pat_state
, "State:[ \t]*([A-Z])", REG_EXTENDED
);
45 static void __attribute__((destructor
)) measure_fini() {
52 int measure_process(pid_t pid
, struct pid_info
*pi
) {
54 char buffer
[512] = "";
56 regmatch_t matches
[2];
60 memset(pi
, 0, sizeof(*pi
));
62 snprintf(buffer
, sizeof(buffer
), "/proc/%i/fd/*", (int)pid
);
64 if (glob(buffer
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
)) {
65 fprintf(stderr
, "glob failed on %s\n", buffer
);
69 for (i
= 0; i
< gl
.gl_pathc
; i
++)
70 if (isdigit(basename(gl
.gl_pathv
[i
])[0]))
74 snprintf(buffer
, sizeof(buffer
), "/proc/%i/status", (int)pid
);
75 fd
= open(buffer
, O_RDONLY
);
79 rxed
= read(fd
, buffer
, sizeof(buffer
) - 1);
86 if (!regexec(&pat_state
, buffer
, 2, matches
, 0))
87 pi
->stat
= buffer
[matches
[1].rm_so
];
89 if (!regexec(&pat_ppid
, buffer
, 2, matches
, 0))
90 pi
->ppid
= atoi(buffer
+ matches
[1].rm_so
);
92 if (!regexec(&pat_uid
, buffer
, 2, matches
, 0))
93 pi
->uid
= atoi(buffer
+ matches
[1].rm_so
);
95 if (!regexec(&pat_vmsize
, buffer
, 2, matches
, 0))
96 pi
->vmsize
= atoi(buffer
+ matches
[1].rm_so
) * 1024;