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