add a way for procd to queue async init.d calls
[project/procd.git] / measure.c
1 /**
2  *   Copyright (C) 2010 Steven Barth <steven@midlink.org>
3  *   Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
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.
9  *
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.
14  *
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.
18  *
19  */
20
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <regex.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <glob.h>
32 #include <libgen.h>
33
34 #include "procd.h"
35
36 static regex_t pat_vmsize, pat_ppid, pat_state, pat_uid;
37
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);
43 }
44
45 static void __attribute__((destructor)) measure_fini() {
46         regfree(&pat_vmsize);
47         regfree(&pat_ppid);
48         regfree(&pat_uid);
49         regfree(&pat_state);
50 }
51
52 int measure_process(pid_t pid, struct pid_info *pi) {
53         int fd;
54         char buffer[512] = "";
55         ssize_t rxed;
56         regmatch_t matches[2];
57         glob_t gl;
58         int i;
59
60         memset(pi, 0, sizeof(*pi));
61
62         snprintf(buffer, sizeof(buffer), "/proc/%i/fd/*", (int)pid);
63
64         if (glob(buffer, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
65                 fprintf(stderr, "glob failed on %s\n", buffer);
66                 return -1;
67         }
68
69         for (i = 0; i < gl.gl_pathc; i++)
70                 if (isdigit(basename(gl.gl_pathv[i])[0]))
71                         pi->fdcount++;
72         globfree(&gl);
73
74         snprintf(buffer, sizeof(buffer), "/proc/%i/status", (int)pid);
75         fd = open(buffer, O_RDONLY);
76         if (fd == -1)
77                 return -1;
78
79         rxed = read(fd, buffer, sizeof(buffer) - 1);
80         close(fd);
81         if (rxed == -1)
82                 return -1;
83
84         buffer[rxed] = 0;
85
86         if (!regexec(&pat_state, buffer, 2, matches, 0))
87                 pi->stat = buffer[matches[1].rm_so];
88
89         if (!regexec(&pat_ppid, buffer, 2, matches, 0))
90                 pi->ppid = atoi(buffer + matches[1].rm_so);
91
92         if (!regexec(&pat_uid, buffer, 2, matches, 0))
93                 pi->uid = atoi(buffer + matches[1].rm_so);
94
95         if (!regexec(&pat_vmsize, buffer, 2, matches, 0))
96                 pi->vmsize = atoi(buffer + matches[1].rm_so) * 1024;
97
98         return 0;
99 }