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