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