blob: cce1aac5c45396212bd60c678de523017cd8357e [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 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
9
Eric Andersenbc341901999-11-06 04:56:00 +000010#include <stdlib.h>
11#include <unistd.h>
12#include <errno.h>
13#include <string.h>
14#include <stdio.h>
15#include <mntent.h>
Eric Andersenc4cef5a2001-04-01 16:01:11 +000016#include "libbb.h"
Eric Andersenbc341901999-11-06 04:56:00 +000017
Eric Andersena62665b2004-10-08 08:57:35 +000018#define MTAB_MAX_ENTRIES 40
Eric Andersenbc341901999-11-06 04:56:00 +000019
Rob Landley6a6798b2005-08-10 20:35:54 +000020#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Erik Andersene49d5ec2000-02-08 19:58:47 +000021void erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000022{
Eric Andersena62665b2004-10-08 08:57:35 +000023 struct mntent entries[MTAB_MAX_ENTRIES];
Erik Andersene49d5ec2000-02-08 19:58:47 +000024 int count = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +000026 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000027
Eric Andersen0ecb54a1999-12-05 23:24:55 +000028 /* Check if reading the mtab file failed */
Erik Andersene49d5ec2000-02-08 19:58:47 +000029 if (mountTable == 0
Eric Andersenaf4ac772001-02-01 22:43:49 +000030 /* Bummer. fall back on trying the /proc filesystem */
31 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000033 return;
34 }
35
Eric Andersena62665b2004-10-08 08:57:35 +000036 while (((m = getmntent(mountTable)) != 0) && (count < MTAB_MAX_ENTRIES))
37 {
Erik Andersenfac10d72000-02-07 05:29:42 +000038 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 Andersenbc341901999-11-06 04:56:00 +000042 entries[count].mnt_freq = m->mnt_freq;
43 entries[count].mnt_passno = m->mnt_passno;
44 count++;
45 }
46 endmntent(mountTable);
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 if ((mountTable = setmntent(bb_path_mtab_file, "w"))) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 int i;
Eric Andersenbc341901999-11-06 04:56:00 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 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 Andersenbc341901999-11-06 04:56:00 +000055 continue;
56 else
57 addmntent(mountTable, &entries[i]);
58 }
59 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 } else if (errno != EROFS)
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000062}
Rob Landley6a6798b2005-08-10 20:35:54 +000063#endif