blob: aa1a2a103b05d1b46e62a817bd117c203e5e6820 [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 */
Eric Andersenbc341901999-11-06 04:56:00 +00009#include <mntent.h>
Eric Andersenc4cef5a2001-04-01 16:01:11 +000010#include "libbb.h"
Eric Andersenbc341901999-11-06 04:56:00 +000011
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000012#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000013void FAST_FUNC erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000014{
Denis Vlasenko96c45b02008-07-09 19:44:08 +000015 struct mntent *entries;
16 int i, count;
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000017 FILE *mountTable;
Erik Andersene49d5ec2000-02-08 19:58:47 +000018 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000019
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000020 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 Byrne69374872019-07-02 11:35:03 +020024 bb_simple_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000025 return;
26 }
27
Denis Vlasenko96c45b02008-07-09 19:44:08 +000028 entries = NULL;
29 count = 0;
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000030 while ((m = getmntent(mountTable)) != 0) {
Denis Vlasenko96c45b02008-07-09 19:44:08 +000031 entries = xrealloc_vector(entries, 3, count);
Denis Vlasenkodeeed592008-07-08 05:14:36 +000032 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 Vlasenko96c45b02008-07-09 19:44:08 +000038 count++;
Eric Andersenbc341901999-11-06 04:56:00 +000039 }
40 endmntent(mountTable);
Eric Andersenbc341901999-11-06 04:56:00 +000041
Denis Vlasenko96c45b02008-07-09 19:44:08 +000042//TODO: make update atomic
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000043 mountTable = setmntent(bb_path_mtab_file, "w");
44 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 for (i = 0; i < count; i++) {
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000046 if (strcmp(entries[i].mnt_fsname, name) != 0
47 && strcmp(entries[i].mnt_dir, name) != 0)
Eric Andersenbc341901999-11-06 04:56:00 +000048 addmntent(mountTable, &entries[i]);
49 }
50 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 } else if (errno != EROFS)
James Byrne69374872019-07-02 11:35:03 +020052 bb_simple_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000053}
Rob Landley6a6798b2005-08-10 20:35:54 +000054#endif