1eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include <stdio.h> 2eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include <string.h> 3eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 4eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#ifdef CONFIG_GETMNTENT 5eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include <mntent.h> 6eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 7eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include "mountcheck.h" 8eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 9eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#define MTAB "/etc/mtab" 10eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 11eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughesint device_is_mounted(const char *dev) 12eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes{ 13eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes FILE *mtab; 14eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes struct mntent *mnt; 15eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes int ret = 0; 16eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 17eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes mtab = setmntent(MTAB, "r"); 18eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (!mtab) 19eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 0; 20eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 21eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes while ((mnt = getmntent(mtab)) != NULL) { 22eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (!mnt->mnt_fsname) 23eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes continue; 24eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (!strcmp(mnt->mnt_fsname, dev)) { 25eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes ret = 1; 26eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes break; 27eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes } 28eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes } 29eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 30eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes endmntent(mtab); 31eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return ret; 32eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes} 33eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 34eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#elif defined(CONFIG_GETMNTINFO) 35eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes/* for most BSDs */ 36eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include <sys/param.h> 37eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include <sys/mount.h> 38eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 39eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughesint device_is_mounted(const char *dev) 40eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes{ 41eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes struct statfs *st; 42eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes int i, ret; 43eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 44eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes ret = getmntinfo(&st, MNT_NOWAIT); 45eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (ret <= 0) 46eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 0; 47eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 48eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes for (i = 0; i < ret; i++) { 49eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (!strcmp(st[i].f_mntfromname, dev)) 50eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 1; 51eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes } 52eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 53eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 0; 54eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes} 55eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 56eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#elif defined(CONFIG_GETMNTINFO_STATVFS) 57eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes/* for NetBSD */ 58eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#include <sys/statvfs.h> 59eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 60eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughesint device_is_mounted(const char *dev) 61eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes{ 62eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes struct statvfs *st; 63eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes int i, ret; 64eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 65eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes ret = getmntinfo(&st, MNT_NOWAIT); 66eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (ret <= 0) 67eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 0; 68eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 69eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes for (i = 0; i < ret; i++) { 70eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes if (!strcmp(st[i].f_mntfromname, dev)) 71eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 1; 72eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes } 73eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 74eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 0; 75eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes} 76eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 77eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#else 78eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes/* others */ 79eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 80eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughesint device_is_mounted(const char *dev) 81eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes{ 82eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes return 0; 83eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes} 84eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes 85eda3a60699e1d96bb68875ef2169ca819eb8f4f9Elliott Hughes#endif 86