blob: a5a7a70a9874144814cdc5d07df1f5c844375e5c [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>
30#include <cutils/ashmem.h>
31
32#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
33#include <sys/_system_properties.h>
34
35#include <sys/socket.h>
36#include <sys/un.h>
37#include <sys/select.h>
38#include <sys/types.h>
39#include <netinet/in.h>
40#include <sys/mman.h>
41#include <sys/atomics.h>
42#include <private/android_filesystem_config.h>
43
44#include "property_service.h"
45#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070046#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070047#include "log.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49#define PERSISTENT_PROPERTY_DIR "/data/property"
50
51static int persistent_properties_loaded = 0;
52
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 },
64 { "ril.", AID_RADIO, 0 },
65 { "gsm.", AID_RADIO, 0 },
66 { "persist.radio", AID_RADIO, 0 },
67 { "net.dns", AID_RADIO, 0 },
68 { "net.", AID_SYSTEM, 0 },
69 { "dev.", AID_SYSTEM, 0 },
70 { "runtime.", AID_SYSTEM, 0 },
71 { "hw.", AID_SYSTEM, 0 },
72 { "sys.", AID_SYSTEM, 0 },
73 { "service.", AID_SYSTEM, 0 },
74 { "wlan.", AID_SYSTEM, 0 },
75 { "dhcp.", AID_SYSTEM, 0 },
76 { "dhcp.", AID_DHCP, 0 },
77 { "vpn.", AID_SYSTEM, 0 },
78 { "vpn.", AID_VPN, 0 },
79 { "debug.", AID_SHELL, 0 },
80 { "log.", AID_SHELL, 0 },
81 { "service.adb.root", AID_SHELL, 0 },
82 { "persist.sys.", AID_SYSTEM, 0 },
83 { "persist.service.", AID_SYSTEM, 0 },
Oscar Montemayoref4e2152009-11-12 12:02:24 -080084 { "persist.security.", AID_SYSTEM, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040085 { NULL, 0, 0 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086};
87
88/*
89 * White list of UID that are allowed to start/stop services.
90 * Currently there are no user apps that require.
91 */
92struct {
93 const char *service;
94 unsigned int uid;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040095 unsigned int gid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096} control_perms[] = {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040097 { "dumpstate",AID_SHELL, AID_LOG },
98 {NULL, 0, 0 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099};
100
101typedef struct {
102 void *data;
103 size_t size;
104 int fd;
105} workspace;
106
107static int init_workspace(workspace *w, size_t size)
108{
109 void *data;
110 int fd;
111
112 fd = ashmem_create_region("system_properties", size);
113 if(fd < 0)
114 return -1;
115
116 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
117 if(data == MAP_FAILED)
118 goto out;
119
120 /* allow the wolves we share with to do nothing but read */
121 ashmem_set_prot_region(fd, PROT_READ);
122
123 w->data = data;
124 w->size = size;
125 w->fd = fd;
126
127 return 0;
128
129out:
130 close(fd);
131 return -1;
132}
133
134/* (8 header words + 247 toc words) = 1020 bytes */
135/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */
136
137#define PA_COUNT_MAX 247
138#define PA_INFO_START 1024
139#define PA_SIZE 32768
140
141static workspace pa_workspace;
142static prop_info *pa_info_array;
143
144extern prop_area *__system_property_area__;
145
146static int init_property_area(void)
147{
148 prop_area *pa;
149
150 if(pa_info_array)
151 return -1;
152
153 if(init_workspace(&pa_workspace, PA_SIZE))
154 return -1;
155
156 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
157
158 pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START);
159
160 pa = pa_workspace.data;
161 memset(pa, 0, PA_SIZE);
162 pa->magic = PROP_AREA_MAGIC;
163 pa->version = PROP_AREA_VERSION;
164
165 /* plug into the lib property services */
166 __system_property_area__ = pa;
167
168 return 0;
169}
170
171static void update_prop_info(prop_info *pi, const char *value, unsigned len)
172{
173 pi->serial = pi->serial | 1;
174 memcpy(pi->value, value, len + 1);
175 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
176 __futex_wake(&pi->serial, INT32_MAX);
177}
178
179static int property_write(prop_info *pi, const char *value)
180{
181 int valuelen = strlen(value);
182 if(valuelen >= PROP_VALUE_MAX) return -1;
183 update_prop_info(pi, value, valuelen);
184 return 0;
185}
186
187
188/*
189 * Checks permissions for starting/stoping system services.
190 * AID_SYSTEM and AID_ROOT are always allowed.
191 *
192 * Returns 1 if uid allowed, 0 otherwise.
193 */
Colin Crossd11beb22010-04-13 19:33:37 -0700194static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 int i;
196 if (uid == AID_SYSTEM || uid == AID_ROOT)
197 return 1;
198
199 /* Search the ACL */
200 for (i = 0; control_perms[i].service; i++) {
201 if (strcmp(control_perms[i].service, name) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400202 if ((uid && control_perms[i].uid == uid) ||
203 (gid && control_perms[i].gid == gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 return 1;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400205 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 }
207 }
208 return 0;
209}
210
211/*
212 * Checks permissions for setting system properties.
213 * Returns 1 if uid allowed, 0 otherwise.
214 */
Colin Crossd11beb22010-04-13 19:33:37 -0700215static int check_perms(const char *name, unsigned int uid, unsigned int gid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216{
217 int i;
218 if (uid == 0)
219 return 1;
220
221 if(!strncmp(name, "ro.", 3))
222 name +=3;
223
224 for (i = 0; property_perms[i].prefix; i++) {
225 int tmp;
226 if (strncmp(property_perms[i].prefix, name,
227 strlen(property_perms[i].prefix)) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400228 if ((uid && property_perms[i].uid == uid) ||
229 (gid && property_perms[i].gid == gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 return 1;
231 }
232 }
233 }
234
235 return 0;
236}
237
238const char* property_get(const char *name)
239{
240 prop_info *pi;
241
242 if(strlen(name) >= PROP_NAME_MAX) return 0;
243
244 pi = (prop_info*) __system_property_find(name);
245
246 if(pi != 0) {
247 return pi->value;
248 } else {
249 return 0;
250 }
251}
252
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800253static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254{
255 const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp";
256 char path[PATH_MAX];
257 int fd, length;
258
259 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
260
261 fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
262 if (fd < 0) {
263 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800264 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266 write(fd, value, strlen(value));
267 close(fd);
268
269 if (rename(tempPath, path)) {
270 unlink(tempPath);
271 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
272 }
273}
274
275int property_set(const char *name, const char *value)
276{
277 prop_area *pa;
278 prop_info *pi;
279
280 int namelen = strlen(name);
281 int valuelen = strlen(value);
282
283 if(namelen >= PROP_NAME_MAX) return -1;
284 if(valuelen >= PROP_VALUE_MAX) return -1;
285 if(namelen < 1) return -1;
286
287 pi = (prop_info*) __system_property_find(name);
288
289 if(pi != 0) {
290 /* ro.* properties may NEVER be modified once set */
291 if(!strncmp(name, "ro.", 3)) return -1;
292
293 pa = __system_property_area__;
294 update_prop_info(pi, value, valuelen);
295 pa->serial++;
296 __futex_wake(&pa->serial, INT32_MAX);
297 } else {
298 pa = __system_property_area__;
299 if(pa->count == PA_COUNT_MAX) return -1;
300
301 pi = pa_info_array + pa->count;
302 pi->serial = (valuelen << 24);
303 memcpy(pi->name, name, namelen + 1);
304 memcpy(pi->value, value, valuelen + 1);
305
306 pa->toc[pa->count] =
307 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
308
309 pa->count++;
310 pa->serial++;
311 __futex_wake(&pa->serial, INT32_MAX);
312 }
313 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400314 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 if (strcmp("net.change", name) == 0) {
316 return 0;
317 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800318 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 * The 'net.change' property is a special property used track when any
320 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
321 * contains the last updated 'net.*' property.
322 */
323 property_set("net.change", name);
324 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400325 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800326 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 * Don't write properties to disk until after we have read all default properties
328 * to prevent them from being overwritten by default values.
329 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800330 write_persistent_property(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 }
332 property_changed(name, value);
333 return 0;
334}
335
336static int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
337 void *cookie)
338{
339 char name[PROP_NAME_MAX];
340 char value[PROP_VALUE_MAX];
341 const prop_info *pi;
342 unsigned n;
343
344 for(n = 0; (pi = __system_property_find_nth(n)); n++) {
345 __system_property_read(pi, name, value);
346 propfn(name, value, cookie);
347 }
348 return 0;
349}
350
Colin Crossd11beb22010-04-13 19:33:37 -0700351void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352{
353 prop_msg msg;
354 int s;
355 int r;
356 int res;
357 struct ucred cr;
358 struct sockaddr_un addr;
359 socklen_t addr_size = sizeof(addr);
360 socklen_t cr_size = sizeof(cr);
361
Colin Crossd11beb22010-04-13 19:33:37 -0700362 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 return;
364 }
365
366 /* Check socket options here */
367 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
368 close(s);
369 ERROR("Unable to recieve socket options\n");
370 return;
371 }
372
373 r = recv(s, &msg, sizeof(msg), 0);
374 close(s);
375 if(r != sizeof(prop_msg)) {
376 ERROR("sys_prop: mis-match msg size recieved: %d expected: %d\n",
377 r, sizeof(prop_msg));
378 return;
379 }
380
381 switch(msg.cmd) {
382 case PROP_MSG_SETPROP:
383 msg.name[PROP_NAME_MAX-1] = 0;
384 msg.value[PROP_VALUE_MAX-1] = 0;
385
386 if(memcmp(msg.name,"ctl.",4) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400387 if (check_control_perms(msg.value, cr.uid, cr.gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 handle_control_message((char*) msg.name + 4, (char*) msg.value);
389 } else {
390 ERROR("sys_prop: Unable to %s service ctl [%s] uid: %d pid:%d\n",
391 msg.name + 4, msg.value, cr.uid, cr.pid);
392 }
393 } else {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400394 if (check_perms(msg.name, cr.uid, cr.gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 property_set((char*) msg.name, (char*) msg.value);
396 } else {
397 ERROR("sys_prop: permission denied uid:%d name:%s\n",
398 cr.uid, msg.name);
399 }
400 }
401 break;
402
403 default:
404 break;
405 }
406}
407
408void get_property_workspace(int *fd, int *sz)
409{
410 *fd = pa_workspace.fd;
411 *sz = pa_workspace.size;
412}
413
414static void load_properties(char *data)
415{
416 char *key, *value, *eol, *sol, *tmp;
417
418 sol = data;
419 while((eol = strchr(sol, '\n'))) {
420 key = sol;
421 *eol++ = 0;
422 sol = eol;
423
424 value = strchr(key, '=');
425 if(value == 0) continue;
426 *value++ = 0;
427
428 while(isspace(*key)) key++;
429 if(*key == '#') continue;
430 tmp = value - 2;
431 while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
432
433 while(isspace(*value)) value++;
434 tmp = eol - 2;
435 while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
436
437 property_set(key, value);
438 }
439}
440
441static void load_properties_from_file(const char *fn)
442{
443 char *data;
444 unsigned sz;
445
446 data = read_file(fn, &sz);
447
448 if(data != 0) {
449 load_properties(data);
450 free(data);
451 }
452}
453
454static void load_persistent_properties()
455{
456 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
457 struct dirent* entry;
458 char path[PATH_MAX];
459 char value[PROP_VALUE_MAX];
460 int fd, length;
461
462 if (dir) {
463 while ((entry = readdir(dir)) != NULL) {
Mike Lockwoodb3779552009-05-08 14:27:42 -0400464 if (strncmp("persist.", entry->d_name, strlen("persist.")))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 continue;
466#if HAVE_DIRENT_D_TYPE
467 if (entry->d_type != DT_REG)
468 continue;
469#endif
470 /* open the file and read the property value */
471 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name);
472 fd = open(path, O_RDONLY);
473 if (fd >= 0) {
474 length = read(fd, value, sizeof(value) - 1);
475 if (length >= 0) {
476 value[length] = 0;
477 property_set(entry->d_name, value);
478 } else {
479 ERROR("Unable to read persistent property file %s errno: %d\n", path, errno);
480 }
481 close(fd);
482 } else {
483 ERROR("Unable to open persistent property file %s errno: %d\n", path, errno);
484 }
485 }
486 closedir(dir);
487 } else {
488 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
489 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 persistent_properties_loaded = 1;
492}
493
494void property_init(void)
495{
496 init_property_area();
497 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
498}
499
Colin Crossd11beb22010-04-13 19:33:37 -0700500void start_property_service(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501{
502 int fd;
503
504 load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
505 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
506 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
507 /* Read persistent properties after all default values have been loaded. */
508 load_persistent_properties();
509
510 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
Colin Crossd11beb22010-04-13 19:33:37 -0700511 if(fd < 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 fcntl(fd, F_SETFD, FD_CLOEXEC);
513 fcntl(fd, F_SETFL, O_NONBLOCK);
514
515 listen(fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700516 property_set_fd = fd;
517}
518
519int get_property_set_fd()
520{
521 return property_set_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522}