Line data Source code
1 : #include "get_procstat.hpp"
2 : #include <stdio.h>
3 : #include <sys/stat.h>
4 : #include <errno.h>
5 : #include <string.h>
6 :
7 :
8 : namespace wfc{ namespace core{
9 :
10 0 : int get_procstat(procstat* pinfo)
11 : {
12 0 : return get_procstat( ::getpid(), pinfo);
13 : }
14 :
15 0 : int get_procstat(pid_t pid, procstat* pinfo)
16 : {
17 : char szFileName [_POSIX_PATH_MAX],
18 : szStatStr [2048],
19 : *s, *t;
20 0 : FILE *fp = NULL;
21 : struct stat st;
22 :
23 0 : if ( nullptr == pinfo) {
24 0 : errno = EINVAL;
25 0 : return -1;
26 : }
27 :
28 0 : ::sprintf (szFileName, "/proc/%u/stat", static_cast<unsigned>(pid) );
29 :
30 0 : if (-1 == ::access(szFileName, R_OK)) {
31 0 : return (pinfo->pid = -1);
32 : } /** if **/
33 :
34 0 : if (-1 != ::stat(szFileName, &st)) {
35 0 : pinfo->euid = st.st_uid;
36 0 : pinfo->egid = st.st_gid;
37 : } else {
38 0 : pinfo->euid = static_cast<unsigned>(-1);
39 0 : pinfo->egid = static_cast<unsigned>(-1);
40 : }
41 :
42 :
43 0 : if ((fp = ::fopen (szFileName, "r")) == NULL) {
44 0 : return (pinfo->pid = -1);
45 : } /** IF_NULL **/
46 :
47 0 : if ((s = ::fgets (szStatStr, 2048, fp)) == NULL) {
48 0 : ::fclose (fp);
49 0 : return (pinfo->pid = -1);
50 : }
51 :
52 : /** pid **/
53 0 : ::sscanf (szStatStr, "%d", &(pinfo->pid));
54 0 : s = ::strchr (szStatStr, '(') + 1;
55 0 : t = ::strchr (szStatStr, ')');
56 0 : ::strncpy (pinfo->exName, s, static_cast<size_t>( t - s ) );
57 0 : pinfo->exName [t - s] = '\0';
58 :
59 : sscanf (t + 2, "%c %d %d %d %d %d %u %u %u %u %u %d %d %d %d %d %d %u %u %d %u %u %u %u %u %u %u %u %d %d %d %d %u",
60 : /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33*/
61 : &(pinfo->state),
62 : &(pinfo->ppid),
63 : &(pinfo->pgrp),
64 : &(pinfo->session),
65 : &(pinfo->tty),
66 : &(pinfo->tpgid),
67 : &(pinfo->flags),
68 : &(pinfo->minflt),
69 : &(pinfo->cminflt),
70 : &(pinfo->majflt),
71 : &(pinfo->cmajflt),
72 : &(pinfo->utime),
73 : &(pinfo->stime),
74 : &(pinfo->cutime),
75 : &(pinfo->cstime),
76 : &(pinfo->counter),
77 : &(pinfo->priority),
78 : &(pinfo->timeout),
79 : &(pinfo->itrealvalue),
80 : &(pinfo->starttime),
81 : &(pinfo->vsize),
82 : &(pinfo->rss),
83 : &(pinfo->rlim),
84 : &(pinfo->startcode),
85 : &(pinfo->endcode),
86 : &(pinfo->startstack),
87 : &(pinfo->kstkesp),
88 : &(pinfo->kstkeip),
89 : &(pinfo->signal),
90 : &(pinfo->blocked),
91 : &(pinfo->sigignore),
92 : &(pinfo->sigcatch),
93 0 : &(pinfo->wchan));
94 0 : ::fclose(fp);
95 0 : return 0;
96 : }
97 :
98 : }}
|