summaryrefslogtreecommitdiff
path: root/libps/filters.c
blob: ffbb83769507cfc47e384223dca90f3d6d062c41 (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
/* Some ps_filter_t's to restrict proc_stat_list's in various ways.

   Copyright (C) 1995, 1996 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   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, 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. */

#include <hurd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pwd.h>
#include <unistd.h>

#include "ps.h"
#include "common.h"

/* ---------------------------------------------------------------- */

static bool 
ps_own_p (proc_stat_t ps)
{
  static int own_uid = -2;	/* -1 means no uid at all.  */
  if (own_uid == -2)
    own_uid = getuid ();
  return own_uid >= 0 && own_uid == proc_stat_owner_uid (ps);
}
struct ps_filter ps_own_filter =
{"own", PSTAT_OWNER_UID, ps_own_p};

static bool 
ps_not_leader_p (proc_stat_t ps)
{
  return
    !(proc_stat_state(ps) & (PSTAT_STATE_P_SESSLDR | PSTAT_STATE_P_LOGINLDR));
}
struct ps_filter ps_not_leader_filter =
{"not-sess-leader", PSTAT_STATE, ps_not_leader_p};

static bool 
ps_unorphaned_p(proc_stat_t ps)
{
  int state = proc_stat_state(ps);
  return
    !(state & PSTAT_STATE_P_ORPHAN)
      || (state & (PSTAT_STATE_P_SESSLDR | PSTAT_STATE_P_LOGINLDR));
}
struct ps_filter ps_unorphaned_filter =
{"unorphaned", PSTAT_STATE, ps_unorphaned_p};

static bool 
ps_ctty_p(proc_stat_t ps)
{
  return proc_stat_cttyid(ps) != MACH_PORT_NULL;
}
struct ps_filter ps_ctty_filter =
{"ctty", PSTAT_CTTYID, ps_ctty_p};

static bool 
ps_parent_p(proc_stat_t ps)
{
  return !(proc_stat_state(ps) & PSTAT_STATE_P_NOPARENT);
}
struct ps_filter ps_parent_filter =
{"parent", PSTAT_STATE, ps_parent_p};

static bool
ps_alive_p (proc_stat_t ps)
{
  ps_flags_t test_flag =
    proc_stat_is_thread (ps) ? PSTAT_THREAD_BASIC : PSTAT_PROC_INFO;
  if (proc_stat_has (ps, test_flag))
    return 1;
  proc_stat_set_flags (ps, test_flag);
  return proc_stat_has (ps, test_flag);
}
struct ps_filter ps_alive_filter =
{"alive", 0, ps_alive_p};