1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
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   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <cf3.defs.h>
26 
27 #include <process_lib.h>
28 #include <process_unix_priv.h>
29 #include <files_lib.h>
30 
31 
32 typedef struct
33 {
34     time_t starttime;
35     char state;
36 } ProcessStat;
37 
GetProcessStat(pid_t pid,ProcessStat * state)38 static bool GetProcessStat(pid_t pid, ProcessStat *state)
39 {
40     char filename[64];
41     xsnprintf(filename, sizeof(filename), "/proc/%jd/stat", (intmax_t) pid);
42 
43     int fd;
44     for (;;)
45     {
46         if ((fd = open(filename, O_RDONLY)) != -1)
47         {
48             break;
49         }
50 
51         if (errno == EINTR)
52         {
53             continue;
54         }
55 
56         if (errno == ENOENT || errno == ENOTDIR)
57         {
58             return false;
59         }
60 
61         if (errno == EACCES)
62         {
63             return false;
64         }
65 
66         assert (fd != -1 && "Unable to open /proc/<pid>/stat");
67     }
68 
69     char stat[CF_BUFSIZE];
70     int res = FullRead(fd, stat, sizeof(stat) - 1); /* -1 for the '\0', below */
71     close(fd);
72 
73     if (res < 0)
74     {
75         return false;
76     }
77     assert(res < CF_BUFSIZE);
78     stat[res] = '\0'; /* read() doesn't '\0'-terminate */
79 
80     /* stat entry is of form: <pid> (<task name>) <various info...>
81      * To avoid choking on weird task names, we search for the closing
82      * parenthesis first: */
83     char *p = memrchr(stat, ')', res);
84     if (p == NULL)
85     {
86         /* Wrong field format! */
87         return false;
88     }
89 
90     p++; // Skip the parenthesis
91 
92     char proc_state[2];
93     unsigned long long starttime;
94 
95     if (sscanf(p,
96                "%1s" /* state */
97                "%*s" /* ppid */
98                "%*s" /* pgrp */
99                "%*s" /* session */
100                "%*s" /* tty_nr */
101                "%*s" /* tpgid */
102                "%*s" /* flags */
103                "%*s" /* minflt */
104                "%*s" /* cminflt */
105                "%*s" /* majflt */
106                "%*s" /* cmajflt */
107                "%*s" /* utime */
108                "%*s" /* stime */
109                "%*s" /* cutime */
110                "%*s" /* cstime */
111                "%*s" /* priority */
112                "%*s" /* nice */
113                "%*s" /* num_threads */
114                "%*s" /* itrealvalue */
115                "%llu" /* starttime */,
116                proc_state,
117                &starttime) != 2)
118     {
119         return false;
120     }
121 
122     state->state = proc_state[0];
123     state->starttime = (time_t)(starttime / sysconf(_SC_CLK_TCK));
124 
125     return true;
126 }
127 
GetProcessStartTime(pid_t pid)128 time_t GetProcessStartTime(pid_t pid)
129 {
130     ProcessStat st;
131     if (GetProcessStat(pid, &st))
132     {
133         return st.starttime;
134     }
135     else
136     {
137         return PROCESS_START_TIME_UNKNOWN;
138     }
139 }
140 
GetProcessState(pid_t pid)141 ProcessState GetProcessState(pid_t pid)
142 {
143     ProcessStat st;
144     if (GetProcessStat(pid, &st))
145     {
146         switch (st.state)
147         {
148         case 'T':
149             return PROCESS_STATE_STOPPED;
150         case 'Z':
151             return PROCESS_STATE_ZOMBIE;
152         default:
153             return PROCESS_STATE_RUNNING;
154         }
155     }
156     else
157     {
158         return PROCESS_STATE_DOES_NOT_EXIST;
159     }
160 }
161