blob: 22bff649004cdce14d756d4e88c1d48793f23f17 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
9
Eric Andersenbc341901999-11-06 04:56:00 +000010#include <mntent.h>
Eric Andersenc4cef5a2001-04-01 16:01:11 +000011#include "libbb.h"
Eric Andersenbc341901999-11-06 04:56:00 +000012
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000013#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000014void FAST_FUNC erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000015{
Denis Vlasenko96c45b02008-07-09 19:44:08 +000016 struct mntent *entries;
17 int i, count;
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000018 FILE *mountTable;
Erik Andersene49d5ec2000-02-08 19:58:47 +000019 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000020
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000021 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 cad53642003-03-19 09:13:01 +000025 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000026 return;
27 }
28
Denis Vlasenko96c45b02008-07-09 19:44:08 +000029 entries = NULL;
30 count = 0;
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000031 while ((m = getmntent(mountTable)) != 0) {
Denis Vlasenko96c45b02008-07-09 19:44:08 +000032 entries = xrealloc_vector(entries, 3, count);
Denis Vlasenkodeeed592008-07-08 05:14:36 +000033 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 Vlasenko96c45b02008-07-09 19:44:08 +000039 count++;
Eric Andersenbc341901999-11-06 04:56:00 +000040 }
41 endmntent(mountTable);
Eric Andersenbc341901999-11-06 04:56:00 +000042
Denis Vlasenko96c45b02008-07-09 19:44:08 +000043//TODO: make update atomic
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000044 mountTable = setmntent(bb_path_mtab_file, "w");
45 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 for (i = 0; i < count; i++) {
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000047 if (strcmp(entries[i].mnt_fsname, name) != 0
48 && strcmp(entries[i].mnt_dir, name) != 0)
Eric Andersenbc341901999-11-06 04:56:00 +000049 addmntent(mountTable, &entries[i]);
50 }
51 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 } else if (errno != EROFS)
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000054}
Rob Landley6a6798b2005-08-10 20:35:54 +000055#endif