-
Notifications
You must be signed in to change notification settings - Fork 0
/
rom_quirks.c
140 lines (124 loc) · 3.8 KB
/
rom_quirks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include "rom_quirks.h"
#include "lib/log.h"
#include "lib/util.h"
static void workaround_mount_in_sh(const char *path)
{
char line[512];
char *tmp_name = NULL;
FILE *f_in, *f_out;
f_in = fopen(path, "re");
if(!f_in)
return;
const int size = strlen(path) + 5;
tmp_name = malloc(size);
snprintf(tmp_name, size, "%s-new", path);
f_out = fopen(tmp_name, "we");
if(!f_out)
{
fclose(f_in);
free(tmp_name);
return;
}
while(fgets(line, sizeof(line), f_in))
{
if(strstr(line, "mount ") && strstr(line, "/system"))
fputc('#', f_out);
fputs(line, f_out);
}
fclose(f_in);
fclose(f_out);
rename(tmp_name, path);
free(tmp_name);
}
static void inject_file_contexts(void)
{
FILE *f;
char line[512];
f = fopen("/file_contexts", "re");
if(!f)
{
ERROR("Failed to open /file_contexts!");
return;
}
while(fgets(line, sizeof(line), f))
{
if(strstartswith(line, "/data/media/multirom"))
{
INFO("/file_contexts has been already injected.");
fclose(f);
return;
}
}
fclose(f);
INFO("Injecting /file_contexts\n");
f = fopen("/file_contexts", "ae");
if(!f)
{
ERROR("Failed to open /file_contexts for appending!");
return;
}
fputs("\n"
"# MultiROM folders\n"
"/data/media/multirom(/.*)? <<none>>\n"
"/data/media/0/multirom(/.*)? <<none>>\n"
"/realdata/media/multirom(/.*)? <<none>>\n"
"/realdata/media/0/multirom(/.*)? <<none>>\n"
"/mnt/mrom(/.*)? <<none>>\n",
f);
fclose(f);
}
void rom_quirks_on_initrd_finalized(void)
{
// walk over all _regular_ files in /
DIR *d = opendir("/");
if(d)
{
struct dirent *dt;
char buff[128];
while((dt = readdir(d)))
{
if(dt->d_type != DT_REG)
continue;
// The Android L and later releases have SELinux
// set to "enforcing" and "restorecon_recursive /data" line in init.rc.
// Restorecon on /data goes into /data/media/0/multirom/roms/ and changes
// context of all secondary ROMs files to that of /data, including the files
// in secondary ROMs /system dirs. We need to prevent that.
// Right now, we do that by adding entries into /file_contexts that say
// MultiROM folders don't have any context
if(strcmp(dt->d_name, "file_contexts") == 0)
inject_file_contexts();
// franco.Kernel includes script init.fk.sh which remounts /system as read only
// comment out lines with mount and /system in all .sh scripts in /
if(strendswith(dt->d_name, ".sh"))
{
snprintf(buff, sizeof(buff), "/%s", dt->d_name);
workaround_mount_in_sh(buff);
}
}
closedir(d);
}
}