summaryrefslogtreecommitdiff
path: root/src/misc/s6-linux-init-umountall.c
blob: 42fae159694f7582780164242823b4de367289eb (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
/* ISC license. */

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <mntent.h>
#include <sys/mount.h>

#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/skamisc.h>

#include <s6-linux-init/config.h>

#define MAXLINES 99

#define EXCLUDEN 3
static char const *exclude_type[EXCLUDEN] = { "devtmpfs", "proc", "sysfs" } ;

int main (int argc, char const *const *argv)
{
  size_t mountpoints[MAXLINES] ;
  unsigned int got[EXCLUDEN] = { 0, 0, 0 } ;
  stralloc sa = STRALLOC_ZERO ;
  unsigned int line = 0 ;
  int e = 0 ;
  FILE *fp = setmntent("/proc/mounts", "r") ;
  PROG = "s6-linux-init-umountall" ;

  if (!fp) strerr_diefu1sys(111, "open /proc/mounts") ;
  for (;;)
  {
    struct mntent *p ;
    unsigned int i = 0 ;
    errno = 0 ;
    p = getmntent(fp) ;
    if (!p) break ;
    if (!strcmp(p->mnt_dir, S6_LINUX_INIT_TMPFS)) continue ;
    for (; i < EXCLUDEN ; i++)
      if (!strcmp(p->mnt_type, exclude_type[i]))
      {
        got[i]++ ;
        break ;
      }
    if (i < EXCLUDEN && got[i] == 1) continue ;
    if (line >= MAXLINES)
      strerr_dief1x(100, "too many mount points") ;
    mountpoints[line++] = sa.len ;
    if (!stralloc_cats(&sa, p->mnt_dir) || !stralloc_0(&sa))
      strerr_diefu1sys(111, "add mount point to list") ;
  }
  if (errno) strerr_diefu1sys(111, "read /proc/mounts") ;
  endmntent(fp) ;

  while (line--)
    if (umount(sa.s + mountpoints[line]) == -1)
    {
      e++ ;
      strerr_warnwu2sys("umount ", sa.s + mountpoints[line]) ;
    }
  stralloc_free(&sa) ;
  return e ;
}