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