Ignore:
Timestamp:
Jan 9, 2007, 10:32:21 PM (18 years ago)
Author:
rainer
Message:

Fix for ticket #41 (unable to deactivate processcheck), and minor code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_processcheck.c

    r77 r78  
    4848#endif
    4949
     50#ifdef HAVE_SYS_STATVFS_H
     51#include <sys/statvfs.h>
     52#endif
     53
     54
    5055#ifdef HAVE_REGEX_H
    5156#include <regex.h>
     
    5863#include "sh_error.h"
    5964#include "sh_extern.h"
     65#include "sh_calls.h"
    6066
    6167#ifdef SH_USE_PROCESSCHECK
     
    157163
    158164static const short SH_PR_PRIORITY = 0x0100;
    159 
    160 static const short SH_PR_PS2      = 0x0200;
    161 static const short SH_PR_PS_ANY   = 0x0400;
    162 static const short SH_PR_ALL      = 0x0800;
    163 static const short SH_PR_ANY      = 0x1000;
     165static const short SH_PR_STATVSF  = 0x0200;
     166
     167static const short SH_PR_PS2      = 0x1000;
     168static const short SH_PR_PS_ANY   = 0x2000;
     169static const short SH_PR_ALL      = 0x4000;
     170static const short SH_PR_ANY      = 0x8000;
    164171
    165172/* /proc:
     
    168175 *        solaris10: /proc/pid/path/a.out
    169176 */
     177static char * get_user_and_path (pid_t pid, char * user, size_t usrlen)
     178{
     179  extern char *  sh_unix_getUIDname (int level, uid_t uid);
     180
     181  char        path[128];
     182  char *      buf;
     183  struct stat sbuf;
     184  int         len;
     185  char *      tmp;
     186
     187  sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) pid);
     188
     189  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
     190    {
     191      goto linkread;
     192    }
     193
     194  sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) pid);
     195
     196  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
     197    {
     198      goto linkread;
     199    }
     200
     201  sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) pid);
     202
     203  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
     204    {
     205      goto linkread;
     206    }
     207
     208  return NULL;
     209
     210 linkread:
     211
     212  buf = SH_ALLOC(PATH_MAX);
     213  len = readlink(path, buf, PATH_MAX);   /* flawfinder: ignore */
     214  len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
     215
     216  if (len > 0)
     217    {
     218      buf[len] = '\0';
     219    }
     220  else
     221    {
     222      SH_FREE(buf);
     223      return NULL;
     224    }
     225
     226  tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid);
     227
     228  if (tmp)
     229    sl_strlcpy(user, tmp, usrlen);
     230  else
     231    sl_snprintf (user, usrlen, "%ld", (unsigned long) sbuf.st_uid);
     232
     233  return buf;
     234}
     235
    170236
    171237struct watchlist {
     
    483549  size_t  value;
    484550  char * foo;
    485   int    retval = 0;
     551  int    retval = -1;
    486552
    487553  SL_ENTER(_("sh_prochk_set_maxpid"));
    488554
    489555  value = (size_t) strtoul(str, &foo, 0);
    490   if (*foo != '\0') {
    491     retval = -1;
    492   } else {
     556
     557  if (*foo == '\0' && SL_TRUE == sl_ok_adds(value, 1)) {
    493558    sh_prochk_maxpid = value + 1;
    494559    userdef_maxpid   = 1;
     
    593658  if (res & SH_PR_GETPGID)  sl_strlcat(list, _(" getpgid"), len);
    594659  if (res & SH_PR_KILL)     sl_strlcat(list, _(" kill"), len);
     660  if (res & SH_PR_STATVSF)  sl_strlcat(list, _(" statvfs"), len);
    595661  if (res & SH_PR_PS2)      sl_strlcat(list, _(" ps(final)"), len);
    596662  return;
    597663}
    598664
    599 /* FIXME: add open, statvfs, sched_getaffinity
    600  */
     665
    601666static short sh_processes_check (pid_t pid, short res)
    602667{
     668  int  retval;
    603669  int  have_checks = 0;
    604670  int  need_checks = 0;
     
    608674  DIR * dir;
    609675#endif
     676#ifdef HAVE_STATVFS
     677  struct statvfs vfsbuf;
     678#endif
    610679
    611680#if !defined(sun) && !defined(__sun) && !defined(__sun__)
     
    624693      ++need_checks;
    625694    }
     695
    626696
    627697#ifdef HAVE_GETPGID
     
    666736  sl_snprintf (path, sizeof(path), "/proc/%ld", (unsigned long) pid);
    667737
    668   if (0 == lstat (path, &buf))
     738  if (0 == retry_lstat (FIL__, __LINE__, path, &buf))
    669739    {
    670740      res |= SH_PR_LSTAT;   res |= SH_PR_ANY; ++have_checks;
     
    678748    }
    679749  ++need_checks;
     750
     751#ifdef HAVE_STATVFS
     752  do {
     753    retval = statvfs (path, &vfsbuf);
     754  } while (retval < 0 && errno == EINTR);
     755
     756  if (0 == retval)
     757    {
     758      res |= SH_PR_STATVSF;   res |= SH_PR_ANY; ++have_checks;
     759    }
     760  ++need_checks;
     761#endif
    680762
    681763#if !defined(SH_PROFILE)
     
    9721054                  if (S_FALSE == is_in_list(&list_hidden, NULL, i))
    9731055                    {
    974                       sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
    975                                       MSG_PCK_HIDDEN,
    976                                       (unsigned long) i, tests);
     1056                      char   user[16];
     1057                      char * aout;
     1058                      char * safe;
     1059
     1060                      aout = get_user_and_path ((pid_t) i, user, sizeof(user));
     1061
     1062                      if (aout)
     1063                        {
     1064                          safe = sh_util_safe_name (aout);
     1065                          sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
     1066                                          MSG_PCK_P_HIDDEN,
     1067                                          (unsigned long) i, tests, safe, user);
     1068                          SH_FREE(safe);
     1069                          SH_FREE(aout);
     1070                        }
     1071                      else
     1072                        sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0,
     1073                                        MSG_PCK_HIDDEN,
     1074                                        (unsigned long) i, tests);
     1075                       
    9771076                    }
    9781077                }
Note: See TracChangeset for help on using the changeset viewer.