blob: c617dc6679297d1fda0f2e60735077add4cf0b8c [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <ctype.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <dirent.h>
25#include <limits.h>
26#include <errno.h>
JP Abgrall4515d812014-01-31 14:37:07 -080027#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Elliott Hughes81399e12015-03-10 08:39:45 -070029#include <memory>
30
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <cutils/misc.h>
32#include <cutils/sockets.h>
Matthew Xie40a91a22013-05-20 14:22:01 -070033#include <cutils/multiuser.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
36#include <sys/_system_properties.h>
37
38#include <sys/socket.h>
39#include <sys/un.h>
40#include <sys/select.h>
41#include <sys/types.h>
42#include <netinet/in.h>
43#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
rpcraig63207cd2012-08-09 10:05:49 -040045#include <selinux/selinux.h>
46#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040047
Andres Moralesdb5f5d42015-05-08 08:30:33 -070048#include <fs_mgr.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080049#include <android-base/file.h>
Andres Moralesdb5f5d42015-05-08 08:30:33 -070050#include "bootimg.h"
51
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#include "property_service.h"
53#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070054#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070055#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
57#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralesdb5f5d42015-05-08 08:30:33 -070058#define FSTAB_PREFIX "/fstab."
59#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
61static int persistent_properties_loaded = 0;
62
Colin Crossd11beb22010-04-13 19:33:37 -070063static int property_set_fd = -1;
64
Elliott Hughesda40c002015-03-27 23:20:44 -070065void property_init() {
Elliott Hughesda40c002015-03-27 23:20:44 -070066 if (__system_property_area_init()) {
Tom Cherry60361142015-12-01 17:16:53 -080067 ERROR("Failed to initialize property area\n");
68 exit(1);
Elliott Hughesda40c002015-03-27 23:20:44 -070069 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070}
71
William Robertsd7aea442015-10-01 16:03:47 -070072static int check_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -040073{
rpcraig63207cd2012-08-09 10:05:49 -040074 char *tctx = NULL;
rpcraig63207cd2012-08-09 10:05:49 -040075 int result = 0;
William Robertsd7aea442015-10-01 16:03:47 -070076 property_audit_data audit_data;
rpcraig63207cd2012-08-09 10:05:49 -040077
78 if (!sctx)
79 goto err;
80
81 if (!sehandle_prop)
82 goto err;
83
84 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
85 goto err;
86
William Robertsd7aea442015-10-01 16:03:47 -070087 audit_data.name = name;
88 audit_data.cr = cr;
89
90 if (selinux_check_access(sctx, tctx, "property_service", "set", reinterpret_cast<void*>(&audit_data)) == 0)
rpcraig63207cd2012-08-09 10:05:49 -040091 result = 1;
92
93 freecon(tctx);
94 err:
95 return result;
rpcraig63207cd2012-08-09 10:05:49 -040096}
97
William Robertsd7aea442015-10-01 16:03:47 -070098static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
rpcraig63207cd2012-08-09 10:05:49 -040099{
rpcraig63207cd2012-08-09 10:05:49 -0400100 /*
101 * Create a name prefix out of ctl.<service name>
102 * The new prefix allows the use of the existing
103 * property service backend labeling while avoiding
104 * mislabels based on true property prefixes.
105 */
106 char ctl_name[PROP_VALUE_MAX+4];
107 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
108
109 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
110 return 0;
111
William Robertsd7aea442015-10-01 16:03:47 -0700112 return check_mac_perms(ctl_name, sctx, cr);
rpcraig63207cd2012-08-09 10:05:49 -0400113}
114
Yabin Cui74edcea2015-07-24 10:11:05 -0700115std::string property_get(const char* name) {
116 char value[PROP_VALUE_MAX] = {0};
117 __system_property_get(name, value);
118 return value;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119}
120
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800121static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700123 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700125 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700127 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
128 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 if (fd < 0) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700130 ERROR("Unable to write persistent property to temp file %s: %s\n", tempPath, strerror(errno));
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800131 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800134 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 close(fd);
136
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700137 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 if (rename(tempPath, path)) {
139 unlink(tempPath);
140 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
141 }
142}
143
Nick Kralevich69463612013-09-13 17:21:28 -0700144static bool is_legal_property_name(const char* name, size_t namelen)
145{
146 size_t i;
Nick Kralevich69463612013-09-13 17:21:28 -0700147 if (namelen >= PROP_NAME_MAX) return false;
148 if (namelen < 1) return false;
149 if (name[0] == '.') return false;
150 if (name[namelen - 1] == '.') return false;
151
152 /* Only allow alphanumeric, plus '.', '-', or '_' */
153 /* Don't allow ".." to appear in a property name */
154 for (i = 0; i < namelen; i++) {
155 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700156 // i=0 is guaranteed to never have a dot. See above.
157 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700158 continue;
159 }
Nick Kralevich69463612013-09-13 17:21:28 -0700160 if (name[i] == '_' || name[i] == '-') continue;
161 if (name[i] >= 'a' && name[i] <= 'z') continue;
162 if (name[i] >= 'A' && name[i] <= 'Z') continue;
163 if (name[i] >= '0' && name[i] <= '9') continue;
164 return false;
165 }
166
167 return true;
168}
169
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700170static int property_set_impl(const char* name, const char* value) {
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700171 size_t namelen = strlen(name);
172 size_t valuelen = strlen(value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Nick Kralevich69463612013-09-13 17:21:28 -0700174 if (!is_legal_property_name(name, namelen)) return -1;
175 if (valuelen >= PROP_VALUE_MAX) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Janis Danisevskis3d1dff22016-03-08 18:35:28 +0000177 if (strcmp("selinux.restorecon_recursive", name) == 0 && valuelen > 0) {
Jeff Sharkey76417512015-06-09 11:02:55 -0700178 if (restorecon_recursive(value) != 0) {
179 ERROR("Failed to restorecon_recursive %s\n", value);
180 }
181 }
182
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700183 prop_info* pi = (prop_info*) __system_property_find(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
185 if(pi != 0) {
186 /* ro.* properties may NEVER be modified once set */
187 if(!strncmp(name, "ro.", 3)) return -1;
188
Colin Cross9f5af632013-01-23 23:09:48 -0800189 __system_property_update(pi, value, valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 } else {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700191 int rc = __system_property_add(name, namelen, value, valuelen);
192 if (rc < 0) {
193 return rc;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200194 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 }
196 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400197 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 if (strcmp("net.change", name) == 0) {
199 return 0;
200 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800201 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 * The 'net.change' property is a special property used track when any
203 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
204 * contains the last updated 'net.*' property.
205 */
206 property_set("net.change", name);
207 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400208 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800209 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 * Don't write properties to disk until after we have read all default properties
211 * to prevent them from being overwritten by default values.
212 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800213 write_persistent_property(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
215 property_changed(name, value);
216 return 0;
217}
218
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700219int property_set(const char* name, const char* value) {
220 int rc = property_set_impl(name, value);
221 if (rc == -1) {
Elliott Hughes930974c2015-03-23 08:07:19 -0700222 ERROR("property_set(\"%s\", \"%s\") failed\n", name, value);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700223 }
224 return rc;
225}
226
Elliott Hughes929f4072015-04-24 21:13:44 -0700227static void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228{
229 prop_msg msg;
230 int s;
231 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 struct ucred cr;
233 struct sockaddr_un addr;
234 socklen_t addr_size = sizeof(addr);
235 socklen_t cr_size = sizeof(cr);
rpcraig63207cd2012-08-09 10:05:49 -0400236 char * source_ctx = NULL;
JP Abgrall4515d812014-01-31 14:37:07 -0800237 struct pollfd ufds[1];
238 const int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
239 int nr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
Colin Crossd11beb22010-04-13 19:33:37 -0700241 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 return;
243 }
244
245 /* Check socket options here */
246 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
247 close(s);
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700248 ERROR("Unable to receive socket options\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 return;
250 }
251
JP Abgrall4515d812014-01-31 14:37:07 -0800252 ufds[0].fd = s;
253 ufds[0].events = POLLIN;
254 ufds[0].revents = 0;
255 nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
256 if (nr == 0) {
257 ERROR("sys_prop: timeout waiting for uid=%d to send property message.\n", cr.uid);
258 close(s);
259 return;
260 } else if (nr < 0) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700261 ERROR("sys_prop: error waiting for uid=%d to send property message: %s\n", cr.uid, strerror(errno));
JP Abgrall4515d812014-01-31 14:37:07 -0800262 close(s);
263 return;
264 }
265
266 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 if(r != sizeof(prop_msg)) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700268 ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s\n",
269 r, sizeof(prop_msg), strerror(errno));
Brad Fitzpatrick9f1e0e32011-03-30 12:39:56 -0700270 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 return;
272 }
273
274 switch(msg.cmd) {
275 case PROP_MSG_SETPROP:
276 msg.name[PROP_NAME_MAX-1] = 0;
277 msg.value[PROP_VALUE_MAX-1] = 0;
278
Nick Kralevich69463612013-09-13 17:21:28 -0700279 if (!is_legal_property_name(msg.name, strlen(msg.name))) {
280 ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
281 close(s);
282 return;
283 }
284
rpcraig63207cd2012-08-09 10:05:49 -0400285 getpeercon(s, &source_ctx);
rpcraig63207cd2012-08-09 10:05:49 -0400286
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 if(memcmp(msg.name,"ctl.",4) == 0) {
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700288 // Keep the old close-socket-early behavior when handling
289 // ctl.* properties.
290 close(s);
William Robertsd7aea442015-10-01 16:03:47 -0700291 if (check_control_mac_perms(msg.value, source_ctx, &cr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 handle_control_message((char*) msg.name + 4, (char*) msg.value);
293 } else {
Wink Savillecfa0d842010-10-03 13:30:11 -0700294 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
295 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 }
297 } else {
Tom Cherryc787cf22016-01-11 13:04:19 -0800298 if (check_mac_perms(msg.name, source_ctx, &cr)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 property_set((char*) msg.name, (char*) msg.value);
300 } else {
301 ERROR("sys_prop: permission denied uid:%d name:%s\n",
302 cr.uid, msg.name);
303 }
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700304
305 // Note: bionic's property client code assumes that the
306 // property server will not close the socket until *AFTER*
307 // the property is written to memory.
308 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 }
rpcraig63207cd2012-08-09 10:05:49 -0400310 freecon(source_ctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 break;
312
313 default:
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700314 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 break;
316 }
317}
318
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800319static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800321/*
322 * Filter is used to decide which properties to load: NULL loads all keys,
323 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
324 */
325static void load_properties(char *data, const char *filter)
326{
327 char *key, *value, *eol, *sol, *tmp, *fn;
328 size_t flen = 0;
329
330 if (filter) {
331 flen = strlen(filter);
332 }
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800335 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 key = sol;
337 *eol++ = 0;
338 sol = eol;
339
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800340 while (isspace(*key)) key++;
341 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800344 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800346 if (!strncmp(key, "import ", 7) && flen == 0) {
347 fn = key + 7;
348 while (isspace(*fn)) fn++;
349
350 key = strchr(fn, ' ');
351 if (key) {
352 *key++ = 0;
353 while (isspace(*key)) key++;
354 }
355
356 load_properties_from_file(fn, key);
357
358 } else {
359 value = strchr(key, '=');
360 if (!value) continue;
361 *value++ = 0;
362
363 tmp = value - 2;
364 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
365
366 while (isspace(*value)) value++;
367
368 if (flen > 0) {
369 if (filter[flen - 1] == '*') {
370 if (strncmp(key, filter, flen - 1)) continue;
371 } else {
372 if (strcmp(key, filter)) continue;
373 }
374 }
375
376 property_set(key, value);
377 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 }
379}
380
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800381/*
382 * Filter is used to decide which properties to load: NULL loads all keys,
383 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
384 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700385static void load_properties_from_file(const char* filename, const char* filter) {
386 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800387 std::string data;
Elliott Hughesda40c002015-03-27 23:20:44 -0700388 if (read_file(filename, &data)) {
Tom Cherrycce7e932015-05-12 13:54:41 -0700389 data.push_back('\n');
Elliott Hughesf682b472015-02-06 12:19:48 -0800390 load_properties(&data[0], filter);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 }
Elliott Hughesda40c002015-03-27 23:20:44 -0700392 NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393}
394
Elliott Hughes81399e12015-03-10 08:39:45 -0700395static void load_persistent_properties() {
396 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397
Elliott Hughes81399e12015-03-10 08:39:45 -0700398 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
399 if (!dir) {
400 ERROR("Unable to open persistent property directory \"%s\": %s\n",
401 PERSISTENT_PROPERTY_DIR, strerror(errno));
402 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800404
Elliott Hughes81399e12015-03-10 08:39:45 -0700405 struct dirent* entry;
406 while ((entry = readdir(dir.get())) != NULL) {
407 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
408 continue;
409 }
410 if (entry->d_type != DT_REG) {
411 continue;
412 }
413
414 // Open the file and read the property value.
415 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
416 if (fd == -1) {
417 ERROR("Unable to open persistent property file \"%s\": %s\n",
418 entry->d_name, strerror(errno));
419 continue;
420 }
421
422 struct stat sb;
423 if (fstat(fd, &sb) == -1) {
424 ERROR("fstat on property file \"%s\" failed: %s\n", entry->d_name, strerror(errno));
425 close(fd);
426 continue;
427 }
428
429 // File must not be accessible to others, be owned by root/root, and
430 // not be a hard link to any other file.
431 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || (sb.st_uid != 0) || (sb.st_gid != 0) ||
432 (sb.st_nlink != 1)) {
433 ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
434 entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
435 (unsigned int)sb.st_nlink, sb.st_mode);
436 close(fd);
437 continue;
438 }
439
440 char value[PROP_VALUE_MAX];
441 int length = read(fd, value, sizeof(value) - 1);
442 if (length >= 0) {
443 value[length] = 0;
444 property_set(entry->d_name, value);
445 } else {
446 ERROR("Unable to read persistent property file %s: %s\n",
447 entry->d_name, strerror(errno));
448 }
449 close(fd);
450 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451}
452
Elliott Hughesda40c002015-03-27 23:20:44 -0700453void property_load_boot_defaults() {
Andrew Boie3899f522012-10-12 15:23:40 -0700454 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455}
456
Nick Kralevich32b90232012-09-19 11:15:24 -0700457static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800458 if (ALLOW_LOCAL_PROP_OVERRIDE) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700459 std::string debuggable = property_get("ro.debuggable");
460 if (debuggable == "1") {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800461 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
462 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700463 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700464}
465
Ken Sumrallc5c51032011-03-08 17:01:29 -0800466/* When booting an encrypted system, /data is not mounted when the
467 * property service is started, so any properties stored there are
468 * not loaded. Vold triggers init to load these properties once it
469 * has mounted /data.
470 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700471void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700472 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800473 /* Read persistent properties after all default values have been loaded. */
474 load_persistent_properties();
475}
476
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700477void load_recovery_id_prop() {
Yabin Cui74edcea2015-07-24 10:11:05 -0700478 std::string ro_hardware = property_get("ro.hardware");
479 if (ro_hardware.empty()) {
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700480 ERROR("ro.hardware not set - unable to load recovery id\n");
481 return;
482 }
Yabin Cui74edcea2015-07-24 10:11:05 -0700483 std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700484
Yabin Cui74edcea2015-07-24 10:11:05 -0700485 std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700486 fs_mgr_free_fstab);
487 if (!tab) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700488 ERROR("unable to read fstab %s: %s\n", fstab_filename.c_str(), strerror(errno));
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700489 return;
490 }
491
492 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
493 if (rec == NULL) {
494 ERROR("/recovery not specified in fstab\n");
495 return;
496 }
497
498 int fd = open(rec->blk_device, O_RDONLY);
499 if (fd == -1) {
500 ERROR("error opening block device %s: %s\n", rec->blk_device, strerror(errno));
501 return;
502 }
503
504 boot_img_hdr hdr;
505 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
506 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
507 property_set("ro.recovery_id", hex.c_str());
508 } else {
509 ERROR("error reading /recovery: %s\n", strerror(errno));
510 }
511
512 close(fd);
513}
514
Paul Lawrence948410a2015-07-01 14:40:56 -0700515void load_system_props() {
Andrew Boie3899f522012-10-12 15:23:40 -0700516 load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
Daniel Rosenbergb9512222014-11-10 17:01:33 -0800517 load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800518 load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700519 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700520}
521
Elliott Hughesda40c002015-03-27 23:20:44 -0700522void start_property_service() {
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700523 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
524 0666, 0, 0, NULL);
525 if (property_set_fd == -1) {
526 ERROR("start_property_service socket creation failed: %s\n", strerror(errno));
527 exit(1);
528 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700530 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700531
Elliott Hughes929f4072015-04-24 21:13:44 -0700532 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533}