forked from sztupy/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.c
207 lines (187 loc) · 4.56 KB
/
config.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "config.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "pthread.h"
#include "sys/stat.h"
static pthread_mutex_t gMutex1 = PTHREAD_MUTEX_INITIALIZER,gMutex2 = PTHREAD_MUTEX_INITIALIZER,gMutex3 = PTHREAD_MUTEX_INITIALIZER,gWrite = PTHREAD_MUTEX_INITIALIZER,gRead = PTHREAD_MUTEX_INITIALIZER;
static int numreaders = 0, numwriters = 0;
struct confel;
struct confel {
char key[KEY_MAX_LENGTH];
char value[VALUE_MAX_LENGTH];
struct confel* next;
};
static struct confel firstconf = {"","",NULL};
// thread safe reading
static int read_conf(const char* key, char* value)
{
pthread_mutex_lock(&gMutex3);
pthread_mutex_lock(&gRead);
pthread_mutex_lock(&gMutex1);
numreaders++;
if (numreaders==1) pthread_mutex_lock(&gWrite);
pthread_mutex_unlock(&gMutex1);
pthread_mutex_unlock(&gRead);
pthread_mutex_unlock(&gMutex3);
int found = 0;
struct confel* n = firstconf.next;
while (n) {
if (strcmp(key,n->key)==0) {
if (value) {
strcpy(value,n->value);
}
found = 1;
break;
}
n = n->next;
}
pthread_mutex_lock(&gMutex1);
numreaders--;
if (numreaders==0) pthread_mutex_unlock(&gWrite);
pthread_mutex_unlock(&gMutex1);
return found;
}
// thread safe writing
static int write_conf(const char* key, const char* value)
{
pthread_mutex_lock(&gMutex2);
numwriters++;
if (numwriters==1) pthread_mutex_lock(&gRead);
pthread_mutex_unlock(&gMutex2);
pthread_mutex_lock(&gWrite);
int found = 0;
struct confel* n = firstconf.next;
struct confel* p = &firstconf;
while (n) {
if (strcmp(key,n->key)==0) {
if (value) {
strcpy(n->value,value);
found = 1;
} else {
p->next = n->next;
free(n);
}
break;
}
if (!n->next) {
if (value) {
n->next = malloc(sizeof(struct confel));
n->next->next = NULL;
strcpy(n->next->key,key);
}
}
n = n->next;
p = p->next;
}
struct stat s;
if (stat(CONFIG_SYSTEM,&s)==0) {
FILE* f = fopen(CONFIG_SYSTEM,"w+");
if (f) {
n = firstconf.next;
while (n) {
fprintf(f,"%s=%s\n",n->key,n->value);
n = n->next;
}
fclose(f);
sync();
}
}
pthread_mutex_unlock(&gWrite);
pthread_mutex_lock(&gMutex2);
numwriters--;
if (numwriters==0) pthread_mutex_unlock(&gRead);
pthread_mutex_unlock(&gMutex2);
return found;
}
int get_conf_ro_from(const char* filename, const char* key, char* value)
{
FILE* f = fopen(filename,"r");
int found = 0;
char r[KEY_MAX_LENGTH+VALUE_MAX_LENGTH+3];
if (f) {
while (fgets(r, KEY_MAX_LENGTH+VALUE_MAX_LENGTH+3, f)) {
if (r[0]=='#') continue;
char* fp = strchr(r,'=');
if (fp) {
if (fp[strlen(fp)-1]=='\n') fp[strlen(fp)-1]='\0';
fp[0] = '\0';
fp++;
if (strlen(r)<KEY_MAX_LENGTH && strlen(fp)<VALUE_MAX_LENGTH)
if (strcmp(r,key)==0) {
strcpy(value,fp);
found = 1;
break;
}
}
}
fclose(f);
}
return found;
}
int get_conf_ro(const char* key, char* value)
{
return get_conf_ro_from(CONFIG_SBIN,key,value);
}
int init_conf(void)
{
FILE* f = fopen(CONFIG_SYSTEM,"r");
int found = 0;
char r[KEY_MAX_LENGTH+VALUE_MAX_LENGTH+3];
firstconf.next = NULL;
struct confel* n = &firstconf;
if (f) {
while (fgets(r, KEY_MAX_LENGTH+VALUE_MAX_LENGTH+3, f)) {
if (r[0]=='#') continue;
char* fp = strchr(r,'=');
if (fp) {
if (fp[strlen(fp)-1]=='\n') fp[strlen(fp)-1]='\0';
fp[0] = '\0';
fp++;
if (strlen(r)<KEY_MAX_LENGTH && strlen(fp)<VALUE_MAX_LENGTH) {
if (!read_conf(r,NULL)) {
n->next = malloc(sizeof(struct confel));
n = n->next;
n->next = NULL;
strcpy(n->key,r);
strcpy(n->value,fp);
found++;
}
}
}
}
fclose(f);
}
return found;
}
char* get_conf_def(const char* key, char* value, const char* def)
{
int has = get_conf(key, value);
if (!has && value) strcpy(value,def);
return value;
}
int get_conf(const char* key, char* value)
{
int r = read_conf(key, value);
if (!r)
return get_conf_ro(key,value);
else
return r;
}
int set_conf(const char* key, const char* value)
{
return write_conf(key, value);
}
int get_capability(const char* key, char* value)
{
if (strstr(key,"fs.support.")==key) {
char* fsname = key+strlen("fs.support.");
if (call_busybox("grep",fsname,"/proc/filesystems",NULL)==0) {
if (value) {
strcpy(value,"1");
}
return 1;
}
}
return 0;
}