blob: fdfec43dbebb488f4c386c59e6617ac4765cd01f [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
31#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
32#include <sys/_system_properties.h>
33
34#include <sys/socket.h>
35#include <sys/un.h>
36#include <sys/select.h>
37#include <sys/types.h>
38#include <netinet/in.h>
39#include <sys/mman.h>
40#include <sys/atomics.h>
41#include <private/android_filesystem_config.h>
42
43#include "property_service.h"
44#include "init.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070045#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070046#include "log.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;
Colin Cross3294bbb2010-04-19 17:11:33 -070051static int property_area_inited = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
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 },
Amol Bhatkarcf015972011-02-18 17:54:17 -060064 { "net.qmi", AID_RADIO, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040065 { "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 },
Mike Lockwood58aa5b02010-12-10 09:48:41 -080083 { "service.adb.tcp.port", AID_SHELL, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040084 { "persist.sys.", AID_SYSTEM, 0 },
85 { "persist.service.", AID_SYSTEM, 0 },
Oscar Montemayoref4e2152009-11-12 12:02:24 -080086 { "persist.security.", AID_SYSTEM, 0 },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040087 { NULL, 0, 0 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088};
89
90/*
91 * White list of UID that are allowed to start/stop services.
92 * Currently there are no user apps that require.
93 */
94struct {
95 const char *service;
96 unsigned int uid;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040097 unsigned int gid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098} control_perms[] = {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -040099 { "dumpstate",AID_SHELL, AID_LOG },
Wink Savillecfa0d842010-10-03 13:30:11 -0700100 { "ril-daemon",AID_RADIO, AID_RADIO },
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400101 {NULL, 0, 0 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102};
103
104typedef struct {
105 void *data;
106 size_t size;
107 int fd;
108} workspace;
109
110static int init_workspace(workspace *w, size_t size)
111{
112 void *data;
113 int fd;
114
Brian Swetland25b15be2010-07-13 16:43:56 -0700115 /* dev is a tmpfs that we can use to carve a shared workspace
116 * out of, so let's do that...
117 */
118 fd = open("/dev/__properties__", O_RDWR | O_CREAT, 0600);
119 if (fd < 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 return -1;
121
Brian Swetland25b15be2010-07-13 16:43:56 -0700122 if (ftruncate(fd, size) < 0)
123 goto out;
124
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
126 if(data == MAP_FAILED)
127 goto out;
128
Brian Swetland25b15be2010-07-13 16:43:56 -0700129 close(fd);
130
131 fd = open("/dev/__properties__", O_RDONLY);
132 if (fd < 0)
133 return -1;
134
135 unlink("/dev/__properties__");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136
137 w->data = data;
138 w->size = size;
139 w->fd = fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 return 0;
141
142out:
143 close(fd);
144 return -1;
145}
146
147/* (8 header words + 247 toc words) = 1020 bytes */
148/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */
149
150#define PA_COUNT_MAX 247
151#define PA_INFO_START 1024
152#define PA_SIZE 32768
153
154static workspace pa_workspace;
155static prop_info *pa_info_array;
156
157extern prop_area *__system_property_area__;
158
159static int init_property_area(void)
160{
161 prop_area *pa;
162
163 if(pa_info_array)
164 return -1;
165
166 if(init_workspace(&pa_workspace, PA_SIZE))
167 return -1;
168
169 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
170
171 pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START);
172
173 pa = pa_workspace.data;
174 memset(pa, 0, PA_SIZE);
175 pa->magic = PROP_AREA_MAGIC;
176 pa->version = PROP_AREA_VERSION;
177
178 /* plug into the lib property services */
179 __system_property_area__ = pa;
Colin Cross3294bbb2010-04-19 17:11:33 -0700180 property_area_inited = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 return 0;
182}
183
184static void update_prop_info(prop_info *pi, const char *value, unsigned len)
185{
186 pi->serial = pi->serial | 1;
187 memcpy(pi->value, value, len + 1);
188 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
189 __futex_wake(&pi->serial, INT32_MAX);
190}
191
192static int property_write(prop_info *pi, const char *value)
193{
194 int valuelen = strlen(value);
195 if(valuelen >= PROP_VALUE_MAX) return -1;
196 update_prop_info(pi, value, valuelen);
197 return 0;
198}
199
200
201/*
202 * Checks permissions for starting/stoping system services.
203 * AID_SYSTEM and AID_ROOT are always allowed.
204 *
205 * Returns 1 if uid allowed, 0 otherwise.
206 */
Colin Crossd11beb22010-04-13 19:33:37 -0700207static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 int i;
209 if (uid == AID_SYSTEM || uid == AID_ROOT)
210 return 1;
211
212 /* Search the ACL */
213 for (i = 0; control_perms[i].service; i++) {
214 if (strcmp(control_perms[i].service, name) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400215 if ((uid && control_perms[i].uid == uid) ||
216 (gid && control_perms[i].gid == gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 return 1;
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 }
220 }
221 return 0;
222}
223
224/*
225 * Checks permissions for setting system properties.
226 * Returns 1 if uid allowed, 0 otherwise.
227 */
Colin Crossd11beb22010-04-13 19:33:37 -0700228static int check_perms(const char *name, unsigned int uid, unsigned int gid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229{
230 int i;
231 if (uid == 0)
232 return 1;
233
234 if(!strncmp(name, "ro.", 3))
235 name +=3;
236
237 for (i = 0; property_perms[i].prefix; i++) {
238 int tmp;
239 if (strncmp(property_perms[i].prefix, name,
240 strlen(property_perms[i].prefix)) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400241 if ((uid && property_perms[i].uid == uid) ||
242 (gid && property_perms[i].gid == gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 return 1;
244 }
245 }
246 }
247
248 return 0;
249}
250
251const char* property_get(const char *name)
252{
253 prop_info *pi;
254
255 if(strlen(name) >= PROP_NAME_MAX) return 0;
256
257 pi = (prop_info*) __system_property_find(name);
258
259 if(pi != 0) {
260 return pi->value;
261 } else {
262 return 0;
263 }
264}
265
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800266static void write_persistent_property(const char *name, const char *value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267{
268 const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp";
269 char path[PATH_MAX];
270 int fd, length;
271
272 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
273
274 fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
275 if (fd < 0) {
276 ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800277 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 }
279 write(fd, value, strlen(value));
280 close(fd);
281
282 if (rename(tempPath, path)) {
283 unlink(tempPath);
284 ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
285 }
286}
287
288int property_set(const char *name, const char *value)
289{
290 prop_area *pa;
291 prop_info *pi;
292
293 int namelen = strlen(name);
294 int valuelen = strlen(value);
295
296 if(namelen >= PROP_NAME_MAX) return -1;
297 if(valuelen >= PROP_VALUE_MAX) return -1;
298 if(namelen < 1) return -1;
299
300 pi = (prop_info*) __system_property_find(name);
301
302 if(pi != 0) {
303 /* ro.* properties may NEVER be modified once set */
304 if(!strncmp(name, "ro.", 3)) return -1;
305
306 pa = __system_property_area__;
307 update_prop_info(pi, value, valuelen);
308 pa->serial++;
309 __futex_wake(&pa->serial, INT32_MAX);
310 } else {
311 pa = __system_property_area__;
312 if(pa->count == PA_COUNT_MAX) return -1;
313
314 pi = pa_info_array + pa->count;
315 pi->serial = (valuelen << 24);
316 memcpy(pi->name, name, namelen + 1);
317 memcpy(pi->value, value, valuelen + 1);
318
319 pa->toc[pa->count] =
320 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
321
322 pa->count++;
323 pa->serial++;
324 __futex_wake(&pa->serial, INT32_MAX);
325 }
326 /* If name starts with "net." treat as a DNS property. */
Mike Lockwoodb3779552009-05-08 14:27:42 -0400327 if (strncmp("net.", name, strlen("net.")) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 if (strcmp("net.change", name) == 0) {
329 return 0;
330 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800331 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 * The 'net.change' property is a special property used track when any
333 * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
334 * contains the last updated 'net.*' property.
335 */
336 property_set("net.change", name);
337 } else if (persistent_properties_loaded &&
Mike Lockwoodb3779552009-05-08 14:27:42 -0400338 strncmp("persist.", name, strlen("persist.")) == 0) {
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800339 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 * Don't write properties to disk until after we have read all default properties
341 * to prevent them from being overwritten by default values.
342 */
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800343 write_persistent_property(name, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
345 property_changed(name, value);
346 return 0;
347}
348
349static int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
350 void *cookie)
351{
352 char name[PROP_NAME_MAX];
353 char value[PROP_VALUE_MAX];
354 const prop_info *pi;
355 unsigned n;
356
357 for(n = 0; (pi = __system_property_find_nth(n)); n++) {
358 __system_property_read(pi, name, value);
359 propfn(name, value, cookie);
360 }
361 return 0;
362}
363
Colin Crossd11beb22010-04-13 19:33:37 -0700364void handle_property_set_fd()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365{
366 prop_msg msg;
367 int s;
368 int r;
369 int res;
370 struct ucred cr;
371 struct sockaddr_un addr;
372 socklen_t addr_size = sizeof(addr);
373 socklen_t cr_size = sizeof(cr);
374
Colin Crossd11beb22010-04-13 19:33:37 -0700375 if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 return;
377 }
378
379 /* Check socket options here */
380 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
381 close(s);
382 ERROR("Unable to recieve socket options\n");
383 return;
384 }
385
386 r = recv(s, &msg, sizeof(msg), 0);
387 close(s);
388 if(r != sizeof(prop_msg)) {
389 ERROR("sys_prop: mis-match msg size recieved: %d expected: %d\n",
390 r, sizeof(prop_msg));
391 return;
392 }
393
394 switch(msg.cmd) {
395 case PROP_MSG_SETPROP:
396 msg.name[PROP_NAME_MAX-1] = 0;
397 msg.value[PROP_VALUE_MAX-1] = 0;
398
399 if(memcmp(msg.name,"ctl.",4) == 0) {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400400 if (check_control_perms(msg.value, cr.uid, cr.gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 handle_control_message((char*) msg.name + 4, (char*) msg.value);
402 } else {
Wink Savillecfa0d842010-10-03 13:30:11 -0700403 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
404 msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 }
406 } else {
Mike Lockwoodc5e7ef22009-09-02 18:08:52 -0400407 if (check_perms(msg.name, cr.uid, cr.gid)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 property_set((char*) msg.name, (char*) msg.value);
409 } else {
410 ERROR("sys_prop: permission denied uid:%d name:%s\n",
411 cr.uid, msg.name);
412 }
413 }
414 break;
415
416 default:
417 break;
418 }
419}
420
421void get_property_workspace(int *fd, int *sz)
422{
423 *fd = pa_workspace.fd;
424 *sz = pa_workspace.size;
425}
426
427static void load_properties(char *data)
428{
429 char *key, *value, *eol, *sol, *tmp;
430
431 sol = data;
432 while((eol = strchr(sol, '\n'))) {
433 key = sol;
434 *eol++ = 0;
435 sol = eol;
436
437 value = strchr(key, '=');
438 if(value == 0) continue;
439 *value++ = 0;
440
441 while(isspace(*key)) key++;
442 if(*key == '#') continue;
443 tmp = value - 2;
444 while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
445
446 while(isspace(*value)) value++;
447 tmp = eol - 2;
448 while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
449
450 property_set(key, value);
451 }
452}
453
454static void load_properties_from_file(const char *fn)
455{
456 char *data;
457 unsigned sz;
458
459 data = read_file(fn, &sz);
460
461 if(data != 0) {
462 load_properties(data);
463 free(data);
464 }
465}
466
467static void load_persistent_properties()
468{
469 DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
470 struct dirent* entry;
471 char path[PATH_MAX];
472 char value[PROP_VALUE_MAX];
473 int fd, length;
474
475 if (dir) {
476 while ((entry = readdir(dir)) != NULL) {
Mike Lockwoodb3779552009-05-08 14:27:42 -0400477 if (strncmp("persist.", entry->d_name, strlen("persist.")))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 continue;
479#if HAVE_DIRENT_D_TYPE
480 if (entry->d_type != DT_REG)
481 continue;
482#endif
483 /* open the file and read the property value */
484 snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name);
485 fd = open(path, O_RDONLY);
486 if (fd >= 0) {
487 length = read(fd, value, sizeof(value) - 1);
488 if (length >= 0) {
489 value[length] = 0;
490 property_set(entry->d_name, value);
491 } else {
492 ERROR("Unable to read persistent property file %s errno: %d\n", path, errno);
493 }
494 close(fd);
495 } else {
496 ERROR("Unable to open persistent property file %s errno: %d\n", path, errno);
497 }
498 }
499 closedir(dir);
500 } else {
501 ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
502 }
Tammo Spalink3dfe6c62009-08-26 10:13:20 +0800503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 persistent_properties_loaded = 1;
505}
506
507void property_init(void)
508{
509 init_property_area();
510 load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT);
511}
512
Colin Cross3294bbb2010-04-19 17:11:33 -0700513int properties_inited(void)
514{
515 return property_area_inited;
516}
517
Colin Crossd11beb22010-04-13 19:33:37 -0700518void start_property_service(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519{
520 int fd;
521
522 load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
523 load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
524 load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
525 /* Read persistent properties after all default values have been loaded. */
526 load_persistent_properties();
527
528 fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
Colin Crossd11beb22010-04-13 19:33:37 -0700529 if(fd < 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 fcntl(fd, F_SETFD, FD_CLOEXEC);
531 fcntl(fd, F_SETFL, O_NONBLOCK);
532
533 listen(fd, 8);
Colin Crossd11beb22010-04-13 19:33:37 -0700534 property_set_fd = fd;
535}
536
537int get_property_set_fd()
538{
539 return property_set_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540}