blob: 5b7a1cbc6b7f5782a2d61ea8d97f373e1fa11eac [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#include <private/android_filesystem_config.h>
45
rpcraig63207cd2012-08-09 10:05:49 -040046#include <selinux/selinux.h>
47#include <selinux/label.h>
rpcraig63207cd2012-08-09 10:05:49 -040048
Andres Moralescb3fce82015-05-08 08:30:33 -070049#include <fs_mgr.h>
50#include <base/file.h>
51#include "bootimg.h"
52
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#include "property_service.h"
54#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070055#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58#define PERSISTENT_PROPERTY_DIR "/data/property"
Andres Moralescb3fce82015-05-08 08:30:33 -070059#define FSTAB_PREFIX "/fstab."
60#define RECOVERY_MOUNT_POINT "/recovery"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62static int persistent_properties_loaded = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070063static bool property_area_initialized = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Colin Crossd11beb22010-04-13 19:33:37 -070065static int property_set_fd = -1;
66
Elliott Hughesc0e919c2015-02-04 14:46:36 -080067struct workspace {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 size_t size;
Nick Kralevich28406472013-01-22 12:46:09 -080069 int fd;
Elliott Hughesc0e919c2015-02-04 14:46:36 -080070};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072static workspace pa_workspace;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
Elliott Hughesda40c002015-03-27 23:20:44 -070074void property_init() {
75 if (property_area_initialized) {
76 return;
77 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughesda40c002015-03-27 23:20:44 -070079 property_area_initialized = true;
Greg Hackmannf14eef02013-02-12 14:39:31 -080080
Elliott Hughesda40c002015-03-27 23:20:44 -070081 if (__system_property_area_init()) {
82 return;
83 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084
Elliott Hughesda40c002015-03-27 23:20:44 -070085 pa_workspace.size = 0;
86 pa_workspace.fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
87 if (pa_workspace.fd == -1) {
88 ERROR("Failed to open %s: %s\n", PROP_FILENAME, strerror(errno));
89 return;
90 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091}
92
rpcraig63207cd2012-08-09 10:05:49 -040093static int check_mac_perms(const char *name, char *sctx)
94{
rpcraig63207cd2012-08-09 10:05:49 -040095 char *tctx = NULL;
rpcraig63207cd2012-08-09 10:05:49 -040096 int result = 0;
97
98 if (!sctx)
99 goto err;
100
101 if (!sehandle_prop)
102 goto err;
103
104 if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
105 goto err;
106
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800107 if (selinux_check_access(sctx, tctx, "property_service", "set", (void*) name) == 0)
rpcraig63207cd2012-08-09 10:05:49 -0400108 result = 1;
109
110 freecon(tctx);
111 err:
112 return result;
rpcraig63207cd2012-08-09 10:05:49 -0400113}
114
115static int check_control_mac_perms(const char *name, char *sctx)
116{
rpcraig63207cd2012-08-09 10:05:49 -0400117 /*
118 * Create a name prefix out of ctl.<service name>
119 * The new prefix allows the use of the existing
120 * property service backend labeling while avoiding
121 * mislabels based on true property prefixes.
122 */
123 char ctl_name[PROP_VALUE_MAX+4];
124 int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
125
126 if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
127 return 0;
128
129 return check_mac_perms(ctl_name, sctx);
rpcraig63207cd2012-08-09 10:05:49 -0400130}
131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 * Checks permissions for setting system properties.
134 * Returns 1 if uid allowed, 0 otherwise.
135 */
Nick Kralevich528c13e2014-06-17 22:23:54 -0700136static int check_perms(const char *name, char *sctx)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 if(!strncmp(name, "ro.", 3))
139 name +=3;
140
Nick Kralevich528c13e2014-06-17 22:23:54 -0700141 return check_mac_perms(name, sctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142}
143
Colin Cross88ac54a2013-01-29 14:58:57 -0800144int __property_get(const char *name, char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145{
Colin Cross2deedfe2013-01-28 17:13:35 -0800146 return __system_property_get(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147}
148
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800149static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150{
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700151 char tempPath[PATH_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 char path[PATH_MAX];
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700153 int fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700155 snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
156 fd = mkstemp(tempPath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 if (fd < 0) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700158 ERROR("Unable to write persistent property to temp file %s: %s\n", tempPath, strerror(errno));
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800159 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 }
161 write(fd, value, strlen(value));
OPPOde73a0c2014-03-28 19:12:47 +0800162 fsync(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 close(fd);
164
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700165 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 if (rename(tempPath, path)) {
167 unlink(tempPath);
168 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
169 }
170}
171
Nick Kralevich69463612013-09-13 17:21:28 -0700172static bool is_legal_property_name(const char* name, size_t namelen)
173{
174 size_t i;
Nick Kralevich69463612013-09-13 17:21:28 -0700175 if (namelen >= PROP_NAME_MAX) return false;
176 if (namelen < 1) return false;
177 if (name[0] == '.') return false;
178 if (name[namelen - 1] == '.') return false;
179
180 /* Only allow alphanumeric, plus '.', '-', or '_' */
181 /* Don't allow ".." to appear in a property name */
182 for (i = 0; i < namelen; i++) {
183 if (name[i] == '.') {
Nick Kralevichc2c5a242013-09-16 11:30:48 -0700184 // i=0 is guaranteed to never have a dot. See above.
185 if (name[i-1] == '.') return false;
Nick Kralevich69463612013-09-13 17:21:28 -0700186 continue;
187 }
Nick Kralevich69463612013-09-13 17:21:28 -0700188 if (name[i] == '_' || name[i] == '-') continue;
189 if (name[i] >= 'a' && name[i] <= 'z') continue;
190 if (name[i] >= 'A' && name[i] <= 'Z') continue;
191 if (name[i] >= '0' && name[i] <= '9') continue;
192 return false;
193 }
194
195 return true;
196}
197
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700198static int property_set_impl(const char* name, const char* value) {
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700199 size_t namelen = strlen(name);
200 size_t valuelen = strlen(value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Nick Kralevich69463612013-09-13 17:21:28 -0700202 if (!is_legal_property_name(name, namelen)) return -1;
203 if (valuelen >= PROP_VALUE_MAX) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700205 prop_info* pi = (prop_info*) __system_property_find(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206
207 if(pi != 0) {
208 /* ro.* properties may NEVER be modified once set */
209 if(!strncmp(name, "ro.", 3)) return -1;
210
Colin Cross9f5af632013-01-23 23:09:48 -0800211 __system_property_update(pi, value, valuelen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 } else {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700213 int rc = __system_property_add(name, namelen, value, valuelen);
214 if (rc < 0) {
215 return rc;
Johan Redestigfd7ffb12013-04-29 09:11:57 +0200216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 }
218 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400219 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if (strcmp("net.change", name) == 0) {
221 return 0;
222 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800223 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 * The 'net.change' property is a special property used track when any
225 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
226 * contains the last updated 'net.*' property.
227 */
228 property_set("net.change", name);
229 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400230 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800231 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 * Don't write properties to disk until after we have read all default properties
233 * to prevent them from being overwritten by default values.
234 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800235 write_persistent_property(name, value);
repo sync8a387872013-05-17 12:47:04 -0700236 } else if (strcmp("selinux.reload_policy", name) == 0 &&
237 strcmp("1", value) == 0) {
238 selinux_reload_policy();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
240 property_changed(name, value);
241 return 0;
242}
243
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700244int property_set(const char* name, const char* value) {
245 int rc = property_set_impl(name, value);
246 if (rc == -1) {
Elliott Hughes930974c2015-03-23 08:07:19 -0700247 ERROR("property_set(\"%s\", \"%s\") failed\n", name, value);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700248 }
249 return rc;
250}
251
Elliott Hughes929f4072015-04-24 21:13:44 -0700252static void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253{
254 prop_msg msg;
255 int s;
256 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 struct ucred cr;
258 struct sockaddr_un addr;
259 socklen_t addr_size = sizeof(addr);
260 socklen_t cr_size = sizeof(cr);
rpcraig63207cd2012-08-09 10:05:49 -0400261 char * source_ctx = NULL;
JP Abgrall4515d812014-01-31 14:37:07 -0800262 struct pollfd ufds[1];
263 const int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
264 int nr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
Colin Crossd11beb22010-04-13 19:33:37 -0700266 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 return;
268 }
269
270 /* Check socket options here */
271 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
272 close(s);
Nick Kralevich7ecfe6a2012-10-02 14:59:59 -0700273 ERROR("Unable to receive socket options\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 return;
275 }
276
JP Abgrall4515d812014-01-31 14:37:07 -0800277 ufds[0].fd = s;
278 ufds[0].events = POLLIN;
279 ufds[0].revents = 0;
280 nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
281 if (nr == 0) {
282 ERROR("sys_prop: timeout waiting for uid=%d to send property message.\n", cr.uid);
283 close(s);
284 return;
285 } else if (nr < 0) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700286 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 -0800287 close(s);
288 return;
289 }
290
291 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 if(r != sizeof(prop_msg)) {
Elliott Hughescd67f002015-03-20 17:05:56 -0700293 ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s\n",
294 r, sizeof(prop_msg), strerror(errno));
Brad Fitzpatrick9f1e0e32011-03-30 12:39:56 -0700295 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 return;
297 }
298
299 switch(msg.cmd) {
300 case PROP_MSG_SETPROP:
301 msg.name[PROP_NAME_MAX-1] = 0;
302 msg.value[PROP_VALUE_MAX-1] = 0;
303
Nick Kralevich69463612013-09-13 17:21:28 -0700304 if (!is_legal_property_name(msg.name, strlen(msg.name))) {
305 ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
306 close(s);
307 return;
308 }
309
rpcraig63207cd2012-08-09 10:05:49 -0400310 getpeercon(s, &source_ctx);
rpcraig63207cd2012-08-09 10:05:49 -0400311
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 if(memcmp(msg.name,"ctl.",4) == 0) {
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700313 // Keep the old close-socket-early behavior when handling
314 // ctl.* properties.
315 close(s);
Nick Kralevich528c13e2014-06-17 22:23:54 -0700316 if (check_control_mac_perms(msg.value, source_ctx)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 handle_control_message((char*) msg.name + 4, (char*) msg.value);
318 } else {
Wink Savillecfa0d842010-10-03 13:30:11 -0700319 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
320 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 }
322 } else {
Nick Kralevich528c13e2014-06-17 22:23:54 -0700323 if (check_perms(msg.name, source_ctx)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 property_set((char*) msg.name, (char*) msg.value);
325 } else {
326 ERROR("sys_prop: permission denied uid:%d name:%s\n",
327 cr.uid, msg.name);
328 }
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700329
330 // Note: bionic's property client code assumes that the
331 // property server will not close the socket until *AFTER*
332 // the property is written to memory.
333 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 }
rpcraig63207cd2012-08-09 10:05:49 -0400335 freecon(source_ctx);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 break;
337
338 default:
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700339 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 break;
341 }
342}
343
Nick Kralevich28406472013-01-22 12:46:09 -0800344void get_property_workspace(int *fd, int *sz)
345{
346 *fd = pa_workspace.fd;
347 *sz = pa_workspace.size;
348}
349
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800350static void load_properties_from_file(const char *, const char *);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800352/*
353 * Filter is used to decide which properties to load: NULL loads all keys,
354 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
355 */
356static void load_properties(char *data, const char *filter)
357{
358 char *key, *value, *eol, *sol, *tmp, *fn;
359 size_t flen = 0;
360
361 if (filter) {
362 flen = strlen(filter);
363 }
364
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 sol = data;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800366 while ((eol = strchr(sol, '\n'))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 key = sol;
368 *eol++ = 0;
369 sol = eol;
370
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800371 while (isspace(*key)) key++;
372 if (*key == '#') continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 tmp = eol - 2;
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800375 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800377 if (!strncmp(key, "import ", 7) && flen == 0) {
378 fn = key + 7;
379 while (isspace(*fn)) fn++;
380
381 key = strchr(fn, ' ');
382 if (key) {
383 *key++ = 0;
384 while (isspace(*key)) key++;
385 }
386
387 load_properties_from_file(fn, key);
388
389 } else {
390 value = strchr(key, '=');
391 if (!value) continue;
392 *value++ = 0;
393
394 tmp = value - 2;
395 while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
396
397 while (isspace(*value)) value++;
398
399 if (flen > 0) {
400 if (filter[flen - 1] == '*') {
401 if (strncmp(key, filter, flen - 1)) continue;
402 } else {
403 if (strcmp(key, filter)) continue;
404 }
405 }
406
407 property_set(key, value);
408 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 }
410}
411
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800412/*
413 * Filter is used to decide which properties to load: NULL loads all keys,
414 * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
415 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700416static void load_properties_from_file(const char* filename, const char* filter) {
417 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800418 std::string data;
Elliott Hughesda40c002015-03-27 23:20:44 -0700419 if (read_file(filename, &data)) {
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700420 data.push_back('\n');
Elliott Hughesf682b472015-02-06 12:19:48 -0800421 load_properties(&data[0], filter);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 }
Elliott Hughesda40c002015-03-27 23:20:44 -0700423 NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424}
425
Elliott Hughes81399e12015-03-10 08:39:45 -0700426static void load_persistent_properties() {
427 persistent_properties_loaded = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
Elliott Hughes81399e12015-03-10 08:39:45 -0700429 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
430 if (!dir) {
431 ERROR("Unable to open persistent property directory \"%s\": %s\n",
432 PERSISTENT_PROPERTY_DIR, strerror(errno));
433 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800435
Elliott Hughes81399e12015-03-10 08:39:45 -0700436 struct dirent* entry;
437 while ((entry = readdir(dir.get())) != NULL) {
438 if (strncmp("persist.", entry->d_name, strlen("persist."))) {
439 continue;
440 }
441 if (entry->d_type != DT_REG) {
442 continue;
443 }
444
445 // Open the file and read the property value.
446 int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
447 if (fd == -1) {
448 ERROR("Unable to open persistent property file \"%s\": %s\n",
449 entry->d_name, strerror(errno));
450 continue;
451 }
452
453 struct stat sb;
454 if (fstat(fd, &sb) == -1) {
455 ERROR("fstat on property file \"%s\" failed: %s\n", entry->d_name, strerror(errno));
456 close(fd);
457 continue;
458 }
459
460 // File must not be accessible to others, be owned by root/root, and
461 // not be a hard link to any other file.
462 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || (sb.st_uid != 0) || (sb.st_gid != 0) ||
463 (sb.st_nlink != 1)) {
464 ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
465 entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
466 (unsigned int)sb.st_nlink, sb.st_mode);
467 close(fd);
468 continue;
469 }
470
471 char value[PROP_VALUE_MAX];
472 int length = read(fd, value, sizeof(value) - 1);
473 if (length >= 0) {
474 value[length] = 0;
475 property_set(entry->d_name, value);
476 } else {
477 ERROR("Unable to read persistent property file %s: %s\n",
478 entry->d_name, strerror(errno));
479 }
480 close(fd);
481 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482}
483
Elliott Hughesda40c002015-03-27 23:20:44 -0700484void property_load_boot_defaults() {
Andrew Boie3899f522012-10-12 15:23:40 -0700485 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486}
487
Elliott Hughesda40c002015-03-27 23:20:44 -0700488bool properties_initialized() {
489 return property_area_initialized;
Colin Cross3294bbb2010-04-19 17:11:33 -0700490}
491
Nick Kralevich32b90232012-09-19 11:15:24 -0700492static void load_override_properties() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800493 if (ALLOW_LOCAL_PROP_OVERRIDE) {
494 char debuggable[PROP_VALUE_MAX];
495 int ret = property_get("ro.debuggable", debuggable);
496 if (ret && (strcmp(debuggable, "1") == 0)) {
497 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
498 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700499 }
Nick Kralevich32b90232012-09-19 11:15:24 -0700500}
501
Ken Sumrallc5c51032011-03-08 17:01:29 -0800502/* When booting an encrypted system, /data is not mounted when the
503 * property service is started, so any properties stored there are
504 * not loaded. Vold triggers init to load these properties once it
505 * has mounted /data.
506 */
Elliott Hughesda40c002015-03-27 23:20:44 -0700507void load_persist_props(void) {
Nick Kralevich32b90232012-09-19 11:15:24 -0700508 load_override_properties();
Ken Sumrallc5c51032011-03-08 17:01:29 -0800509 /* Read persistent properties after all default values have been loaded. */
510 load_persistent_properties();
511}
512
Andres Moralescb3fce82015-05-08 08:30:33 -0700513void load_recovery_id_prop() {
514 char fstab_filename[PROP_VALUE_MAX + sizeof(FSTAB_PREFIX)];
515 char propbuf[PROP_VALUE_MAX];
516 int ret = property_get("ro.hardware", propbuf);
517 if (!ret) {
518 ERROR("ro.hardware not set - unable to load recovery id\n");
519 return;
520 }
521 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX "%s", propbuf);
522
523 std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename),
524 fs_mgr_free_fstab);
525 if (!tab) {
526 ERROR("unable to read fstab %s: %s\n", fstab_filename, strerror(errno));
527 return;
528 }
529
530 fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
531 if (rec == NULL) {
532 ERROR("/recovery not specified in fstab\n");
533 return;
534 }
535
536 int fd = open(rec->blk_device, O_RDONLY);
537 if (fd == -1) {
538 ERROR("error opening block device %s: %s\n", rec->blk_device, strerror(errno));
539 return;
540 }
541
542 boot_img_hdr hdr;
543 if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
544 std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
545 property_set("ro.recovery_id", hex.c_str());
546 } else {
547 ERROR("error reading /recovery: %s\n", strerror(errno));
548 }
549
550 close(fd);
551}
552
Elliott Hughesda40c002015-03-27 23:20:44 -0700553void load_all_props() {
Andrew Boie3899f522012-10-12 15:23:40 -0700554 load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
Daniel Rosenbergb9512222014-11-10 17:01:33 -0800555 load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800556 load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
557
Nick Kralevich32b90232012-09-19 11:15:24 -0700558 load_override_properties();
Jeff Sharkeyf96b0442014-03-06 14:26:36 -0800559
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 /* Read persistent properties after all default values have been loaded. */
561 load_persistent_properties();
Andres Moralescb3fce82015-05-08 08:30:33 -0700562
563 load_recovery_id_prop();
Riley Andrewse4b7b292014-06-16 15:06:21 -0700564}
565
Elliott Hughesda40c002015-03-27 23:20:44 -0700566void start_property_service() {
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700567 property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
568 0666, 0, 0, NULL);
569 if (property_set_fd == -1) {
570 ERROR("start_property_service socket creation failed: %s\n", strerror(errno));
571 exit(1);
572 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700574 listen(property_set_fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700575
Elliott Hughes929f4072015-04-24 21:13:44 -0700576 register_epoll_handler(property_set_fd, handle_property_set_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577}