summaryrefslogtreecommitdiffstats
path: root/src/sh_processcheck.c
blob: f5601c91a9f59f718ba2d233aaff52babd119396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
/* SAMHAIN file system integrity testing                                   */
/* Copyright (C) 2006 Rainer Wichmann                                      */
/*                                                                         */
/*  This program is free software; you can redistribute it                 */
/*  and/or modify                                                          */
/*  it under the terms of the GNU General Public License as                */
/*  published by                                                           */
/*  the Free Software Foundation; either version 2 of the License, or      */
/*  (at your option) any later version.                                    */
/*                                                                         */
/*  This program is distributed in the hope that it will be useful,        */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/*  GNU General Public License for more details.                           */
/*                                                                         */
/*  You should have received a copy of the GNU General Public License      */
/*  along with this program; if not, write to the Free Software            */
/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */

/***************************************************************************
 *
 * This file provides a module for samhain to check for hidden/faked/missing
 * processes on the host.
 *
 */

#include "config_xor.h"

/* changed from 500 to 600 b/o FreeBSD (see sys/cdefs.h) 
 * which needs _POSIX_C_SOURCE >= 200112 for lstat()
 */
#if defined(__sun) || defined(__sun__) || defined(sun)
#define _XOPEN_SOURCE 500
#else
#define _XOPEN_SOURCE 600
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <unistd.h>

#ifdef _POSIX_PRIORITY_SCHEDULING
#include <sched.h>
#endif

#ifdef HAVE_GETPRIORITY
#include <errno.h>
#include <sys/resource.h>
#endif

#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif


#ifdef HAVE_REGEX_H
#include <regex.h>
#endif

#include "samhain.h"
#include "sh_modules.h"
#include "sh_processcheck.h"
#include "sh_utils.h"
#include "sh_error.h"
#include "sh_extern.h"
#include "sh_calls.h"
#include "sh_pthread.h"

#ifdef SH_USE_PROCESSCHECK

#define FIL__  _("sh_processcheck.c")

#ifdef __linux__
#define PS_THREADS
#endif

/* We won't want to build this into yule 
 */
#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)

SH_MUTEX_STATIC(mutex_proc_check, PTHREAD_MUTEX_INITIALIZER);

/* sh_prochk_maxpid is one more than the largest pid
 */
static  size_t  sh_prochk_minpid = 0x0001;
static  size_t  sh_prochk_maxpid = 0x8000;
static  size_t  sh_prochk_size   = 0;

static  int     ShProchkActive  = S_TRUE;
static  short * sh_prochk_res   = NULL; 

static  char  * sh_prochk_pspath = NULL;
static  char  * sh_prochk_psarg  = NULL;

#define SH_PROCHK_INTERVAL 300
static time_t   sh_prochk_interval = SH_PROCHK_INTERVAL;
static int      sh_prochk_severity = SH_ERR_SEVERE;
static int      sh_prochk_openvz   = S_FALSE;

static int sh_prochk_set_maxpid  (const char * str);
static int sh_prochk_set_minpid  (const char * str);
static int sh_prochk_set_active  (const char *str);
static int sh_prochk_add_process (const char *str);
static int sh_prochk_set_pspath  (const char *str);
static int sh_prochk_set_psarg   (const char *str);
static int sh_prochk_set_interval(const char *str);
static int sh_prochk_set_severity(const char *str);
static int sh_prochk_set_openvz  (const char *str);

sh_rconf sh_prochk_table[] = {
    {
        N_("severityprocesscheck"),
        sh_prochk_set_severity,
    },
    {
        N_("processcheckexists"),
        sh_prochk_add_process,
    },
    {
        N_("processcheckactive"),
        sh_prochk_set_active,
    },
    {
        N_("processcheckminpid"),
        sh_prochk_set_minpid,
    },
    {
        N_("processcheckmaxpid"),
        sh_prochk_set_maxpid,
    },
    {
        N_("processcheckpspath"),
        sh_prochk_set_pspath,
    },
    {
        N_("processcheckpsarg"),
        sh_prochk_set_psarg,
    },
    {
        N_("processcheckinterval"),
        sh_prochk_set_interval,
    },
    {
        N_("processcheckisopenvz"),
        sh_prochk_set_openvz,
    },
    {
        NULL,
        NULL
    }
};

#define    SH_PROC_MISSING 1
#define    SH_PROC_FAKED   2
#define    SH_PROC_HIDDEN  4
#define    SH_PROC_EXISTS  8

#ifndef HAVE_LSTAT
#define lstat(x,y) stat(x,y)
#endif /* HAVE_LSTAT */

#if defined(S_IFLNK) && !defined(S_ISLNK)
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
#else
#if !defined(S_ISLNK)
#define S_ISLNK(mode) (0)
#endif
#endif

static const short SH_PR_PS       = 0x0001;

static const short SH_PR_GETSID   = 0x0002;
static const short SH_PR_KILL     = 0x0004;
static const short SH_PR_GETPGID  = 0x0008;

static const short SH_PR_LSTAT    = 0x0010;
static const short SH_PR_OPENDIR  = 0x0020;
static const short SH_PR_CHDIR    = 0x0040;
static const short SH_PR_SCHED    = 0x0080;

static const short SH_PR_PRIORITY = 0x0100;
static const short SH_PR_STATVSF  = 0x0200;

static const short SH_PR_PS2      = 0x1000;
static const short SH_PR_PS_ANY   = 0x2000;
static const short SH_PR_ALL      = 0x4000;
static const short SH_PR_ANY      = 0x8000;

/* /proc: 
 *        linux:     /proc/pid/exe
 *        freebsd:   /proc/pid/file
 *        solaris10: /proc/pid/path/a.out
 */
static char * get_user_and_path (pid_t pid, char * user, size_t usrlen)
{
  extern char *  sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);

  char        path[128];
  char *      buf;
  struct stat sbuf;
  int         len;
  char *      tmp;

  sl_snprintf (path, sizeof(path), _("/proc/%ld/exe"), (unsigned long) pid);

  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
    {
      goto linkread;
    }

  sl_snprintf (path, sizeof(path), _("/proc/%ld/file"), (unsigned long) pid);

  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
    {
      goto linkread;
    }

  sl_snprintf (path, sizeof(path), _("/proc/%ld/path/a.out"), (unsigned long) pid);

  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
    {
      goto linkread;
    }

  return NULL;

 linkread:

  buf = SH_ALLOC(PATH_MAX);
  len = readlink(path, buf, PATH_MAX);   /* flawfinder: ignore */
  len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;

  if (len > 0)
    { 
      buf[len] = '\0';
    }
  else
    {
      SH_FREE(buf);
      return NULL;
    }

  tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, user, usrlen);

  if (!tmp)
    sl_snprintf (user, usrlen, "%ld", (unsigned long) sbuf.st_uid);

  return buf;
}


struct watchlist {
  char        * str;
  unsigned long pid;
#ifdef HAVE_REGEX_H
  regex_t       preg;
#endif
  int           seen;

  struct watchlist *next;
};

static struct watchlist * process_check = NULL;

static struct watchlist * list_missing  = NULL;
static struct watchlist * list_fake     = NULL;
static struct watchlist * list_hidden   = NULL;

/* recursively remove all list entries
 */
static void kill_list (struct watchlist * head)
{
  if (head->next)
    kill_list (head->next);

  if (head->str)
    SH_FREE(head->str);
  SH_FREE(head);

  return;
}

  
/* check the list for old entries; clean out old entries; reset others
 * Return number of non-obsolete entries
 */
static size_t clean_list (struct watchlist ** head_ptr)
{
  size_t count = 0;
  struct watchlist * ptr = *head_ptr;
  struct watchlist * pre = *head_ptr;

  while (ptr)
    {
      if (ptr->seen == S_FALSE) /* obsolete entry */
	{
	  if (ptr == pre)       /* at head        */
	    {
	      ptr       = pre->next;
	      *head_ptr = pre->next;
	      if (pre->str) 
		SH_FREE(pre->str);
	      SH_FREE(pre);
	      pre       = ptr;
	    }
	  else
	    {
	      pre->next = ptr->next;
	      if (ptr->str) 
		SH_FREE(ptr->str);
	      SH_FREE(ptr);
	      ptr       = pre->next;
	    }
	}
      else
	{
	  ++count;
	  ptr->seen = S_FALSE; /* reset status */
	  pre = ptr;
	  ptr = ptr->next;
	}
    }
  return count;
}

/* check if process is in list; if not, add it and return false
 */
static int  is_in_list (struct watchlist ** head_ptr, 
			char * str, unsigned long pid)
{
  struct watchlist * ptr = *head_ptr;

  if (str)
    {
      while (ptr)
	{
	  if (ptr->str && (0 == strcmp(str, ptr->str)))
	    {
	      ptr->seen = S_TRUE;
	      return S_TRUE;
	    }
	  ptr = ptr->next;
	}
    }
  else
    {
      while (ptr)
	{
	  if (ptr->pid == pid)
	    {
	      ptr->seen = S_TRUE;
	      return S_TRUE;
	    }
	  ptr = ptr->next;
	}
    }

  ptr = SH_ALLOC(sizeof(struct watchlist));

  if (str)
    {
      ptr->str = sh_util_strdup(str);
    }
  else
    {
      ptr->str = NULL;
      ptr->pid = pid;
    }
  ptr->next = *head_ptr;
  ptr->seen = S_TRUE;
  *head_ptr = ptr;

  return S_FALSE;
}

static int is_in_watchlist (const char *str, unsigned long num)
{
  struct watchlist * list = process_check;

  while (list) 
    {
#ifdef HAVE_REGEX_H
      if (0 == regexec(&(list->preg), str, 0, NULL, 0))
	{
	  list->seen = S_TRUE;
	  list->pid  = num;
	  return S_TRUE;
	}
#else
      if (strstr(str, list->str)) 
	{
	  list->seen = S_TRUE;
	  list->pid  = num;
	  return S_TRUE;
	}
#endif
      list = list->next;
    }
  return S_FALSE;
} 

/* These variables are not used anywhere. They only exist
 * to assign &userold, &user to them, which keeps gcc from
 * putting them into a register, and avoids the 'clobbered
 * by longjmp' warning. And no, 'volatile' proved insufficient.
 */
void * sh_dummy_413_watchlist = NULL;

static void check_watchlist (short * res)
{
  struct watchlist * list = process_check;
  char * tmp;
  size_t indx;

  /* Take the address to keep gcc from putting them into registers. 
   * Avoids the 'clobbered by longjmp' warning. 
   */
  sh_dummy_413_watchlist = (void*) &list;

  while (list) 
    {
      if (list->seen == S_FALSE)
	{
	  /* avoid repetition of messages
	   */
	  if (S_FALSE == is_in_list(&list_missing, list->str, 0))
	    {
	      SH_MUTEX_LOCK(mutex_thread_nolog);
	      tmp = sh_util_safe_name (list->str);
	      sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
			      MSG_PCK_MISS,
			      tmp);
	      SH_FREE(tmp);
	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
	    }
	}
      else
	{
	  indx = list->pid - sh_prochk_minpid;

	  if (list->pid < sh_prochk_maxpid && list->pid >= sh_prochk_minpid && 
	      ((res[indx] & SH_PR_ANY) == 0) && /* not found         */
	      ((res[indx] & SH_PR_PS)  != 0) && /* seen in first ps  */ 
	      ((res[indx] & SH_PR_PS2) != 0))   /* seen in second ps */
	    {
	      /* fake process, thus considered missing
	       */
	      if (S_FALSE == is_in_list(&list_missing, list->str, 0))
		{
		  SH_MUTEX_LOCK(mutex_thread_nolog);
		  tmp = sh_util_safe_name (list->str);
		  sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
				  MSG_PCK_MISS, 
				  tmp);
		  SH_FREE(tmp);
		  SH_MUTEX_UNLOCK(mutex_thread_nolog);
		}
	    }
	  list->seen = S_FALSE;
	}
      list = list->next;
    }

  sh_dummy_413_watchlist = NULL;
  return;
}

/* Add 'str' to the list of watched processes for which
 * existence should be checked.
 */
int sh_prochk_add_process (const char *str) 
{
  struct watchlist *new;
  int               status;
  char              errbuf[256];
    
  SL_ENTER(_("sh_prochk_add_process"));

  if( str == NULL )
    SL_RETURN(-1, _("sh_prochk_add_process") );

  new       = SH_ALLOC(sizeof(struct watchlist));
  new->next = process_check;
  new->str  = sh_util_strdup(str);
#ifdef HAVE_REGEX_H
  status = regcomp(&(new->preg), str, REG_NOSUB|REG_EXTENDED);
  if (status != 0)
    {
      regerror(status, &(new->preg), errbuf, sizeof(errbuf));
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, 
		      errbuf, _("sh_processes_add_process"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
      SH_FREE(new->str);
      SH_FREE(new);
      SL_RETURN(-1, _("sh_prochk_add_process") );
    }
#endif
  new->pid  = 0;
  new->seen = S_FALSE;

  process_check = new;
  SL_RETURN(0, _("sh_prochk_add_process") );
}

/* severity
 */
int sh_prochk_set_severity  (const char * c)
{
  char tmp[32];
  tmp[0] = '='; tmp[1] = '\0';
  sl_strlcat (tmp, c, 32);
  return sh_error_set_level (tmp, &sh_prochk_severity);
}



/* Path to ps
 */
int sh_prochk_set_pspath(const char *str) 
{
  SL_ENTER(_("sh_prochk_set_pspath"));

  if (!str || ('/' != str[0]))
    SL_RETURN((-1), _("sh_prochk_set_pspath"));
  if (sh_prochk_pspath)
    SH_FREE(sh_prochk_pspath);
#ifdef SH_EVAL_SHELL
  sh_prochk_pspath = sh_util_strdup (str);
  SL_RETURN((0), _("sh_prochk_set_pspath"));
#else
  sh_prochk_pspath = NULL;
  SL_RETURN((-1), _("sh_prochk_set_pspath"));
#endif
}

/* argument for ps
 */
int sh_prochk_set_psarg(const char *str) 
{
  SL_ENTER(_("sh_prochk_set_psarg"));

  if (sh_prochk_psarg)
    SH_FREE(sh_prochk_psarg);
#ifdef SH_EVAL_SHELL
  sh_prochk_psarg = sh_util_strdup (str);
  SL_RETURN((0), _("sh_prochk_set_psarg"));
#else
  (void) str;
  sh_prochk_psarg = NULL;
  SL_RETURN((-1), _("sh_prochk_set_psarg"));
#endif
}


/* Decide if we're active.
 */
int sh_prochk_set_active(const char *str) 
{
  int value;
    
  SL_ENTER(_("sh_prochk_set_active"));

  value = sh_util_flagval(str, &ShProchkActive);

  SL_RETURN((value), _("sh_prochk_set_active"));
}

/* Are we on openvz.
 */
static int openvz_hidden = 0;

int sh_prochk_set_openvz(const char *str) 
{
  int value;
    
  SL_ENTER(_("sh_prochk_set_openvz"));

  value = sh_util_flagval(str, &sh_prochk_openvz);

  if (sh_prochk_openvz != S_FALSE) {
    openvz_hidden = 1;
  }

  SL_RETURN((value), _("sh_prochk_set_openvz"));
}

/* Minimum PID
 */
int sh_prochk_set_minpid(const char * str)
{
  size_t  value;
  char * foo;
  int    retval = 0;

  SL_ENTER(_("sh_prochk_set_minpid"));

  value = (size_t) strtoul(str, &foo, 0);
  if (*foo != '\0')
    retval = -1;
  else
    sh_prochk_minpid = value;

  SL_RETURN((retval), _("sh_prochk_set_minpid"));
}

/* Maximum PID
 */
static int userdef_maxpid = 0;

int sh_prochk_set_maxpid(const char * str)
{
  size_t  value;
  char * foo;
  int    retval = -1;

  SL_ENTER(_("sh_prochk_set_maxpid"));

  value = (size_t) strtoul(str, &foo, 0);

  if (*foo == '\0' && S_TRUE == sl_ok_adds(value, 1)) {
    sh_prochk_maxpid = value + 1;
    userdef_maxpid   = 1;
    retval = 0;
  }

  SL_RETURN((retval), _("sh_prochk_set_maxpid"));
}

int sh_prochk_set_interval (const char * c)
{
  int retval = 0;
  long val;

  SL_ENTER(_("sh_prochk_set_interval"));
  val = strtol (c, (char **)NULL, 10);
  if (val <= 0)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
		       _("process check interval"), c);
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
      retval = -1;
    }
  else
    {
      sh_prochk_interval = (time_t) val;
    }
  SL_RETURN(retval, _("sh_prochk_set_interval"));
}



/* Recurse to the end of the list and then free the data as we return
 * back up towards the start, making sure to free any strdupped strings
 */
static void sh_prochk_free_list(struct watchlist *head) 
{
  if ( head != NULL ) 
    {
      sh_prochk_free_list(head->next);
      if (head->str)
	SH_FREE(head->str);
#ifdef HAVE_REGEX_H
      regfree(&(head->preg));
#endif
      SH_FREE(head);
    }
  return;
}

#if defined(__linux__)
#define PROC_PID_MAX _("/proc/sys/kernel/pid_max")

static int proc_max_pid (size_t * procpid)
{
  char * ret;
  unsigned long  pid;
  FILE * fd;
  char   str[128];
  char * ptr;

  SL_ENTER(_("proc_max_pid"));

  if (userdef_maxpid != 0)
    SL_RETURN((-1), _("proc_max_pid"));
    
  if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
    {
      if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
	{
	  str[0] = '\0';
	  ret = fgets(str, 128, fd);
	  if (ret && *str != '\0')
	    {
	      pid = strtoul(str, &ptr, 0);
	      if (*ptr == '\0' || *ptr == '\n')
		{
		  sl_fclose(FIL__, __LINE__, fd);
		  *procpid = (size_t) pid;
		  SL_RETURN(0, _("proc_max_pid"));
		}
	    }
	  sl_fclose(FIL__, __LINE__, fd);
	}
    }
  SL_RETURN((-1), _("proc_max_pid"));
}
#else
static int proc_max_pid(size_t * dummy)
{
  (void) dummy;
  return -1;
}
#endif

static void sh_processes_tlist (char * list, size_t len, short res)
{
  if (res & SH_PR_PS)       sl_strlcat(list, _(" ps(initial)"), len);
  if (res & SH_PR_CHDIR)    sl_strlcat(list, _(" chdir"), len);
  if (res & SH_PR_OPENDIR)  sl_strlcat(list, _(" opendir"), len);
  if (res & SH_PR_LSTAT)    sl_strlcat(list, _(" lstat"), len);
  if (res & SH_PR_PRIORITY) sl_strlcat(list, _(" getpriority"), len);
  if (res & SH_PR_SCHED)    sl_strlcat(list, _(" sched_getparam"), len);
  if (res & SH_PR_GETSID)   sl_strlcat(list, _(" getsid"), len);
  if (res & SH_PR_GETPGID)  sl_strlcat(list, _(" getpgid"), len);
  if (res & SH_PR_KILL)     sl_strlcat(list, _(" kill"), len);
  if (res & SH_PR_STATVSF)  sl_strlcat(list, _(" statvfs"), len);
  if (res & SH_PR_PS2)      sl_strlcat(list, _(" ps(final)"), len);
  return;
}


static short sh_processes_check (pid_t pid, short res)
{
  int  have_checks = 0;
  int  need_checks = 0;
#ifdef HAVE_PROCFS
  char path[128];
  struct stat buf;
  DIR * dir;
  int  retval;
#if defined(HAVE_STATVFS) && !defined(__FreeBSD__)
  struct statvfs vfsbuf;
#endif
#endif

#if !defined(sun) && !defined(__sun) && !defined(__sun__)
#ifdef _POSIX_PRIORITY_SCHEDULING
  struct sched_param p;
#endif
#endif

  if (0 == kill(pid, 0))
    { 
      res |= SH_PR_KILL;    res |= SH_PR_ANY; ++have_checks;
      ++need_checks;
    }
  else if (errno != EPERM)
    {
      ++need_checks;
    }


#ifdef HAVE_GETPGID
  if ((pid_t)-1 != getpgid(pid))
    { 
      res |= SH_PR_GETPGID; res |= SH_PR_ANY; ++have_checks;
    }
  ++need_checks;
#endif

#ifdef HAVE_GETSID
  if ((pid_t)-1 != getsid(pid))
    { 
      res |= SH_PR_GETSID;  res |= SH_PR_ANY; ++have_checks;
    }
  ++need_checks;
#endif

  /* sched_getparam() is broken on solaris 10, may segfault in librt
   */
#if !defined(sun) && !defined(__sun) && !defined(__sun__)
#ifdef _POSIX_PRIORITY_SCHEDULING
  if (0 == sched_getparam (pid, &p))
    { 
      res |= SH_PR_SCHED;   res |= SH_PR_ANY; ++have_checks;
    }
  ++need_checks;
#endif
#endif

#ifdef HAVE_GETPRIORITY
  errno = 0;
  if (((-1) == getpriority (PRIO_PROCESS, (int) pid)) && (errno == ESRCH));
  else
    { 
      res |= SH_PR_PRIORITY; res |= SH_PR_ANY; ++have_checks;
    }
  ++need_checks;
#endif

#ifdef HAVE_PROCFS
  sl_snprintf (path, sizeof(path), "/proc/%ld", (unsigned long) pid);

  do {
    retval = lstat (path, &buf);
  } while (retval < 0 && errno == EINTR);

  if (0 == retval)
    { 
      res |= SH_PR_LSTAT;   res |= SH_PR_ANY; ++have_checks;
    }
  ++need_checks;

  if (NULL != (dir = opendir(path)))
    {
      res |= SH_PR_OPENDIR; res |= SH_PR_ANY; ++have_checks;
      closedir(dir);
    }
  ++need_checks;

#if defined(HAVE_STATVFS) && !defined(__FreeBSD__)
  do {
    retval = statvfs (path, &vfsbuf);
  } while (retval < 0 && errno == EINTR);

  if (0 == retval)
    { 
      res |= SH_PR_STATVSF;   res |= SH_PR_ANY; ++have_checks;
    }
  ++need_checks;
#endif

#if !defined(SH_PROFILE)
  if (0 == chdir(path))
    {
      res |= SH_PR_CHDIR;   res |= SH_PR_ANY; ++have_checks;
      do {
	retval = chdir ("/");
      } while (retval < 0 && errno == EINTR);
    }
  ++need_checks;
#endif
#endif

  if (have_checks == need_checks)
    {
      res |= SH_PR_ALL;
    }
  return res;
}

extern int flag_err_debug;

static int sh_processes_readps (FILE * in, short * res, 
				char * str, size_t len, 
				short flag, pid_t pid)
{
  int  cc; 
  volatile unsigned int  lnum   = 0;
  volatile unsigned long num    = 0;
  char c;
  unsigned int  pos = 0;
#define SH_TWAIT_MAX 60
  volatile unsigned int  twait = 0;
  char tstr[256];
  enum { SKIP_TO_WS, SKIP_WS, SKIP_TO_WS2, SKIP_WS2, GET_NUM, SKIP_END, GET_NUM2 } line;

  SL_ENTER(_("sh_processes_readps"));

  if (!in) {
    SL_RETURN((-1), _("sh_processes_readps"));
  }

  tstr[(sizeof(tstr)-1)] = '\0';
  tstr[0]                = '\0';
  line = SKIP_END;		/* Skip 1st line */

  do
    {
      cc = fgetc(in);

      if (EOF == cc) 
	{
	  if (feof(in))
	    {
	      break;
	    }
	  else if ((errno == EAGAIN) && (twait < SH_TWAIT_MAX))
	    {
	      clearerr(in);
	      retry_msleep(1, 0);
	      ++twait;
	      continue;
	    }
#ifdef HOST_IS_OPENBSD
	  else if (errno == ENODEV)
	    {
	      clearerr(in);
	      continue;
	    }
#endif
	  else
	    {
	      char errbuf[SH_ERRBUF_SIZE];

	      SH_MUTEX_LOCK(mutex_thread_nolog);
	      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, errno, MSG_E_SUBGEN,
			      sh_error_message(errno, errbuf, sizeof(errbuf)),
			      _("sh_processes_readps"));
	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
	      break;
	    }
	}

      c = (char) cc;

      if (pos < (sizeof(tstr)-1))
	{ 
	  tstr[pos] = c; ++pos; 
	}

      switch(line)
	{
	case SKIP_END:
	  if (c == '\n')
	    { 
	      tstr[pos-1] = '\0';
	      if (flag_err_debug == S_TRUE)
		{
		  SH_MUTEX_LOCK(mutex_thread_nolog);
		  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, num, 
				  MSG_E_SUBGEN,
				  tstr,
				  _("sh_processes_readps"));
		  SH_MUTEX_UNLOCK(mutex_thread_nolog);
		}
	      /* fprintf(stderr, "<%ld> %s\n", num, tstr); */
	      line = SKIP_WS; pos = 0;
	      if (str != NULL && num == (unsigned long) pid)
		sl_strlcpy(str, tstr, len);
	      if (lnum != 0)
		is_in_watchlist (tstr, num);
	      ++lnum;
	    }
	  break;
	case SKIP_TO_WS:
	  if (!isspace(cc))
	    break;
	  line = SKIP_WS;
	  /* fallthrough */
	case SKIP_WS:
	  if (isspace(cc))
	    break;
	  num  = 0;
	  line = GET_NUM;
	  /* fallthrough */
	case GET_NUM:
	  if (isdigit(cc))
	    {
	      num = num * 10 + (c - '0');
	      break;
	    }
	  else if (isspace(cc))
	    {
#ifdef PS_THREADS
	      num  = 0;
	      line = SKIP_WS2;
#else
	      if (num < sh_prochk_maxpid && num >= sh_prochk_minpid)
		{
		  res[num - sh_prochk_minpid] |= flag;
		}
	      line = SKIP_END;
#endif
	      break;
	    }
	  else
	    {
	      line = SKIP_TO_WS;
	      break;
	    }
	case SKIP_TO_WS2:
	  if (!isspace(cc))
	    break;
	  line = SKIP_WS2;
	  /* fallthrough */
	case SKIP_WS2:
	  if (isspace(cc))
	    break;
	  num  = 0;
	  line = GET_NUM2;
	  /* fallthrough */
	case GET_NUM2:
	  if (isdigit(cc))
	    {
	      num = num * 10 + (c - '0');
	      break;
	    }
	  else if (isspace(cc))
	    {
	      if (num < sh_prochk_maxpid && num >= sh_prochk_minpid)
		{
		  res[num - sh_prochk_minpid] |= flag;
		}
	      line = SKIP_END;
	      break;
	    }
	  else
	    {
	      line = SKIP_TO_WS2;
	      break;
	    }
	default:
	  SL_RETURN ((-1), _("sh_processes_readps"));
	}
    } while (1);

  if (ferror(in))
    {
      SL_RETURN ((-1), _("sh_processes_readps"));
    }

  SL_RETURN ((0), _("sh_processes_readps"));
}

static int sh_processes_runps (short * res, char * str, size_t len, 
			       short flag, pid_t pid)
{
  sh_tas_t task;

  int    status = 0;
  char * p;
  int retval = 0;
  char  dir[SH_PATHBUF];

  SL_ENTER(_("sh_processes_runps"));

  sh_ext_tas_init(&task);
  p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid, dir, sizeof(dir));
  if (p)
    {
      (void) sh_ext_tas_add_envv (&task, _("HOME"), p);
    }
  (void) sh_ext_tas_add_envv (&task, _("SHELL"), 
			      _("/bin/sh")); 
  (void) sh_ext_tas_add_envv (&task, _("PATH"),  
			      _("/sbin:/usr/sbin:/bin:/usr/bin")); 
  if (sh.timezone != NULL)
    {
      (void) sh_ext_tas_add_envv(&task,  "TZ", sh.timezone);
    }

  if (!sh_prochk_pspath)
    sh_ext_tas_command(&task,  PSPATH);
  else
    sh_ext_tas_command(&task,  sh_prochk_pspath);

  (void) sh_ext_tas_add_argv(&task,  _("ps"));

  if (!sh_prochk_psarg)
    {
#ifdef PS_THREADS
      (void) sh_ext_tas_add_argv(&task,  _("-eT"));
#else
      (void) sh_ext_tas_add_argv(&task,  PSARG);
#endif
    }
  else
    {
      (void) sh_ext_tas_add_argv(&task,  sh_prochk_psarg);
    }

  task.rw = 'r';
  task.fork_twice = S_FALSE;

  status = sh_ext_popen(&task);
  if (status != 0)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN, 
		      _("Could not open pipe"), _("sh_processes_runps"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
      SL_RETURN ((-1), _("sh_processes_runps"));
    }

  /* read from the open pipe
   */
  if (task.pipe != NULL)
    {
      retval = sh_processes_readps (task.pipe, res, str, len, flag, pid);
    }

  /* close pipe and return exit status
   */
  (void) sh_ext_pclose(&task);
  sh_ext_tas_free (&task);
  SL_RETURN ((retval), _("sh_processes_runps"));
}

/* Check whether there is a visible process
 * with PID = i + 1024
 */
static size_t p_store = 0;

static int openvz_ok(short * res, size_t i)
{

  if (sh_prochk_openvz == S_FALSE) {
    return 0;
  }

  i += 1024;

  if (i >= sh_prochk_size) {
    return 0;
  }

  if ( ((res[i] & SH_PR_PS) || (res[i] & SH_PR_PS2)) && (res[i] & SH_PR_ANY))
    {
      /* This is a system process corresponding to a 'virtual'
       * process that has a PID offset by 1024
       */
      return 1;
    }

  if (openvz_hidden > 0)
    {
      p_store = i;
      --openvz_hidden;
      return 1;
    }
  else if (i == p_store)
    {
      return 1;
    }

  return 0;
}

static int sh_process_check_int (short * res)
{
  volatile size_t i;
  size_t j;
  char  tests[512];
  volatile int   retval;

  pid_t this_pid;

  SL_ENTER(_("sh_process_check_int"));

  this_pid = getpid();

  if (!res)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
		      _("Internal error: NULL argument, switching off"), 
		      _("sh_process_check_int"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
      SL_RETURN ((-1), _("sh_process_check_int"));
    }

  retval = sh_processes_runps (res, NULL, 0, SH_PR_PS, 0);

  for (i = sh_prochk_minpid; i != sh_prochk_maxpid; ++i)
    {
      j      = i - sh_prochk_minpid; 
      res[j] = sh_processes_check ((pid_t) i, res[j]);
    }

  retval += sh_processes_runps (res, NULL, 0, SH_PR_PS2, 0);

  if (retval != 0)
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
		      _("Failed to run ps, switching off"), 
		      _("sh_process_check_int"));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);
      SL_RETURN ((-1), _("sh_process_check_int"));
    }

  /* Evaluate results
   */
  for (i = sh_prochk_minpid; i != sh_prochk_maxpid; ++i)
    {
      /* don't check the current process
       */
      if (i == (size_t) this_pid)
	continue;

      j      = i - sh_prochk_minpid;

      if (((res[j] & SH_PR_PS) != 0) || ((res[j] & SH_PR_PS2) != 0))
	{
	  res[j] |= SH_PR_PS_ANY;
	}
      else
	{
	  res[j] &= ~SH_PR_PS_ANY;
	}

      tests[0] = '\0';

      if ((res[j] & SH_PR_ANY) || (res[j] & SH_PR_PS_ANY))
	{
	  /* list all tests where the pid was found
	   */
	  sh_processes_tlist (tests, sizeof(tests), res[j]);

	  /* 
	   * case 1: in ps and found 
	   */
	  if ((res[j] & SH_PR_PS_ANY) && (res[j] & SH_PR_ANY))
	    {
	      SH_MUTEX_LOCK(mutex_thread_nolog);
	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_PCK_OK, 
			      (unsigned long) i, tests);
	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
	    }

	  /* 
	   * case 2: not in ps and found
	   */
	  else if ((res[j] & SH_PR_PS_ANY) == 0) 
	    {
	      res[j] = sh_processes_check ((pid_t) i, 0);
	      /*
	       * if still there, it is real and hidden
	       */
	      if ((res[j] & SH_PR_ANY) && !openvz_ok(res, j))
		{
		  if (S_FALSE == is_in_list(&list_hidden, NULL, i))
		    {
		      char   user[16];
		      char * aout;
		      char * safe;

		      SH_MUTEX_LOCK(mutex_thread_nolog);
		      aout = get_user_and_path ((pid_t) i, user, sizeof(user));
		      SH_MUTEX_UNLOCK(mutex_thread_nolog);

		      if (aout)
			{
			  safe = sh_util_safe_name (aout);
			  SH_MUTEX_LOCK(mutex_thread_nolog);
			  sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
					  MSG_PCK_P_HIDDEN,
					  (unsigned long) i, tests, safe, user);
			  SH_MUTEX_UNLOCK(mutex_thread_nolog);
			  SH_FREE(safe);
			  SH_FREE(aout);
			}
		      else
			{
			  SH_MUTEX_LOCK(mutex_thread_nolog);
			  sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
					  MSG_PCK_HIDDEN,
					  (unsigned long) i, tests);
			  SH_MUTEX_UNLOCK(mutex_thread_nolog);
			}
		    }
		}
	    }

	  /*
	   * case 3: in ps, but not found
	   */
	  else
	    {
	      if (((res[j] & SH_PR_PS) != 0) && ((res[j] & SH_PR_PS2) != 0))
		{
		  if (S_FALSE == is_in_list(&list_fake, NULL, i))
		    {
		      SH_MUTEX_LOCK(mutex_thread_nolog);
		      sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
				      MSG_PCK_FAKE, 
				      (unsigned long) i, tests);
		      SH_MUTEX_UNLOCK(mutex_thread_nolog);
		    }
		}
	    }
	}
    } /* loop end */

  check_watchlist (res);

  SL_RETURN (0, _("sh_process_check_int"));
}

/* Initialise. 
 */
static int sh_prochk_init_internal(void) 
{
  SL_ENTER(_("sh_prochk_init"));

  (void) proc_max_pid (&sh_prochk_maxpid);

  if (sh_prochk_minpid > sh_prochk_maxpid)
    ShProchkActive = S_FALSE;

  /* We need to free anything allocated by the configuration functions if
   * we find that the module is to be left inactive - otherwise _reconf()
   * won't quite work. 
   */
  if( ShProchkActive == S_FALSE ) 
    {
      sh_prochk_free_list(process_check);
      process_check = NULL;
      SL_RETURN(-1, _("sh_prochk_init"));
    }

  sh_prochk_size = sh_prochk_maxpid - sh_prochk_minpid;

  if (sh_prochk_res == NULL)
    {
      sh_prochk_res  = SH_ALLOC(sizeof(short) * sh_prochk_size);
    }
  memset (sh_prochk_res, 0, sizeof(short) * sh_prochk_size);
  
  SL_RETURN(0, _("sh_prochk_init"));
}

int sh_prochk_init (struct mod_type * arg)
{
#ifndef HAVE_PTHREAD
  (void) arg;
#endif

  if (ShProchkActive == S_FALSE)
    return SH_MOD_FAILED;
#ifdef HAVE_PTHREAD
  if (arg != NULL && arg->initval < 0 &&
      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
    {
      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
	return SH_MOD_THREAD;
      else
	return SH_MOD_FAILED;
    }
  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
    {
      sh_prochk_init_internal();
      return SH_MOD_THREAD;
    }
#endif
  return sh_prochk_init_internal();
}

int sh_prochk_timer(time_t tcurrent) 
{
  static time_t lastcheck = 0;

  SL_ENTER(_("sh_prochk_timer"));
  if ((time_t) (tcurrent - lastcheck) >= sh_prochk_interval)
    {
      lastcheck  = tcurrent;
      SL_RETURN((-1), _("sh_prochk_timer"));
    }
  SL_RETURN(0, _("sh_prochk_timer"));
}

int sh_prochk_check(void) 
{
  int status;

  SL_ENTER(_("sh_prochk_check"));

  SH_MUTEX_LOCK(mutex_proc_check);

  status = 0;

  if( ShProchkActive != S_FALSE )
    {
      SH_MUTEX_LOCK(mutex_thread_nolog);
      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_PCK_CHECK, 
		      (unsigned long) sh_prochk_minpid, 
		      (unsigned long) (sh_prochk_maxpid-1));
      SH_MUTEX_UNLOCK(mutex_thread_nolog);

      if (sh_prochk_res) {
	memset (sh_prochk_res, 0, sizeof(short) * sh_prochk_size);
      }
      status = sh_process_check_int(sh_prochk_res);

      if (status != 0)
	ShProchkActive = S_FALSE;

      /* clean out old entries which are not marked 
       * as missing/hidden/fake anymore
       */
      clean_list (&list_missing);
      clean_list (&list_hidden);
      clean_list (&list_fake);
    }

  SH_MUTEX_UNLOCK(mutex_proc_check);

  SL_RETURN(status, _("sh_prochk_check"));
}

/* Free our lists and the associated memory 
 */
int sh_prochk_cleanup(void) 
{
  SL_ENTER(_("sh_prochk_cleanup"));

  sh_prochk_reconf();

  if (list_missing) {
    kill_list(list_missing);
    list_missing = NULL;
  }
  if (list_hidden) {
    kill_list(list_hidden);
    list_hidden  = NULL;
  }
  if (list_fake) {
    kill_list(list_fake);
    list_fake    = NULL;
  }
  
  SL_RETURN(0, _("sh_prochk_cleanup"));
}

/* Free our lists and the associated memory 
 */
int sh_prochk_reconf(void) 
{
  SL_ENTER(_("sh_prochk_reconf"));

  SH_MUTEX_LOCK(mutex_proc_check);
  userdef_maxpid     = 0;
  sh_prochk_maxpid   = 0x8000;
  sh_prochk_minpid   = 0x0001;
  sh_prochk_interval = SH_PROCHK_INTERVAL;
  sh_prochk_openvz   = S_FALSE;
  p_store            = 0;
  openvz_hidden      = 0;

  sh_prochk_free_list(process_check);
  process_check = NULL;
  if (sh_prochk_res != NULL)
    SH_FREE(sh_prochk_res);
  sh_prochk_res = NULL;

  if (sh_prochk_psarg)
    SH_FREE(sh_prochk_psarg);
  sh_prochk_psarg = NULL;
  if (sh_prochk_pspath)
    SH_FREE(sh_prochk_pspath);
  sh_prochk_pspath = NULL;
  SH_MUTEX_UNLOCK(mutex_proc_check);

  SL_RETURN(0, _("sh_prochk_reconf"));
}

/* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
#endif

/* #ifdef SH_USE_PROCESSCHECK */
#endif


#ifdef SH_CUTEST
#include "CuTest.h"

void Test_processcheck_watchlist_ok (CuTest *tc) {
#if defined(SH_USE_PROCESSCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
  CuAssertTrue(tc, 0 == sh_prochk_add_process("init"));
  CuAssertTrue(tc, 
	       S_TRUE  == is_in_watchlist("    1 ?        00:00:00 init", 0));
  CuAssertTrue(tc, 
	       S_FALSE == is_in_watchlist("    1 ?        00:00:00 flix", 0));
  CuAssertTrue(tc, 
	       S_TRUE  == is_in_watchlist("25218 ?        SNs    0:01 /usr/sbin/init -k start -DSSL", 0));
  CuAssertTrue(tc, 
	       S_FALSE  == is_in_watchlist("25218 ?        SNs    0:01 /usr/sbin/apache2 -k start -DSSL", 0));


  sh_prochk_free_list(process_check);
  process_check = NULL;
  CuAssertTrue(tc, S_FALSE == is_in_watchlist("init", 0));

  CuAssertTrue(tc, 0 == sh_prochk_add_process("init"));
  CuAssertTrue(tc, 0 == sh_prochk_add_process("ssh"));
  CuAssertTrue(tc, 0 == sh_prochk_add_process("syslog"));
  CuAssertTrue(tc, S_TRUE  == is_in_watchlist("init", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_watchlist("ssh", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_watchlist("syslog", 0));

  sh_prochk_free_list(process_check);
  process_check = NULL;
  CuAssertTrue(tc, S_FALSE == is_in_watchlist("init", 0));
  CuAssertTrue(tc, S_FALSE == is_in_watchlist("ssh", 0));
  CuAssertTrue(tc, S_FALSE == is_in_watchlist("syslog", 0));
#else
  (void) tc; /* fix compiler warning */
#endif
  return;
}

void Test_processcheck_listhandle_ok (CuTest *tc) {
#if defined(SH_USE_PROCESSCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));

  if (list_missing)
    kill_list(list_missing);
  list_missing = NULL;

  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));

  if (list_missing)
    kill_list(list_missing);
  list_missing = NULL;

  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));

  CuAssertTrue(tc, 2  == clean_list(&list_missing));
  CuAssertPtrNotNull(tc, list_missing);

  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));

  CuAssertTrue(tc, 2  == clean_list(&list_missing));
  CuAssertPtrNotNull(tc, list_missing);

  CuAssertTrue(tc, 0  == clean_list(&list_missing));
  CuAssertTrue(tc, NULL == list_missing);
#else
  (void) tc; /* fix compiler warning */
#endif
  return;
}


/* #ifdef SH_CUTEST */
#endif