Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 3 | * Mini mount implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 8 | * |
Rob Landley | 7b363fd | 2005-12-20 17:18:01 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 10 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 11 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 12 | /* todo: |
| 13 | * bb_getopt_ulflags(); |
| 14 | */ |
| 15 | |
Rob Landley | 22d26fc | 2006-06-15 15:49:36 +0000 | [diff] [blame] | 16 | /* Design notes: There is no spec for mount. Remind me to write one. |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 17 | |
| 18 | mount_main() calls singlemount() which calls mount_it_now(). |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 19 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 20 | mount_main() can loop through /etc/fstab for mount -a |
| 21 | singlemount() can loop through /etc/filesystems for fstype detection. |
| 22 | mount_it_now() does the actual mount. |
| 23 | */ |
| 24 | |
Rob Landley | 22d26fc | 2006-06-15 15:49:36 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | #include <mntent.h> |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 28 | /* Needed for nfs support only... */ |
| 29 | #include <syslog.h> |
| 30 | #include <sys/utsname.h> |
| 31 | #undef TRUE |
| 32 | #undef FALSE |
| 33 | #include <rpc/rpc.h> |
| 34 | #include <rpc/pmap_prot.h> |
| 35 | #include <rpc/pmap_clnt.h> |
| 36 | |
| 37 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 38 | // Not real flags, but we want to be able to check for this. |
| 39 | #define MOUNT_NOAUTO (1<<29) |
| 40 | #define MOUNT_SWAP (1<<30) |
Rob Landley | 3ba7bd1 | 2006-08-09 19:51:13 +0000 | [diff] [blame] | 41 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 42 | /* Standard mount options (from -o options or --options), with corresponding |
| 43 | * flags */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 45 | struct { |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 46 | char *name; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 47 | long flags; |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 48 | } static mount_options[] = { |
| 49 | // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs. |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 50 | |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 51 | USE_FEATURE_MOUNT_LOOP( |
| 52 | {"loop", 0}, |
| 53 | ) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 54 | |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 55 | USE_FEATURE_MOUNT_FSTAB( |
| 56 | {"defaults", 0}, |
| 57 | {"quiet", 0}, |
| 58 | {"noauto",MOUNT_NOAUTO}, |
| 59 | {"swap",MOUNT_SWAP}, |
| 60 | ) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 61 | |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 62 | USE_FEATURE_MOUNT_FLAGS( |
| 63 | // vfs flags |
| 64 | {"nosuid", MS_NOSUID}, |
| 65 | {"suid", ~MS_NOSUID}, |
| 66 | {"dev", ~MS_NODEV}, |
| 67 | {"nodev", MS_NODEV}, |
| 68 | {"exec", ~MS_NOEXEC}, |
| 69 | {"noexec", MS_NOEXEC}, |
| 70 | {"sync", MS_SYNCHRONOUS}, |
| 71 | {"async", ~MS_SYNCHRONOUS}, |
| 72 | {"atime", ~MS_NOATIME}, |
| 73 | {"noatime", MS_NOATIME}, |
| 74 | {"diratime", ~MS_NODIRATIME}, |
| 75 | {"nodiratime", MS_NODIRATIME}, |
| 76 | {"loud", ~MS_SILENT}, |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 77 | |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 78 | // action flags |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 79 | |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 80 | {"bind", MS_BIND}, |
| 81 | {"move", MS_MOVE}, |
| 82 | {"shared", MS_SHARED}, |
| 83 | {"slave", MS_SLAVE}, |
| 84 | {"private", MS_PRIVATE}, |
| 85 | {"unbindable", MS_UNBINDABLE}, |
| 86 | {"rshared", MS_SHARED|MS_RECURSIVE}, |
| 87 | {"rslave", MS_SLAVE|MS_RECURSIVE}, |
| 88 | {"rprivate", MS_SLAVE|MS_RECURSIVE}, |
| 89 | {"runbindable", MS_UNBINDABLE|MS_RECURSIVE}, |
| 90 | ) |
| 91 | |
| 92 | // Always understood. |
| 93 | |
| 94 | {"ro", MS_RDONLY}, // vfs flag |
| 95 | {"rw", ~MS_RDONLY}, // vfs flag |
| 96 | {"remount", MS_REMOUNT}, // action flag |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 100 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 101 | /* Append mount options to string */ |
| 102 | static void append_mount_options(char **oldopts, char *newopts) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | { |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 104 | if (*oldopts && **oldopts) { |
Denis Vlasenko | 39e93cc | 2006-09-10 18:38:17 +0000 | [diff] [blame] | 105 | char *temp = xasprintf("%s,%s",*oldopts,newopts); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 106 | free(*oldopts); |
Denis Vlasenko | 39e93cc | 2006-09-10 18:38:17 +0000 | [diff] [blame] | 107 | *oldopts = temp; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 108 | } else { |
| 109 | if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 110 | *oldopts = xstrdup(newopts); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /* Use the mount_options list to parse options into flags. |
Denis Vlasenko | 00d7d6c | 2006-09-11 17:42:44 +0000 | [diff] [blame] | 115 | * Also return list of unrecognized options if unrecognized!=NULL */ |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 116 | static int parse_mount_options(char *options, char **unrecognized) |
| 117 | { |
| 118 | int flags = MS_SILENT; |
| 119 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 120 | // Loop through options |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 121 | for (;;) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 122 | int i; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | char *comma = strchr(options, ','); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 124 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 125 | if (comma) *comma = 0; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 126 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 127 | // Find this option in mount_options |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 128 | for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) { |
| 129 | if (!strcasecmp(mount_options[i].name, options)) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 130 | long fl = mount_options[i].flags; |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 131 | if (fl < 0) flags &= fl; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 132 | else flags |= fl; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | break; |
| 134 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 135 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 136 | // If unrecognized not NULL, append unrecognized mount options */ |
| 137 | if (unrecognized |
| 138 | && i == (sizeof(mount_options) / sizeof(*mount_options))) |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 139 | { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 140 | // Add it to strflags, to pass on to kernel |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 141 | i = *unrecognized ? strlen(*unrecognized) : 0; |
| 142 | *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 143 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 144 | // Comma separated if it's not the first one |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 145 | if (i) (*unrecognized)[i++] = ','; |
| 146 | strcpy((*unrecognized)+i, options); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 148 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 149 | // Advance to next option, or finish |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 150 | if (comma) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | *comma = ','; |
| 152 | options = ++comma; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 153 | } else break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 155 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 156 | return flags; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 159 | // Return a list of all block device backed filesystems |
Matt Kraai | 1240082 | 2001-04-17 04:32:50 +0000 | [diff] [blame] | 160 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 161 | static llist_t *get_block_backed_filesystems(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 162 | { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 163 | char *fs, *buf, |
| 164 | *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0}; |
| 165 | llist_t *list = 0; |
| 166 | int i; |
| 167 | FILE *f; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 169 | for (i = 0; filesystems[i]; i++) { |
| 170 | f = fopen(filesystems[i], "r"); |
| 171 | if (!f) continue; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 172 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 173 | for (fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f)); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 174 | free(buf)) |
| 175 | { |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 176 | if (!strncmp(buf,"nodev",5) && isspace(buf[5])) continue; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 177 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 178 | while (isspace(*fs)) fs++; |
| 179 | if (*fs=='#' || *fs=='*') continue; |
| 180 | if (!*fs) continue; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 181 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 182 | llist_add_to_end(&list,xstrdup(fs)); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 183 | } |
| 184 | if (ENABLE_FEATURE_CLEAN_UP) fclose(f); |
| 185 | } |
| 186 | |
| 187 | return list; |
| 188 | } |
| 189 | |
| 190 | llist_t *fslist = 0; |
| 191 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 192 | #if ENABLE_FEATURE_CLEAN_UP |
| 193 | static void delete_block_backed_filesystems(void) |
| 194 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 195 | llist_free(fslist, free); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 196 | } |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 197 | #else |
| 198 | void delete_block_backed_filesystems(void); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 199 | #endif |
| 200 | |
| 201 | #if ENABLE_FEATURE_MTAB_SUPPORT |
Denis Vlasenko | 00d7d6c | 2006-09-11 17:42:44 +0000 | [diff] [blame] | 202 | static int useMtab = 1; |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 203 | static int fakeIt; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 204 | #else |
| 205 | #define useMtab 0 |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 206 | #define fakeIt 0 |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
| 209 | // Perform actual mount of specific filesystem at specific location. |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 210 | // NB: mp->xxx fields may be trashed on exit |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 211 | static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 212 | { |
| 213 | int rc; |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 215 | if (fakeIt) return 0; |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 216 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 217 | // Mount, with fallback to read-only if necessary. |
| 218 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 219 | for (;;) { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 220 | rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type, |
| 221 | vfsflags, filteropts); |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 222 | if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS)) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 223 | break; |
| 224 | bb_error_msg("%s is write-protected, mounting read-only", |
| 225 | mp->mnt_fsname); |
| 226 | vfsflags |= MS_RDONLY; |
| 227 | } |
| 228 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 229 | // Abort entirely if permission denied. |
| 230 | |
| 231 | if (rc && errno == EPERM) |
| 232 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
| 233 | |
| 234 | /* If the mount was successful, and we're maintaining an old-style |
| 235 | * mtab file by hand, add the new entry to it now. */ |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 236 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 237 | if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) { |
| 238 | char *dir,*fsname; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 239 | FILE *mountTable = setmntent(bb_path_mtab_file, "a+"); |
| 240 | int i; |
| 241 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 242 | if (!mountTable) |
Denis Vlasenko | 00d7d6c | 2006-09-11 17:42:44 +0000 | [diff] [blame] | 243 | bb_error_msg("no %s",bb_path_mtab_file); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 244 | |
| 245 | // Add vfs string flags |
| 246 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 247 | for (i=0; mount_options[i].flags != MS_REMOUNT; i++) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 248 | if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags)) |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 249 | append_mount_options(&(mp->mnt_opts), mount_options[i].name); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 250 | |
| 251 | // Remove trailing / (if any) from directory we mounted on |
| 252 | |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 253 | i = strlen(mp->mnt_dir) - 1; |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 254 | if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0; |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 256 | // Convert to canonical pathnames as needed |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 258 | mp->mnt_dir = dir = bb_simplify_path(mp->mnt_dir); |
| 259 | fsname = 0; |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 260 | if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */ |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 261 | mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname); |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 262 | mp->mnt_type = "none"; |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 263 | } |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 264 | mp->mnt_freq = mp->mnt_passno = 0; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 265 | |
| 266 | // Write and close. |
| 267 | |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 268 | addmntent(mountTable, mp); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 269 | endmntent(mountTable); |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 270 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 271 | free(dir); |
| 272 | free(fsname); |
| 273 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 274 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 275 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 276 | return rc; |
| 277 | } |
| 278 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 279 | #if ENABLE_FEATURE_MOUNT_NFS |
| 280 | |
| 281 | /* |
| 282 | * Linux NFS mount |
| 283 | * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com> |
| 284 | * |
| 285 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 286 | * |
| 287 | * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port |
| 288 | * numbers to be specified on the command line. |
| 289 | * |
| 290 | * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>: |
| 291 | * Omit the call to connect() for Linux version 1.3.11 or later. |
| 292 | * |
| 293 | * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com> |
| 294 | * Implemented the "bg", "fg" and "retry" mount options for NFS. |
| 295 | * |
| 296 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org> |
| 297 | * - added Native Language Support |
| 298 | * |
| 299 | * Modified by Olaf Kirch and Trond Myklebust for new NFS code, |
| 300 | * plus NFSv3 stuff. |
| 301 | */ |
| 302 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 303 | /* This is just a warning of a common mistake. Possibly this should be a |
| 304 | * uclibc faq entry rather than in busybox... */ |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 305 | #if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 306 | #error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support." |
| 307 | #endif |
| 308 | |
| 309 | #define MOUNTPORT 635 |
| 310 | #define MNTPATHLEN 1024 |
| 311 | #define MNTNAMLEN 255 |
| 312 | #define FHSIZE 32 |
| 313 | #define FHSIZE3 64 |
| 314 | |
| 315 | typedef char fhandle[FHSIZE]; |
| 316 | |
| 317 | typedef struct { |
| 318 | unsigned int fhandle3_len; |
| 319 | char *fhandle3_val; |
| 320 | } fhandle3; |
| 321 | |
| 322 | enum mountstat3 { |
| 323 | MNT_OK = 0, |
| 324 | MNT3ERR_PERM = 1, |
| 325 | MNT3ERR_NOENT = 2, |
| 326 | MNT3ERR_IO = 5, |
| 327 | MNT3ERR_ACCES = 13, |
| 328 | MNT3ERR_NOTDIR = 20, |
| 329 | MNT3ERR_INVAL = 22, |
| 330 | MNT3ERR_NAMETOOLONG = 63, |
| 331 | MNT3ERR_NOTSUPP = 10004, |
| 332 | MNT3ERR_SERVERFAULT = 10006, |
| 333 | }; |
| 334 | typedef enum mountstat3 mountstat3; |
| 335 | |
| 336 | struct fhstatus { |
| 337 | unsigned int fhs_status; |
| 338 | union { |
| 339 | fhandle fhs_fhandle; |
| 340 | } fhstatus_u; |
| 341 | }; |
| 342 | typedef struct fhstatus fhstatus; |
| 343 | |
| 344 | struct mountres3_ok { |
| 345 | fhandle3 fhandle; |
| 346 | struct { |
| 347 | unsigned int auth_flavours_len; |
| 348 | char *auth_flavours_val; |
| 349 | } auth_flavours; |
| 350 | }; |
| 351 | typedef struct mountres3_ok mountres3_ok; |
| 352 | |
| 353 | struct mountres3 { |
| 354 | mountstat3 fhs_status; |
| 355 | union { |
| 356 | mountres3_ok mountinfo; |
| 357 | } mountres3_u; |
| 358 | }; |
| 359 | typedef struct mountres3 mountres3; |
| 360 | |
| 361 | typedef char *dirpath; |
| 362 | |
| 363 | typedef char *name; |
| 364 | |
| 365 | typedef struct mountbody *mountlist; |
| 366 | |
| 367 | struct mountbody { |
| 368 | name ml_hostname; |
| 369 | dirpath ml_directory; |
| 370 | mountlist ml_next; |
| 371 | }; |
| 372 | typedef struct mountbody mountbody; |
| 373 | |
| 374 | typedef struct groupnode *groups; |
| 375 | |
| 376 | struct groupnode { |
| 377 | name gr_name; |
| 378 | groups gr_next; |
| 379 | }; |
| 380 | typedef struct groupnode groupnode; |
| 381 | |
| 382 | typedef struct exportnode *exports; |
| 383 | |
| 384 | struct exportnode { |
| 385 | dirpath ex_dir; |
| 386 | groups ex_groups; |
| 387 | exports ex_next; |
| 388 | }; |
| 389 | typedef struct exportnode exportnode; |
| 390 | |
| 391 | struct ppathcnf { |
| 392 | int pc_link_max; |
| 393 | short pc_max_canon; |
| 394 | short pc_max_input; |
| 395 | short pc_name_max; |
| 396 | short pc_path_max; |
| 397 | short pc_pipe_buf; |
| 398 | u_char pc_vdisable; |
| 399 | char pc_xxx; |
| 400 | short pc_mask[2]; |
| 401 | }; |
| 402 | typedef struct ppathcnf ppathcnf; |
| 403 | |
| 404 | #define MOUNTPROG 100005 |
| 405 | #define MOUNTVERS 1 |
| 406 | |
| 407 | #define MOUNTPROC_NULL 0 |
| 408 | #define MOUNTPROC_MNT 1 |
| 409 | #define MOUNTPROC_DUMP 2 |
| 410 | #define MOUNTPROC_UMNT 3 |
| 411 | #define MOUNTPROC_UMNTALL 4 |
| 412 | #define MOUNTPROC_EXPORT 5 |
| 413 | #define MOUNTPROC_EXPORTALL 6 |
| 414 | |
| 415 | #define MOUNTVERS_POSIX 2 |
| 416 | |
| 417 | #define MOUNTPROC_PATHCONF 7 |
| 418 | |
| 419 | #define MOUNT_V3 3 |
| 420 | |
| 421 | #define MOUNTPROC3_NULL 0 |
| 422 | #define MOUNTPROC3_MNT 1 |
| 423 | #define MOUNTPROC3_DUMP 2 |
| 424 | #define MOUNTPROC3_UMNT 3 |
| 425 | #define MOUNTPROC3_UMNTALL 4 |
| 426 | #define MOUNTPROC3_EXPORT 5 |
| 427 | |
| 428 | enum { |
| 429 | #ifndef NFS_FHSIZE |
| 430 | NFS_FHSIZE = 32, |
| 431 | #endif |
| 432 | #ifndef NFS_PORT |
| 433 | NFS_PORT = 2049 |
| 434 | #endif |
| 435 | }; |
| 436 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 437 | /* |
| 438 | * We want to be able to compile mount on old kernels in such a way |
| 439 | * that the binary will work well on more recent kernels. |
| 440 | * Thus, if necessary we teach nfsmount.c the structure of new fields |
| 441 | * that will come later. |
| 442 | * |
| 443 | * Moreover, the new kernel includes conflict with glibc includes |
| 444 | * so it is easiest to ignore the kernel altogether (at compile time). |
| 445 | */ |
| 446 | |
| 447 | struct nfs2_fh { |
| 448 | char data[32]; |
| 449 | }; |
| 450 | struct nfs3_fh { |
| 451 | unsigned short size; |
| 452 | unsigned char data[64]; |
| 453 | }; |
| 454 | |
| 455 | struct nfs_mount_data { |
| 456 | int version; /* 1 */ |
| 457 | int fd; /* 1 */ |
| 458 | struct nfs2_fh old_root; /* 1 */ |
| 459 | int flags; /* 1 */ |
| 460 | int rsize; /* 1 */ |
| 461 | int wsize; /* 1 */ |
| 462 | int timeo; /* 1 */ |
| 463 | int retrans; /* 1 */ |
| 464 | int acregmin; /* 1 */ |
| 465 | int acregmax; /* 1 */ |
| 466 | int acdirmin; /* 1 */ |
| 467 | int acdirmax; /* 1 */ |
| 468 | struct sockaddr_in addr; /* 1 */ |
| 469 | char hostname[256]; /* 1 */ |
| 470 | int namlen; /* 2 */ |
| 471 | unsigned int bsize; /* 3 */ |
| 472 | struct nfs3_fh root; /* 4 */ |
| 473 | }; |
| 474 | |
| 475 | /* bits in the flags field */ |
| 476 | enum { |
| 477 | NFS_MOUNT_SOFT = 0x0001, /* 1 */ |
| 478 | NFS_MOUNT_INTR = 0x0002, /* 1 */ |
| 479 | NFS_MOUNT_SECURE = 0x0004, /* 1 */ |
| 480 | NFS_MOUNT_POSIX = 0x0008, /* 1 */ |
| 481 | NFS_MOUNT_NOCTO = 0x0010, /* 1 */ |
| 482 | NFS_MOUNT_NOAC = 0x0020, /* 1 */ |
| 483 | NFS_MOUNT_TCP = 0x0040, /* 2 */ |
| 484 | NFS_MOUNT_VER3 = 0x0080, /* 3 */ |
| 485 | NFS_MOUNT_KERBEROS = 0x0100, /* 3 */ |
| 486 | NFS_MOUNT_NONLM = 0x0200 /* 3 */ |
| 487 | }; |
| 488 | |
| 489 | |
| 490 | /* |
| 491 | * We need to translate between nfs status return values and |
| 492 | * the local errno values which may not be the same. |
| 493 | * |
| 494 | * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno: |
| 495 | * "after #include <errno.h> the symbol errno is reserved for any use, |
| 496 | * it cannot even be used as a struct tag or field name". |
| 497 | */ |
| 498 | |
| 499 | #ifndef EDQUOT |
| 500 | #define EDQUOT ENOSPC |
| 501 | #endif |
| 502 | |
| 503 | // Convert each NFSERR_BLAH into EBLAH |
| 504 | |
| 505 | static const struct { |
| 506 | int stat; |
| 507 | int errnum; |
| 508 | } nfs_errtbl[] = { |
| 509 | {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST}, |
| 510 | {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG}, |
| 511 | {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT}, |
| 512 | {70,ESTALE}, {71,EREMOTE}, {-1,EIO} |
| 513 | }; |
| 514 | |
| 515 | static char *nfs_strerror(int status) |
| 516 | { |
| 517 | int i; |
Denis Vlasenko | c097519 | 2006-09-17 15:06:34 +0000 | [diff] [blame^] | 518 | static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3]; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 519 | |
| 520 | for (i = 0; nfs_errtbl[i].stat != -1; i++) { |
| 521 | if (nfs_errtbl[i].stat == status) |
| 522 | return strerror(nfs_errtbl[i].errnum); |
| 523 | } |
| 524 | sprintf(buf, "unknown nfs status return value: %d", status); |
| 525 | return buf; |
| 526 | } |
| 527 | |
| 528 | static bool_t xdr_fhandle(XDR *xdrs, fhandle objp) |
| 529 | { |
| 530 | if (!xdr_opaque(xdrs, objp, FHSIZE)) |
| 531 | return FALSE; |
| 532 | return TRUE; |
| 533 | } |
| 534 | |
| 535 | static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp) |
| 536 | { |
| 537 | if (!xdr_u_int(xdrs, &objp->fhs_status)) |
| 538 | return FALSE; |
| 539 | switch (objp->fhs_status) { |
| 540 | case 0: |
| 541 | if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle)) |
| 542 | return FALSE; |
| 543 | break; |
| 544 | default: |
| 545 | break; |
| 546 | } |
| 547 | return TRUE; |
| 548 | } |
| 549 | |
| 550 | static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp) |
| 551 | { |
| 552 | if (!xdr_string(xdrs, objp, MNTPATHLEN)) |
| 553 | return FALSE; |
| 554 | return TRUE; |
| 555 | } |
| 556 | |
| 557 | static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp) |
| 558 | { |
| 559 | if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3)) |
| 560 | return FALSE; |
| 561 | return TRUE; |
| 562 | } |
| 563 | |
| 564 | static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp) |
| 565 | { |
| 566 | if (!xdr_fhandle3(xdrs, &objp->fhandle)) |
| 567 | return FALSE; |
| 568 | if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0, |
| 569 | sizeof (int), (xdrproc_t) xdr_int)) |
| 570 | return FALSE; |
| 571 | return TRUE; |
| 572 | } |
| 573 | |
| 574 | static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp) |
| 575 | { |
| 576 | if (!xdr_enum(xdrs, (enum_t *) objp)) |
| 577 | return FALSE; |
| 578 | return TRUE; |
| 579 | } |
| 580 | |
| 581 | static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp) |
| 582 | { |
| 583 | if (!xdr_mountstat3(xdrs, &objp->fhs_status)) |
| 584 | return FALSE; |
| 585 | switch (objp->fhs_status) { |
| 586 | case MNT_OK: |
| 587 | if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo)) |
| 588 | return FALSE; |
| 589 | break; |
| 590 | default: |
| 591 | break; |
| 592 | } |
| 593 | return TRUE; |
| 594 | } |
| 595 | |
| 596 | #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2) |
| 597 | |
| 598 | /* |
| 599 | * nfs_mount_version according to the sources seen at compile time. |
| 600 | */ |
| 601 | static int nfs_mount_version; |
| 602 | static int kernel_version; |
| 603 | |
| 604 | /* |
| 605 | * Unfortunately, the kernel prints annoying console messages |
| 606 | * in case of an unexpected nfs mount version (instead of |
| 607 | * just returning some error). Therefore we'll have to try |
| 608 | * and figure out what version the kernel expects. |
| 609 | * |
| 610 | * Variables: |
| 611 | * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time |
| 612 | * NFS_MOUNT_VERSION: these nfsmount sources at compile time |
| 613 | * nfs_mount_version: version this source and running kernel can handle |
| 614 | */ |
| 615 | static void |
| 616 | find_kernel_nfs_mount_version(void) |
| 617 | { |
| 618 | if (kernel_version) |
| 619 | return; |
| 620 | |
| 621 | nfs_mount_version = 4; /* default */ |
| 622 | |
| 623 | kernel_version = get_linux_version_code(); |
| 624 | if (kernel_version) { |
| 625 | if (kernel_version < KERNEL_VERSION(2,1,32)) |
| 626 | nfs_mount_version = 1; |
| 627 | else if (kernel_version < KERNEL_VERSION(2,2,18) || |
| 628 | (kernel_version >= KERNEL_VERSION(2,3,0) && |
| 629 | kernel_version < KERNEL_VERSION(2,3,99))) |
| 630 | nfs_mount_version = 3; |
| 631 | /* else v4 since 2.3.99pre4 */ |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | static struct pmap * |
| 636 | get_mountport(struct sockaddr_in *server_addr, |
| 637 | long unsigned prog, |
| 638 | long unsigned version, |
| 639 | long unsigned proto, |
| 640 | long unsigned port) |
| 641 | { |
| 642 | struct pmaplist *pmap; |
| 643 | static struct pmap p = {0, 0, 0, 0}; |
| 644 | |
| 645 | server_addr->sin_port = PMAPPORT; |
| 646 | pmap = pmap_getmaps(server_addr); |
| 647 | |
| 648 | if (version > MAX_NFSPROT) |
| 649 | version = MAX_NFSPROT; |
| 650 | if (!prog) |
| 651 | prog = MOUNTPROG; |
| 652 | p.pm_prog = prog; |
| 653 | p.pm_vers = version; |
| 654 | p.pm_prot = proto; |
| 655 | p.pm_port = port; |
| 656 | |
| 657 | while (pmap) { |
| 658 | if (pmap->pml_map.pm_prog != prog) |
| 659 | goto next; |
| 660 | if (!version && p.pm_vers > pmap->pml_map.pm_vers) |
| 661 | goto next; |
| 662 | if (version > 2 && pmap->pml_map.pm_vers != version) |
| 663 | goto next; |
| 664 | if (version && version <= 2 && pmap->pml_map.pm_vers > 2) |
| 665 | goto next; |
| 666 | if (pmap->pml_map.pm_vers > MAX_NFSPROT || |
| 667 | (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) || |
| 668 | (port && pmap->pml_map.pm_port != port)) |
| 669 | goto next; |
| 670 | memcpy(&p, &pmap->pml_map, sizeof(p)); |
| 671 | next: |
| 672 | pmap = pmap->pml_next; |
| 673 | } |
| 674 | if (!p.pm_vers) |
| 675 | p.pm_vers = MOUNTVERS; |
| 676 | if (!p.pm_port) |
| 677 | p.pm_port = MOUNTPORT; |
| 678 | if (!p.pm_prot) |
| 679 | p.pm_prot = IPPROTO_TCP; |
| 680 | return &p; |
| 681 | } |
| 682 | |
| 683 | static int daemonize(void) |
| 684 | { |
| 685 | int fd; |
| 686 | int pid = fork(); |
| 687 | if (pid < 0) /* error */ |
| 688 | return -errno; |
| 689 | if (pid > 0) /* parent */ |
| 690 | return 0; |
| 691 | /* child */ |
| 692 | fd = xopen(bb_dev_null, O_RDWR); |
| 693 | dup2(fd, 0); |
| 694 | dup2(fd, 1); |
| 695 | dup2(fd, 2); |
| 696 | if (fd > 2) close(fd); |
| 697 | setsid(); |
| 698 | openlog(bb_applet_name, LOG_PID, LOG_DAEMON); |
| 699 | logmode = LOGMODE_SYSLOG; |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | // TODO |
| 704 | static inline int we_saw_this_host_before(const char *hostname) |
| 705 | { |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | /* RPC strerror analogs are terminally idiotic: |
| 710 | * *mandatory* prefix and \n at end. |
| 711 | * This hopefully helps. Usage: |
| 712 | * error_msg_rpc(clnt_*error*(" ")) */ |
| 713 | static void error_msg_rpc(const char *msg) |
| 714 | { |
| 715 | size_t len; |
| 716 | while (msg[0] == ' ' || msg[0] == ':') msg++; |
| 717 | len = strlen(msg); |
| 718 | while (len && msg[len-1] == '\n') len--; |
| 719 | bb_error_msg("%.*s", len, msg); |
| 720 | } |
| 721 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 722 | // NB: mp->xxx fields may be trashed on exit |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 723 | static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts) |
| 724 | { |
| 725 | CLIENT *mclient; |
| 726 | char *hostname; |
| 727 | char *pathname; |
| 728 | char *mounthost; |
| 729 | struct nfs_mount_data data; |
| 730 | char *opt; |
| 731 | struct hostent *hp; |
| 732 | struct sockaddr_in server_addr; |
| 733 | struct sockaddr_in mount_server_addr; |
| 734 | int msock, fsock; |
| 735 | union { |
| 736 | struct fhstatus nfsv2; |
| 737 | struct mountres3 nfsv3; |
| 738 | } status; |
| 739 | int daemonized; |
| 740 | char *s; |
| 741 | int port; |
| 742 | int mountport; |
| 743 | int proto; |
| 744 | int bg; |
| 745 | int soft; |
| 746 | int intr; |
| 747 | int posix; |
| 748 | int nocto; |
| 749 | int noac; |
| 750 | int nolock; |
| 751 | int retry; |
| 752 | int tcp; |
| 753 | int mountprog; |
| 754 | int mountvers; |
| 755 | int nfsprog; |
| 756 | int nfsvers; |
| 757 | int retval; |
| 758 | |
| 759 | find_kernel_nfs_mount_version(); |
| 760 | |
| 761 | daemonized = 0; |
| 762 | mounthost = NULL; |
| 763 | retval = ETIMEDOUT; |
| 764 | msock = fsock = -1; |
| 765 | mclient = NULL; |
| 766 | |
| 767 | /* NB: hostname, mounthost, filteropts must be free()d prior to return */ |
| 768 | |
| 769 | filteropts = xstrdup(filteropts); /* going to trash it later... */ |
| 770 | |
| 771 | hostname = xstrdup(mp->mnt_fsname); |
| 772 | /* mount_main() guarantees that ':' is there */ |
| 773 | s = strchr(hostname, ':'); |
| 774 | pathname = s + 1; |
| 775 | *s = '\0'; |
| 776 | /* Ignore all but first hostname in replicated mounts |
| 777 | until they can be fully supported. (mack@sgi.com) */ |
| 778 | s = strchr(hostname, ','); |
| 779 | if (s) { |
| 780 | *s = '\0'; |
| 781 | bb_error_msg("warning: multiple hostnames not supported"); |
| 782 | } |
| 783 | |
| 784 | server_addr.sin_family = AF_INET; |
| 785 | if (!inet_aton(hostname, &server_addr.sin_addr)) { |
| 786 | hp = gethostbyname(hostname); |
| 787 | if (hp == NULL) { |
| 788 | bb_herror_msg("%s", hostname); |
| 789 | goto fail; |
| 790 | } |
| 791 | if (hp->h_length > sizeof(struct in_addr)) { |
| 792 | bb_error_msg("got bad hp->h_length"); |
| 793 | hp->h_length = sizeof(struct in_addr); |
| 794 | } |
| 795 | memcpy(&server_addr.sin_addr, |
| 796 | hp->h_addr, hp->h_length); |
| 797 | } |
| 798 | |
| 799 | memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr)); |
| 800 | |
| 801 | /* add IP address to mtab options for use when unmounting */ |
| 802 | |
| 803 | if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */ |
| 804 | mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr)); |
| 805 | } else { |
| 806 | char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts, |
| 807 | mp->mnt_opts[0] ? "," : "", |
| 808 | inet_ntoa(server_addr.sin_addr)); |
| 809 | free(mp->mnt_opts); |
| 810 | mp->mnt_opts = tmp; |
| 811 | } |
| 812 | |
| 813 | /* Set default options. |
| 814 | * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to |
| 815 | * let the kernel decide. |
| 816 | * timeo is filled in after we know whether it'll be TCP or UDP. */ |
| 817 | memset(&data, 0, sizeof(data)); |
| 818 | data.retrans = 3; |
| 819 | data.acregmin = 3; |
| 820 | data.acregmax = 60; |
| 821 | data.acdirmin = 30; |
| 822 | data.acdirmax = 60; |
| 823 | data.namlen = NAME_MAX; |
| 824 | |
| 825 | bg = 0; |
| 826 | soft = 0; |
| 827 | intr = 0; |
| 828 | posix = 0; |
| 829 | nocto = 0; |
| 830 | nolock = 0; |
| 831 | noac = 0; |
| 832 | retry = 10000; /* 10000 minutes ~ 1 week */ |
| 833 | tcp = 0; |
| 834 | |
| 835 | mountprog = MOUNTPROG; |
| 836 | mountvers = 0; |
| 837 | port = 0; |
| 838 | mountport = 0; |
| 839 | nfsprog = 100003; |
| 840 | nfsvers = 0; |
| 841 | |
| 842 | /* parse options */ |
| 843 | |
| 844 | for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) { |
| 845 | char *opteq = strchr(opt, '='); |
| 846 | if (opteq) { |
| 847 | int val = atoi(opteq + 1); |
| 848 | *opteq = '\0'; |
| 849 | if (!strcmp(opt, "rsize")) |
| 850 | data.rsize = val; |
| 851 | else if (!strcmp(opt, "wsize")) |
| 852 | data.wsize = val; |
| 853 | else if (!strcmp(opt, "timeo")) |
| 854 | data.timeo = val; |
| 855 | else if (!strcmp(opt, "retrans")) |
| 856 | data.retrans = val; |
| 857 | else if (!strcmp(opt, "acregmin")) |
| 858 | data.acregmin = val; |
| 859 | else if (!strcmp(opt, "acregmax")) |
| 860 | data.acregmax = val; |
| 861 | else if (!strcmp(opt, "acdirmin")) |
| 862 | data.acdirmin = val; |
| 863 | else if (!strcmp(opt, "acdirmax")) |
| 864 | data.acdirmax = val; |
| 865 | else if (!strcmp(opt, "actimeo")) { |
| 866 | data.acregmin = val; |
| 867 | data.acregmax = val; |
| 868 | data.acdirmin = val; |
| 869 | data.acdirmax = val; |
| 870 | } |
| 871 | else if (!strcmp(opt, "retry")) |
| 872 | retry = val; |
| 873 | else if (!strcmp(opt, "port")) |
| 874 | port = val; |
| 875 | else if (!strcmp(opt, "mountport")) |
| 876 | mountport = val; |
| 877 | else if (!strcmp(opt, "mounthost")) |
| 878 | mounthost = xstrndup(opteq+1, |
| 879 | strcspn(opteq+1," \t\n\r,")); |
| 880 | else if (!strcmp(opt, "mountprog")) |
| 881 | mountprog = val; |
| 882 | else if (!strcmp(opt, "mountvers")) |
| 883 | mountvers = val; |
| 884 | else if (!strcmp(opt, "nfsprog")) |
| 885 | nfsprog = val; |
| 886 | else if (!strcmp(opt, "nfsvers") || |
| 887 | !strcmp(opt, "vers")) |
| 888 | nfsvers = val; |
| 889 | else if (!strcmp(opt, "proto")) { |
| 890 | if (!strncmp(opteq+1, "tcp", 3)) |
| 891 | tcp = 1; |
| 892 | else if (!strncmp(opteq+1, "udp", 3)) |
| 893 | tcp = 0; |
| 894 | else |
| 895 | bb_error_msg("warning: unrecognized proto= option"); |
| 896 | } else if (!strcmp(opt, "namlen")) { |
| 897 | if (nfs_mount_version >= 2) |
| 898 | data.namlen = val; |
| 899 | else |
| 900 | bb_error_msg("warning: option namlen is not supported\n"); |
| 901 | } else if (!strcmp(opt, "addr")) |
| 902 | /* ignore */; |
| 903 | else { |
| 904 | bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val); |
| 905 | goto fail; |
| 906 | } |
| 907 | } |
| 908 | else { |
| 909 | int val = 1; |
| 910 | if (!strncmp(opt, "no", 2)) { |
| 911 | val = 0; |
| 912 | opt += 2; |
| 913 | } |
| 914 | if (!strcmp(opt, "bg")) |
| 915 | bg = val; |
| 916 | else if (!strcmp(opt, "fg")) |
| 917 | bg = !val; |
| 918 | else if (!strcmp(opt, "soft")) |
| 919 | soft = val; |
| 920 | else if (!strcmp(opt, "hard")) |
| 921 | soft = !val; |
| 922 | else if (!strcmp(opt, "intr")) |
| 923 | intr = val; |
| 924 | else if (!strcmp(opt, "posix")) |
| 925 | posix = val; |
| 926 | else if (!strcmp(opt, "cto")) |
| 927 | nocto = !val; |
| 928 | else if (!strcmp(opt, "ac")) |
| 929 | noac = !val; |
| 930 | else if (!strcmp(opt, "tcp")) |
| 931 | tcp = val; |
| 932 | else if (!strcmp(opt, "udp")) |
| 933 | tcp = !val; |
| 934 | else if (!strcmp(opt, "lock")) { |
| 935 | if (nfs_mount_version >= 3) |
| 936 | nolock = !val; |
| 937 | else |
| 938 | bb_error_msg("warning: option nolock is not supported"); |
| 939 | } else { |
| 940 | bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt); |
| 941 | goto fail; |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP; |
| 946 | |
| 947 | data.flags = (soft ? NFS_MOUNT_SOFT : 0) |
| 948 | | (intr ? NFS_MOUNT_INTR : 0) |
| 949 | | (posix ? NFS_MOUNT_POSIX : 0) |
| 950 | | (nocto ? NFS_MOUNT_NOCTO : 0) |
| 951 | | (noac ? NFS_MOUNT_NOAC : 0); |
| 952 | if (nfs_mount_version >= 2) |
| 953 | data.flags |= (tcp ? NFS_MOUNT_TCP : 0); |
| 954 | if (nfs_mount_version >= 3) |
| 955 | data.flags |= (nolock ? NFS_MOUNT_NONLM : 0); |
| 956 | if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) { |
| 957 | bb_error_msg("NFSv%d not supported", nfsvers); |
| 958 | goto fail; |
| 959 | } |
| 960 | if (nfsvers && !mountvers) |
| 961 | mountvers = (nfsvers < 3) ? 1 : nfsvers; |
| 962 | if (nfsvers && nfsvers < mountvers) { |
| 963 | mountvers = nfsvers; |
| 964 | } |
| 965 | |
| 966 | /* Adjust options if none specified */ |
| 967 | if (!data.timeo) |
| 968 | data.timeo = tcp ? 70 : 7; |
| 969 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 970 | data.version = nfs_mount_version; |
| 971 | |
| 972 | if (vfsflags & MS_REMOUNT) |
| 973 | goto do_mount; |
| 974 | |
| 975 | /* |
| 976 | * If the previous mount operation on the same host was |
| 977 | * backgrounded, and the "bg" for this mount is also set, |
| 978 | * give up immediately, to avoid the initial timeout. |
| 979 | */ |
| 980 | if (bg && we_saw_this_host_before(hostname)) { |
| 981 | daemonized = daemonize(); /* parent or error */ |
| 982 | if (daemonized <= 0) { /* parent or error */ |
| 983 | retval = -daemonized; |
| 984 | goto ret; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* create mount daemon client */ |
| 989 | /* See if the nfs host = mount host. */ |
| 990 | if (mounthost) { |
| 991 | if (mounthost[0] >= '0' && mounthost[0] <= '9') { |
| 992 | mount_server_addr.sin_family = AF_INET; |
| 993 | mount_server_addr.sin_addr.s_addr = inet_addr(hostname); |
| 994 | } else { |
| 995 | hp = gethostbyname(mounthost); |
| 996 | if (hp == NULL) { |
| 997 | bb_herror_msg("%s", mounthost); |
| 998 | goto fail; |
| 999 | } else { |
| 1000 | if (hp->h_length > sizeof(struct in_addr)) { |
| 1001 | bb_error_msg("got bad hp->h_length?"); |
| 1002 | hp->h_length = sizeof(struct in_addr); |
| 1003 | } |
| 1004 | mount_server_addr.sin_family = AF_INET; |
| 1005 | memcpy(&mount_server_addr.sin_addr, |
| 1006 | hp->h_addr, hp->h_length); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * The following loop implements the mount retries. When the mount |
| 1013 | * times out, and the "bg" option is set, we background ourself |
| 1014 | * and continue trying. |
| 1015 | * |
| 1016 | * The case where the mount point is not present and the "bg" |
| 1017 | * option is set, is treated as a timeout. This is done to |
| 1018 | * support nested mounts. |
| 1019 | * |
| 1020 | * The "retry" count specified by the user is the number of |
| 1021 | * minutes to retry before giving up. |
| 1022 | */ |
| 1023 | { |
| 1024 | struct timeval total_timeout; |
| 1025 | struct timeval retry_timeout; |
| 1026 | struct pmap* pm_mnt; |
| 1027 | time_t t; |
| 1028 | time_t prevt; |
| 1029 | time_t timeout; |
| 1030 | |
| 1031 | retry_timeout.tv_sec = 3; |
| 1032 | retry_timeout.tv_usec = 0; |
| 1033 | total_timeout.tv_sec = 20; |
| 1034 | total_timeout.tv_usec = 0; |
| 1035 | timeout = time(NULL) + 60 * retry; |
| 1036 | prevt = 0; |
| 1037 | t = 30; |
| 1038 | retry: |
| 1039 | /* be careful not to use too many CPU cycles */ |
| 1040 | if (t - prevt < 30) |
| 1041 | sleep(30); |
| 1042 | |
| 1043 | pm_mnt = get_mountport(&mount_server_addr, |
| 1044 | mountprog, |
| 1045 | mountvers, |
| 1046 | proto, |
| 1047 | mountport); |
| 1048 | nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers; |
| 1049 | |
| 1050 | /* contact the mount daemon via TCP */ |
| 1051 | mount_server_addr.sin_port = htons(pm_mnt->pm_port); |
| 1052 | msock = RPC_ANYSOCK; |
| 1053 | |
| 1054 | switch (pm_mnt->pm_prot) { |
| 1055 | case IPPROTO_UDP: |
| 1056 | mclient = clntudp_create(&mount_server_addr, |
| 1057 | pm_mnt->pm_prog, |
| 1058 | pm_mnt->pm_vers, |
| 1059 | retry_timeout, |
| 1060 | &msock); |
| 1061 | if (mclient) |
| 1062 | break; |
| 1063 | mount_server_addr.sin_port = htons(pm_mnt->pm_port); |
| 1064 | msock = RPC_ANYSOCK; |
| 1065 | case IPPROTO_TCP: |
| 1066 | mclient = clnttcp_create(&mount_server_addr, |
| 1067 | pm_mnt->pm_prog, |
| 1068 | pm_mnt->pm_vers, |
| 1069 | &msock, 0, 0); |
| 1070 | break; |
| 1071 | default: |
| 1072 | mclient = 0; |
| 1073 | } |
| 1074 | if (!mclient) { |
| 1075 | if (!daemonized && prevt == 0) |
| 1076 | error_msg_rpc(clnt_spcreateerror(" ")); |
| 1077 | } else { |
| 1078 | enum clnt_stat clnt_stat; |
| 1079 | /* try to mount hostname:pathname */ |
| 1080 | mclient->cl_auth = authunix_create_default(); |
| 1081 | |
| 1082 | /* make pointers in xdr_mountres3 NULL so |
| 1083 | * that xdr_array allocates memory for us |
| 1084 | */ |
| 1085 | memset(&status, 0, sizeof(status)); |
| 1086 | |
| 1087 | if (pm_mnt->pm_vers == 3) |
| 1088 | clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT, |
| 1089 | (xdrproc_t) xdr_dirpath, |
| 1090 | (caddr_t) &pathname, |
| 1091 | (xdrproc_t) xdr_mountres3, |
| 1092 | (caddr_t) &status, |
| 1093 | total_timeout); |
| 1094 | else |
| 1095 | clnt_stat = clnt_call(mclient, MOUNTPROC_MNT, |
| 1096 | (xdrproc_t) xdr_dirpath, |
| 1097 | (caddr_t) &pathname, |
| 1098 | (xdrproc_t) xdr_fhstatus, |
| 1099 | (caddr_t) &status, |
| 1100 | total_timeout); |
| 1101 | |
| 1102 | if (clnt_stat == RPC_SUCCESS) |
| 1103 | goto prepare_kernel_data; /* we're done */ |
| 1104 | if (errno != ECONNREFUSED) { |
| 1105 | error_msg_rpc(clnt_sperror(mclient, " ")); |
| 1106 | goto fail; /* don't retry */ |
| 1107 | } |
| 1108 | /* Connection refused */ |
| 1109 | if (!daemonized && prevt == 0) /* print just once */ |
| 1110 | error_msg_rpc(clnt_sperror(mclient, " ")); |
| 1111 | auth_destroy(mclient->cl_auth); |
| 1112 | clnt_destroy(mclient); |
| 1113 | mclient = 0; |
| 1114 | close(msock); |
| 1115 | } |
| 1116 | |
| 1117 | /* Timeout. We are going to retry... maybe */ |
| 1118 | |
| 1119 | if (!bg) |
| 1120 | goto fail; |
| 1121 | if (!daemonized) { |
| 1122 | daemonized = daemonize(); |
| 1123 | if (daemonized <= 0) { /* parent or error */ |
| 1124 | retval = -daemonized; |
| 1125 | goto ret; |
| 1126 | } |
| 1127 | } |
| 1128 | prevt = t; |
| 1129 | t = time(NULL); |
| 1130 | if (t >= timeout) |
| 1131 | /* TODO error message */ |
| 1132 | goto fail; |
| 1133 | |
| 1134 | goto retry; |
| 1135 | } |
| 1136 | |
| 1137 | prepare_kernel_data: |
| 1138 | |
| 1139 | if (nfsvers == 2) { |
| 1140 | if (status.nfsv2.fhs_status != 0) { |
| 1141 | bb_error_msg("%s:%s failed, reason given by server: %s", |
| 1142 | hostname, pathname, |
| 1143 | nfs_strerror(status.nfsv2.fhs_status)); |
| 1144 | goto fail; |
| 1145 | } |
| 1146 | memcpy(data.root.data, |
| 1147 | (char *) status.nfsv2.fhstatus_u.fhs_fhandle, |
| 1148 | NFS_FHSIZE); |
| 1149 | data.root.size = NFS_FHSIZE; |
| 1150 | memcpy(data.old_root.data, |
| 1151 | (char *) status.nfsv2.fhstatus_u.fhs_fhandle, |
| 1152 | NFS_FHSIZE); |
| 1153 | } else { |
| 1154 | fhandle3 *my_fhandle; |
| 1155 | if (status.nfsv3.fhs_status != 0) { |
| 1156 | bb_error_msg("%s:%s failed, reason given by server: %s", |
| 1157 | hostname, pathname, |
| 1158 | nfs_strerror(status.nfsv3.fhs_status)); |
| 1159 | goto fail; |
| 1160 | } |
| 1161 | my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle; |
| 1162 | memset(data.old_root.data, 0, NFS_FHSIZE); |
| 1163 | memset(&data.root, 0, sizeof(data.root)); |
| 1164 | data.root.size = my_fhandle->fhandle3_len; |
| 1165 | memcpy(data.root.data, |
| 1166 | (char *) my_fhandle->fhandle3_val, |
| 1167 | my_fhandle->fhandle3_len); |
| 1168 | |
| 1169 | data.flags |= NFS_MOUNT_VER3; |
| 1170 | } |
| 1171 | |
| 1172 | /* create nfs socket for kernel */ |
| 1173 | |
| 1174 | if (tcp) { |
| 1175 | if (nfs_mount_version < 3) { |
| 1176 | bb_error_msg("NFS over TCP is not supported"); |
| 1177 | goto fail; |
| 1178 | } |
| 1179 | fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1180 | } else |
| 1181 | fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 1182 | if (fsock < 0) { |
| 1183 | bb_perror_msg("nfs socket"); |
| 1184 | goto fail; |
| 1185 | } |
| 1186 | if (bindresvport(fsock, 0) < 0) { |
| 1187 | bb_perror_msg("nfs bindresvport"); |
| 1188 | goto fail; |
| 1189 | } |
| 1190 | if (port == 0) { |
| 1191 | server_addr.sin_port = PMAPPORT; |
| 1192 | port = pmap_getport(&server_addr, nfsprog, nfsvers, |
| 1193 | tcp ? IPPROTO_TCP : IPPROTO_UDP); |
| 1194 | if (port == 0) |
| 1195 | port = NFS_PORT; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1196 | } |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1197 | server_addr.sin_port = htons(port); |
| 1198 | |
| 1199 | /* prepare data structure for kernel */ |
| 1200 | |
| 1201 | data.fd = fsock; |
| 1202 | memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr)); |
| 1203 | strncpy(data.hostname, hostname, sizeof(data.hostname)); |
| 1204 | |
| 1205 | /* clean up */ |
| 1206 | |
| 1207 | auth_destroy(mclient->cl_auth); |
| 1208 | clnt_destroy(mclient); |
| 1209 | close(msock); |
| 1210 | |
| 1211 | if (bg) { |
| 1212 | /* We must wait until mount directory is available */ |
| 1213 | struct stat statbuf; |
| 1214 | int delay = 1; |
| 1215 | while (stat(mp->mnt_dir, &statbuf) == -1) { |
| 1216 | if (!daemonized) { |
| 1217 | daemonized = daemonize(); |
| 1218 | if (daemonized <= 0) { /* parent or error */ |
| 1219 | retval = -daemonized; |
| 1220 | goto ret; |
| 1221 | } |
| 1222 | } |
| 1223 | sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */ |
| 1224 | delay *= 2; |
| 1225 | if (delay > 30) |
| 1226 | delay = 30; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | do_mount: /* perform actual mount */ |
| 1231 | |
| 1232 | mp->mnt_type = "nfs"; |
| 1233 | retval = mount_it_now(mp, vfsflags, (char*)&data); |
| 1234 | goto ret; |
| 1235 | |
| 1236 | fail: /* abort */ |
| 1237 | |
| 1238 | if (msock != -1) { |
| 1239 | if (mclient) { |
| 1240 | auth_destroy(mclient->cl_auth); |
| 1241 | clnt_destroy(mclient); |
| 1242 | } |
| 1243 | close(msock); |
| 1244 | } |
| 1245 | if (fsock != -1) |
| 1246 | close(fsock); |
| 1247 | |
| 1248 | ret: |
| 1249 | free(hostname); |
| 1250 | free(mounthost); |
| 1251 | free(filteropts); |
| 1252 | return retval; |
| 1253 | } |
| 1254 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1255 | #else /* !ENABLE_FEATURE_MOUNT_NFS */ |
| 1256 | |
| 1257 | /* Never called. Call should be optimized out. */ |
| 1258 | int nfsmount(struct mntent *mp, int vfsflags, char *filteropts); |
| 1259 | |
| 1260 | #endif /* !ENABLE_FEATURE_MOUNT_NFS */ |
| 1261 | |
| 1262 | // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem |
| 1263 | // type detection. Returns 0 for success, nonzero for failure. |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1264 | // NB: mp->xxx fields may be trashed on exit |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1265 | static int singlemount(struct mntent *mp, int ignore_busy) |
| 1266 | { |
| 1267 | int rc = -1, vfsflags; |
| 1268 | char *loopFile = 0, *filteropts = 0; |
| 1269 | llist_t *fl = 0; |
| 1270 | struct stat st; |
| 1271 | |
| 1272 | vfsflags = parse_mount_options(mp->mnt_opts, &filteropts); |
| 1273 | |
| 1274 | // Treat fstype "auto" as unspecified. |
| 1275 | |
| 1276 | if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0; |
| 1277 | |
| 1278 | // Might this be an CIFS filesystem? |
| 1279 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 1280 | if (ENABLE_FEATURE_MOUNT_CIFS && |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1281 | (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) && |
| 1282 | (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\'))) |
| 1283 | { |
| 1284 | struct hostent *he; |
| 1285 | char ip[32], *s; |
| 1286 | |
| 1287 | rc = 1; |
| 1288 | // Replace '/' with '\' and verify that unc points to "//server/share". |
| 1289 | |
| 1290 | for (s = mp->mnt_fsname; *s; ++s) |
| 1291 | if (*s == '/') *s = '\\'; |
| 1292 | |
| 1293 | // get server IP |
| 1294 | |
| 1295 | s = strrchr(mp->mnt_fsname, '\\'); |
| 1296 | if (s == mp->mnt_fsname+1) goto report_error; |
| 1297 | *s = 0; |
| 1298 | he = gethostbyname(mp->mnt_fsname+2); |
| 1299 | *s = '\\'; |
| 1300 | if (!he) goto report_error; |
| 1301 | |
| 1302 | // Insert ip=... option into string flags. (NOTE: Add IPv6 support.) |
| 1303 | |
| 1304 | sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1], |
| 1305 | he->h_addr[2], he->h_addr[3]); |
| 1306 | parse_mount_options(ip, &filteropts); |
| 1307 | |
| 1308 | // compose new unc '\\server-ip\share' |
| 1309 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1310 | mp->mnt_fsname = xasprintf("\\\\%s%s", ip+3, |
| 1311 | strchr(mp->mnt_fsname+2,'\\')); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1312 | |
| 1313 | // lock is required |
| 1314 | vfsflags |= MS_MANDLOCK; |
| 1315 | |
| 1316 | mp->mnt_type = "cifs"; |
| 1317 | rc = mount_it_now(mp, vfsflags, filteropts); |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1318 | if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1319 | goto report_error; |
| 1320 | } |
| 1321 | |
| 1322 | // Might this be an NFS filesystem? |
| 1323 | |
| 1324 | if (ENABLE_FEATURE_MOUNT_NFS && |
| 1325 | (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) && |
| 1326 | strchr(mp->mnt_fsname, ':') != NULL) |
| 1327 | { |
| 1328 | rc = nfsmount(mp, vfsflags, filteropts); |
| 1329 | goto report_error; |
| 1330 | } |
| 1331 | |
| 1332 | // Look at the file. (Not found isn't a failure for remount, or for |
| 1333 | // a synthetic filesystem like proc or sysfs.) |
| 1334 | |
| 1335 | if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) |
| 1336 | { |
| 1337 | // Do we need to allocate a loopback device for it? |
| 1338 | |
| 1339 | if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) { |
| 1340 | loopFile = bb_simplify_path(mp->mnt_fsname); |
| 1341 | mp->mnt_fsname = 0; |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 1342 | switch (set_loop(&(mp->mnt_fsname), loopFile, 0)) { |
Denis Vlasenko | 13b4924 | 2006-09-17 15:04:35 +0000 | [diff] [blame] | 1343 | case 0: |
| 1344 | case 1: |
| 1345 | break; |
| 1346 | default: |
| 1347 | bb_error_msg( errno == EPERM || errno == EACCES |
| 1348 | ? bb_msg_perm_denied_are_you_root |
| 1349 | : "cannot setup loop device"); |
| 1350 | return errno; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | // Autodetect bind mounts |
| 1354 | |
| 1355 | } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) |
| 1356 | vfsflags |= MS_BIND; |
| 1357 | } |
| 1358 | |
| 1359 | /* If we know the fstype (or don't need to), jump straight |
| 1360 | * to the actual mount. */ |
| 1361 | |
| 1362 | if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) |
| 1363 | rc = mount_it_now(mp, vfsflags, filteropts); |
| 1364 | |
| 1365 | // Loop through filesystem types until mount succeeds or we run out |
| 1366 | |
| 1367 | else { |
| 1368 | |
| 1369 | /* Initialize list of block backed filesystems. This has to be |
| 1370 | * done here so that during "mount -a", mounts after /proc shows up |
| 1371 | * can autodetect. */ |
| 1372 | |
| 1373 | if (!fslist) { |
| 1374 | fslist = get_block_backed_filesystems(); |
| 1375 | if (ENABLE_FEATURE_CLEAN_UP && fslist) |
| 1376 | atexit(delete_block_backed_filesystems); |
| 1377 | } |
| 1378 | |
| 1379 | for (fl = fslist; fl; fl = fl->link) { |
| 1380 | mp->mnt_type = fl->data; |
Denis Vlasenko | 13b4924 | 2006-09-17 15:04:35 +0000 | [diff] [blame] | 1381 | rc = mount_it_now(mp, vfsflags, filteropts); |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 1382 | if (!rc) break; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | // If mount failed, clean up loop file (if any). |
| 1387 | |
| 1388 | if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) { |
| 1389 | del_loop(mp->mnt_fsname); |
| 1390 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 1391 | free(loopFile); |
| 1392 | free(mp->mnt_fsname); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | report_error: |
| 1397 | if (ENABLE_FEATURE_CLEAN_UP) free(filteropts); |
| 1398 | |
| 1399 | if (rc && errno == EBUSY && ignore_busy) rc = 0; |
| 1400 | if (rc < 0) |
| 1401 | /* perror here sometimes says "mounting ... on ... failed: Success" */ |
| 1402 | bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir); |
| 1403 | |
| 1404 | return rc; |
| 1405 | } |
| 1406 | |
| 1407 | // Parse options, if necessary parse fstab/mtab, and call singlemount for |
| 1408 | // each directory to be mounted. |
| 1409 | |
| 1410 | int mount_main(int argc, char **argv) |
| 1411 | { |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1412 | enum { OPT_ALL = 0x8 }; |
| 1413 | |
| 1414 | char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0; |
| 1415 | char *opt_o, *fstabname; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1416 | FILE *fstab; |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1417 | int i, j, rc = 0; |
| 1418 | unsigned long opt; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1419 | struct mntent mtpair[2], *mtcur = mtpair; |
| 1420 | |
| 1421 | /* parse long options, like --bind and --move. Note that -o option |
| 1422 | * and --option are synonymous. Yes, this means --remount,rw works. */ |
| 1423 | |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1424 | for (i = j = 0; i < argc; i++) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1425 | if (argv[i][0] == '-' && argv[i][1] == '-') { |
| 1426 | append_mount_options(&cmdopts,argv[i]+2); |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1427 | } else argv[j++] = argv[i]; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1428 | } |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1429 | argc = j; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1430 | |
| 1431 | // Parse remaining options |
| 1432 | |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1433 | opt = bb_getopt_ulflags(argc, argv, "o:t:rwavnf", &opt_o, &fstype); |
| 1434 | if (opt & 1) // -o |
| 1435 | append_mount_options(&cmdopts, opt_o); |
| 1436 | //if (opt & 1) // -t |
| 1437 | if (opt & 2) // -r |
| 1438 | append_mount_options(&cmdopts, "ro"); |
| 1439 | if (opt & 4) // -w |
| 1440 | append_mount_options(&cmdopts, "rw"); |
| 1441 | //if (opt & 8) // -a |
| 1442 | if (opt & 0x10) // -n |
| 1443 | USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE); |
| 1444 | if (opt & 0x20) // -f |
| 1445 | USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE); |
| 1446 | //if (opt & 0x40) // ignore -v |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1447 | argv += optind; |
| 1448 | argc -= optind; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Three or more non-option arguments? Die with a usage message. |
| 1451 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1452 | if (argc > 2) bb_show_usage(); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1453 | |
| 1454 | // If we have no arguments, show currently mounted filesystems |
| 1455 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1456 | if (!argc) { |
Denis Vlasenko | 9c99b62 | 2006-09-17 15:05:31 +0000 | [diff] [blame] | 1457 | if (!(opt & OPT_ALL)) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1458 | FILE *mountTable = setmntent(bb_path_mtab_file, "r"); |
| 1459 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 1460 | if (!mountTable) bb_error_msg_and_die("no %s",bb_path_mtab_file); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1461 | |
| 1462 | while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1, |
| 1463 | sizeof(bb_common_bufsiz1))) |
| 1464 | { |
| 1465 | // Don't show rootfs. |
| 1466 | if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue; |
| 1467 | |
| 1468 | if (!fstype || !strcmp(mtpair->mnt_type, fstype)) |
| 1469 | printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname, |
| 1470 | mtpair->mnt_dir, mtpair->mnt_type, |
| 1471 | mtpair->mnt_opts); |
| 1472 | } |
| 1473 | if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable); |
| 1474 | return EXIT_SUCCESS; |
| 1475 | } |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1476 | } else storage_path = bb_simplify_path(argv[0]); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1477 | |
| 1478 | // When we have two arguments, the second is the directory and we can |
| 1479 | // skip looking at fstab entirely. We can always abspath() the directory |
| 1480 | // argument when we get it. |
| 1481 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1482 | if (argc == 2) { |
| 1483 | mtpair->mnt_fsname = argv[0]; |
| 1484 | mtpair->mnt_dir = argv[1]; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1485 | mtpair->mnt_type = fstype; |
| 1486 | mtpair->mnt_opts = cmdopts; |
| 1487 | rc = singlemount(mtpair, 0); |
| 1488 | goto clean_up; |
| 1489 | } |
| 1490 | |
| 1491 | // If we have a shared subtree flag, don't worry about fstab or mtab. |
| 1492 | i = parse_mount_options(cmdopts,0); |
| 1493 | if (ENABLE_FEATURE_MOUNT_FLAGS && |
| 1494 | (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE ))) |
| 1495 | { |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1496 | rc = mount("", argv[0], "", i, ""); |
| 1497 | if (rc) bb_perror_msg_and_die("%s", argv[0]); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1498 | goto clean_up; |
| 1499 | } |
| 1500 | |
| 1501 | // Open either fstab or mtab |
| 1502 | |
| 1503 | if (parse_mount_options(cmdopts,0) & MS_REMOUNT) |
| 1504 | fstabname = bb_path_mtab_file; |
| 1505 | else fstabname="/etc/fstab"; |
| 1506 | |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 1507 | fstab = setmntent(fstabname,"r"); |
| 1508 | if (!fstab) |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1509 | bb_perror_msg_and_die("cannot read %s",fstabname); |
| 1510 | |
| 1511 | // Loop through entries until we find what we're looking for. |
| 1512 | |
| 1513 | memset(mtpair,0,sizeof(mtpair)); |
| 1514 | for (;;) { |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 1515 | struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Get next fstab entry |
| 1518 | |
| 1519 | if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1 |
| 1520 | + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0), |
| 1521 | sizeof(bb_common_bufsiz1)/2)) |
| 1522 | { |
| 1523 | // Were we looking for something specific? |
| 1524 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1525 | if (argc) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1526 | |
| 1527 | // If we didn't find anything, complain. |
| 1528 | |
| 1529 | if (!mtnext->mnt_fsname) |
| 1530 | bb_error_msg_and_die("can't find %s in %s", |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1531 | argv[0], fstabname); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1532 | |
| 1533 | // Mount the last thing we found. |
| 1534 | |
| 1535 | mtcur = mtnext; |
| 1536 | mtcur->mnt_opts = xstrdup(mtcur->mnt_opts); |
| 1537 | append_mount_options(&(mtcur->mnt_opts),cmdopts); |
| 1538 | rc = singlemount(mtcur, 0); |
| 1539 | free(mtcur->mnt_opts); |
| 1540 | } |
| 1541 | goto clean_up; |
| 1542 | } |
| 1543 | |
| 1544 | /* If we're trying to mount something specific and this isn't it, |
| 1545 | * skip it. Note we must match both the exact text in fstab (ala |
| 1546 | * "proc") or a full path from root */ |
| 1547 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1548 | if (argc) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1549 | |
| 1550 | // Is this what we're looking for? |
| 1551 | |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1552 | if (strcmp(argv[0],mtcur->mnt_fsname) && |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1553 | strcmp(storage_path,mtcur->mnt_fsname) && |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1554 | strcmp(argv[0],mtcur->mnt_dir) && |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1555 | strcmp(storage_path,mtcur->mnt_dir)) continue; |
| 1556 | |
| 1557 | // Remember this entry. Something later may have overmounted |
| 1558 | // it, and we want the _last_ match. |
| 1559 | |
| 1560 | mtcur = mtnext; |
| 1561 | |
| 1562 | // If we're mounting all. |
| 1563 | |
| 1564 | } else { |
| 1565 | |
| 1566 | // Do we need to match a filesystem type? |
| 1567 | if (fstype && strcmp(mtcur->mnt_type,fstype)) continue; |
| 1568 | |
| 1569 | // Skip noauto and swap anyway. |
| 1570 | |
| 1571 | if (parse_mount_options(mtcur->mnt_opts,0) |
| 1572 | & (MOUNT_NOAUTO | MOUNT_SWAP)) continue; |
| 1573 | |
| 1574 | // Mount this thing. |
| 1575 | |
| 1576 | if (singlemount(mtcur, 1)) { |
| 1577 | /* Count number of failed mounts */ |
| 1578 | rc++; |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab); |
| 1583 | |
| 1584 | clean_up: |
| 1585 | |
| 1586 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 1587 | free(storage_path); |
| 1588 | free(cmdopts); |
| 1589 | } |
| 1590 | |
| 1591 | return rc; |
| 1592 | } |