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