| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _GNU_SOURCE |
| 22 |
# define _GNU_SOURCE |
| 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 |
|
| 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; |
| 65 |
#ifdef RLIMIT_NPROC |
| 66 |
rlim_t rlimit_nproc_soft; |
| 67 |
rlim_t rlimit_nproc_hard; |
| 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 |
|
| 115 |
new->uid = -1; |
| 116 |
new->nbr = 0; |
| 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 |
|
| 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 |
|
| 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; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 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; |
| 220 |
|
| 221 |
|
| 222 |
gotname = gotuid = gotthreads = false; |
| 223 |
threads_nbr = 0; |
| 224 |
while ((chread = getline (&line, &len, fp)) != -1) |
| 225 |
{ |
| 226 |
|
| 227 |
|
| 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'; |
| 234 |
gotname = true; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if (strncmp (line, "Threads:", 8) == 0) |
| 240 |
{ |
| 241 |
threads_nbr = strtol (line + 8, NULL, 10); |
| 242 |
gotthreads = true; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 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 |
} |