[67] | 1 | /* SAMHAIN file system integrity testing */
|
---|
| 2 | /* Copyright (C) 2006 Rainer Wichmann */
|
---|
| 3 | /* */
|
---|
| 4 | /* This program is free software; you can redistribute it */
|
---|
| 5 | /* and/or modify */
|
---|
| 6 | /* it under the terms of the GNU General Public License as */
|
---|
| 7 | /* published by */
|
---|
| 8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
| 9 | /* (at your option) any later version. */
|
---|
| 10 | /* */
|
---|
| 11 | /* This program is distributed in the hope that it will be useful, */
|
---|
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
| 14 | /* GNU General Public License for more details. */
|
---|
| 15 | /* */
|
---|
| 16 | /* You should have received a copy of the GNU General Public License */
|
---|
| 17 | /* along with this program; if not, write to the Free Software */
|
---|
| 18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 19 |
|
---|
| 20 | /***************************************************************************
|
---|
| 21 | *
|
---|
| 22 | * This file provides a module for samhain to check for hidden/faked/missing
|
---|
| 23 | * processes on the host.
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | #include "config_xor.h"
|
---|
| 28 |
|
---|
[384] | 29 | /* changed from 500 to 600 b/o FreeBSD (see sys/cdefs.h)
|
---|
| 30 | * which needs _POSIX_C_SOURCE >= 200112 for lstat()
|
---|
| 31 | */
|
---|
[394] | 32 | #if defined(__sun) || defined(__sun__) || defined(sun)
|
---|
| 33 | #define _XOPEN_SOURCE 500
|
---|
| 34 | #else
|
---|
[384] | 35 | #define _XOPEN_SOURCE 600
|
---|
[394] | 36 | #endif
|
---|
[67] | 37 |
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include <ctype.h>
|
---|
| 41 | #include <string.h>
|
---|
| 42 | #include <sys/types.h>
|
---|
[384] | 43 | #include <sys/stat.h>
|
---|
[67] | 44 | #include <dirent.h>
|
---|
| 45 | #include <signal.h>
|
---|
| 46 | #include <unistd.h>
|
---|
| 47 |
|
---|
| 48 | #ifdef _POSIX_PRIORITY_SCHEDULING
|
---|
| 49 | #include <sched.h>
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
| 52 | #ifdef HAVE_GETPRIORITY
|
---|
| 53 | #include <errno.h>
|
---|
| 54 | #include <sys/resource.h>
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
[78] | 57 | #ifdef HAVE_SYS_STATVFS_H
|
---|
| 58 | #include <sys/statvfs.h>
|
---|
| 59 | #endif
|
---|
| 60 |
|
---|
| 61 |
|
---|
[67] | 62 | #ifdef HAVE_REGEX_H
|
---|
| 63 | #include <regex.h>
|
---|
| 64 | #endif
|
---|
| 65 |
|
---|
| 66 | #include "samhain.h"
|
---|
| 67 | #include "sh_modules.h"
|
---|
| 68 | #include "sh_processcheck.h"
|
---|
| 69 | #include "sh_utils.h"
|
---|
| 70 | #include "sh_error.h"
|
---|
| 71 | #include "sh_extern.h"
|
---|
[78] | 72 | #include "sh_calls.h"
|
---|
[143] | 73 | #include "sh_pthread.h"
|
---|
[67] | 74 |
|
---|
| 75 | #ifdef SH_USE_PROCESSCHECK
|
---|
| 76 |
|
---|
| 77 | #define FIL__ _("sh_processcheck.c")
|
---|
| 78 |
|
---|
| 79 | #ifdef __linux__
|
---|
[143] | 80 | #define PS_THREADS
|
---|
[67] | 81 | #endif
|
---|
| 82 |
|
---|
| 83 | /* We won't want to build this into yule
|
---|
| 84 | */
|
---|
| 85 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
| 86 |
|
---|
[143] | 87 | SH_MUTEX_STATIC(mutex_proc_check, PTHREAD_MUTEX_INITIALIZER);
|
---|
| 88 |
|
---|
[67] | 89 | /* sh_prochk_maxpid is one more than the largest pid
|
---|
| 90 | */
|
---|
| 91 | static size_t sh_prochk_minpid = 0x0001;
|
---|
| 92 | static size_t sh_prochk_maxpid = 0x8000;
|
---|
| 93 | static size_t sh_prochk_size = 0;
|
---|
| 94 |
|
---|
| 95 | static int ShProchkActive = S_TRUE;
|
---|
| 96 | static short * sh_prochk_res = NULL;
|
---|
| 97 |
|
---|
| 98 | static char * sh_prochk_pspath = NULL;
|
---|
| 99 | static char * sh_prochk_psarg = NULL;
|
---|
| 100 |
|
---|
| 101 | #define SH_PROCHK_INTERVAL 300
|
---|
| 102 | static time_t sh_prochk_interval = SH_PROCHK_INTERVAL;
|
---|
| 103 | static int sh_prochk_severity = SH_ERR_SEVERE;
|
---|
[204] | 104 | static int sh_prochk_openvz = S_FALSE;
|
---|
[67] | 105 |
|
---|
| 106 | static int sh_prochk_set_maxpid (const char * str);
|
---|
| 107 | static int sh_prochk_set_minpid (const char * str);
|
---|
| 108 | static int sh_prochk_set_active (const char *str);
|
---|
| 109 | static int sh_prochk_add_process (const char *str);
|
---|
| 110 | static int sh_prochk_set_pspath (const char *str);
|
---|
| 111 | static int sh_prochk_set_psarg (const char *str);
|
---|
| 112 | static int sh_prochk_set_interval(const char *str);
|
---|
| 113 | static int sh_prochk_set_severity(const char *str);
|
---|
[204] | 114 | static int sh_prochk_set_openvz (const char *str);
|
---|
[67] | 115 |
|
---|
| 116 | sh_rconf sh_prochk_table[] = {
|
---|
| 117 | {
|
---|
| 118 | N_("severityprocesscheck"),
|
---|
| 119 | sh_prochk_set_severity,
|
---|
| 120 | },
|
---|
| 121 | {
|
---|
| 122 | N_("processcheckexists"),
|
---|
| 123 | sh_prochk_add_process,
|
---|
| 124 | },
|
---|
| 125 | {
|
---|
| 126 | N_("processcheckactive"),
|
---|
| 127 | sh_prochk_set_active,
|
---|
| 128 | },
|
---|
| 129 | {
|
---|
| 130 | N_("processcheckminpid"),
|
---|
| 131 | sh_prochk_set_minpid,
|
---|
| 132 | },
|
---|
| 133 | {
|
---|
| 134 | N_("processcheckmaxpid"),
|
---|
| 135 | sh_prochk_set_maxpid,
|
---|
| 136 | },
|
---|
| 137 | {
|
---|
| 138 | N_("processcheckpspath"),
|
---|
| 139 | sh_prochk_set_pspath,
|
---|
| 140 | },
|
---|
| 141 | {
|
---|
| 142 | N_("processcheckpsarg"),
|
---|
| 143 | sh_prochk_set_psarg,
|
---|
| 144 | },
|
---|
| 145 | {
|
---|
| 146 | N_("processcheckinterval"),
|
---|
| 147 | sh_prochk_set_interval,
|
---|
| 148 | },
|
---|
| 149 | {
|
---|
[204] | 150 | N_("processcheckisopenvz"),
|
---|
| 151 | sh_prochk_set_openvz,
|
---|
| 152 | },
|
---|
| 153 | {
|
---|
[67] | 154 | NULL,
|
---|
| 155 | NULL
|
---|
| 156 | }
|
---|
| 157 | };
|
---|
| 158 |
|
---|
| 159 | #define SH_PROC_MISSING 1
|
---|
| 160 | #define SH_PROC_FAKED 2
|
---|
| 161 | #define SH_PROC_HIDDEN 4
|
---|
| 162 | #define SH_PROC_EXISTS 8
|
---|
| 163 |
|
---|
| 164 | #ifndef HAVE_LSTAT
|
---|
| 165 | #define lstat(x,y) stat(x,y)
|
---|
| 166 | #endif /* HAVE_LSTAT */
|
---|
| 167 |
|
---|
[82] | 168 | #if defined(S_IFLNK) && !defined(S_ISLNK)
|
---|
| 169 | #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
|
---|
| 170 | #else
|
---|
| 171 | #if !defined(S_ISLNK)
|
---|
| 172 | #define S_ISLNK(mode) (0)
|
---|
| 173 | #endif
|
---|
| 174 | #endif
|
---|
| 175 |
|
---|
[67] | 176 | static const short SH_PR_PS = 0x0001;
|
---|
| 177 |
|
---|
| 178 | static const short SH_PR_GETSID = 0x0002;
|
---|
| 179 | static const short SH_PR_KILL = 0x0004;
|
---|
| 180 | static const short SH_PR_GETPGID = 0x0008;
|
---|
| 181 |
|
---|
| 182 | static const short SH_PR_LSTAT = 0x0010;
|
---|
| 183 | static const short SH_PR_OPENDIR = 0x0020;
|
---|
| 184 | static const short SH_PR_CHDIR = 0x0040;
|
---|
| 185 | static const short SH_PR_SCHED = 0x0080;
|
---|
| 186 |
|
---|
| 187 | static const short SH_PR_PRIORITY = 0x0100;
|
---|
[78] | 188 | static const short SH_PR_STATVSF = 0x0200;
|
---|
[67] | 189 |
|
---|
[78] | 190 | static const short SH_PR_PS2 = 0x1000;
|
---|
| 191 | static const short SH_PR_PS_ANY = 0x2000;
|
---|
| 192 | static const short SH_PR_ALL = 0x4000;
|
---|
| 193 | static const short SH_PR_ANY = 0x8000;
|
---|
[67] | 194 |
|
---|
[77] | 195 | /* /proc:
|
---|
| 196 | * linux: /proc/pid/exe
|
---|
| 197 | * freebsd: /proc/pid/file
|
---|
| 198 | * solaris10: /proc/pid/path/a.out
|
---|
| 199 | */
|
---|
[78] | 200 | static char * get_user_and_path (pid_t pid, char * user, size_t usrlen)
|
---|
| 201 | {
|
---|
[132] | 202 | extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
|
---|
[77] | 203 |
|
---|
[78] | 204 | char path[128];
|
---|
| 205 | char * buf;
|
---|
| 206 | struct stat sbuf;
|
---|
| 207 | int len;
|
---|
| 208 | char * tmp;
|
---|
| 209 |
|
---|
[203] | 210 | sl_snprintf (path, sizeof(path), _("/proc/%ld/exe"), (unsigned long) pid);
|
---|
[78] | 211 |
|
---|
| 212 | if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
|
---|
| 213 | {
|
---|
| 214 | goto linkread;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[203] | 217 | sl_snprintf (path, sizeof(path), _("/proc/%ld/file"), (unsigned long) pid);
|
---|
[78] | 218 |
|
---|
| 219 | if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
|
---|
| 220 | {
|
---|
| 221 | goto linkread;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[203] | 224 | sl_snprintf (path, sizeof(path), _("/proc/%ld/path/a.out"), (unsigned long) pid);
|
---|
[78] | 225 |
|
---|
| 226 | if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
|
---|
| 227 | {
|
---|
| 228 | goto linkread;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | return NULL;
|
---|
| 232 |
|
---|
| 233 | linkread:
|
---|
| 234 |
|
---|
| 235 | buf = SH_ALLOC(PATH_MAX);
|
---|
| 236 | len = readlink(path, buf, PATH_MAX); /* flawfinder: ignore */
|
---|
| 237 | len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
|
---|
| 238 |
|
---|
| 239 | if (len > 0)
|
---|
| 240 | {
|
---|
| 241 | buf[len] = '\0';
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | {
|
---|
| 245 | SH_FREE(buf);
|
---|
| 246 | return NULL;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[132] | 249 | tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, user, usrlen);
|
---|
[78] | 250 |
|
---|
[132] | 251 | if (!tmp)
|
---|
[78] | 252 | sl_snprintf (user, usrlen, "%ld", (unsigned long) sbuf.st_uid);
|
---|
| 253 |
|
---|
| 254 | return buf;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
[67] | 258 | struct watchlist {
|
---|
| 259 | char * str;
|
---|
| 260 | unsigned long pid;
|
---|
| 261 | #ifdef HAVE_REGEX_H
|
---|
| 262 | regex_t preg;
|
---|
| 263 | #endif
|
---|
| 264 | int seen;
|
---|
| 265 |
|
---|
| 266 | struct watchlist *next;
|
---|
| 267 | };
|
---|
| 268 |
|
---|
| 269 | static struct watchlist * process_check = NULL;
|
---|
| 270 |
|
---|
| 271 | static struct watchlist * list_missing = NULL;
|
---|
| 272 | static struct watchlist * list_fake = NULL;
|
---|
| 273 | static struct watchlist * list_hidden = NULL;
|
---|
| 274 |
|
---|
| 275 | /* recursively remove all list entries
|
---|
| 276 | */
|
---|
| 277 | static void kill_list (struct watchlist * head)
|
---|
| 278 | {
|
---|
| 279 | if (head->next)
|
---|
| 280 | kill_list (head->next);
|
---|
| 281 |
|
---|
| 282 | if (head->str)
|
---|
| 283 | SH_FREE(head->str);
|
---|
| 284 | SH_FREE(head);
|
---|
| 285 |
|
---|
| 286 | return;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 |
|
---|
| 290 | /* check the list for old entries; clean out old entries; reset others
|
---|
| 291 | * Return number of non-obsolete entries
|
---|
| 292 | */
|
---|
| 293 | static size_t clean_list (struct watchlist ** head_ptr)
|
---|
| 294 | {
|
---|
| 295 | size_t count = 0;
|
---|
| 296 | struct watchlist * ptr = *head_ptr;
|
---|
| 297 | struct watchlist * pre = *head_ptr;
|
---|
| 298 |
|
---|
| 299 | while (ptr)
|
---|
| 300 | {
|
---|
| 301 | if (ptr->seen == S_FALSE) /* obsolete entry */
|
---|
| 302 | {
|
---|
| 303 | if (ptr == pre) /* at head */
|
---|
| 304 | {
|
---|
| 305 | ptr = pre->next;
|
---|
| 306 | *head_ptr = pre->next;
|
---|
| 307 | if (pre->str)
|
---|
| 308 | SH_FREE(pre->str);
|
---|
| 309 | SH_FREE(pre);
|
---|
| 310 | pre = ptr;
|
---|
| 311 | }
|
---|
| 312 | else
|
---|
| 313 | {
|
---|
| 314 | pre->next = ptr->next;
|
---|
| 315 | if (ptr->str)
|
---|
| 316 | SH_FREE(ptr->str);
|
---|
| 317 | SH_FREE(ptr);
|
---|
| 318 | ptr = pre->next;
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 | else
|
---|
| 322 | {
|
---|
| 323 | ++count;
|
---|
| 324 | ptr->seen = S_FALSE; /* reset status */
|
---|
| 325 | pre = ptr;
|
---|
| 326 | ptr = ptr->next;
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | return count;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | /* check if process is in list; if not, add it and return false
|
---|
| 333 | */
|
---|
| 334 | static int is_in_list (struct watchlist ** head_ptr,
|
---|
| 335 | char * str, unsigned long pid)
|
---|
| 336 | {
|
---|
| 337 | struct watchlist * ptr = *head_ptr;
|
---|
| 338 |
|
---|
| 339 | if (str)
|
---|
| 340 | {
|
---|
| 341 | while (ptr)
|
---|
| 342 | {
|
---|
| 343 | if (ptr->str && (0 == strcmp(str, ptr->str)))
|
---|
| 344 | {
|
---|
| 345 | ptr->seen = S_TRUE;
|
---|
| 346 | return S_TRUE;
|
---|
| 347 | }
|
---|
| 348 | ptr = ptr->next;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | else
|
---|
| 352 | {
|
---|
| 353 | while (ptr)
|
---|
| 354 | {
|
---|
| 355 | if (ptr->pid == pid)
|
---|
| 356 | {
|
---|
| 357 | ptr->seen = S_TRUE;
|
---|
| 358 | return S_TRUE;
|
---|
| 359 | }
|
---|
| 360 | ptr = ptr->next;
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | ptr = SH_ALLOC(sizeof(struct watchlist));
|
---|
| 365 |
|
---|
| 366 | if (str)
|
---|
| 367 | {
|
---|
| 368 | ptr->str = sh_util_strdup(str);
|
---|
| 369 | }
|
---|
| 370 | else
|
---|
| 371 | {
|
---|
| 372 | ptr->str = NULL;
|
---|
| 373 | ptr->pid = pid;
|
---|
| 374 | }
|
---|
| 375 | ptr->next = *head_ptr;
|
---|
| 376 | ptr->seen = S_TRUE;
|
---|
| 377 | *head_ptr = ptr;
|
---|
| 378 |
|
---|
| 379 | return S_FALSE;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | static int is_in_watchlist (const char *str, unsigned long num)
|
---|
| 383 | {
|
---|
| 384 | struct watchlist * list = process_check;
|
---|
| 385 |
|
---|
| 386 | while (list)
|
---|
| 387 | {
|
---|
| 388 | #ifdef HAVE_REGEX_H
|
---|
| 389 | if (0 == regexec(&(list->preg), str, 0, NULL, 0))
|
---|
| 390 | {
|
---|
| 391 | list->seen = S_TRUE;
|
---|
| 392 | list->pid = num;
|
---|
| 393 | return S_TRUE;
|
---|
| 394 | }
|
---|
| 395 | #else
|
---|
| 396 | if (strstr(str, list->str))
|
---|
| 397 | {
|
---|
| 398 | list->seen = S_TRUE;
|
---|
| 399 | list->pid = num;
|
---|
| 400 | return S_TRUE;
|
---|
| 401 | }
|
---|
| 402 | #endif
|
---|
| 403 | list = list->next;
|
---|
| 404 | }
|
---|
| 405 | return S_FALSE;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[170] | 408 | /* These variables are not used anywhere. They only exist
|
---|
| 409 | * to assign &userold, &user to them, which keeps gcc from
|
---|
| 410 | * putting them into a register, and avoids the 'clobbered
|
---|
| 411 | * by longjmp' warning. And no, 'volatile' proved insufficient.
|
---|
| 412 | */
|
---|
[481] | 413 | void * sh_dummy_413_watchlist = NULL;
|
---|
[170] | 414 |
|
---|
[67] | 415 | static void check_watchlist (short * res)
|
---|
| 416 | {
|
---|
| 417 | struct watchlist * list = process_check;
|
---|
| 418 | char * tmp;
|
---|
| 419 | size_t indx;
|
---|
| 420 |
|
---|
[170] | 421 | /* Take the address to keep gcc from putting them into registers.
|
---|
| 422 | * Avoids the 'clobbered by longjmp' warning.
|
---|
| 423 | */
|
---|
[481] | 424 | sh_dummy_413_watchlist = (void*) &list;
|
---|
[170] | 425 |
|
---|
[67] | 426 | while (list)
|
---|
| 427 | {
|
---|
| 428 | if (list->seen == S_FALSE)
|
---|
| 429 | {
|
---|
| 430 | /* avoid repetition of messages
|
---|
| 431 | */
|
---|
| 432 | if (S_FALSE == is_in_list(&list_missing, list->str, 0))
|
---|
| 433 | {
|
---|
[143] | 434 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[170] | 435 | tmp = sh_util_safe_name (list->str);
|
---|
[67] | 436 | sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
|
---|
| 437 | MSG_PCK_MISS,
|
---|
[170] | 438 | tmp);
|
---|
[230] | 439 | SH_FREE(tmp);
|
---|
[143] | 440 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 441 | }
|
---|
| 442 | }
|
---|
| 443 | else
|
---|
| 444 | {
|
---|
| 445 | indx = list->pid - sh_prochk_minpid;
|
---|
| 446 |
|
---|
| 447 | if (list->pid < sh_prochk_maxpid && list->pid >= sh_prochk_minpid &&
|
---|
| 448 | ((res[indx] & SH_PR_ANY) == 0) && /* not found */
|
---|
| 449 | ((res[indx] & SH_PR_PS) != 0) && /* seen in first ps */
|
---|
| 450 | ((res[indx] & SH_PR_PS2) != 0)) /* seen in second ps */
|
---|
| 451 | {
|
---|
| 452 | /* fake process, thus considered missing
|
---|
| 453 | */
|
---|
| 454 | if (S_FALSE == is_in_list(&list_missing, list->str, 0))
|
---|
| 455 | {
|
---|
[170] | 456 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 457 | tmp = sh_util_safe_name (list->str);
|
---|
| 458 | sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
|
---|
| 459 | MSG_PCK_MISS,
|
---|
| 460 | tmp);
|
---|
[230] | 461 | SH_FREE(tmp);
|
---|
[143] | 462 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 463 | }
|
---|
| 464 | }
|
---|
| 465 | list->seen = S_FALSE;
|
---|
| 466 | }
|
---|
| 467 | list = list->next;
|
---|
| 468 | }
|
---|
[383] | 469 |
|
---|
[481] | 470 | sh_dummy_413_watchlist = NULL;
|
---|
[383] | 471 | return;
|
---|
[67] | 472 | }
|
---|
| 473 |
|
---|
| 474 | /* Add 'str' to the list of watched processes for which
|
---|
| 475 | * existence should be checked.
|
---|
| 476 | */
|
---|
| 477 | int sh_prochk_add_process (const char *str)
|
---|
| 478 | {
|
---|
| 479 | struct watchlist *new;
|
---|
| 480 | int status;
|
---|
| 481 | char errbuf[256];
|
---|
| 482 |
|
---|
| 483 | SL_ENTER(_("sh_prochk_add_process"));
|
---|
| 484 |
|
---|
| 485 | if( str == NULL )
|
---|
| 486 | SL_RETURN(-1, _("sh_prochk_add_process") );
|
---|
| 487 |
|
---|
| 488 | new = SH_ALLOC(sizeof(struct watchlist));
|
---|
| 489 | new->next = process_check;
|
---|
| 490 | new->str = sh_util_strdup(str);
|
---|
| 491 | #ifdef HAVE_REGEX_H
|
---|
| 492 | status = regcomp(&(new->preg), str, REG_NOSUB|REG_EXTENDED);
|
---|
| 493 | if (status != 0)
|
---|
| 494 | {
|
---|
| 495 | regerror(status, &(new->preg), errbuf, sizeof(errbuf));
|
---|
[143] | 496 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 497 | sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
[143] | 498 | errbuf, _("sh_processes_add_process"));
|
---|
| 499 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 500 | SH_FREE(new->str);
|
---|
| 501 | SH_FREE(new);
|
---|
| 502 | SL_RETURN(-1, _("sh_prochk_add_process") );
|
---|
| 503 | }
|
---|
| 504 | #endif
|
---|
| 505 | new->pid = 0;
|
---|
| 506 | new->seen = S_FALSE;
|
---|
| 507 |
|
---|
| 508 | process_check = new;
|
---|
| 509 | SL_RETURN(0, _("sh_prochk_add_process") );
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /* severity
|
---|
| 513 | */
|
---|
| 514 | int sh_prochk_set_severity (const char * c)
|
---|
| 515 | {
|
---|
| 516 | char tmp[32];
|
---|
| 517 | tmp[0] = '='; tmp[1] = '\0';
|
---|
| 518 | sl_strlcat (tmp, c, 32);
|
---|
| 519 | return sh_error_set_level (tmp, &sh_prochk_severity);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 |
|
---|
| 523 |
|
---|
| 524 | /* Path to ps
|
---|
| 525 | */
|
---|
| 526 | int sh_prochk_set_pspath(const char *str)
|
---|
| 527 | {
|
---|
| 528 | SL_ENTER(_("sh_prochk_set_pspath"));
|
---|
| 529 |
|
---|
[383] | 530 | if (!str || ('/' != str[0]))
|
---|
[204] | 531 | SL_RETURN((-1), _("sh_prochk_set_pspath"));
|
---|
[67] | 532 | if (sh_prochk_pspath)
|
---|
| 533 | SH_FREE(sh_prochk_pspath);
|
---|
[374] | 534 | #ifdef SH_EVAL_SHELL
|
---|
[67] | 535 | sh_prochk_pspath = sh_util_strdup (str);
|
---|
| 536 | SL_RETURN((0), _("sh_prochk_set_pspath"));
|
---|
[374] | 537 | #else
|
---|
| 538 | sh_prochk_pspath = NULL;
|
---|
| 539 | SL_RETURN((-1), _("sh_prochk_set_pspath"));
|
---|
| 540 | #endif
|
---|
[67] | 541 | }
|
---|
| 542 |
|
---|
| 543 | /* argument for ps
|
---|
| 544 | */
|
---|
| 545 | int sh_prochk_set_psarg(const char *str)
|
---|
| 546 | {
|
---|
| 547 | SL_ENTER(_("sh_prochk_set_psarg"));
|
---|
| 548 |
|
---|
| 549 | if (sh_prochk_psarg)
|
---|
| 550 | SH_FREE(sh_prochk_psarg);
|
---|
[374] | 551 | #ifdef SH_EVAL_SHELL
|
---|
[67] | 552 | sh_prochk_psarg = sh_util_strdup (str);
|
---|
| 553 | SL_RETURN((0), _("sh_prochk_set_psarg"));
|
---|
[374] | 554 | #else
|
---|
[399] | 555 | (void) str;
|
---|
[374] | 556 | sh_prochk_psarg = NULL;
|
---|
| 557 | SL_RETURN((-1), _("sh_prochk_set_psarg"));
|
---|
| 558 | #endif
|
---|
[67] | 559 | }
|
---|
| 560 |
|
---|
| 561 |
|
---|
| 562 | /* Decide if we're active.
|
---|
| 563 | */
|
---|
| 564 | int sh_prochk_set_active(const char *str)
|
---|
| 565 | {
|
---|
| 566 | int value;
|
---|
| 567 |
|
---|
| 568 | SL_ENTER(_("sh_prochk_set_active"));
|
---|
| 569 |
|
---|
| 570 | value = sh_util_flagval(str, &ShProchkActive);
|
---|
| 571 |
|
---|
| 572 | SL_RETURN((value), _("sh_prochk_set_active"));
|
---|
| 573 | }
|
---|
| 574 |
|
---|
[204] | 575 | /* Are we on openvz.
|
---|
| 576 | */
|
---|
| 577 | static int openvz_hidden = 0;
|
---|
| 578 |
|
---|
| 579 | int sh_prochk_set_openvz(const char *str)
|
---|
| 580 | {
|
---|
| 581 | int value;
|
---|
| 582 |
|
---|
| 583 | SL_ENTER(_("sh_prochk_set_openvz"));
|
---|
| 584 |
|
---|
| 585 | value = sh_util_flagval(str, &sh_prochk_openvz);
|
---|
| 586 |
|
---|
| 587 | if (sh_prochk_openvz != S_FALSE) {
|
---|
| 588 | openvz_hidden = 1;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | SL_RETURN((value), _("sh_prochk_set_openvz"));
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[67] | 594 | /* Minimum PID
|
---|
| 595 | */
|
---|
| 596 | int sh_prochk_set_minpid(const char * str)
|
---|
| 597 | {
|
---|
| 598 | size_t value;
|
---|
| 599 | char * foo;
|
---|
| 600 | int retval = 0;
|
---|
| 601 |
|
---|
| 602 | SL_ENTER(_("sh_prochk_set_minpid"));
|
---|
| 603 |
|
---|
| 604 | value = (size_t) strtoul(str, &foo, 0);
|
---|
| 605 | if (*foo != '\0')
|
---|
| 606 | retval = -1;
|
---|
| 607 | else
|
---|
| 608 | sh_prochk_minpid = value;
|
---|
| 609 |
|
---|
| 610 | SL_RETURN((retval), _("sh_prochk_set_minpid"));
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | /* Maximum PID
|
---|
| 614 | */
|
---|
| 615 | static int userdef_maxpid = 0;
|
---|
| 616 |
|
---|
| 617 | int sh_prochk_set_maxpid(const char * str)
|
---|
| 618 | {
|
---|
| 619 | size_t value;
|
---|
| 620 | char * foo;
|
---|
[78] | 621 | int retval = -1;
|
---|
[67] | 622 |
|
---|
| 623 | SL_ENTER(_("sh_prochk_set_maxpid"));
|
---|
| 624 |
|
---|
| 625 | value = (size_t) strtoul(str, &foo, 0);
|
---|
[78] | 626 |
|
---|
[481] | 627 | if (*foo == '\0' && S_TRUE == sl_ok_adds(value, 1)) {
|
---|
[67] | 628 | sh_prochk_maxpid = value + 1;
|
---|
| 629 | userdef_maxpid = 1;
|
---|
[290] | 630 | retval = 0;
|
---|
[67] | 631 | }
|
---|
| 632 |
|
---|
| 633 | SL_RETURN((retval), _("sh_prochk_set_maxpid"));
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | int sh_prochk_set_interval (const char * c)
|
---|
| 637 | {
|
---|
| 638 | int retval = 0;
|
---|
| 639 | long val;
|
---|
| 640 |
|
---|
| 641 | SL_ENTER(_("sh_prochk_set_interval"));
|
---|
| 642 | val = strtol (c, (char **)NULL, 10);
|
---|
| 643 | if (val <= 0)
|
---|
| 644 | {
|
---|
[143] | 645 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 646 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 647 | _("process check interval"), c);
|
---|
[143] | 648 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 649 | retval = -1;
|
---|
| 650 | }
|
---|
[362] | 651 | else
|
---|
| 652 | {
|
---|
| 653 | sh_prochk_interval = (time_t) val;
|
---|
| 654 | }
|
---|
| 655 | SL_RETURN(retval, _("sh_prochk_set_interval"));
|
---|
[67] | 656 | }
|
---|
| 657 |
|
---|
| 658 |
|
---|
| 659 |
|
---|
| 660 | /* Recurse to the end of the list and then free the data as we return
|
---|
| 661 | * back up towards the start, making sure to free any strdupped strings
|
---|
| 662 | */
|
---|
| 663 | static void sh_prochk_free_list(struct watchlist *head)
|
---|
| 664 | {
|
---|
| 665 | if ( head != NULL )
|
---|
| 666 | {
|
---|
| 667 | sh_prochk_free_list(head->next);
|
---|
| 668 | if (head->str)
|
---|
| 669 | SH_FREE(head->str);
|
---|
| 670 | #ifdef HAVE_REGEX_H
|
---|
| 671 | regfree(&(head->preg));
|
---|
| 672 | #endif
|
---|
| 673 | SH_FREE(head);
|
---|
| 674 | }
|
---|
| 675 | return;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | #if defined(__linux__)
|
---|
| 679 | #define PROC_PID_MAX _("/proc/sys/kernel/pid_max")
|
---|
| 680 |
|
---|
| 681 | static int proc_max_pid (size_t * procpid)
|
---|
| 682 | {
|
---|
[170] | 683 | char * ret;
|
---|
[67] | 684 | unsigned long pid;
|
---|
| 685 | FILE * fd;
|
---|
| 686 | char str[128];
|
---|
| 687 | char * ptr;
|
---|
| 688 |
|
---|
| 689 | SL_ENTER(_("proc_max_pid"));
|
---|
| 690 |
|
---|
| 691 | if (userdef_maxpid != 0)
|
---|
| 692 | SL_RETURN((-1), _("proc_max_pid"));
|
---|
| 693 |
|
---|
[73] | 694 | if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
|
---|
[67] | 695 | {
|
---|
| 696 | if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
|
---|
| 697 | {
|
---|
| 698 | str[0] = '\0';
|
---|
[170] | 699 | ret = fgets(str, 128, fd);
|
---|
| 700 | if (ret && *str != '\0')
|
---|
[67] | 701 | {
|
---|
| 702 | pid = strtoul(str, &ptr, 0);
|
---|
| 703 | if (*ptr == '\0' || *ptr == '\n')
|
---|
| 704 | {
|
---|
[252] | 705 | sl_fclose(FIL__, __LINE__, fd);
|
---|
[67] | 706 | *procpid = (size_t) pid;
|
---|
| 707 | SL_RETURN(0, _("proc_max_pid"));
|
---|
| 708 | }
|
---|
| 709 | }
|
---|
[252] | 710 | sl_fclose(FIL__, __LINE__, fd);
|
---|
[67] | 711 | }
|
---|
| 712 | }
|
---|
| 713 | SL_RETURN((-1), _("proc_max_pid"));
|
---|
| 714 | }
|
---|
| 715 | #else
|
---|
| 716 | static int proc_max_pid(size_t * dummy)
|
---|
| 717 | {
|
---|
| 718 | (void) dummy;
|
---|
| 719 | return -1;
|
---|
| 720 | }
|
---|
| 721 | #endif
|
---|
| 722 |
|
---|
| 723 | static void sh_processes_tlist (char * list, size_t len, short res)
|
---|
| 724 | {
|
---|
| 725 | if (res & SH_PR_PS) sl_strlcat(list, _(" ps(initial)"), len);
|
---|
| 726 | if (res & SH_PR_CHDIR) sl_strlcat(list, _(" chdir"), len);
|
---|
| 727 | if (res & SH_PR_OPENDIR) sl_strlcat(list, _(" opendir"), len);
|
---|
| 728 | if (res & SH_PR_LSTAT) sl_strlcat(list, _(" lstat"), len);
|
---|
| 729 | if (res & SH_PR_PRIORITY) sl_strlcat(list, _(" getpriority"), len);
|
---|
| 730 | if (res & SH_PR_SCHED) sl_strlcat(list, _(" sched_getparam"), len);
|
---|
| 731 | if (res & SH_PR_GETSID) sl_strlcat(list, _(" getsid"), len);
|
---|
| 732 | if (res & SH_PR_GETPGID) sl_strlcat(list, _(" getpgid"), len);
|
---|
| 733 | if (res & SH_PR_KILL) sl_strlcat(list, _(" kill"), len);
|
---|
[78] | 734 | if (res & SH_PR_STATVSF) sl_strlcat(list, _(" statvfs"), len);
|
---|
[67] | 735 | if (res & SH_PR_PS2) sl_strlcat(list, _(" ps(final)"), len);
|
---|
| 736 | return;
|
---|
| 737 | }
|
---|
| 738 |
|
---|
[78] | 739 |
|
---|
[67] | 740 | static short sh_processes_check (pid_t pid, short res)
|
---|
| 741 | {
|
---|
| 742 | int have_checks = 0;
|
---|
| 743 | int need_checks = 0;
|
---|
| 744 | #ifdef HAVE_PROCFS
|
---|
| 745 | char path[128];
|
---|
| 746 | struct stat buf;
|
---|
| 747 | DIR * dir;
|
---|
[144] | 748 | int retval;
|
---|
[258] | 749 | #if defined(HAVE_STATVFS) && !defined(__FreeBSD__)
|
---|
[78] | 750 | struct statvfs vfsbuf;
|
---|
| 751 | #endif
|
---|
[82] | 752 | #endif
|
---|
[67] | 753 |
|
---|
[574] | 754 | #if !defined(sun) && !defined(__sun) && !defined(__sun__) && !defined(__OpenBSD__) && !defined(__APPLE__)
|
---|
[67] | 755 | #ifdef _POSIX_PRIORITY_SCHEDULING
|
---|
| 756 | struct sched_param p;
|
---|
| 757 | #endif
|
---|
| 758 | #endif
|
---|
| 759 |
|
---|
| 760 | if (0 == kill(pid, 0))
|
---|
| 761 | {
|
---|
| 762 | res |= SH_PR_KILL; res |= SH_PR_ANY; ++have_checks;
|
---|
| 763 | ++need_checks;
|
---|
| 764 | }
|
---|
| 765 | else if (errno != EPERM)
|
---|
| 766 | {
|
---|
| 767 | ++need_checks;
|
---|
| 768 | }
|
---|
| 769 |
|
---|
[78] | 770 |
|
---|
[67] | 771 | #ifdef HAVE_GETPGID
|
---|
| 772 | if ((pid_t)-1 != getpgid(pid))
|
---|
| 773 | {
|
---|
| 774 | res |= SH_PR_GETPGID; res |= SH_PR_ANY; ++have_checks;
|
---|
| 775 | }
|
---|
| 776 | ++need_checks;
|
---|
| 777 | #endif
|
---|
| 778 |
|
---|
| 779 | #ifdef HAVE_GETSID
|
---|
| 780 | if ((pid_t)-1 != getsid(pid))
|
---|
| 781 | {
|
---|
| 782 | res |= SH_PR_GETSID; res |= SH_PR_ANY; ++have_checks;
|
---|
| 783 | }
|
---|
| 784 | ++need_checks;
|
---|
| 785 | #endif
|
---|
| 786 |
|
---|
| 787 | /* sched_getparam() is broken on solaris 10, may segfault in librt
|
---|
[574] | 788 | * also not on MacOS
|
---|
[67] | 789 | */
|
---|
[574] | 790 | #if !defined(sun) && !defined(__sun) && !defined(__sun__) && !defined(__OpenBSD__) && !defined(__APPLE__)
|
---|
[67] | 791 | #ifdef _POSIX_PRIORITY_SCHEDULING
|
---|
| 792 | if (0 == sched_getparam (pid, &p))
|
---|
| 793 | {
|
---|
| 794 | res |= SH_PR_SCHED; res |= SH_PR_ANY; ++have_checks;
|
---|
| 795 | }
|
---|
| 796 | ++need_checks;
|
---|
| 797 | #endif
|
---|
| 798 | #endif
|
---|
| 799 |
|
---|
| 800 | #ifdef HAVE_GETPRIORITY
|
---|
| 801 | errno = 0;
|
---|
| 802 | if (((-1) == getpriority (PRIO_PROCESS, (int) pid)) && (errno == ESRCH));
|
---|
| 803 | else
|
---|
| 804 | {
|
---|
| 805 | res |= SH_PR_PRIORITY; res |= SH_PR_ANY; ++have_checks;
|
---|
| 806 | }
|
---|
| 807 | ++need_checks;
|
---|
| 808 | #endif
|
---|
| 809 |
|
---|
| 810 | #ifdef HAVE_PROCFS
|
---|
| 811 | sl_snprintf (path, sizeof(path), "/proc/%ld", (unsigned long) pid);
|
---|
| 812 |
|
---|
[144] | 813 | do {
|
---|
| 814 | retval = lstat (path, &buf);
|
---|
| 815 | } while (retval < 0 && errno == EINTR);
|
---|
| 816 |
|
---|
| 817 | if (0 == retval)
|
---|
[67] | 818 | {
|
---|
| 819 | res |= SH_PR_LSTAT; res |= SH_PR_ANY; ++have_checks;
|
---|
| 820 | }
|
---|
| 821 | ++need_checks;
|
---|
| 822 |
|
---|
| 823 | if (NULL != (dir = opendir(path)))
|
---|
| 824 | {
|
---|
| 825 | res |= SH_PR_OPENDIR; res |= SH_PR_ANY; ++have_checks;
|
---|
| 826 | closedir(dir);
|
---|
| 827 | }
|
---|
| 828 | ++need_checks;
|
---|
| 829 |
|
---|
[258] | 830 | #if defined(HAVE_STATVFS) && !defined(__FreeBSD__)
|
---|
[78] | 831 | do {
|
---|
| 832 | retval = statvfs (path, &vfsbuf);
|
---|
| 833 | } while (retval < 0 && errno == EINTR);
|
---|
| 834 |
|
---|
| 835 | if (0 == retval)
|
---|
| 836 | {
|
---|
| 837 | res |= SH_PR_STATVSF; res |= SH_PR_ANY; ++have_checks;
|
---|
| 838 | }
|
---|
| 839 | ++need_checks;
|
---|
| 840 | #endif
|
---|
| 841 |
|
---|
[67] | 842 | #if !defined(SH_PROFILE)
|
---|
| 843 | if (0 == chdir(path))
|
---|
| 844 | {
|
---|
| 845 | res |= SH_PR_CHDIR; res |= SH_PR_ANY; ++have_checks;
|
---|
[144] | 846 | do {
|
---|
| 847 | retval = chdir ("/");
|
---|
| 848 | } while (retval < 0 && errno == EINTR);
|
---|
[67] | 849 | }
|
---|
| 850 | ++need_checks;
|
---|
| 851 | #endif
|
---|
| 852 | #endif
|
---|
| 853 |
|
---|
| 854 | if (have_checks == need_checks)
|
---|
| 855 | {
|
---|
| 856 | res |= SH_PR_ALL;
|
---|
| 857 | }
|
---|
| 858 | return res;
|
---|
| 859 | }
|
---|
| 860 |
|
---|
[102] | 861 | extern int flag_err_debug;
|
---|
| 862 |
|
---|
[67] | 863 | static int sh_processes_readps (FILE * in, short * res,
|
---|
| 864 | char * str, size_t len,
|
---|
| 865 | short flag, pid_t pid)
|
---|
| 866 | {
|
---|
| 867 | int cc;
|
---|
[398] | 868 | volatile unsigned int lnum = 0;
|
---|
| 869 | volatile unsigned long num = 0;
|
---|
[67] | 870 | char c;
|
---|
| 871 | unsigned int pos = 0;
|
---|
[399] | 872 | #define SH_TWAIT_MAX 60
|
---|
| 873 | volatile unsigned int twait = 0;
|
---|
[67] | 874 | char tstr[256];
|
---|
[103] | 875 | enum { SKIP_TO_WS, SKIP_WS, SKIP_TO_WS2, SKIP_WS2, GET_NUM, SKIP_END, GET_NUM2 } line;
|
---|
[67] | 876 |
|
---|
| 877 | SL_ENTER(_("sh_processes_readps"));
|
---|
| 878 |
|
---|
| 879 | if (!in) {
|
---|
| 880 | SL_RETURN((-1), _("sh_processes_readps"));
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 | tstr[(sizeof(tstr)-1)] = '\0';
|
---|
| 884 | tstr[0] = '\0';
|
---|
| 885 | line = SKIP_END; /* Skip 1st line */
|
---|
| 886 |
|
---|
| 887 | do
|
---|
| 888 | {
|
---|
| 889 | cc = fgetc(in);
|
---|
| 890 |
|
---|
| 891 | if (EOF == cc)
|
---|
| 892 | {
|
---|
| 893 | if (feof(in))
|
---|
| 894 | {
|
---|
| 895 | break;
|
---|
| 896 | }
|
---|
[399] | 897 | else if ((errno == EAGAIN) && (twait < SH_TWAIT_MAX))
|
---|
[67] | 898 | {
|
---|
[169] | 899 | clearerr(in);
|
---|
[399] | 900 | retry_msleep(1, 0);
|
---|
| 901 | ++twait;
|
---|
[67] | 902 | continue;
|
---|
| 903 | }
|
---|
[169] | 904 | #ifdef HOST_IS_OPENBSD
|
---|
| 905 | else if (errno == ENODEV)
|
---|
| 906 | {
|
---|
| 907 | clearerr(in);
|
---|
| 908 | continue;
|
---|
| 909 | }
|
---|
| 910 | #endif
|
---|
[67] | 911 | else
|
---|
| 912 | {
|
---|
[132] | 913 | char errbuf[SH_ERRBUF_SIZE];
|
---|
| 914 |
|
---|
[398] | 915 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 916 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
[132] | 917 | sh_error_message(errno, errbuf, sizeof(errbuf)),
|
---|
[67] | 918 | _("sh_processes_readps"));
|
---|
[398] | 919 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 920 | break;
|
---|
| 921 | }
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | c = (char) cc;
|
---|
| 925 |
|
---|
| 926 | if (pos < (sizeof(tstr)-1))
|
---|
| 927 | {
|
---|
| 928 | tstr[pos] = c; ++pos;
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 | switch(line)
|
---|
| 932 | {
|
---|
| 933 | case SKIP_END:
|
---|
| 934 | if (c == '\n')
|
---|
| 935 | {
|
---|
| 936 | tstr[pos-1] = '\0';
|
---|
[481] | 937 | if (flag_err_debug == S_TRUE)
|
---|
[102] | 938 | {
|
---|
[398] | 939 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[143] | 940 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, num,
|
---|
| 941 | MSG_E_SUBGEN,
|
---|
[102] | 942 | tstr,
|
---|
| 943 | _("sh_processes_readps"));
|
---|
[398] | 944 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[102] | 945 | }
|
---|
[67] | 946 | /* fprintf(stderr, "<%ld> %s\n", num, tstr); */
|
---|
| 947 | line = SKIP_WS; pos = 0;
|
---|
| 948 | if (str != NULL && num == (unsigned long) pid)
|
---|
| 949 | sl_strlcpy(str, tstr, len);
|
---|
| 950 | if (lnum != 0)
|
---|
| 951 | is_in_watchlist (tstr, num);
|
---|
| 952 | ++lnum;
|
---|
| 953 | }
|
---|
| 954 | break;
|
---|
[103] | 955 | case SKIP_TO_WS:
|
---|
| 956 | if (!isspace(cc))
|
---|
| 957 | break;
|
---|
| 958 | line = SKIP_WS;
|
---|
| 959 | /* fallthrough */
|
---|
[67] | 960 | case SKIP_WS:
|
---|
| 961 | if (isspace(cc))
|
---|
| 962 | break;
|
---|
| 963 | num = 0;
|
---|
| 964 | line = GET_NUM;
|
---|
| 965 | /* fallthrough */
|
---|
| 966 | case GET_NUM:
|
---|
| 967 | if (isdigit(cc))
|
---|
| 968 | {
|
---|
| 969 | num = num * 10 + (c - '0');
|
---|
| 970 | break;
|
---|
| 971 | }
|
---|
[103] | 972 | else if (isspace(cc))
|
---|
| 973 | {
|
---|
[143] | 974 | #ifdef PS_THREADS
|
---|
[103] | 975 | num = 0;
|
---|
| 976 | line = SKIP_WS2;
|
---|
[67] | 977 | #else
|
---|
[103] | 978 | if (num < sh_prochk_maxpid && num >= sh_prochk_minpid)
|
---|
| 979 | {
|
---|
| 980 | res[num - sh_prochk_minpid] |= flag;
|
---|
| 981 | }
|
---|
| 982 | line = SKIP_END;
|
---|
| 983 | #endif
|
---|
| 984 | break;
|
---|
| 985 | }
|
---|
| 986 | else
|
---|
[67] | 987 | {
|
---|
[103] | 988 | line = SKIP_TO_WS;
|
---|
| 989 | break;
|
---|
[67] | 990 | }
|
---|
[103] | 991 | case SKIP_TO_WS2:
|
---|
| 992 | if (!isspace(cc))
|
---|
| 993 | break;
|
---|
| 994 | line = SKIP_WS2;
|
---|
| 995 | /* fallthrough */
|
---|
[67] | 996 | case SKIP_WS2:
|
---|
| 997 | if (isspace(cc))
|
---|
| 998 | break;
|
---|
| 999 | num = 0;
|
---|
| 1000 | line = GET_NUM2;
|
---|
| 1001 | /* fallthrough */
|
---|
| 1002 | case GET_NUM2:
|
---|
| 1003 | if (isdigit(cc))
|
---|
| 1004 | {
|
---|
| 1005 | num = num * 10 + (c - '0');
|
---|
| 1006 | break;
|
---|
| 1007 | }
|
---|
[103] | 1008 | else if (isspace(cc))
|
---|
[67] | 1009 | {
|
---|
[103] | 1010 | if (num < sh_prochk_maxpid && num >= sh_prochk_minpid)
|
---|
| 1011 | {
|
---|
| 1012 | res[num - sh_prochk_minpid] |= flag;
|
---|
| 1013 | }
|
---|
| 1014 | line = SKIP_END;
|
---|
| 1015 | break;
|
---|
[67] | 1016 | }
|
---|
[103] | 1017 | else
|
---|
| 1018 | {
|
---|
| 1019 | line = SKIP_TO_WS2;
|
---|
| 1020 | break;
|
---|
| 1021 | }
|
---|
[67] | 1022 | default:
|
---|
| 1023 | SL_RETURN ((-1), _("sh_processes_readps"));
|
---|
| 1024 | }
|
---|
| 1025 | } while (1);
|
---|
| 1026 |
|
---|
| 1027 | if (ferror(in))
|
---|
| 1028 | {
|
---|
| 1029 | SL_RETURN ((-1), _("sh_processes_readps"));
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | SL_RETURN ((0), _("sh_processes_readps"));
|
---|
| 1033 | }
|
---|
| 1034 |
|
---|
| 1035 | static int sh_processes_runps (short * res, char * str, size_t len,
|
---|
| 1036 | short flag, pid_t pid)
|
---|
| 1037 | {
|
---|
| 1038 | sh_tas_t task;
|
---|
| 1039 |
|
---|
| 1040 | int status = 0;
|
---|
| 1041 | char * p;
|
---|
| 1042 | int retval = 0;
|
---|
[132] | 1043 | char dir[SH_PATHBUF];
|
---|
[67] | 1044 |
|
---|
| 1045 | SL_ENTER(_("sh_processes_runps"));
|
---|
| 1046 |
|
---|
| 1047 | sh_ext_tas_init(&task);
|
---|
[132] | 1048 | p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid, dir, sizeof(dir));
|
---|
[67] | 1049 | if (p)
|
---|
| 1050 | {
|
---|
| 1051 | (void) sh_ext_tas_add_envv (&task, _("HOME"), p);
|
---|
| 1052 | }
|
---|
| 1053 | (void) sh_ext_tas_add_envv (&task, _("SHELL"),
|
---|
| 1054 | _("/bin/sh"));
|
---|
| 1055 | (void) sh_ext_tas_add_envv (&task, _("PATH"),
|
---|
| 1056 | _("/sbin:/usr/sbin:/bin:/usr/bin"));
|
---|
| 1057 | if (sh.timezone != NULL)
|
---|
| 1058 | {
|
---|
| 1059 | (void) sh_ext_tas_add_envv(&task, "TZ", sh.timezone);
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | if (!sh_prochk_pspath)
|
---|
| 1063 | sh_ext_tas_command(&task, PSPATH);
|
---|
| 1064 | else
|
---|
| 1065 | sh_ext_tas_command(&task, sh_prochk_pspath);
|
---|
| 1066 |
|
---|
| 1067 | (void) sh_ext_tas_add_argv(&task, _("ps"));
|
---|
| 1068 |
|
---|
| 1069 | if (!sh_prochk_psarg)
|
---|
| 1070 | {
|
---|
[143] | 1071 | #ifdef PS_THREADS
|
---|
[67] | 1072 | (void) sh_ext_tas_add_argv(&task, _("-eT"));
|
---|
| 1073 | #else
|
---|
| 1074 | (void) sh_ext_tas_add_argv(&task, PSARG);
|
---|
| 1075 | #endif
|
---|
| 1076 | }
|
---|
| 1077 | else
|
---|
| 1078 | {
|
---|
| 1079 | (void) sh_ext_tas_add_argv(&task, sh_prochk_psarg);
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
| 1082 | task.rw = 'r';
|
---|
| 1083 | task.fork_twice = S_FALSE;
|
---|
| 1084 |
|
---|
| 1085 | status = sh_ext_popen(&task);
|
---|
| 1086 | if (status != 0)
|
---|
| 1087 | {
|
---|
[398] | 1088 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 1089 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
| 1090 | _("Could not open pipe"), _("sh_processes_runps"));
|
---|
[398] | 1091 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 1092 | SL_RETURN ((-1), _("sh_processes_runps"));
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 | /* read from the open pipe
|
---|
| 1096 | */
|
---|
| 1097 | if (task.pipe != NULL)
|
---|
| 1098 | {
|
---|
| 1099 | retval = sh_processes_readps (task.pipe, res, str, len, flag, pid);
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
| 1102 | /* close pipe and return exit status
|
---|
| 1103 | */
|
---|
| 1104 | (void) sh_ext_pclose(&task);
|
---|
[103] | 1105 | sh_ext_tas_free (&task);
|
---|
[67] | 1106 | SL_RETURN ((retval), _("sh_processes_runps"));
|
---|
| 1107 | }
|
---|
| 1108 |
|
---|
[204] | 1109 | /* Check whether there is a visible process
|
---|
| 1110 | * with PID = i + 1024
|
---|
| 1111 | */
|
---|
[205] | 1112 | static size_t p_store = 0;
|
---|
| 1113 |
|
---|
[204] | 1114 | static int openvz_ok(short * res, size_t i)
|
---|
| 1115 | {
|
---|
[205] | 1116 |
|
---|
[204] | 1117 | if (sh_prochk_openvz == S_FALSE) {
|
---|
| 1118 | return 0;
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 | i += 1024;
|
---|
| 1122 |
|
---|
| 1123 | if (i >= sh_prochk_size) {
|
---|
| 1124 | return 0;
|
---|
| 1125 | }
|
---|
| 1126 |
|
---|
| 1127 | if ( ((res[i] & SH_PR_PS) || (res[i] & SH_PR_PS2)) && (res[i] & SH_PR_ANY))
|
---|
| 1128 | {
|
---|
| 1129 | /* This is a system process corresponding to a 'virtual'
|
---|
| 1130 | * process that has a PID offset by 1024
|
---|
| 1131 | */
|
---|
| 1132 | return 1;
|
---|
| 1133 | }
|
---|
| 1134 |
|
---|
[205] | 1135 | if (openvz_hidden > 0)
|
---|
[204] | 1136 | {
|
---|
[205] | 1137 | p_store = i;
|
---|
[204] | 1138 | --openvz_hidden;
|
---|
| 1139 | return 1;
|
---|
| 1140 | }
|
---|
[205] | 1141 | else if (i == p_store)
|
---|
| 1142 | {
|
---|
| 1143 | return 1;
|
---|
| 1144 | }
|
---|
[204] | 1145 |
|
---|
| 1146 | return 0;
|
---|
| 1147 | }
|
---|
| 1148 |
|
---|
[67] | 1149 | static int sh_process_check_int (short * res)
|
---|
| 1150 | {
|
---|
[170] | 1151 | volatile size_t i;
|
---|
| 1152 | size_t j;
|
---|
[67] | 1153 | char tests[512];
|
---|
[170] | 1154 | volatile int retval;
|
---|
[67] | 1155 |
|
---|
| 1156 | pid_t this_pid;
|
---|
| 1157 |
|
---|
| 1158 | SL_ENTER(_("sh_process_check_int"));
|
---|
| 1159 |
|
---|
| 1160 | this_pid = getpid();
|
---|
| 1161 |
|
---|
| 1162 | if (!res)
|
---|
| 1163 | {
|
---|
[143] | 1164 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 1165 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
[169] | 1166 | _("Internal error: NULL argument, switching off"),
|
---|
[67] | 1167 | _("sh_process_check_int"));
|
---|
[143] | 1168 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 1169 | SL_RETURN ((-1), _("sh_process_check_int"));
|
---|
| 1170 | }
|
---|
| 1171 |
|
---|
[169] | 1172 | retval = sh_processes_runps (res, NULL, 0, SH_PR_PS, 0);
|
---|
[398] | 1173 |
|
---|
[67] | 1174 | for (i = sh_prochk_minpid; i != sh_prochk_maxpid; ++i)
|
---|
| 1175 | {
|
---|
| 1176 | j = i - sh_prochk_minpid;
|
---|
| 1177 | res[j] = sh_processes_check ((pid_t) i, res[j]);
|
---|
| 1178 | }
|
---|
[398] | 1179 |
|
---|
[169] | 1180 | retval += sh_processes_runps (res, NULL, 0, SH_PR_PS2, 0);
|
---|
[67] | 1181 |
|
---|
[169] | 1182 | if (retval != 0)
|
---|
| 1183 | {
|
---|
| 1184 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 1185 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 1186 | _("Failed to run ps, switching off"),
|
---|
| 1187 | _("sh_process_check_int"));
|
---|
| 1188 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 1189 | SL_RETURN ((-1), _("sh_process_check_int"));
|
---|
| 1190 | }
|
---|
| 1191 |
|
---|
[67] | 1192 | /* Evaluate results
|
---|
| 1193 | */
|
---|
| 1194 | for (i = sh_prochk_minpid; i != sh_prochk_maxpid; ++i)
|
---|
| 1195 | {
|
---|
| 1196 | /* don't check the current process
|
---|
| 1197 | */
|
---|
| 1198 | if (i == (size_t) this_pid)
|
---|
| 1199 | continue;
|
---|
| 1200 |
|
---|
| 1201 | j = i - sh_prochk_minpid;
|
---|
| 1202 |
|
---|
| 1203 | if (((res[j] & SH_PR_PS) != 0) || ((res[j] & SH_PR_PS2) != 0))
|
---|
| 1204 | {
|
---|
| 1205 | res[j] |= SH_PR_PS_ANY;
|
---|
| 1206 | }
|
---|
| 1207 | else
|
---|
| 1208 | {
|
---|
| 1209 | res[j] &= ~SH_PR_PS_ANY;
|
---|
| 1210 | }
|
---|
| 1211 |
|
---|
| 1212 | tests[0] = '\0';
|
---|
| 1213 |
|
---|
| 1214 | if ((res[j] & SH_PR_ANY) || (res[j] & SH_PR_PS_ANY))
|
---|
| 1215 | {
|
---|
[102] | 1216 | /* list all tests where the pid was found
|
---|
| 1217 | */
|
---|
[67] | 1218 | sh_processes_tlist (tests, sizeof(tests), res[j]);
|
---|
[102] | 1219 |
|
---|
[67] | 1220 | /*
|
---|
| 1221 | * case 1: in ps and found
|
---|
| 1222 | */
|
---|
| 1223 | if ((res[j] & SH_PR_PS_ANY) && (res[j] & SH_PR_ANY))
|
---|
| 1224 | {
|
---|
[143] | 1225 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 1226 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_PCK_OK,
|
---|
| 1227 | (unsigned long) i, tests);
|
---|
[143] | 1228 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | /*
|
---|
| 1232 | * case 2: not in ps and found
|
---|
| 1233 | */
|
---|
| 1234 | else if ((res[j] & SH_PR_PS_ANY) == 0)
|
---|
| 1235 | {
|
---|
| 1236 | res[j] = sh_processes_check ((pid_t) i, 0);
|
---|
| 1237 | /*
|
---|
| 1238 | * if still there, it is real and hidden
|
---|
| 1239 | */
|
---|
[204] | 1240 | if ((res[j] & SH_PR_ANY) && !openvz_ok(res, j))
|
---|
[67] | 1241 | {
|
---|
| 1242 | if (S_FALSE == is_in_list(&list_hidden, NULL, i))
|
---|
| 1243 | {
|
---|
[78] | 1244 | char user[16];
|
---|
| 1245 | char * aout;
|
---|
| 1246 | char * safe;
|
---|
| 1247 |
|
---|
[143] | 1248 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[78] | 1249 | aout = get_user_and_path ((pid_t) i, user, sizeof(user));
|
---|
[143] | 1250 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[78] | 1251 |
|
---|
| 1252 | if (aout)
|
---|
| 1253 | {
|
---|
| 1254 | safe = sh_util_safe_name (aout);
|
---|
[143] | 1255 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[78] | 1256 | sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
|
---|
| 1257 | MSG_PCK_P_HIDDEN,
|
---|
| 1258 | (unsigned long) i, tests, safe, user);
|
---|
[143] | 1259 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[78] | 1260 | SH_FREE(safe);
|
---|
| 1261 | SH_FREE(aout);
|
---|
| 1262 | }
|
---|
| 1263 | else
|
---|
[143] | 1264 | {
|
---|
| 1265 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 1266 | sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
|
---|
| 1267 | MSG_PCK_HIDDEN,
|
---|
| 1268 | (unsigned long) i, tests);
|
---|
| 1269 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 1270 | }
|
---|
[67] | 1271 | }
|
---|
| 1272 | }
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
| 1275 | /*
|
---|
| 1276 | * case 3: in ps, but not found
|
---|
| 1277 | */
|
---|
| 1278 | else
|
---|
| 1279 | {
|
---|
| 1280 | if (((res[j] & SH_PR_PS) != 0) && ((res[j] & SH_PR_PS2) != 0))
|
---|
| 1281 | {
|
---|
| 1282 | if (S_FALSE == is_in_list(&list_fake, NULL, i))
|
---|
| 1283 | {
|
---|
[143] | 1284 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 1285 | sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
|
---|
| 1286 | MSG_PCK_FAKE,
|
---|
| 1287 | (unsigned long) i, tests);
|
---|
[143] | 1288 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[67] | 1289 | }
|
---|
| 1290 | }
|
---|
| 1291 | }
|
---|
| 1292 | }
|
---|
| 1293 | } /* loop end */
|
---|
| 1294 |
|
---|
| 1295 | check_watchlist (res);
|
---|
| 1296 |
|
---|
| 1297 | SL_RETURN (0, _("sh_process_check_int"));
|
---|
| 1298 | }
|
---|
| 1299 |
|
---|
| 1300 | /* Initialise.
|
---|
| 1301 | */
|
---|
[170] | 1302 | static int sh_prochk_init_internal(void)
|
---|
[67] | 1303 | {
|
---|
| 1304 | SL_ENTER(_("sh_prochk_init"));
|
---|
| 1305 |
|
---|
| 1306 | (void) proc_max_pid (&sh_prochk_maxpid);
|
---|
| 1307 |
|
---|
| 1308 | if (sh_prochk_minpid > sh_prochk_maxpid)
|
---|
| 1309 | ShProchkActive = S_FALSE;
|
---|
| 1310 |
|
---|
| 1311 | /* We need to free anything allocated by the configuration functions if
|
---|
| 1312 | * we find that the module is to be left inactive - otherwise _reconf()
|
---|
| 1313 | * won't quite work.
|
---|
| 1314 | */
|
---|
| 1315 | if( ShProchkActive == S_FALSE )
|
---|
| 1316 | {
|
---|
| 1317 | sh_prochk_free_list(process_check);
|
---|
| 1318 | process_check = NULL;
|
---|
| 1319 | SL_RETURN(-1, _("sh_prochk_init"));
|
---|
| 1320 | }
|
---|
| 1321 |
|
---|
| 1322 | sh_prochk_size = sh_prochk_maxpid - sh_prochk_minpid;
|
---|
| 1323 |
|
---|
[338] | 1324 | if (sh_prochk_res == NULL)
|
---|
| 1325 | {
|
---|
| 1326 | sh_prochk_res = SH_ALLOC(sizeof(short) * sh_prochk_size);
|
---|
| 1327 | }
|
---|
[67] | 1328 | memset (sh_prochk_res, 0, sizeof(short) * sh_prochk_size);
|
---|
| 1329 |
|
---|
| 1330 | SL_RETURN(0, _("sh_prochk_init"));
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
[143] | 1333 | int sh_prochk_init (struct mod_type * arg)
|
---|
| 1334 | {
|
---|
[210] | 1335 | #ifndef HAVE_PTHREAD
|
---|
| 1336 | (void) arg;
|
---|
| 1337 | #endif
|
---|
| 1338 |
|
---|
[143] | 1339 | if (ShProchkActive == S_FALSE)
|
---|
| 1340 | return SH_MOD_FAILED;
|
---|
| 1341 | #ifdef HAVE_PTHREAD
|
---|
[144] | 1342 | if (arg != NULL && arg->initval < 0 &&
|
---|
| 1343 | (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
|
---|
[143] | 1344 | {
|
---|
| 1345 | if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
|
---|
| 1346 | return SH_MOD_THREAD;
|
---|
| 1347 | else
|
---|
| 1348 | return SH_MOD_FAILED;
|
---|
| 1349 | }
|
---|
[335] | 1350 | else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
|
---|
| 1351 | (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
|
---|
| 1352 | {
|
---|
[338] | 1353 | sh_prochk_init_internal();
|
---|
[335] | 1354 | return SH_MOD_THREAD;
|
---|
| 1355 | }
|
---|
[143] | 1356 | #endif
|
---|
| 1357 | return sh_prochk_init_internal();
|
---|
| 1358 | }
|
---|
[67] | 1359 |
|
---|
| 1360 | int sh_prochk_timer(time_t tcurrent)
|
---|
| 1361 | {
|
---|
| 1362 | static time_t lastcheck = 0;
|
---|
| 1363 |
|
---|
| 1364 | SL_ENTER(_("sh_prochk_timer"));
|
---|
| 1365 | if ((time_t) (tcurrent - lastcheck) >= sh_prochk_interval)
|
---|
| 1366 | {
|
---|
| 1367 | lastcheck = tcurrent;
|
---|
| 1368 | SL_RETURN((-1), _("sh_prochk_timer"));
|
---|
| 1369 | }
|
---|
| 1370 | SL_RETURN(0, _("sh_prochk_timer"));
|
---|
| 1371 | }
|
---|
| 1372 |
|
---|
| 1373 | int sh_prochk_check(void)
|
---|
| 1374 | {
|
---|
[170] | 1375 | int status;
|
---|
[67] | 1376 |
|
---|
| 1377 | SL_ENTER(_("sh_prochk_check"));
|
---|
| 1378 |
|
---|
[143] | 1379 | SH_MUTEX_LOCK(mutex_proc_check);
|
---|
[170] | 1380 |
|
---|
| 1381 | status = 0;
|
---|
| 1382 |
|
---|
[67] | 1383 | if( ShProchkActive != S_FALSE )
|
---|
| 1384 | {
|
---|
[143] | 1385 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
[67] | 1386 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_PCK_CHECK,
|
---|
| 1387 | (unsigned long) sh_prochk_minpid,
|
---|
| 1388 | (unsigned long) (sh_prochk_maxpid-1));
|
---|
[143] | 1389 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
[103] | 1390 |
|
---|
| 1391 | if (sh_prochk_res) {
|
---|
| 1392 | memset (sh_prochk_res, 0, sizeof(short) * sh_prochk_size);
|
---|
| 1393 | }
|
---|
[67] | 1394 | status = sh_process_check_int(sh_prochk_res);
|
---|
[103] | 1395 |
|
---|
[67] | 1396 | if (status != 0)
|
---|
| 1397 | ShProchkActive = S_FALSE;
|
---|
| 1398 |
|
---|
| 1399 | /* clean out old entries which are not marked
|
---|
| 1400 | * as missing/hidden/fake anymore
|
---|
| 1401 | */
|
---|
| 1402 | clean_list (&list_missing);
|
---|
| 1403 | clean_list (&list_hidden);
|
---|
| 1404 | clean_list (&list_fake);
|
---|
| 1405 | }
|
---|
[183] | 1406 |
|
---|
[143] | 1407 | SH_MUTEX_UNLOCK(mutex_proc_check);
|
---|
[67] | 1408 |
|
---|
| 1409 | SL_RETURN(status, _("sh_prochk_check"));
|
---|
| 1410 | }
|
---|
| 1411 |
|
---|
| 1412 | /* Free our lists and the associated memory
|
---|
| 1413 | */
|
---|
| 1414 | int sh_prochk_cleanup(void)
|
---|
| 1415 | {
|
---|
| 1416 | SL_ENTER(_("sh_prochk_cleanup"));
|
---|
| 1417 |
|
---|
| 1418 | sh_prochk_reconf();
|
---|
| 1419 |
|
---|
| 1420 | if (list_missing) {
|
---|
| 1421 | kill_list(list_missing);
|
---|
| 1422 | list_missing = NULL;
|
---|
| 1423 | }
|
---|
| 1424 | if (list_hidden) {
|
---|
| 1425 | kill_list(list_hidden);
|
---|
| 1426 | list_hidden = NULL;
|
---|
| 1427 | }
|
---|
| 1428 | if (list_fake) {
|
---|
| 1429 | kill_list(list_fake);
|
---|
| 1430 | list_fake = NULL;
|
---|
| 1431 | }
|
---|
| 1432 |
|
---|
| 1433 | SL_RETURN(0, _("sh_prochk_cleanup"));
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
| 1436 | /* Free our lists and the associated memory
|
---|
| 1437 | */
|
---|
| 1438 | int sh_prochk_reconf(void)
|
---|
| 1439 | {
|
---|
| 1440 | SL_ENTER(_("sh_prochk_reconf"));
|
---|
| 1441 |
|
---|
[143] | 1442 | SH_MUTEX_LOCK(mutex_proc_check);
|
---|
[67] | 1443 | userdef_maxpid = 0;
|
---|
| 1444 | sh_prochk_maxpid = 0x8000;
|
---|
[144] | 1445 | sh_prochk_minpid = 0x0001;
|
---|
[67] | 1446 | sh_prochk_interval = SH_PROCHK_INTERVAL;
|
---|
[204] | 1447 | sh_prochk_openvz = S_FALSE;
|
---|
[205] | 1448 | p_store = 0;
|
---|
| 1449 | openvz_hidden = 0;
|
---|
[67] | 1450 |
|
---|
| 1451 | sh_prochk_free_list(process_check);
|
---|
| 1452 | process_check = NULL;
|
---|
| 1453 | if (sh_prochk_res != NULL)
|
---|
| 1454 | SH_FREE(sh_prochk_res);
|
---|
| 1455 | sh_prochk_res = NULL;
|
---|
| 1456 |
|
---|
| 1457 | if (sh_prochk_psarg)
|
---|
| 1458 | SH_FREE(sh_prochk_psarg);
|
---|
| 1459 | sh_prochk_psarg = NULL;
|
---|
| 1460 | if (sh_prochk_pspath)
|
---|
| 1461 | SH_FREE(sh_prochk_pspath);
|
---|
| 1462 | sh_prochk_pspath = NULL;
|
---|
[143] | 1463 | SH_MUTEX_UNLOCK(mutex_proc_check);
|
---|
[67] | 1464 |
|
---|
| 1465 | SL_RETURN(0, _("sh_prochk_reconf"));
|
---|
| 1466 | }
|
---|
| 1467 |
|
---|
| 1468 | /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
|
---|
| 1469 | #endif
|
---|
| 1470 |
|
---|
| 1471 | /* #ifdef SH_USE_PROCESSCHECK */
|
---|
| 1472 | #endif
|
---|
| 1473 |
|
---|
| 1474 |
|
---|
| 1475 | #ifdef SH_CUTEST
|
---|
| 1476 | #include "CuTest.h"
|
---|
| 1477 |
|
---|
| 1478 | void Test_processcheck_watchlist_ok (CuTest *tc) {
|
---|
| 1479 | #if defined(SH_USE_PROCESSCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
|
---|
| 1480 | CuAssertTrue(tc, 0 == sh_prochk_add_process("init"));
|
---|
| 1481 | CuAssertTrue(tc,
|
---|
| 1482 | S_TRUE == is_in_watchlist(" 1 ? 00:00:00 init", 0));
|
---|
| 1483 | CuAssertTrue(tc,
|
---|
| 1484 | S_FALSE == is_in_watchlist(" 1 ? 00:00:00 flix", 0));
|
---|
| 1485 | CuAssertTrue(tc,
|
---|
| 1486 | S_TRUE == is_in_watchlist("25218 ? SNs 0:01 /usr/sbin/init -k start -DSSL", 0));
|
---|
| 1487 | CuAssertTrue(tc,
|
---|
| 1488 | S_FALSE == is_in_watchlist("25218 ? SNs 0:01 /usr/sbin/apache2 -k start -DSSL", 0));
|
---|
| 1489 |
|
---|
| 1490 |
|
---|
| 1491 | sh_prochk_free_list(process_check);
|
---|
| 1492 | process_check = NULL;
|
---|
| 1493 | CuAssertTrue(tc, S_FALSE == is_in_watchlist("init", 0));
|
---|
| 1494 |
|
---|
| 1495 | CuAssertTrue(tc, 0 == sh_prochk_add_process("init"));
|
---|
| 1496 | CuAssertTrue(tc, 0 == sh_prochk_add_process("ssh"));
|
---|
| 1497 | CuAssertTrue(tc, 0 == sh_prochk_add_process("syslog"));
|
---|
| 1498 | CuAssertTrue(tc, S_TRUE == is_in_watchlist("init", 0));
|
---|
| 1499 | CuAssertTrue(tc, S_TRUE == is_in_watchlist("ssh", 0));
|
---|
| 1500 | CuAssertTrue(tc, S_TRUE == is_in_watchlist("syslog", 0));
|
---|
| 1501 |
|
---|
| 1502 | sh_prochk_free_list(process_check);
|
---|
| 1503 | process_check = NULL;
|
---|
| 1504 | CuAssertTrue(tc, S_FALSE == is_in_watchlist("init", 0));
|
---|
| 1505 | CuAssertTrue(tc, S_FALSE == is_in_watchlist("ssh", 0));
|
---|
| 1506 | CuAssertTrue(tc, S_FALSE == is_in_watchlist("syslog", 0));
|
---|
| 1507 | #else
|
---|
| 1508 | (void) tc; /* fix compiler warning */
|
---|
| 1509 | #endif
|
---|
| 1510 | return;
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
| 1513 | void Test_processcheck_listhandle_ok (CuTest *tc) {
|
---|
| 1514 | #if defined(SH_USE_PROCESSCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
|
---|
| 1515 | CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
|
---|
| 1516 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "init", 0));
|
---|
| 1517 | CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1518 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1519 |
|
---|
| 1520 | if (list_missing)
|
---|
| 1521 | kill_list(list_missing);
|
---|
| 1522 | list_missing = NULL;
|
---|
| 1523 |
|
---|
| 1524 | CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
|
---|
| 1525 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "init", 0));
|
---|
| 1526 | CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1527 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1528 |
|
---|
| 1529 | if (list_missing)
|
---|
| 1530 | kill_list(list_missing);
|
---|
| 1531 | list_missing = NULL;
|
---|
| 1532 |
|
---|
| 1533 | CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
|
---|
| 1534 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "init", 0));
|
---|
| 1535 | CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1536 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1537 |
|
---|
| 1538 | CuAssertTrue(tc, 2 == clean_list(&list_missing));
|
---|
| 1539 | CuAssertPtrNotNull(tc, list_missing);
|
---|
| 1540 |
|
---|
| 1541 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "init", 0));
|
---|
| 1542 | CuAssertTrue(tc, S_TRUE == is_in_list(&list_missing, "foobar", 0));
|
---|
| 1543 |
|
---|
| 1544 | CuAssertTrue(tc, 2 == clean_list(&list_missing));
|
---|
| 1545 | CuAssertPtrNotNull(tc, list_missing);
|
---|
| 1546 |
|
---|
| 1547 | CuAssertTrue(tc, 0 == clean_list(&list_missing));
|
---|
| 1548 | CuAssertTrue(tc, NULL == list_missing);
|
---|
| 1549 | #else
|
---|
| 1550 | (void) tc; /* fix compiler warning */
|
---|
| 1551 | #endif
|
---|
| 1552 | return;
|
---|
| 1553 | }
|
---|
| 1554 |
|
---|
| 1555 |
|
---|
| 1556 | /* #ifdef SH_CUTEST */
|
---|
| 1557 | #endif
|
---|
| 1558 |
|
---|