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