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