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 #include <process_lib.h>
27 #include <process_unix_priv.h>
28 #include <file_lib.h>
29 
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/user.h>
33 
34 typedef struct
35 {
36     time_t starttime;
37     char state;
38 } ProcessStat;
39 
GetProcessStat(pid_t pid,ProcessStat * state)40 static bool GetProcessStat(pid_t pid, ProcessStat *state)
41 {
42     int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
43     struct kinfo_proc psinfo;
44     size_t len = sizeof(psinfo);
45 
46     if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &psinfo, &len, NULL, 0) == 0)
47     {
48         state->starttime = psinfo.ki_start.tv_sec;
49 
50         switch(psinfo.ki_stat)
51         {
52             case SRUN:
53             case SIDL:
54                 state->state = 'R';
55                 break;
56             case SSTOP:
57                 state->state = 'T';
58                 break;
59             case SSLEEP:
60                 state->state = 'S';
61                 break;
62             case SZOMB:
63                 state->state = 'Z';
64                 break;
65             default:
66                 state->state = 'X';
67         }
68     }
69     else
70     {
71         return false;
72     }
73     return true;
74 }
75 
GetProcessStartTime(pid_t pid)76 time_t GetProcessStartTime(pid_t pid)
77 {
78     ProcessStat st;
79     if (GetProcessStat(pid, &st))
80     {
81         return st.starttime;
82     }
83     else
84     {
85         return PROCESS_START_TIME_UNKNOWN;
86     }
87 }
88 
GetProcessState(pid_t pid)89 ProcessState GetProcessState(pid_t pid)
90 {
91     ProcessStat st;
92     if (GetProcessStat(pid, &st))
93     {
94         switch (st.state)
95         {
96         case 'T':
97             return PROCESS_STATE_STOPPED;
98         case 'Z':
99             return PROCESS_STATE_ZOMBIE;
100         default:
101             return PROCESS_STATE_RUNNING;
102         }
103     }
104     else
105     {
106         return PROCESS_STATE_DOES_NOT_EXIST;
107     }
108 }
109