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 <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <errno.h> |
| 13 | #include <string.h> |
| 14 | #include <stdio.h> |
| 15 | #include <mntent.h> |
Eric Andersen | c4cef5a | 2001-04-01 16:01:11 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 17 | |
Eric Andersen | a62665b | 2004-10-08 08:57:35 +0000 | [diff] [blame] | 18 | #define MTAB_MAX_ENTRIES 40 |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 19 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 20 | #ifdef CONFIG_FEATURE_MTAB_SUPPORT |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 21 | void erase_mtab(const char *name) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 22 | { |
Eric Andersen | a62665b | 2004-10-08 08:57:35 +0000 | [diff] [blame] | 23 | struct mntent entries[MTAB_MAX_ENTRIES]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 24 | int count = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | FILE *mountTable = setmntent(bb_path_mtab_file, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 26 | struct mntent *m; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 28 | /* Check if reading the mtab file failed */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | if (mountTable == 0 |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 30 | /* Bummer. fall back on trying the /proc filesystem */ |
| 31 | && (mountTable = setmntent("/proc/mounts", "r")) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | |
Eric Andersen | a62665b | 2004-10-08 08:57:35 +0000 | [diff] [blame] | 36 | while (((m = getmntent(mountTable)) != 0) && (count < MTAB_MAX_ENTRIES)) |
| 37 | { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 38 | entries[count].mnt_fsname = strdup(m->mnt_fsname); |
| 39 | entries[count].mnt_dir = strdup(m->mnt_dir); |
| 40 | entries[count].mnt_type = strdup(m->mnt_type); |
| 41 | entries[count].mnt_opts = strdup(m->mnt_opts); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 42 | entries[count].mnt_freq = m->mnt_freq; |
| 43 | entries[count].mnt_passno = m->mnt_passno; |
| 44 | count++; |
| 45 | } |
| 46 | endmntent(mountTable); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | if ((mountTable = setmntent(bb_path_mtab_file, "w"))) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | int i; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 49 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | for (i = 0; i < count; i++) { |
| 51 | int result = (strcmp(entries[i].mnt_fsname, name) == 0 |
| 52 | || strcmp(entries[i].mnt_dir, name) == 0); |
| 53 | |
| 54 | if (result) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 55 | continue; |
| 56 | else |
| 57 | addmntent(mountTable, &entries[i]); |
| 58 | } |
| 59 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | } else if (errno != EROFS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 62 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 63 | #endif |