blob: 0497657758089d28cd60a1a93e7b1360fef580b7 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbc341901999-11-06 04:56:00 +00002#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <string.h>
6#include <stdio.h>
7#include <mntent.h>
Eric Andersencbe31da2001-02-20 06:14:08 +00008#include "busybox.h"
Eric Andersenbc341901999-11-06 04:56:00 +00009
Erik Andersene49d5ec2000-02-08 19:58:47 +000010extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersen741f2c92001-03-14 01:48:10 +000011static const int MS_RDONLY = 1; /* Mount read-only. */
Eric Andersenbc341901999-11-06 04:56:00 +000012
Erik Andersene49d5ec2000-02-08 19:58:47 +000013void erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000014{
Erik Andersene49d5ec2000-02-08 19:58:47 +000015 struct mntent entries[20];
16 int count = 0;
Eric Andersenbc341901999-11-06 04:56:00 +000017 FILE *mountTable = setmntent(mtab_file, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +000018 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000019
Eric Andersen0ecb54a1999-12-05 23:24:55 +000020 /* Check if reading the mtab file failed */
Erik Andersene49d5ec2000-02-08 19:58:47 +000021 if (mountTable == 0
Eric Andersenaf4ac772001-02-01 22:43:49 +000022 /* Bummer. fall back on trying the /proc filesystem */
23 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +000024 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000025 return;
26 }
27
Erik Andersene49d5ec2000-02-08 19:58:47 +000028 while ((m = getmntent(mountTable)) != 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +000029 entries[count].mnt_fsname = strdup(m->mnt_fsname);
30 entries[count].mnt_dir = strdup(m->mnt_dir);
31 entries[count].mnt_type = strdup(m->mnt_type);
32 entries[count].mnt_opts = strdup(m->mnt_opts);
Eric Andersenbc341901999-11-06 04:56:00 +000033 entries[count].mnt_freq = m->mnt_freq;
34 entries[count].mnt_passno = m->mnt_passno;
35 count++;
36 }
37 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 if ((mountTable = setmntent(mtab_file, "w"))) {
39 int i;
Eric Andersenbc341901999-11-06 04:56:00 +000040
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 for (i = 0; i < count; i++) {
42 int result = (strcmp(entries[i].mnt_fsname, name) == 0
43 || strcmp(entries[i].mnt_dir, name) == 0);
44
45 if (result)
Eric Andersenbc341901999-11-06 04:56:00 +000046 continue;
47 else
48 addmntent(mountTable, &entries[i]);
49 }
50 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 } else if (errno != EROFS)
Matt Kraaia9819b22000-12-22 01:48:07 +000052 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000053}
54
Erik Andersene49d5ec2000-02-08 19:58:47 +000055void write_mtab(char *blockDevice, char *directory,
56 char *filesystemType, long flags, char *string_flags)
Eric Andersenbc341901999-11-06 04:56:00 +000057{
58 FILE *mountTable = setmntent(mtab_file, "a+");
59 struct mntent m;
60
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 if (mountTable == 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +000062 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000063 return;
64 }
65 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 int length = strlen(directory);
Eric Andersenbc341901999-11-06 04:56:00 +000067
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 if (length > 1 && directory[length - 1] == '/')
69 directory[length - 1] = '\0';
Eric Andersenbc341901999-11-06 04:56:00 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 if (filesystemType == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000072 struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
Eric Andersenbc341901999-11-06 04:56:00 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 if (p && p->mnt_type)
75 filesystemType = p->mnt_type;
76 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 m.mnt_fsname = blockDevice;
78 m.mnt_dir = directory;
79 m.mnt_type = filesystemType ? filesystemType : "default";
Eric Andersenbc341901999-11-06 04:56:00 +000080
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 if (*string_flags) {
82 m.mnt_opts = string_flags;
83 } else {
84 if ((flags | MS_RDONLY) == flags)
85 m.mnt_opts = "ro";
86 else
87 m.mnt_opts = "rw";
88 }
89
90 m.mnt_freq = 0;
91 m.mnt_passno = 0;
92 addmntent(mountTable, &m);
93 endmntent(mountTable);
94 }
95}