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