blob: 35929f0a3e2b6e0c4ac9a9a2e09326d54e1335ee [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"
46
47#define PERSISTENT_PROPERTY_DIR "/data/property"
48
49static int persistent_properties_loaded = 0;
50
51/* White list of permissions for setting property services. */
52struct {
53 const char *prefix;
54 unsigned int uid;
55} property_perms[] = {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +080056 { "net.rmnet0.", AID_RADIO },
57 { "net.gprs.", AID_RADIO },
58 { "net.ppp", AID_RADIO },
59 { "ril.", AID_RADIO },
60 { "gsm.", AID_RADIO },
61 { "persist.radio", AID_RADIO },
62 { "net.dns", AID_RADIO },
63 { "net.", AID_SYSTEM },
64 { "dev.", AID_SYSTEM },
65 { "runtime.", AID_SYSTEM },
66 { "hw.", AID_SYSTEM },
67 { "sys.", AID_SYSTEM },
68 { "service.", AID_SYSTEM },
69 { "wlan.", AID_SYSTEM },
70 { "dhcp.", AID_SYSTEM },
71 { "dhcp.", AID_DHCP },
72 { "vpn.", AID_SYSTEM },
73 { "vpn.", AID_VPN },
74 { "debug.", AID_SHELL },
75 { "log.", AID_SHELL },
76 { "service.adb.root", AID_SHELL },
77 { "persist.sys.", AID_SYSTEM },
78 { "persist.service.", AID_SYSTEM },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 { NULL, 0 }
80};
81
82/*
83 * White list of UID that are allowed to start/stop services.
84 * Currently there are no user apps that require.
85 */
86struct {
87 const char *service;
88 unsigned int uid;
89} control_perms[] = {
90 {NULL, 0 }
91};
92
93typedef struct {
94 void *data;
95 size_t size;
96 int fd;
97} workspace;
98
99static int init_workspace(workspace *w, size_t size)
100{
101 void *data;
102 int fd;
103
104 fd = ashmem_create_region("system_properties", size);
105 if(fd < 0)
106 return -1;
107
108 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
109 if(data == MAP_FAILED)
110 goto out;
111
112 /* allow the wolves we share with to do nothing but read */
113 ashmem_set_prot_region(fd, PROT_READ);
114
115 w->data = data;
116 w->size = size;
117 w->fd = fd;
118
119 return 0;
120
121out:
122 close(fd);
123 return -1;
124}
125
126/* (8 header words + 247 toc words) = 1020 bytes */
127/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */
128
129#define PA_COUNT_MAX 247
130#define PA_INFO_START 1024
131#define PA_SIZE 32768
132
133static workspace pa_workspace;
134static prop_info *pa_info_array;
135
136extern prop_area *__system_property_area__;
137
138static int init_property_area(void)
139{
140 prop_area *pa;
141
142 if(pa_info_array)
143 return -1;
144
145 if(init_workspace(&pa_workspace, PA_SIZE))
146 return -1;
147
148 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
149
150 pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START);
151
152 pa = pa_workspace.data;
153 memset(pa, 0, PA_SIZE);
154 pa->magic = PROP_AREA_MAGIC;
155 pa->version = PROP_AREA_VERSION;
156
157 /* plug into the lib property services */
158 __system_property_area__ = pa;
159
160 return 0;
161}
162
163static void update_prop_info(prop_info *pi, const char *value, unsigned len)
164{
165 pi->serial = pi->serial | 1;
166 memcpy(pi->value, value, len + 1);
167 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
168 __futex_wake(&pi->serial, INT32_MAX);
169}
170
171static int property_write(prop_info *pi, const char *value)
172{
173 int valuelen = strlen(value);
174 if(valuelen >= PROP_VALUE_MAX) return -1;
175 update_prop_info(pi, value, valuelen);
176 return 0;
177}
178
179
180/*
181 * Checks permissions for starting/stoping system services.
182 * AID_SYSTEM and AID_ROOT are always allowed.
183 *
184 * Returns 1 if uid allowed, 0 otherwise.
185 */
186static int check_control_perms(const char *name, int uid) {
187 int i;
188 if (uid == AID_SYSTEM || uid == AID_ROOT)
189 return 1;
190
191 /* Search the ACL */
192 for (i = 0; control_perms[i].service; i++) {
193 if (strcmp(control_perms[i].service, name) == 0) {
194 if (control_perms[i].uid == uid)
195 return 1;
196 }
197 }
198 return 0;
199}
200
201/*
202 * Checks permissions for setting system properties.
203 * Returns 1 if uid allowed, 0 otherwise.
204 */
205static int check_perms(const char *name, unsigned int uid)
206{
207 int i;
208 if (uid == 0)
209 return 1;
210
211 if(!strncmp(name, "ro.", 3))
212 name +=3;
213
214 for (i = 0; property_perms[i].prefix; i++) {
215 int tmp;
216 if (strncmp(property_perms[i].prefix, name,
217 strlen(property_perms[i].prefix)) == 0) {
218 if (property_perms[i].uid == uid) {
219 return 1;
220 }
221 }
222 }
223
224 return 0;
225}
226
227const char* property_get(const char *name)
228{
229 prop_info *pi;
230
231 if(strlen(name) >= PROP_NAME_MAX) return 0;
232
233 pi = (prop_info*) __system_property_find(name);
234
235 if(pi != 0) {
236 return pi->value;
237 } else {
238 return 0;
239 }
240}
241
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800242static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243{
244 const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp";
245 char path[PATH_MAX];
246 int fd, length;
247
248 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
249
250 fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
251 if (fd < 0) {
252 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800253 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 }
255 write(fd, value, strlen(value));
256 close(fd);
257
258 if (rename(tempPath, path)) {
259 unlink(tempPath);
260 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
261 }
262}
263
264int property_set(const char *name, const char *value)
265{
266 prop_area *pa;
267 prop_info *pi;
268
269 int namelen = strlen(name);
270 int valuelen = strlen(value);
271
272 if(namelen >= PROP_NAME_MAX) return -1;
273 if(valuelen >= PROP_VALUE_MAX) return -1;
274 if(namelen < 1) return -1;
275
276 pi = (prop_info*) __system_property_find(name);
277
278 if(pi != 0) {
279 /* ro.* properties may NEVER be modified once set */
280 if(!strncmp(name, "ro.", 3)) return -1;
281
282 pa = __system_property_area__;
283 update_prop_info(pi, value, valuelen);
284 pa->serial++;
285 __futex_wake(&pa->serial, INT32_MAX);
286 } else {
287 pa = __system_property_area__;
288 if(pa->count == PA_COUNT_MAX) return -1;
289
290 pi = pa_info_array + pa->count;
291 pi->serial = (valuelen << 24);
292 memcpy(pi->name, name, namelen + 1);
293 memcpy(pi->value, value, valuelen + 1);
294
295 pa->toc[pa->count] =
296 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
297
298 pa->count++;
299 pa->serial++;
300 __futex_wake(&pa->serial, INT32_MAX);
301 }
302 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400303 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 if (strcmp("net.change", name) == 0) {
305 return 0;
306 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800307 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 * The 'net.change' property is a special property used track when any
309 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
310 * contains the last updated 'net.*' property.
311 */
312 property_set("net.change", name);
313 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400314 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800315 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 * Don't write properties to disk until after we have read all default properties
317 * to prevent them from being overwritten by default values.
318 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800319 write_persistent_property(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 }
321 property_changed(name, value);
322 return 0;
323}
324
325static int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
326 void *cookie)
327{
328 char name[PROP_NAME_MAX];
329 char value[PROP_VALUE_MAX];
330 const prop_info *pi;
331 unsigned n;
332
333 for(n = 0; (pi = __system_property_find_nth(n)); n++) {
334 __system_property_read(pi, name, value);
335 propfn(name, value, cookie);
336 }
337 return 0;
338}
339
340void handle_property_set_fd(int fd)
341{
342 prop_msg msg;
343 int s;
344 int r;
345 int res;
346 struct ucred cr;
347 struct sockaddr_un addr;
348 socklen_t addr_size = sizeof(addr);
349 socklen_t cr_size = sizeof(cr);
350
351 if ((s = accept(fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
352 return;
353 }
354
355 /* Check socket options here */
356 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
357 close(s);
358 ERROR("Unable to recieve socket options\n");
359 return;
360 }
361
362 r = recv(s, &msg, sizeof(msg), 0);
363 close(s);
364 if(r != sizeof(prop_msg)) {
365 ERROR("sys_prop: mis-match msg size recieved: %d expected: %d\n",
366 r, sizeof(prop_msg));
367 return;
368 }
369
370 switch(msg.cmd) {
371 case PROP_MSG_SETPROP:
372 msg.name[PROP_NAME_MAX-1] = 0;
373 msg.value[PROP_VALUE_MAX-1] = 0;
374
375 if(memcmp(msg.name,"ctl.",4) == 0) {
376 if (check_control_perms(msg.value, cr.uid)) {
377 handle_control_message((char*) msg.name + 4, (char*) msg.value);
378 } else {
379 ERROR("sys_prop: Unable to %s service ctl [%s] uid: %d pid:%d\n",
380 msg.name + 4, msg.value, cr.uid, cr.pid);
381 }
382 } else {
383 if (check_perms(msg.name, cr.uid)) {
384 property_set((char*) msg.name, (char*) msg.value);
385 } else {
386 ERROR("sys_prop: permission denied uid:%d name:%s\n",
387 cr.uid, msg.name);
388 }
389 }
390 break;
391
392 default:
393 break;
394 }
395}
396
397void get_property_workspace(int *fd, int *sz)
398{
399 *fd = pa_workspace.fd;
400 *sz = pa_workspace.size;
401}
402
403static void load_properties(char *data)
404{
405 char *key, *value, *eol, *sol, *tmp;
406
407 sol = data;
408 while((eol = strchr(sol, '\n'))) {
409 key = sol;
410 *eol++ = 0;
411 sol = eol;
412
413 value = strchr(key, '=');
414 if(value == 0) continue;
415 *value++ = 0;
416
417 while(isspace(*key)) key++;
418 if(*key == '#') continue;
419 tmp = value - 2;
420 while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
421
422 while(isspace(*value)) value++;
423 tmp = eol - 2;
424 while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
425
426 property_set(key, value);
427 }
428}
429
430static void load_properties_from_file(const char *fn)
431{
432 char *data;
433 unsigned sz;
434
435 data = read_file(fn, &sz);
436
437 if(data != 0) {
438 load_properties(data);
439 free(data);
440 }
441}
442
443static void load_persistent_properties()
444{
445 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
446 struct dirent* entry;
447 char path[PATH_MAX];
448 char value[PROP_VALUE_MAX];
449 int fd, length;
450
451 if (dir) {
452 while ((entry = readdir(dir)) != NULL) {
Mike Lockwoodb3779552009-05-08 14:27:42 -0400453 if (strncmp("persist.", entry->d_name, strlen("persist.")))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 continue;
455#if HAVE_DIRENT_D_TYPE
456 if (entry->d_type != DT_REG)
457 continue;
458#endif
459 /* open the file and read the property value */
460 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name);
461 fd = open(path, O_RDONLY);
462 if (fd >= 0) {
463 length = read(fd, value, sizeof(value) - 1);
464 if (length >= 0) {
465 value[length] = 0;
466 property_set(entry->d_name, value);
467 } else {
468 ERROR("Unable to read persistent property file %s errno: %d\n", path, errno);
469 }
470 close(fd);
471 } else {
472 ERROR("Unable to open persistent property file %s errno: %d\n", path, errno);
473 }
474 }
475 closedir(dir);
476 } else {
477 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
478 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 persistent_properties_loaded = 1;
481}
482
483void property_init(void)
484{
485 init_property_area();
486 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
487}
488
489int start_property_service(void)
490{
491 int fd;
492
493 load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
494 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
495 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
496 /* Read persistent properties after all default values have been loaded. */
497 load_persistent_properties();
498
499 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
500 if(fd < 0) return -1;
501 fcntl(fd, F_SETFD, FD_CLOEXEC);
502 fcntl(fd, F_SETFL, O_NONBLOCK);
503
504 listen(fd, 8);
505 return fd;
506}