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