1/*
2 This file is part of Mtproto-proxy Library.
3
4 Mtproto-proxy Library is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 Mtproto-proxy Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright 2014 Telegram Messenger Inc
18 2014 Vitaly Valtman
19*/
20#include "common/proc-stat.h"
21
22#include <stdio.h>
23
24int read_proc_stats (int pid, int tid, struct proc_stats *s) {
25 const char *format = "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu";
26
27 char buf[256];
28 if (tid <= 0) {
29 sprintf (buf, "/proc/%d/stat", pid);
30 } else {
31 sprintf (buf, "/proc/%d/task/%d/stat", pid, tid);
32 }
33
34 FILE *proc = fopen (buf, "r");
35 if (proc) {
36 if (42 == fscanf (proc, format,
37 &s->pid,
38 s->comm,
39 &s->state,
40 &s->ppid,
41 &s->pgrp,
42 &s->session,
43 &s->tty_nr,
44 &s->tpgid,
45 &s->flags,
46 &s->minflt,
47 &s->cminflt,
48 &s->majflt,
49 &s->cmajflt,
50 &s->utime,
51 &s->stime,
52 &s->cutime,
53 &s->cstime,
54 &s->priority,
55 &s->nice,
56 &s->num_threads,
57 &s->itrealvalue,
58 &s->starttime,
59 &s->vsize,
60 &s->rss,
61 &s->rlim,
62 &s->startcode,
63 &s->endcode,
64 &s->startstack,
65 &s->kstkesp,
66 &s->kstkeip,
67 &s->signal,
68 &s->blocked,
69 &s->sigignore,
70 &s->sigcatch,
71 &s->wchan,
72 &s->nswap,
73 &s->cnswap,
74 &s->exit_signal,
75 &s->processor,
76 &s->rt_priority,
77 &s->policy,
78 &s->delayacct_blkio_ticks
79 )
80 ) {
81 fclose(proc);
82 return 1;
83 } else {
84 fclose(proc);
85 return 0;
86 }
87 } else {
88 return 0;
89 }
90}
91