• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "log.h"
16 #include "mount.h"
17 
18 #include <sys/mount.h>
19 #include <securec.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 
25 using namespace Hdc;
26 
FindMountDeviceByPath(const char * toQuery,char * dev)27 bool FindMountDeviceByPath(const char *toQuery, char *dev)
28 {
29     int ret = false;
30     int len = BUF_SIZE_DEFAULT2;
31     char buf[BUF_SIZE_DEFAULT2];
32 
33     FILE *fp = fopen("/proc/mounts", "r");
34     if (fp == nullptr) {
35         WRITE_LOG(LOG_FATAL, "fopen /proc/mounts error:%d", errno);
36         return false;
37     }
38 
39     while (fgets(buf, len, fp) != nullptr) {
40         char dir[BUF_SIZE_SMALL] = "";
41         int freq;
42         int passnno;
43         int res = 0;
44         // clang-format off
45         res = sscanf_s(buf, "%255s %255s %*s %*s %d %d\n", dev, BUF_SIZE_SMALL - 1,
46                        dir, BUF_SIZE_SMALL - 1, &freq, &passnno);
47         // clang-format on
48         dev[BUF_SIZE_SMALL - 1] = '\0';
49         dir[BUF_SIZE_SMALL - 1] = '\0';
50         if (res == 4 && (strcmp(toQuery, dir) == 0)) {  // 4 : The correct number of parameters
51             WRITE_LOG(LOG_DEBUG, "FindMountDeviceByPath dev:%s dir:%s", dev, dir);
52             ret = true;
53             break;
54         }
55     }
56     int rc = fclose(fp);
57     if (rc != 0) {
58         WRITE_LOG(LOG_WARN, "fclose rc:%d error:%d", rc, errno);
59     }
60     if (!ret) {
61         WRITE_LOG(LOG_FATAL, "FindMountDeviceByPath not found %s", toQuery);
62     }
63     return ret;
64 }
65 
RemountPartition(const char * dir)66 bool RemountPartition(const char *dir)
67 {
68     int fd;
69     int off = 0;
70     int ret = 0;
71     char dev[BUF_SIZE_SMALL] = "";
72 
73     if (!FindMountDeviceByPath(dir, dev) || strlen(dev) < 4) {  // 4 : file count
74         WRITE_LOG(LOG_FATAL, "FindMountDeviceByPath dir:%s failed", dir);
75         return false;
76     }
77 
78     if ((fd = open(dev, O_RDONLY | O_CLOEXEC)) < 0) {
79         WRITE_LOG(LOG_FATAL, "open dev:%s failed, error:%d", dev, errno);
80         return false;
81     }
82     ioctl(fd, BLKROSET, &off);
83     close(fd);
84 
85     ret = mount(dev, dir, "none", MS_REMOUNT, nullptr);
86     if (ret < 0) {
87         WRITE_LOG(LOG_FATAL, "mount %s failed, reason is %s", dev, strerror(errno));
88         return false;
89     }
90     return true;
91 }
92 
RemountDevice()93 bool RemountDevice()
94 {
95     if (getuid() != 0) {
96         return false;
97     }
98     struct stat info;
99     if (!lstat("/vendor", &info) && (info.st_mode & S_IFMT) == S_IFDIR) {
100         // has vendor
101         if (!RemountPartition("/vendor")) {
102             return false;
103         }
104     }
105     return true;
106 }