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 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 10 | #include <mntent.h> |
Eric Andersen | c4cef5a | 2001-04-01 16:01:11 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 13 | #if ENABLE_FEATURE_MTAB_SUPPORT |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 14 | void FAST_FUNC erase_mtab(const char *name) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 15 | { |
Denis Vlasenko | 96c45b0 | 2008-07-09 19:44:08 +0000 | [diff] [blame^] | 16 | struct mntent *entries; |
| 17 | int i, count; |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 18 | FILE *mountTable; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 19 | struct mntent *m; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 21 | mountTable = setmntent(bb_path_mtab_file, "r"); |
| 22 | /* Bummer. Fall back on trying the /proc filesystem */ |
| 23 | if (!mountTable) mountTable = setmntent("/proc/mounts", "r"); |
| 24 | if (!mountTable) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 26 | return; |
| 27 | } |
| 28 | |
Denis Vlasenko | 96c45b0 | 2008-07-09 19:44:08 +0000 | [diff] [blame^] | 29 | entries = NULL; |
| 30 | count = 0; |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 31 | while ((m = getmntent(mountTable)) != 0) { |
Denis Vlasenko | 96c45b0 | 2008-07-09 19:44:08 +0000 | [diff] [blame^] | 32 | entries = xrealloc_vector(entries, 3, count); |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 33 | entries[count].mnt_fsname = xstrdup(m->mnt_fsname); |
| 34 | entries[count].mnt_dir = xstrdup(m->mnt_dir); |
| 35 | entries[count].mnt_type = xstrdup(m->mnt_type); |
| 36 | entries[count].mnt_opts = xstrdup(m->mnt_opts); |
| 37 | entries[count].mnt_freq = m->mnt_freq; |
| 38 | entries[count].mnt_passno = m->mnt_passno; |
Denis Vlasenko | 96c45b0 | 2008-07-09 19:44:08 +0000 | [diff] [blame^] | 39 | count++; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 40 | } |
| 41 | endmntent(mountTable); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 96c45b0 | 2008-07-09 19:44:08 +0000 | [diff] [blame^] | 43 | //TODO: make update atomic |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 44 | mountTable = setmntent(bb_path_mtab_file, "w"); |
| 45 | if (mountTable) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | for (i = 0; i < count; i++) { |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 47 | if (strcmp(entries[i].mnt_fsname, name) != 0 |
| 48 | && strcmp(entries[i].mnt_dir, name) != 0) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 49 | addmntent(mountTable, &entries[i]); |
| 50 | } |
| 51 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | } else if (errno != EROFS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 54 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 55 | #endif |