xref: /OK3568_Linux_fs/external/rk_pcba_test/pcba_minui/mtdutils/mounts.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2007 The Android Open Source Project
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Licensed under the Apache License, Version 2.0 (the "License");
5*4882a593Smuzhiyun  * you may not use this file except in compliance with the License.
6*4882a593Smuzhiyun  * You may obtain a copy of the License at
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *      http://www.apache.org/licenses/LICENSE-2.0
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Unless required by applicable law or agreed to in writing, software
11*4882a593Smuzhiyun  * distributed under the License is distributed on an "AS IS" BASIS,
12*4882a593Smuzhiyun  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4882a593Smuzhiyun  * See the License for the specific language governing permissions and
14*4882a593Smuzhiyun  * limitations under the License.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <stdio.h>
18*4882a593Smuzhiyun #include <stdlib.h>
19*4882a593Smuzhiyun #include <string.h>
20*4882a593Smuzhiyun #include <fcntl.h>
21*4882a593Smuzhiyun #include <errno.h>
22*4882a593Smuzhiyun #include <sys/mount.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "mounts.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct MountedVolume {
27*4882a593Smuzhiyun     const char *device;
28*4882a593Smuzhiyun     const char *mount_point;
29*4882a593Smuzhiyun     const char *filesystem;
30*4882a593Smuzhiyun     const char *flags;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun typedef struct {
34*4882a593Smuzhiyun     MountedVolume *volumes;
35*4882a593Smuzhiyun     int volumes_allocd;
36*4882a593Smuzhiyun     int volume_count;
37*4882a593Smuzhiyun } MountsState;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static MountsState g_mounts_state = {
40*4882a593Smuzhiyun     NULL,   // volumes
41*4882a593Smuzhiyun     0,      // volumes_allocd
42*4882a593Smuzhiyun     0       // volume_count
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static inline void
free_volume_internals(const MountedVolume * volume,int zero)46*4882a593Smuzhiyun free_volume_internals(const MountedVolume *volume, int zero)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun     free((char *)volume->device);
49*4882a593Smuzhiyun     free((char *)volume->mount_point);
50*4882a593Smuzhiyun     free((char *)volume->filesystem);
51*4882a593Smuzhiyun     free((char *)volume->flags);
52*4882a593Smuzhiyun     if (zero) {
53*4882a593Smuzhiyun         memset((void *)volume, 0, sizeof(*volume));
54*4882a593Smuzhiyun     }
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #define PROC_MOUNTS_FILENAME   "/proc/mounts"
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun int
scan_mounted_volumes()60*4882a593Smuzhiyun scan_mounted_volumes()
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun     char buf[2048];
63*4882a593Smuzhiyun     const char *bufp;
64*4882a593Smuzhiyun     int fd;
65*4882a593Smuzhiyun     ssize_t nbytes;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun     if (g_mounts_state.volumes == NULL) {
68*4882a593Smuzhiyun         const int numv = 32;
69*4882a593Smuzhiyun         MountedVolume *volumes = malloc(numv * sizeof(*volumes));
70*4882a593Smuzhiyun         if (volumes == NULL) {
71*4882a593Smuzhiyun             errno = ENOMEM;
72*4882a593Smuzhiyun             return -1;
73*4882a593Smuzhiyun         }
74*4882a593Smuzhiyun         g_mounts_state.volumes = volumes;
75*4882a593Smuzhiyun         g_mounts_state.volumes_allocd = numv;
76*4882a593Smuzhiyun         memset(volumes, 0, numv * sizeof(*volumes));
77*4882a593Smuzhiyun     } else {
78*4882a593Smuzhiyun         /* Free the old volume strings.
79*4882a593Smuzhiyun          */
80*4882a593Smuzhiyun         int i;
81*4882a593Smuzhiyun         for (i = 0; i < g_mounts_state.volume_count; i++) {
82*4882a593Smuzhiyun             free_volume_internals(&g_mounts_state.volumes[i], 1);
83*4882a593Smuzhiyun         }
84*4882a593Smuzhiyun     }
85*4882a593Smuzhiyun     g_mounts_state.volume_count = 0;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun     /* Open and read the file contents.
88*4882a593Smuzhiyun      */
89*4882a593Smuzhiyun     fd = open(PROC_MOUNTS_FILENAME, O_RDONLY);
90*4882a593Smuzhiyun     if (fd < 0) {
91*4882a593Smuzhiyun         goto bail;
92*4882a593Smuzhiyun     }
93*4882a593Smuzhiyun     nbytes = read(fd, buf, sizeof(buf) - 1);
94*4882a593Smuzhiyun     close(fd);
95*4882a593Smuzhiyun     if (nbytes < 0) {
96*4882a593Smuzhiyun         goto bail;
97*4882a593Smuzhiyun     }
98*4882a593Smuzhiyun     buf[nbytes] = '\0';
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun     /* Parse the contents of the file, which looks like:
101*4882a593Smuzhiyun      *
102*4882a593Smuzhiyun      *     # cat /proc/mounts
103*4882a593Smuzhiyun      *     rootfs / rootfs rw 0 0
104*4882a593Smuzhiyun      *     /dev/pts /dev/pts devpts rw 0 0
105*4882a593Smuzhiyun      *     /proc /proc proc rw 0 0
106*4882a593Smuzhiyun      *     /sys /sys sysfs rw 0 0
107*4882a593Smuzhiyun      *     /dev/block/mtdblock4 /system yaffs2 rw,nodev,noatime,nodiratime 0 0
108*4882a593Smuzhiyun      *     /dev/block/mtdblock5 /data yaffs2 rw,nodev,noatime,nodiratime 0 0
109*4882a593Smuzhiyun      *     /dev/block/mmcblk0p1 /sdcard vfat rw,sync,dirsync,fmask=0000,dmask=0000,codepage=cp437,iocharset=iso8859-1,utf8 0 0
110*4882a593Smuzhiyun      *
111*4882a593Smuzhiyun      * The zeroes at the end are dummy placeholder fields to make the
112*4882a593Smuzhiyun      * output match Linux's /etc/mtab, but don't represent anything here.
113*4882a593Smuzhiyun      */
114*4882a593Smuzhiyun     bufp = buf;
115*4882a593Smuzhiyun     while (nbytes > 0) {
116*4882a593Smuzhiyun         char device[64];
117*4882a593Smuzhiyun         char mount_point[64];
118*4882a593Smuzhiyun         char filesystem[64];
119*4882a593Smuzhiyun         char flags[128];
120*4882a593Smuzhiyun         int matches;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun         /* %as is a gnu extension that malloc()s a string for each field.
123*4882a593Smuzhiyun          */
124*4882a593Smuzhiyun         matches = sscanf(bufp, "%63s %63s %63s %127s",
125*4882a593Smuzhiyun                 device, mount_point, filesystem, flags);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun         if (matches == 4) {
128*4882a593Smuzhiyun             device[sizeof(device)-1] = '\0';
129*4882a593Smuzhiyun             mount_point[sizeof(mount_point)-1] = '\0';
130*4882a593Smuzhiyun             filesystem[sizeof(filesystem)-1] = '\0';
131*4882a593Smuzhiyun             flags[sizeof(flags)-1] = '\0';
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun             MountedVolume *v =
134*4882a593Smuzhiyun                     &g_mounts_state.volumes[g_mounts_state.volume_count++];
135*4882a593Smuzhiyun             v->device = strdup(device);
136*4882a593Smuzhiyun             v->mount_point = strdup(mount_point);
137*4882a593Smuzhiyun             v->filesystem = strdup(filesystem);
138*4882a593Smuzhiyun             v->flags = strdup(flags);
139*4882a593Smuzhiyun         } else {
140*4882a593Smuzhiyun printf("matches was %d on <<%.40s>>\n", matches, bufp);
141*4882a593Smuzhiyun         }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun         /* Eat the line.
144*4882a593Smuzhiyun          */
145*4882a593Smuzhiyun         while (nbytes > 0 && *bufp != '\n') {
146*4882a593Smuzhiyun             bufp++;
147*4882a593Smuzhiyun             nbytes--;
148*4882a593Smuzhiyun         }
149*4882a593Smuzhiyun         if (nbytes > 0) {
150*4882a593Smuzhiyun             bufp++;
151*4882a593Smuzhiyun             nbytes--;
152*4882a593Smuzhiyun         }
153*4882a593Smuzhiyun     }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun     return 0;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun bail:
158*4882a593Smuzhiyun //TODO: free the strings we've allocated.
159*4882a593Smuzhiyun     g_mounts_state.volume_count = 0;
160*4882a593Smuzhiyun     return -1;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun const MountedVolume *
find_mounted_volume_by_device(const char * device)164*4882a593Smuzhiyun find_mounted_volume_by_device(const char *device)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun     if (g_mounts_state.volumes != NULL) {
167*4882a593Smuzhiyun         int i;
168*4882a593Smuzhiyun         for (i = 0; i < g_mounts_state.volume_count; i++) {
169*4882a593Smuzhiyun             MountedVolume *v = &g_mounts_state.volumes[i];
170*4882a593Smuzhiyun             /* May be null if it was unmounted and we haven't rescanned.
171*4882a593Smuzhiyun              */
172*4882a593Smuzhiyun             if (v->device != NULL) {
173*4882a593Smuzhiyun                 if (strcmp(v->device, device) == 0) {
174*4882a593Smuzhiyun                     return v;
175*4882a593Smuzhiyun                 }
176*4882a593Smuzhiyun             }
177*4882a593Smuzhiyun         }
178*4882a593Smuzhiyun     }
179*4882a593Smuzhiyun     return NULL;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun const MountedVolume *
find_mounted_volume_by_mount_point(const char * mount_point)183*4882a593Smuzhiyun find_mounted_volume_by_mount_point(const char *mount_point)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun     if (g_mounts_state.volumes != NULL) {
186*4882a593Smuzhiyun         int i;
187*4882a593Smuzhiyun         for (i = 0; i < g_mounts_state.volume_count; i++) {
188*4882a593Smuzhiyun             MountedVolume *v = &g_mounts_state.volumes[i];
189*4882a593Smuzhiyun             /* May be null if it was unmounted and we haven't rescanned.
190*4882a593Smuzhiyun              */
191*4882a593Smuzhiyun             if (v->mount_point != NULL) {
192*4882a593Smuzhiyun                 if (strcmp(v->mount_point, mount_point) == 0) {
193*4882a593Smuzhiyun                     return v;
194*4882a593Smuzhiyun                 }
195*4882a593Smuzhiyun             }
196*4882a593Smuzhiyun         }
197*4882a593Smuzhiyun     }
198*4882a593Smuzhiyun     return NULL;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun int
unmount_mounted_volume(const MountedVolume * volume)202*4882a593Smuzhiyun unmount_mounted_volume(const MountedVolume *volume)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun     /* Intentionally pass NULL to umount if the caller tries
205*4882a593Smuzhiyun      * to unmount a volume they already unmounted using this
206*4882a593Smuzhiyun      * function.
207*4882a593Smuzhiyun      */
208*4882a593Smuzhiyun     int ret = umount(volume->mount_point);
209*4882a593Smuzhiyun     if (ret == 0) {
210*4882a593Smuzhiyun         free_volume_internals(volume, 1);
211*4882a593Smuzhiyun         return 0;
212*4882a593Smuzhiyun     }
213*4882a593Smuzhiyun     return ret;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun int
remount_read_only(const MountedVolume * volume)217*4882a593Smuzhiyun remount_read_only(const MountedVolume* volume)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun     return mount(volume->device, volume->mount_point, volume->filesystem,
220*4882a593Smuzhiyun                  MS_NOATIME | MS_NODEV | MS_NODIRATIME |
221*4882a593Smuzhiyun                  MS_RDONLY | MS_REMOUNT, 0);
222*4882a593Smuzhiyun }
223