aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs6rc/s6rc_graph_clean.c
blob: 29352ae08d679c7835667b835be986a597cd40b8 (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
/* ISC license. */

#include <stdint.h>

#include <skalibs/bitarray.h>

#include <s6-rc/s6rc-db.h>
#include <s6-rc/s6rc-utils.h>

typedef struct recinfo_s recinfo_t, *recinfo_t_ref ;
struct recinfo_s
{
  s6rc_db_t const *db ;
  unsigned char *bits ;
  unsigned char statemask ;
  unsigned char volmask ;
  unsigned char resmask ;
} ;

static void s6rc_graph_clean_rec (recinfo_t *info, uint32_t i)
{
  if (!(info->bits[i] & info->volmask))
  {
    uint32_t j = 0 ;
    for (; j < info->db->services[i].ndeps[0] ; j++)
    {
      uint32_t k = info->db->deps[info->db->services[i].deps[0] + j] ;
      if (info->bits[k] & info->statemask && !(info->bits[k] & info->resmask)) break ;
    }
    if (j >= info->db->services[i].ndeps[0])
    {
      info->bits[i] |= info->resmask ;
      for (j = 0 ; j < info->db->services[i].ndeps[1] ; j++)
        s6rc_graph_clean_rec(info, info->db->deps[info->db->ndeps + info->db->services[i].deps[1] + j]) ;
    }
  }
}

void s6rc_graph_clean (s6rc_db_t const *db, unsigned char *bits, unsigned int statebit, unsigned int volbit, unsigned int resbit)
{
  uint32_t n = db->nshort + db->nlong ;
  recinfo_t info = { .db = db, .bits = bits, .statemask = 1 << statebit, .volmask = 1 << volbit, .resmask = 1 << resbit } ;
  for (uint32_t i = 0 ; i < n ; i++) bits[i] &= ~info.resmask ;
  for (uint32_t i = 0 ; i < n ; i++) if (bits[i] & info.statemask) s6rc_graph_clean_rec(&info, i) ;
}