blob: 77aa5e983acfd20e425a7cc2d3abfcb7ef227d62 [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>
27
28#include <cutils/misc.h>
29#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
31#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
32#include <sys/_system_properties.h>
33
34#include <sys/socket.h>
35#include <sys/un.h>
36#include <sys/select.h>
37#include <sys/types.h>
38#include <netinet/in.h>
39#include <sys/mman.h>
40#include <sys/atomics.h>
41#include <private/android_filesystem_config.h>
42
43#include "property_service.h"
44#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070045#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070046#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48#define PERSISTENT_PROPERTY_DIR "/data/property"
49
50static int persistent_properties_loaded = 0;
Colin Cross3294bbb2010-04-19 17:11:33 -070051static int property_area_inited = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Colin Crossd11beb22010-04-13 19:33:37 -070053static int property_set_fd = -1;
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055/* White list of permissions for setting property services. */
56struct {
57 const char *prefix;
58 unsigned int uid;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040059 unsigned int gid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060} property_perms[] = {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040061 { "net.rmnet0.", AID_RADIO, 0 },
62 { "net.gprs.", AID_RADIO, 0 },
63 { "net.ppp", AID_RADIO, 0 },
Amol Bhatkarcf015972011-02-18 17:54:17 -060064 { "net.qmi", AID_RADIO, 0 },
Benoit Goby7100f642011-07-25 18:02:06 -070065 { "net.lte", AID_RADIO, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040066 { "ril.", AID_RADIO, 0 },
67 { "gsm.", AID_RADIO, 0 },
68 { "persist.radio", AID_RADIO, 0 },
69 { "net.dns", AID_RADIO, 0 },
70 { "net.", AID_SYSTEM, 0 },
71 { "dev.", AID_SYSTEM, 0 },
72 { "runtime.", AID_SYSTEM, 0 },
73 { "hw.", AID_SYSTEM, 0 },
74 { "sys.", AID_SYSTEM, 0 },
75 { "service.", AID_SYSTEM, 0 },
76 { "wlan.", AID_SYSTEM, 0 },
77 { "dhcp.", AID_SYSTEM, 0 },
78 { "dhcp.", AID_DHCP, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040079 { "debug.", AID_SHELL, 0 },
80 { "log.", AID_SHELL, 0 },
81 { "service.adb.root", AID_SHELL, 0 },
Mike Lockwood58aa5b02010-12-10 09:48:41 -080082 { "service.adb.tcp.port", AID_SHELL, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040083 { "persist.sys.", AID_SYSTEM, 0 },
84 { "persist.service.", AID_SYSTEM, 0 },
Oscar Montemayoref4e2152009-11-12 12:02:24 -080085 { "persist.security.", AID_SYSTEM, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040086 { NULL, 0, 0 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087};
88
89/*
90 * White list of UID that are allowed to start/stop services.
91 * Currently there are no user apps that require.
92 */
93struct {
94 const char *service;
95 unsigned int uid;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040096 unsigned int gid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097} control_perms[] = {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040098 { "dumpstate",AID_SHELL, AID_LOG },
Wink Savillecfa0d842010-10-03 13:30:11 -070099 { "ril-daemon",AID_RADIO, AID_RADIO },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400100 {NULL, 0, 0 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101};
102
103typedef struct {
104 void *data;
105 size_t size;
106 int fd;
107} workspace;
108
109static int init_workspace(workspace *w, size_t size)
110{
111 void *data;
112 int fd;
113
Brian Swetland25b15be2010-07-13 16:43:56 -0700114 /* dev is a tmpfs that we can use to carve a shared workspace
115 * out of, so let's do that...
116 */
117 fd = open("/dev/__properties__", O_RDWR | O_CREAT, 0600);
118 if (fd < 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 return -1;
120
Brian Swetland25b15be2010-07-13 16:43:56 -0700121 if (ftruncate(fd, size) < 0)
122 goto out;
123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
125 if(data == MAP_FAILED)
126 goto out;
127
Brian Swetland25b15be2010-07-13 16:43:56 -0700128 close(fd);
129
130 fd = open("/dev/__properties__", O_RDONLY);
131 if (fd < 0)
132 return -1;
133
134 unlink("/dev/__properties__");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135
136 w->data = data;
137 w->size = size;
138 w->fd = fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 return 0;
140
141out:
142 close(fd);
143 return -1;
144}
145
146/* (8 header words + 247 toc words) = 1020 bytes */
147/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */
148
149#define PA_COUNT_MAX 247
150#define PA_INFO_START 1024
151#define PA_SIZE 32768
152
153static workspace pa_workspace;
154static prop_info *pa_info_array;
155
156extern prop_area *__system_property_area__;
157
158static int init_property_area(void)
159{
160 prop_area *pa;
161
162 if(pa_info_array)
163 return -1;
164
165 if(init_workspace(&pa_workspace, PA_SIZE))
166 return -1;
167
168 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
169
170 pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START);
171
172 pa = pa_workspace.data;
173 memset(pa, 0, PA_SIZE);
174 pa->magic = PROP_AREA_MAGIC;
175 pa->version = PROP_AREA_VERSION;
176
177 /* plug into the lib property services */
178 __system_property_area__ = pa;
Colin Cross3294bbb2010-04-19 17:11:33 -0700179 property_area_inited = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return 0;
181}
182
183static void update_prop_info(prop_info *pi, const char *value, unsigned len)
184{
185 pi->serial = pi->serial | 1;
186 memcpy(pi->value, value, len + 1);
187 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
188 __futex_wake(&pi->serial, INT32_MAX);
189}
190
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191/*
192 * Checks permissions for starting/stoping system services.
193 * AID_SYSTEM and AID_ROOT are always allowed.
194 *
195 * Returns 1 if uid allowed, 0 otherwise.
196 */
Colin Crossd11beb22010-04-13 19:33:37 -0700197static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 int i;
199 if (uid == AID_SYSTEM || uid == AID_ROOT)
200 return 1;
201
202 /* Search the ACL */
203 for (i = 0; control_perms[i].service; i++) {
204 if (strcmp(control_perms[i].service, name) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400205 if ((uid && control_perms[i].uid == uid) ||
206 (gid && control_perms[i].gid == gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 return 1;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
210 }
211 return 0;
212}
213
214/*
215 * Checks permissions for setting system properties.
216 * Returns 1 if uid allowed, 0 otherwise.
217 */
Colin Crossd11beb22010-04-13 19:33:37 -0700218static int check_perms(const char *name, unsigned int uid, unsigned int gid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219{
220 int i;
221 if (uid == 0)
222 return 1;
223
224 if(!strncmp(name, "ro.", 3))
225 name +=3;
226
227 for (i = 0; property_perms[i].prefix; i++) {
228 int tmp;
229 if (strncmp(property_perms[i].prefix, name,
230 strlen(property_perms[i].prefix)) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400231 if ((uid && property_perms[i].uid == uid) ||
232 (gid && property_perms[i].gid == gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 return 1;
234 }
235 }
236 }
237
238 return 0;
239}
240
241const char* property_get(const char *name)
242{
243 prop_info *pi;
244
245 if(strlen(name) >= PROP_NAME_MAX) return 0;
246
247 pi = (prop_info*) __system_property_find(name);
248
249 if(pi != 0) {
250 return pi->value;
251 } else {
252 return 0;
253 }
254}
255
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800256static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257{
258 const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp";
259 char path[PATH_MAX];
260 int fd, length;
261
262 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
263
264 fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
265 if (fd < 0) {
266 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800267 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 }
269 write(fd, value, strlen(value));
270 close(fd);
271
272 if (rename(tempPath, path)) {
273 unlink(tempPath);
274 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
275 }
276}
277
278int property_set(const char *name, const char *value)
279{
280 prop_area *pa;
281 prop_info *pi;
282
283 int namelen = strlen(name);
284 int valuelen = strlen(value);
285
286 if(namelen >= PROP_NAME_MAX) return -1;
287 if(valuelen >= PROP_VALUE_MAX) return -1;
288 if(namelen < 1) return -1;
289
290 pi = (prop_info*) __system_property_find(name);
291
292 if(pi != 0) {
293 /* ro.* properties may NEVER be modified once set */
294 if(!strncmp(name, "ro.", 3)) return -1;
295
296 pa = __system_property_area__;
297 update_prop_info(pi, value, valuelen);
298 pa->serial++;
299 __futex_wake(&pa->serial, INT32_MAX);
300 } else {
301 pa = __system_property_area__;
302 if(pa->count == PA_COUNT_MAX) return -1;
303
304 pi = pa_info_array + pa->count;
305 pi->serial = (valuelen << 24);
306 memcpy(pi->name, name, namelen + 1);
307 memcpy(pi->value, value, valuelen + 1);
308
309 pa->toc[pa->count] =
310 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
311
312 pa->count++;
313 pa->serial++;
314 __futex_wake(&pa->serial, INT32_MAX);
315 }
316 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400317 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 if (strcmp("net.change", name) == 0) {
319 return 0;
320 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800321 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 * The 'net.change' property is a special property used track when any
323 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
324 * contains the last updated 'net.*' property.
325 */
326 property_set("net.change", name);
327 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400328 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800329 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 * Don't write properties to disk until after we have read all default properties
331 * to prevent them from being overwritten by default values.
332 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800333 write_persistent_property(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 }
335 property_changed(name, value);
336 return 0;
337}
338
339static int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
340 void *cookie)
341{
342 char name[PROP_NAME_MAX];
343 char value[PROP_VALUE_MAX];
344 const prop_info *pi;
345 unsigned n;
346
347 for(n = 0; (pi = __system_property_find_nth(n)); n++) {
348 __system_property_read(pi, name, value);
349 propfn(name, value, cookie);
350 }
351 return 0;
352}
353
Colin Crossd11beb22010-04-13 19:33:37 -0700354void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355{
356 prop_msg msg;
357 int s;
358 int r;
359 int res;
360 struct ucred cr;
361 struct sockaddr_un addr;
362 socklen_t addr_size = sizeof(addr);
363 socklen_t cr_size = sizeof(cr);
364
Colin Crossd11beb22010-04-13 19:33:37 -0700365 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 return;
367 }
368
369 /* Check socket options here */
370 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
371 close(s);
372 ERROR("Unable to recieve socket options\n");
373 return;
374 }
375
Mike Lockwoodd18678f2011-07-01 14:48:50 -0400376 r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 if(r != sizeof(prop_msg)) {
Mike Lockwoodd18678f2011-07-01 14:48:50 -0400378 ERROR("sys_prop: mis-match msg size recieved: %d expected: %d errno: %d\n",
379 r, sizeof(prop_msg), errno);
Brad Fitzpatrick9f1e0e32011-03-30 12:39:56 -0700380 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 return;
382 }
383
384 switch(msg.cmd) {
385 case PROP_MSG_SETPROP:
386 msg.name[PROP_NAME_MAX-1] = 0;
387 msg.value[PROP_VALUE_MAX-1] = 0;
388
389 if(memcmp(msg.name,"ctl.",4) == 0) {
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700390 // Keep the old close-socket-early behavior when handling
391 // ctl.* properties.
392 close(s);
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400393 if (check_control_perms(msg.value, cr.uid, cr.gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 handle_control_message((char*) msg.name + 4, (char*) msg.value);
395 } else {
Wink Savillecfa0d842010-10-03 13:30:11 -0700396 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
397 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 }
399 } else {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400400 if (check_perms(msg.name, cr.uid, cr.gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 property_set((char*) msg.name, (char*) msg.value);
402 } else {
403 ERROR("sys_prop: permission denied uid:%d name:%s\n",
404 cr.uid, msg.name);
405 }
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700406
407 // Note: bionic's property client code assumes that the
408 // property server will not close the socket until *AFTER*
409 // the property is written to memory.
410 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 }
412 break;
413
414 default:
Brad Fitzpatrick71ead182011-04-01 08:24:13 -0700415 close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 break;
417 }
418}
419
420void get_property_workspace(int *fd, int *sz)
421{
422 *fd = pa_workspace.fd;
423 *sz = pa_workspace.size;
424}
425
426static void load_properties(char *data)
427{
428 char *key, *value, *eol, *sol, *tmp;
429
430 sol = data;
431 while((eol = strchr(sol, '\n'))) {
432 key = sol;
433 *eol++ = 0;
434 sol = eol;
435
436 value = strchr(key, '=');
437 if(value == 0) continue;
438 *value++ = 0;
439
440 while(isspace(*key)) key++;
441 if(*key == '#') continue;
442 tmp = value - 2;
443 while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
444
445 while(isspace(*value)) value++;
446 tmp = eol - 2;
447 while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
448
449 property_set(key, value);
450 }
451}
452
453static void load_properties_from_file(const char *fn)
454{
455 char *data;
456 unsigned sz;
457
458 data = read_file(fn, &sz);
459
460 if(data != 0) {
461 load_properties(data);
462 free(data);
463 }
464}
465
466static void load_persistent_properties()
467{
468 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
469 struct dirent* entry;
470 char path[PATH_MAX];
471 char value[PROP_VALUE_MAX];
472 int fd, length;
473
474 if (dir) {
475 while ((entry = readdir(dir)) != NULL) {
Mike Lockwoodb3779552009-05-08 14:27:42 -0400476 if (strncmp("persist.", entry->d_name, strlen("persist.")))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 continue;
478#if HAVE_DIRENT_D_TYPE
479 if (entry->d_type != DT_REG)
480 continue;
481#endif
482 /* open the file and read the property value */
483 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name);
484 fd = open(path, O_RDONLY);
485 if (fd >= 0) {
486 length = read(fd, value, sizeof(value) - 1);
487 if (length >= 0) {
488 value[length] = 0;
489 property_set(entry->d_name, value);
490 } else {
491 ERROR("Unable to read persistent property file %s errno: %d\n", path, errno);
492 }
493 close(fd);
494 } else {
495 ERROR("Unable to open persistent property file %s errno: %d\n", path, errno);
496 }
497 }
498 closedir(dir);
499 } else {
500 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
501 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 persistent_properties_loaded = 1;
504}
505
506void property_init(void)
507{
508 init_property_area();
509 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
510}
511
Colin Cross3294bbb2010-04-19 17:11:33 -0700512int properties_inited(void)
513{
514 return property_area_inited;
515}
516
Ken Sumrallc5c51032011-03-08 17:01:29 -0800517/* When booting an encrypted system, /data is not mounted when the
518 * property service is started, so any properties stored there are
519 * not loaded. Vold triggers init to load these properties once it
520 * has mounted /data.
521 */
522void load_persist_props(void)
523{
524 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
525 /* Read persistent properties after all default values have been loaded. */
526 load_persistent_properties();
527}
528
Colin Crossd11beb22010-04-13 19:33:37 -0700529void start_property_service(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530{
531 int fd;
532
533 load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
534 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
535 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
536 /* Read persistent properties after all default values have been loaded. */
537 load_persistent_properties();
538
539 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
Colin Crossd11beb22010-04-13 19:33:37 -0700540 if(fd < 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 fcntl(fd, F_SETFD, FD_CLOEXEC);
542 fcntl(fd, F_SETFL, O_NONBLOCK);
543
544 listen(fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700545 property_set_fd = fd;
546}
547
548int get_property_set_fd()
549{
550 return property_set_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551}