bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "lib.h"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "mountpoint.h"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include <sys/stat.h>
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen#ifdef HAVE_SYS_VMOUNT_H
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen# include <stdio.h>
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen# include <sys/vmount.h> /* AIX */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen# define MOUNTPOINT_AIX_MNTCTL
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen#elif defined(HAVE_STATVFS_MNTFROMNAME)
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen# include <sys/statvfs.h> /* NetBSD 3.0+, FreeBSD 5.0+ */
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen# define STATVFS_STR "statvfs"
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen# define MOUNTPOINT_STATVFS
6daca8888bbf2b5bf26903cf397d5219ea752241Timo Sirainen#elif defined(HAVE_STATFS_MNTFROMNAME)
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen# include <sys/param.h> /* Older BSDs */
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# include <sys/mount.h>
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen# define statvfs statfs
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen# define STATVFS_STR "statfs"
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen# define MOUNTPOINT_STATVFS
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#elif defined(HAVE_MNTENT_H)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# include <stdio.h>
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# include <mntent.h> /* Linux */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen# define MOUNTPOINT_LINUX
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#elif defined(HAVE_SYS_MNTTAB_H)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# include <stdio.h>
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# include <sys/mnttab.h> /* Solaris */
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen# include <sys/mntent.h>
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen# define MOUNTPOINT_SOLARIS
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#else
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# define MOUNTPOINT_UNKNOWN
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#endif
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_SOLARIS
d8eedfaad386a8776e4931086b039b72e1ad38c4Timo Sirainen# define MTAB_PATH MNTTAB /* Solaris */
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen#else
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen# define MTAB_PATH "/etc/mtab" /* Linux */
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen#endif
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen/* AIX doesn't have these defined */
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#ifndef MNTTYPE_SWAP
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# define MNTTYPE_SWAP "swap"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#endif
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#ifndef MNTTYPE_IGNORE
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen# define MNTTYPE_IGNORE "ignore"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#endif
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen#ifndef MNTTYPE_JFS
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen# define MNTTYPE_JFS "jfs"
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen#endif
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen#ifndef MNTTYPE_NFS
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen# define MNTTYPE_NFS "nfs"
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen#endif
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen/* Linux sometimes has mtab entry for "rootfs" as well as the real root
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen entry. Skip the rootfs. */
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen#ifndef MNTTYPE_ROOTFS
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen# define MNTTYPE_ROOTFS "rootfs"
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen#endif
2dc6cf8bad599bfe9129bb496539a08ee3631cc0Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_STATVFS
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenstatic int
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenmountpoint_get_statvfs(const char *path, pool_t pool,
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct mountpoint *point_r)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen{
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen struct statvfs buf;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(point_r);
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen if (statvfs(path, &buf) < 0) {
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen if (errno == ENOENT)
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen return 0;
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen i_error(STATVFS_STR"(%s) failed: %m", path);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen point_r->device_path = p_strdup(pool, buf.f_mntfromname);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen point_r->mount_path = p_strdup(pool, buf.f_mntonname);
aff1e150e13980354cfd794c74dac76a791a641eTimo Sirainen#ifdef __osf__ /* Tru64 */
aff1e150e13980354cfd794c74dac76a791a641eTimo Sirainen point_r->type = p_strdup(pool, getvfsbynumber(buf.f_type));
aff1e150e13980354cfd794c74dac76a791a641eTimo Sirainen#else
ad3a1b8f8e2a5596afb1b099a69ae6f688887eecTimo Sirainen point_r->type = p_strdup(pool, buf.f_fstypename);
aff1e150e13980354cfd794c74dac76a791a641eTimo Sirainen#endif
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen point_r->block_size = buf.f_bsize;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return 1;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen}
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenint mountpoint_get(const char *path, pool_t pool, struct mountpoint *point_r)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen{
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_UNKNOWN
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(point_r);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen errno = ENOSYS;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return -1;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined (MOUNTPOINT_STATVFS)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen /* BSDs, Tru64 */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return mountpoint_get_statvfs(path, pool, point_r);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#else
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen /* find via mount iteration */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct mountpoint_iter *iter;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen const struct mountpoint *mnt;
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen struct stat st;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(point_r);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (stat(path, &st) < 0) {
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen if (errno == ENOENT)
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen return 0;
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen i_error("stat(%s) failed: %m", path);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter = mountpoint_iter_init();
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen while ((mnt = mountpoint_iter_next(iter)) != NULL) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (minor(st.st_dev) == minor(mnt->dev) &&
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen major(st.st_dev) == major(mnt->dev))
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen break;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (mnt != NULL) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen point_r->device_path = p_strdup(pool, mnt->device_path);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen point_r->mount_path = p_strdup(pool, mnt->mount_path);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen point_r->type = p_strdup(pool, mnt->type);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen point_r->dev = mnt->dev;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen point_r->block_size = st.st_blksize;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen }
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen if (mountpoint_iter_deinit(&iter) < 0 && mnt == NULL)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen return -1;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return mnt != NULL ? 1 : 0;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen}
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenstruct mountpoint_iter {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_AIX_MNTCTL
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen char *mtab;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct vmount *vmt;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen int count;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined(MOUNTPOINT_SOLARIS) || defined(MOUNTPOINT_LINUX)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen FILE *f;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined(HAVE_GETMNTINFO) /* BSDs */
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen#ifndef __NetBSD__
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct statfs *fs;
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen#else
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen struct statvfs *fs;
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen int count;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct mountpoint mnt;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen bool failed;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen};
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenstruct mountpoint_iter *mountpoint_iter_init(void)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen{
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct mountpoint_iter *iter = i_new(struct mountpoint_iter, 1);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_AIX_MNTCTL
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen unsigned int size = STATIC_MTAB_SIZE;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen char *mtab;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen int count;
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen mtab = t_buffer_get(size);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen while ((count = mntctl(MCTL_QUERY, size, mtab)) == 0) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen size = *(unsigned int *)mtab;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen mtab = t_buffer_get(size);
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen }
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen if (count < 0) {
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen i_error("mntctl(MCTL_QUERY) failed: %m");
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->failed = TRUE;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return iter;
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->count = count;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mtab = i_malloc(size);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen memcpy(iter->mtab, mtab, size);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->vmt = (void *)iter->mtab;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined(MOUNTPOINT_SOLARIS)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->f = fopen(MTAB_PATH, "r");
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->f == NULL) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen i_error("fopen(%s) failed: %m", MTAB_PATH);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->failed = TRUE;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return iter;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen resetmnttab(iter->f);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined(MOUNTPOINT_LINUX)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->f = setmntent(MTAB_PATH, "r");
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->f == NULL) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen i_error("setmntent(%s) failed: %m", MTAB_PATH);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->failed = TRUE;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined(HAVE_GETMNTINFO) /* BSDs */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->count = getmntinfo(&iter->fs, MNT_NOWAIT);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->count < 0) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen i_error("getmntinfo() failed: %m");
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->failed = TRUE;
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#else
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->failed = TRUE;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return iter;
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen}
2978aafcf8c306adc2e012aed00546a63c677784Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenconst struct mountpoint *mountpoint_iter_next(struct mountpoint_iter *iter)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen{
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_AIX_MNTCTL
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct vmount *vmt = iter->vmt;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen char *vmt_base = (char *)vmt;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen char *vmt_object, *vmt_stub, *vmt_hostname;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct stat vst;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->count == 0)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->count--;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->vmt = PTR_OFFSET(vmt, vmt->vmt_length);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen vmt_hostname = vmt_base + vmt->vmt_data[VMT_HOSTNAME].vmt_off;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen vmt_object = vmt_base + vmt->vmt_data[VMT_OBJECT].vmt_off;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen vmt_stub = vmt_base + vmt->vmt_data[VMT_STUB].vmt_off;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&iter->mnt);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen switch (vmt->vmt_gfstype) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen case MNT_NFS:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen case MNT_NFS3:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen case MNT_NFS4:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen case MNT_RFS4:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.device_path =
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen t_strconcat(vmt_hostname, ":", vmt_object, NULL);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.mount_path = vmt_stub;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.type = MNTTYPE_NFS;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen break;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen case MNT_J2:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen case MNT_JFS:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.device_path = vmt_object;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.mount_path = vmt_stub;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.type = MNTTYPE_JFS;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen break;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen default:
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen /* unwanted filesystem */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return mountpoint_iter_next(iter);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (stat(iter->mnt.mount_path, &vst) == 0) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.dev = vst.st_dev;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.block_size = vst.st_blksize;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return &iter->mnt;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined (MOUNTPOINT_SOLARIS)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen union {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct mnttab ent;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct extmnttab ext;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen } ent;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->f == NULL)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&iter->mnt);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen while ((getextmntent(iter->f, &ent.ext, sizeof(ent.ext))) == 0) {
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen if (hasmntopt(&ent.ent, MNTOPT_IGNORE) != NULL)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen continue;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen /* mnt_type contains tmpfs with swap */
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen if (strcmp(ent.ent.mnt_special, MNTTYPE_SWAP) == 0)
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen continue;
e02846284483d9d3c4b72835cd3dd6cc7f7e6740Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.device_path = ent.ent.mnt_special;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.mount_path = ent.ent.mnt_mountp;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.type = ent.ent.mnt_fstype;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.dev = makedev(ent.ext.mnt_major, ent.ext.mnt_minor);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return &iter->mnt;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined (MOUNTPOINT_LINUX)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen const struct mntent *ent;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct stat st;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->f == NULL)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&iter->mnt);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen while ((ent = getmntent(iter->f)) != NULL) {
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (strcmp(ent->mnt_type, MNTTYPE_SWAP) == 0 ||
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen strcmp(ent->mnt_type, MNTTYPE_IGNORE) == 0 ||
26bedc1f8aace67ff8f19f625a403a097017845aTimo Sirainen strcmp(ent->mnt_type, MNTTYPE_ROOTFS) == 0)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen continue;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.device_path = ent->mnt_fsname;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.mount_path = ent->mnt_dir;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.type = ent->mnt_type;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (stat(ent->mnt_dir, &st) == 0) {
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.dev = st.st_dev;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.block_size = st.st_blksize;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return &iter->mnt;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined(HAVE_GETMNTINFO) /* BSDs */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen while (iter->count > 0) {
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen#ifndef __NetBSD__
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct statfs *fs = iter->fs;
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen#else
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen struct statvfs *fs = iter->fs;
5fb80928bb367d7410d510d0d889909652553003Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->fs++;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->count--;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.device_path = fs->f_mntfromname;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.mount_path = fs->f_mntonname;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef __osf__ /* Tru64 */
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.type = getvfsbynumber(fs->f_type);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#else
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.type = fs->f_fstypename;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen iter->mnt.block_size = fs->f_bsize;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return &iter->mnt;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen }
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#else
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return NULL;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen}
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainenint mountpoint_iter_deinit(struct mountpoint_iter **_iter)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen{
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen struct mountpoint_iter *iter = *_iter;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen int ret = iter->failed ? -1 : 0;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen *_iter = NULL;
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#ifdef MOUNTPOINT_AIX_MNTCTL
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen i_free(iter->mtab);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined (MOUNTPOINT_SOLARIS)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->f != NULL)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen fclose(iter->f);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen#elif defined (MOUNTPOINT_LINUX)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen if (iter->f != NULL)
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen endmntent(iter->f);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#endif
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen i_free(iter);
7c7d2244502af05f77c74736dc1aec0123fdba80Timo Sirainen return ret;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen}