about summary refs log tree commit diff
path: root/src/minutils/s6-mount.c
blob: b6ac6744578b0f2a8506ea29abe633047e2f82bc (plain) (blame)
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
/* ISC license. */

#include <errno.h>
#include <sys/mount.h>
#include <mntent.h>
#include <stdio.h>
#include <skalibs/sgetopt.h>
#include <skalibs/bytestr.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include "mount-constants.h"

#define USAGE "s6-mount -a [ -z fstab ] | s6-mount [ -n ] [ -t type ] [ -o option[,option...] ]... device mountpoint"
#define BUFSIZE 4096

#define SWITCH(opt) do
#define HCTIWS(opt) while(0) ;
#define CASE(s) if (!str_diffn(opt, (s), str_len(s)))

static void scanopt (stralloc *data, unsigned long *flags, char const *opt)
{
  for (;;)
  {
    register unsigned int n = str_chr(opt, ',') ;

    SWITCH(opt)
    {
      CASE("defaults") { *flags = MS_MGC_VAL ; break ; }
      CASE("ro") { *flags |= MS_RDONLY ; break ; }
      CASE("rw") { *flags &= ~MS_RDONLY ; break ; }
      CASE("remount") { *flags |= MS_REMOUNT ; break ; }
      CASE("sync") { *flags |= MS_SYNCHRONOUS ; break ; }
      CASE("async") { *flags &= ~MS_SYNCHRONOUS ; break ; }
      CASE("nodev") { *flags |= MS_NODEV ; break ; }
      CASE("dev") { *flags &= ~MS_NODEV ; break ; }
      CASE("noexec") { *flags |= MS_NOEXEC ; break ; }
      CASE("exec") { *flags &= ~MS_NOEXEC ; break ; }
      CASE("nosuid") { *flags |= MS_NOSUID ; break ; }
      CASE("suid") { *flags &= ~MS_NOSUID ; break ; }
      CASE("noatime") { *flags |= MS_NOATIME ; break ; }
      CASE("atime") { *flags &= ~MS_NOATIME ; break ; }
      CASE("nodiratime") { *flags |= MS_NODIRATIME ; break ; }
      CASE("diratime") { *flags &= ~MS_NODIRATIME ; break ; }
      CASE("bind") { *flags |= MS_BIND ; break ; }
      CASE("nobind") { *flags &= ~MS_BIND ; break ; }
      CASE("move") { *flags |= MS_MOVE ; break ; }
      CASE("nomove") { *flags &= ~MS_MOVE ; break ; }

        if ((data->s && data->len && !stralloc_catb(data, ",", 1)) || !stralloc_catb(data, opt, n))
          strerr_diefu1sys(111, "build data string") ;
    }
    HCTIWS(opt)

    opt += n ;
    if (!*opt) break ;
    if (*opt != ',') strerr_dief1x(100, "unrecognized option") ;
    opt++ ;
  }
}

static int mountall (char const *fstab)
{
  struct mntent *d ;
  int e = 0 ;
  FILE *yuck = setmntent(fstab, "r") ;
  if (!yuck) strerr_diefu2sys(111, "open ", fstab) ;
  while ((d = getmntent(yuck)))
  {
    unsigned long flags = MS_MGC_VAL ;
    stralloc data = STRALLOC_ZERO ;
    scanopt(&data, &flags, d->mnt_opts) ;
    if (!stralloc_0(&data))
      strerr_diefu1sys(111, "build data string") ;
#ifdef DEBUG
    strerr_warni4x("mounting ", d->mnt_fsname, " on ", d->mnt_dir) ;
#endif
    if (mount(d->mnt_fsname, d->mnt_dir, d->mnt_type, flags, data.s) == -1)
    {
      e++ ;
      strerr_warnwu4sys("mount ", d->mnt_fsname, " on ", d->mnt_dir) ;
    }
    stralloc_free(&data) ;
  }
  endmntent(yuck) ;
  return e ;
}

int main (int argc, char const *const *argv)
{
  stralloc data = STRALLOC_ZERO ;
  unsigned long flags = MS_MGC_VAL ;
  char const *fstype = "none" ;
  char const *fstab = "/etc/fstab" ;
  PROG = "s6-mount" ;
  {
    int doall = 0 ;
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "nz:arwt:o:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'n' : break ;
        case 'z' : fstab = l.arg ; break ;
        case 'a' : doall = 1 ; break ;
        case 't' : fstype = l.arg ; break ;
        case 'w' : scanopt(&data, &flags, "rw") ; break ;
        case 'r' : scanopt(&data, &flags, "ro") ; break ;
        case 'o' : scanopt(&data, &flags, l.arg) ; break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
    if (doall) return mountall(fstab) ;
  }
  if (!argc)
  {
    int fd = open_readb("/proc/mounts") ;
    if (fd < 0) strerr_diefu2sys(111, "read ", "/proc/mounts") ;
    if (fd_cat(fd, 1) < 0) strerr_diefu2sys(111, "fd_cat ", "/proc/mounts") ;
    fd_close(fd) ;
  }
  else if (argc == 1) strerr_dieusage(100, USAGE) ;
  else if (!stralloc_0(&data)) strerr_diefu1sys(111, "build data string") ;  
  else if (mount(argv[0], argv[1], fstype, flags, data.s) == -1)
    strerr_diefu4sys(errno == EBUSY ? 1 : 111, "mount ", argv[0], " on ", argv[1]) ;
  return 0 ;
}