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