Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/processes.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014 Davide Madrisan <[email protected]>
5 *
6 * A library for getting informations on the running processes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* activate extra prototypes for glibc */
23 #endif
24
25 #include <sys/resource.h>
26 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "common.h"
37 #include "logging.h"
38 #include "messages.h"
39 #include "system.h"
40 #include "xalloc.h"
41
42 #define PROC_ROOT "/proc"
43 #ifndef RLIM_INFINITY
44 # define RLIM_INFINITY 65535
45 #endif
46
47 #define NBPROCS_NONE 0x00
48 #define NBPROCS_VERBOSE 0x01
49 #define NBPROCS_THREADS 0x02
50
51 /* Return name corresponding to 'uid', or NULL on error */
52
53 char *
54 uid_to_username (uid_t uid)
55 {
56 struct passwd *pwd = getpwuid (uid);
57 return (pwd == NULL) ? "<no-user>" : pwd->pw_name;
58 }
59
60 struct procs_list_node
61 {
62 uid_t uid;
63 char *username;
64 long nbr; /* number of the occurrences */
65 #ifdef RLIMIT_NPROC
66 rlim_t rlimit_nproc_soft; /* ulimit -Su */
67 rlim_t rlimit_nproc_hard; /* ulimit -Hu */
68 #endif
69 struct procs_list_node *next;
70 };
71
72 char *
73 procs_list_node_get_username (struct procs_list_node *node)
74 {
75 return node->username;
76 }
77
78 long
79 procs_list_node_get_nbr (struct procs_list_node *node)
80 {
81 return node->nbr;
82 }
83
84 #ifdef RLIMIT_NPROC
85 unsigned long
86 procs_list_node_get_rlimit_nproc_soft (struct procs_list_node *node)
87 {
88 return node->rlimit_nproc_soft;
89 }
90
91 unsigned long
92 procs_list_node_get_rlimit_nproc_hard (struct procs_list_node *node)
93 {
94 return node->rlimit_nproc_hard;
95 }
96 #endif
97
98 struct procs_list_node *
99 procs_list_node_get_next (struct procs_list_node *node)
100 {
101 return node->next;
102 }
103
104 long procs_list_node_get_total_procs_nbr (struct procs_list_node *list)
105 {
106 return list->nbr;
107 }
108
109 void
110 procs_list_node_init (struct procs_list_node **list)
111 {
112 struct procs_list_node *new = xmalloc (sizeof (struct procs_list_node));
113
114 /* The list's head points to itself if empty */
115 new->uid = -1;
116 new->nbr = 0; /* will hold the total number of processes */
117 new->next = new;
118 *list = new;
119 }
120
121 struct procs_list_node *
122 procs_list_node_add (uid_t uid, unsigned long inc,
123 struct procs_list_node *plist)
124 {
125 struct procs_list_node *p = plist;
126
127 dbg ("procs_list_node_add (uid %u, #threads %lu)\n", uid,
128 plist->nbr + inc);
129 while (p != p->next)
130 {
131 p = p->next;
132 dbg (" - iteration: uid = %u\n", p->uid);
133 if (p->uid == uid)
134 {
135 p->nbr += inc;
136 plist->nbr += inc;
137 dbg (" - found uid %u: now #%ld\n", uid, p->nbr);
138 return p;
139 }
140 }
141
142 struct procs_list_node *new = xmalloc (sizeof (struct procs_list_node));
143 dbg ("new uid --> append uid %u #1\n", uid);
144 new->uid = uid;
145 new->nbr = inc;
146 plist->nbr += inc;
147 new->username = xstrdup (uid_to_username (uid));
148 #ifdef RLIMIT_NPROC
149 struct rlimit rlim;
150 int res;
151
152 if ((res = getrlimit (RLIMIT_NPROC, &rlim)) < 0)
153 new->rlimit_nproc_soft = new->rlimit_nproc_hard = RLIM_INFINITY;
154 else
155 {
156 #ifdef LIBC_MUSL
157 dbg (" - with rlimits: %llu %llu\n", rlim.rlim_cur, rlim.rlim_max);
158 #else
159 dbg (" - with rlimits: %lu %lu\n", rlim.rlim_cur, rlim.rlim_max);
160 #endif
161 new->rlimit_nproc_soft = rlim.rlim_cur;
162 new->rlimit_nproc_hard = rlim.rlim_max;
163 }
164 #endif
165 new->next = new;
166 p->next = new;
167
168 return new;
169 }
170
171 /* Parses /proc/PID/status files to produce a list of the running processes */
172
173 struct procs_list_node *
174 procs_list_getall (unsigned int flags)
175 {
176 DIR *dirp;
177 FILE *fp;
178 size_t len = 0;
179 bool gotname, gotuid, gotthreads,
180 threads = (flags & NBPROCS_THREADS) ? true : false,
181 verbose = (flags & NBPROCS_VERBOSE) ? true : false;
182 char *line = NULL, *p, path[PATH_MAX];
183 uid_t uid = -1;
184 unsigned long threads_nbr;
185 struct procs_list_node *plist = NULL;
186
187 #define MAX_LINE 128
188 char *cmd = xmalloc (MAX_LINE);
189
190 if ((dirp = opendir (PROC_ROOT)) == NULL)
191 plugin_error (STATE_UNKNOWN, errno, "Cannot open %s", PROC_ROOT);
192
193 procs_list_node_init (&plist);
194
195 /* Scan entries under /proc directory */
196 for (;;)
197 {
198 struct dirent *dp;
199 ssize_t chread;
200 errno = 0;
201
202 if ((dp = readdir (dirp)) == NULL)
203 {
204 if (errno != 0)
205 plugin_error (STATE_UNKNOWN, errno, "readdir() failure");
206 else
207 break; /* end-of-directory */
208 }
209
210 /* Since we are looking for /proc/PID directories, skip entries
211 that are not directories, or don't begin with a digit. */
212
213 if (dp->d_type != DT_DIR || !isdigit ((unsigned char) dp->d_name[0]))
214 continue;
215
216 snprintf (path, PATH_MAX, "/proc/%s/status", dp->d_name);
217
218 if ((fp = fopen (path, "r")) == NULL)
219 continue; /* Ignore errors: fopen() might fail if
220 process has just terminated */
221
222 gotname = gotuid = gotthreads = false;
223 threads_nbr = 0;
224 while ((chread = getline (&line, &len, fp)) != -1)
225 {
226 /* The "Name:" line contains the name of the command that
227 this process is running */
228 if (strncmp (line, "Name:", 5) == 0)
229 {
230 for (p = line + 5; *p != '\0' && isspace ((unsigned char) *p);)
231 p++;
232 strncpy (cmd, p, MAX_LINE - 1);
233 cmd[MAX_LINE - 1] = '\0'; /* Ensure null-terminated */
234 gotname = true;
235 }
236
237 /* The "Threads:" line contains the number of threads in process
238 containing this thread: check with 'ps -e -o pid,user,nlwp' */
239 if (strncmp (line, "Threads:", 8) == 0)
240 {
241 threads_nbr = strtol (line + 8, NULL, 10);
242 gotthreads = true;
243 }
244
245 /* The "Uid:" line contains the real, effective, saved set-,
246 and file-system user IDs */
247 if (strncmp (line, "Uid:", 4) == 0)
248 {
249 uid = strtol (line + 4, NULL, 10);
250 gotuid = true;
251 }
252
253 if (gotname && gotuid && gotthreads)
254 {
255 procs_list_node_add (uid, (threads ? threads_nbr : 1), plist);
256 break;
257 }
258 }
259
260 fclose (fp);
261
262 if (gotname && gotuid && gotthreads && verbose)
263 printf ("%12s: pid: %5s threads: %5lu, cmd: %s",
264 uid_to_username (uid), dp->d_name, threads_nbr, cmd);
265 }
266
267 closedir (dirp);
268 free (cmd);
269 free (line);
270
271 return plist;
272 }