diff options
Diffstat (limited to 'src/misc/s6-linux-init-umountall.c')
-rw-r--r-- | src/misc/s6-linux-init-umountall.c | 76 |
1 files changed, 34 insertions, 42 deletions
diff --git a/src/misc/s6-linux-init-umountall.c b/src/misc/s6-linux-init-umountall.c index 24b22fc..93b0966 100644 --- a/src/misc/s6-linux-init-umountall.c +++ b/src/misc/s6-linux-init-umountall.c @@ -1,70 +1,62 @@ /* ISC license. */ +#include <errno.h> #include <string.h> +#include <stdio.h> +#include <mntent.h> #include <sys/mount.h> -#include <skalibs/bytestr.h> -#include <skalibs/buffer.h> #include <skalibs/strerr2.h> #include <skalibs/stralloc.h> -#include <skalibs/djbunix.h> #include <skalibs/skamisc.h> -#define BUFSIZE 4096 -#define MAXLINES 512 +#define MAXLINES 99 + +#define EXCLUDEN 3 +static char const *exclude_type[EXCLUDEN] = { "devtmpfs", "proc", "sysfs" } ; int main (int argc, char const *const *argv) { - stralloc mountpoints[MAXLINES] ; - char buf[BUFSIZE] ; - buffer b ; + size_t mountpoints[MAXLINES] ; + unsigned int got[EXCLUDEN] = { 0, 0, 0 } ; stralloc sa = STRALLOC_ZERO ; unsigned int line = 0 ; + FILE *fp ; int e = 0 ; - int r ; - int fd ; PROG = "s6-linux-init-umountall" ; - - /* - We need to go through /proc/mounts *in reverse order*, because later mounts - may depend on earlier mounts. - The getmntent() family of functions has obviously not been designed for that - use case at all, and it is actually more difficult to use it than to do the - /proc/mounts parsing by hand. So, we do it by hand with skalibs functions. - That's how you can tell a good API from a terrible one. - */ - - fd = open_readb("/proc/mounts") ; - if (fd < 0) strerr_diefu1sys(111, "open /proc/mounts") ; - memset(mountpoints, 0, sizeof(mountpoints)) ; - buffer_init(&b, &buffer_read, fd, buf, BUFSIZE) ; + fp = setmntent("/proc/mounts", "r") ; + if (!fp) strerr_diefu1sys(111, "open /proc/mounts") ; for (;;) { - size_t n, p ; - if (line >= MAXLINES) strerr_dief1x(111, "/proc/mounts too big") ; - sa.len = 0 ; - r = skagetln(&b, &sa, '\n') ; - if (r <= 0) break ; - p = byte_chr(sa.s, sa.len, ' ') ; - if (p >= sa.len) strerr_dief1x(111, "bad /proc/mounts format") ; - p++ ; - n = byte_chr(sa.s + p, sa.len - p, ' ') ; - if (n == sa.len - p) strerr_dief1x(111, "bad /proc/mounts format") ; - if (!stralloc_catb(&mountpoints[line], sa.s + p, n) || !stralloc_0(&mountpoints[line])) - strerr_diefu1sys(111, "store mount point") ; - line++ ; + struct mntent *p ; + unsigned int i = 0 ; + errno = 0 ; + p = getmntent(fp) ; + if (!p) break ; + 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") ; } - fd_close(fd) ; - stralloc_free(&sa) ; - if (r < 0) strerr_diefu1sys(111, "read /proc/mounts") ; + if (errno) strerr_diefu1sys(111, "read /proc/mounts") ; + endmntent(fp) ; while (line--) - if (umount(mountpoints[line].s) == -1) + if (umount(sa.s + mountpoints[line]) == -1) { e++ ; - strerr_warnwu2sys("umount ", mountpoints[line].s) ; + strerr_warnwu2sys("umount ", sa.s + mountpoints[line]) ; } + stralloc_free(&sa) ; return e ; } |