Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <mntent.h> |
Eric Andersen | c4cef5a | 2001-04-01 16:01:11 +0000 | [diff] [blame] | 28 | #include "libbb.h" |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 29 | |
Eric Andersen | a62665b | 2004-10-08 08:57:35 +0000 | [diff] [blame] | 30 | #define MTAB_MAX_ENTRIES 40 |
Eric Andersen | 741f2c9 | 2001-03-14 01:48:10 +0000 | [diff] [blame] | 31 | static const int MS_RDONLY = 1; /* Mount read-only. */ |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 32 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | void erase_mtab(const char *name) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 34 | { |
Eric Andersen | a62665b | 2004-10-08 08:57:35 +0000 | [diff] [blame] | 35 | struct mntent entries[MTAB_MAX_ENTRIES]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | int count = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | FILE *mountTable = setmntent(bb_path_mtab_file, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | struct mntent *m; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 39 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 40 | /* Check if reading the mtab file failed */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | if (mountTable == 0 |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 42 | /* Bummer. fall back on trying the /proc filesystem */ |
| 43 | && (mountTable = setmntent("/proc/mounts", "r")) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | |
Eric Andersen | a62665b | 2004-10-08 08:57:35 +0000 | [diff] [blame] | 48 | while (((m = getmntent(mountTable)) != 0) && (count < MTAB_MAX_ENTRIES)) |
| 49 | { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 50 | entries[count].mnt_fsname = strdup(m->mnt_fsname); |
| 51 | entries[count].mnt_dir = strdup(m->mnt_dir); |
| 52 | entries[count].mnt_type = strdup(m->mnt_type); |
| 53 | entries[count].mnt_opts = strdup(m->mnt_opts); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 54 | entries[count].mnt_freq = m->mnt_freq; |
| 55 | entries[count].mnt_passno = m->mnt_passno; |
| 56 | count++; |
| 57 | } |
| 58 | endmntent(mountTable); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | if ((mountTable = setmntent(bb_path_mtab_file, "w"))) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | int i; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 61 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | for (i = 0; i < count; i++) { |
| 63 | int result = (strcmp(entries[i].mnt_fsname, name) == 0 |
| 64 | || strcmp(entries[i].mnt_dir, name) == 0); |
| 65 | |
| 66 | if (result) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 67 | continue; |
| 68 | else |
| 69 | addmntent(mountTable, &entries[i]); |
| 70 | } |
| 71 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | } else if (errno != EROFS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | void write_mtab(char *blockDevice, char *directory, |
| 77 | char *filesystemType, long flags, char *string_flags) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 78 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | FILE *mountTable = setmntent(bb_path_mtab_file, "a+"); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 80 | struct mntent m; |
| 81 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | if (mountTable == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | if (mountTable) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | int length = strlen(directory); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 88 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | if (length > 1 && directory[length - 1] == '/') |
| 90 | directory[length - 1] = '\0'; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 91 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | if (filesystemType == 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 93 | struct mntent *p = find_mount_point(blockDevice, "/proc/mounts"); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 94 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | if (p && p->mnt_type) |
| 96 | filesystemType = p->mnt_type; |
| 97 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | m.mnt_fsname = blockDevice; |
| 99 | m.mnt_dir = directory; |
| 100 | m.mnt_type = filesystemType ? filesystemType : "default"; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 101 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | if (*string_flags) { |
| 103 | m.mnt_opts = string_flags; |
| 104 | } else { |
| 105 | if ((flags | MS_RDONLY) == flags) |
| 106 | m.mnt_opts = "ro"; |
| 107 | else |
| 108 | m.mnt_opts = "rw"; |
| 109 | } |
| 110 | |
| 111 | m.mnt_freq = 0; |
| 112 | m.mnt_passno = 0; |
| 113 | addmntent(mountTable, &m); |
| 114 | endmntent(mountTable); |
| 115 | } |
| 116 | } |