| 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 |
#include <errno.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <limits.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <unistd.h> |
| 32 |
|
| 33 |
#include "string-macros.h" |
| 34 |
#include "mountlist.h" |
| 35 |
#include "xalloc.h" |
| 36 |
|
| 37 |
#if HAVE_SYS_PARAM_H |
| 38 |
#include <sys/param.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <mntent.h> |
| 42 |
#if !defined MOUNTED |
| 43 |
# if defined _PATH_MOUNTED |
| 44 |
# define MOUNTED _PATH_MOUNTED |
| 45 |
# endif |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifndef ME_DUMMY |
| 49 |
# define ME_DUMMY(Fs_name, Fs_type) \ |
| 50 |
(STREQ (Fs_type, "autofs") \ |
| 51 |
|| STREQ (Fs_type, "proc") \ |
| 52 |
\ |
| 53 |
|| STREQ (Fs_type, "cgroup") \ |
| 54 |
|| STREQ (Fs_type, "debugfs") \ |
| 55 |
|| STREQ (Fs_type, "devpts") \ |
| 56 |
|| STREQ (Fs_type, "fusectl") \ |
| 57 |
|| STREQ (Fs_type, "hugetlbfs") \ |
| 58 |
|| STREQ (Fs_type, "mqueue") \ |
| 59 |
|| STREQ (Fs_type, "pstore") \ |
| 60 |
|| STREQ (Fs_type, "rpc_pipefs") \ |
| 61 |
|| STREQ (Fs_type, "securityfs") \ |
| 62 |
|| STREQ (Fs_type, "sysfs") \ |
| 63 |
\ |
| 64 |
|| STREQ (Fs_type, "devfs") \ |
| 65 |
|| STREQ (Fs_type, "binfmt_misc") \ |
| 66 |
|| STREQ (Fs_type, "none")) |
| 67 |
#endif |
| 68 |
|
| 69 |
#ifndef ME_REMOTE |
| 70 |
|
| 71 |
|
| 72 |
# define ME_REMOTE(Fs_name, Fs_type) \ |
| 73 |
(strchr (Fs_name, ':') != NULL \ |
| 74 |
|| ((Fs_name)[0] == '/' \ |
| 75 |
&& (Fs_name)[1] == '/' \ |
| 76 |
&& (STREQ (Fs_type, "smbfs") \ |
| 77 |
|| STREQ (Fs_type, "cifs")))) |
| 78 |
#endif |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#if HAVE_HASMNTOPT |
| 83 |
static bool |
| 84 |
fs_check_if_readonly (const struct mntent *mnt) |
| 85 |
{ |
| 86 |
static char const readonly_pattern[] = "ro"; |
| 87 |
return hasmntopt (mnt, readonly_pattern) ? true : false; |
| 88 |
} |
| 89 |
#else |
| 90 |
static bool |
| 91 |
fs_check_if_readonly (char *mount_options) |
| 92 |
{ |
| 93 |
static char const readonly_pattern[] = "ro"; |
| 94 |
|
| 95 |
char *str1, saveptr1; |
| 96 |
int j; |
| 97 |
|
| 98 |
for (j = 1, str1 = mount_options;; j++, str1 = NULL) |
| 99 |
{ |
| 100 |
char *token = strtok_r (str1, ",", &saveptr1); |
| 101 |
if (token == NULL) |
| 102 |
break; |
| 103 |
if (STREQ (token, readonly_pattern)) |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static dev_t |
| 114 |
dev_from_mount_options (char const *mount_options) |
| 115 |
{ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
(void) mount_options; |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
struct mount_entry * |
| 129 |
read_file_system_list (bool need_fs_type) |
| 130 |
{ |
| 131 |
struct mount_entry *mount_list; |
| 132 |
struct mount_entry *me; |
| 133 |
struct mount_entry **mtail = &mount_list; |
| 134 |
(void) need_fs_type; |
| 135 |
|
| 136 |
{ |
| 137 |
struct mntent *mnt; |
| 138 |
char const *table = MOUNTED; |
| 139 |
FILE *fp; |
| 140 |
|
| 141 |
fp = setmntent (table, "r"); |
| 142 |
if (fp == NULL) |
| 143 |
return NULL; |
| 144 |
|
| 145 |
while ((mnt = getmntent (fp))) |
| 146 |
{ |
| 147 |
me = xmalloc (sizeof *me); |
| 148 |
me->me_devname = xstrdup (mnt->mnt_fsname); |
| 149 |
me->me_mountdir = xstrdup (mnt->mnt_dir); |
| 150 |
me->me_type = xstrdup (mnt->mnt_type); |
| 151 |
me->me_type_malloced = 1; |
| 152 |
me->me_opts = xstrdup (mnt->mnt_opts); |
| 153 |
me->me_opts_malloced = 1; |
| 154 |
me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); |
| 155 |
me->me_remote = ME_REMOTE (me->me_devname, me->me_type); |
| 156 |
#if HAVE_HASMNTOPT |
| 157 |
me->me_readonly = fs_check_if_readonly (mnt); |
| 158 |
#else |
| 159 |
me->me_readonly = fs_check_if_readonly (me->me_opts); |
| 160 |
#endif |
| 161 |
me->me_dev = dev_from_mount_options (mnt->mnt_opts); |
| 162 |
|
| 163 |
|
| 164 |
*mtail = me; |
| 165 |
mtail = &me->me_next; |
| 166 |
} |
| 167 |
|
| 168 |
if (endmntent (fp) == 0) |
| 169 |
goto free_then_fail; |
| 170 |
} |
| 171 |
|
| 172 |
*mtail = NULL; |
| 173 |
return mount_list; |
| 174 |
|
| 175 |
|
| 176 |
free_then_fail: |
| 177 |
{ |
| 178 |
int saved_errno = errno; |
| 179 |
*mtail = NULL; |
| 180 |
|
| 181 |
while (mount_list) |
| 182 |
{ |
| 183 |
me = mount_list->me_next; |
| 184 |
free (mount_list->me_devname); |
| 185 |
free (mount_list->me_mountdir); |
| 186 |
if (mount_list->me_type_malloced) |
| 187 |
free (mount_list->me_type); |
| 188 |
if (mount_list->me_opts_malloced) |
| 189 |
free (mount_list->me_opts); |
| 190 |
free (mount_list); |
| 191 |
mount_list = me; |
| 192 |
} |
| 193 |
|
| 194 |
errno = saved_errno; |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
int |
| 200 |
file_system_type_exists (const char *fs_type, char **fs_mp) |
| 201 |
{ |
| 202 |
int exists = 0; |
| 203 |
struct mount_entry *mount_list, *me; |
| 204 |
|
| 205 |
if (NULL == fs_type) |
| 206 |
return -1; |
| 207 |
|
| 208 |
mount_list = read_file_system_list (false); |
| 209 |
if (NULL == mount_list) |
| 210 |
return -1; |
| 211 |
|
| 212 |
for (me = mount_list; me; me = me->me_next) |
| 213 |
if (STREQ (me->me_type, fs_type)) |
| 214 |
{ |
| 215 |
*fs_mp = me->me_mountdir; |
| 216 |
exists = 1; |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
return exists; |
| 221 |
} |