blob: 12865c41750f29c81096ddb52d0d34a6989ea310 [file] [log] [blame]
that7e303cf2014-03-06 07:57:43 +01001/*
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/properties.h>
29
30#include "legacy_properties.h"
31
32#include <sys/mman.h>
33#include <sys/atomics.h>
34#include "legacy_property_service.h"
35
that7e303cf2014-03-06 07:57:43 +010036static int persistent_properties_loaded = 0;
37static int property_area_inited = 0;
38
39static int property_set_fd = -1;
40
that7e303cf2014-03-06 07:57:43 +010041typedef struct {
42 void *data;
43 size_t size;
44 int fd;
45} workspace;
46
47static int init_workspace(workspace *w, size_t size)
48{
49 void *data;
50 int fd;
51
52 /* dev is a tmpfs that we can use to carve a shared workspace
53 * out of, so let's do that...
54 */
55 fd = open("/dev/__legacy_properties__", O_RDWR | O_CREAT, 0600);
56 if (fd < 0)
57 return -1;
58
59 if (ftruncate(fd, size) < 0)
60 goto out;
61
62 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
63 if(data == MAP_FAILED)
64 goto out;
65
66 close(fd);
67
68 fd = open("/dev/__legacy_properties__", O_RDONLY);
69 if (fd < 0)
70 return -1;
71
72 unlink("/dev/__legacy_properties__");
73
74 w->data = data;
75 w->size = size;
76 w->fd = fd;
77 return 0;
78
79out:
80 close(fd);
81 return -1;
82}
83
84/* (8 header words + 247 toc words) = 1020 bytes */
85/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */
86
87#define PA_COUNT_MAX 247
88#define PA_INFO_START 1024
89#define PA_SIZE 32768
90
91static workspace pa_workspace;
92static prop_info *pa_info_array;
93
94prop_area *__legacy_property_area__;
95
96static int init_property_area(void)
97{
98 prop_area *pa;
99
100 if(pa_info_array)
101 return -1;
102
103 if(init_workspace(&pa_workspace, PA_SIZE))
104 return -1;
105
106 fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
107
108 pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START);
109
110 pa = pa_workspace.data;
111 memset(pa, 0, PA_SIZE);
112 pa->magic = PROP_AREA_MAGIC;
113 pa->version = PROP_AREA_VERSION;
114
115 /* plug into the lib property services */
116 __legacy_property_area__ = pa;
117 property_area_inited = 1;
118 return 0;
119}
120
121static void update_prop_info(prop_info *pi, const char *value, unsigned len)
122{
123 pi->serial = pi->serial | 1;
124 memcpy(pi->value, value, len + 1);
125 pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff);
126 __futex_wake(&pi->serial, INT32_MAX);
127}
128
129static const prop_info *__legacy_property_find(const char *name)
130{
131 prop_area *pa = __legacy_property_area__;
132 unsigned count = pa->count;
133 unsigned *toc = pa->toc;
134 unsigned len = strlen(name);
135 prop_info *pi;
136
137 while(count--) {
138 unsigned entry = *toc++;
139 if(TOC_NAME_LEN(entry) != len) continue;
140
141 pi = TOC_TO_INFO(pa, entry);
142 if(memcmp(name, pi->name, len)) continue;
143
144 return pi;
145 }
146
147 return 0;
148}
149
150static int legacy_property_set(const char *name, const char *value)
151{
152 prop_area *pa;
153 prop_info *pi;
154
155 int namelen = strlen(name);
156 int valuelen = strlen(value);
157
158 if(namelen >= PROP_NAME_MAX) return -1;
159 if(valuelen >= PROP_VALUE_MAX) return -1;
160 if(namelen < 1) return -1;
161
162 pi = (prop_info*) __legacy_property_find(name);
163
164
165 if(pi != 0) {
166 /* ro.* properties may NEVER be modified once set */
167 if(!strncmp(name, "ro.", 3)) return -1;
168
169 pa = __legacy_property_area__;
170 update_prop_info(pi, value, valuelen);
171 pa->serial++;
172 __futex_wake(&pa->serial, INT32_MAX);
173 } else {
174 pa = __legacy_property_area__;
175 if(pa->count == PA_COUNT_MAX) return -1;
176
177 pi = pa_info_array + pa->count;
178 pi->serial = (valuelen << 24);
179 memcpy(pi->name, name, namelen + 1);
180 memcpy(pi->value, value, valuelen + 1);
181
182 pa->toc[pa->count] =
183 (namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
184
185 pa->count++;
186 pa->serial++;
187 __futex_wake(&pa->serial, INT32_MAX);
188 }
189
190 return 0;
191}
192
193void legacy_get_property_workspace(int *fd, int *sz)
194{
195 *fd = pa_workspace.fd;
196 *sz = pa_workspace.size;
197}
198
199static void copy_property_to_legacy(const char *key, const char *value, void *cookie)
200{
201 legacy_property_set(key, value);
202}
203
Matt Mowercdd3b332014-03-27 14:38:48 -0500204int legacy_properties_init()
that7e303cf2014-03-06 07:57:43 +0100205{
Matt Mowercdd3b332014-03-27 14:38:48 -0500206 if(init_property_area() != 0)
207 return -1;
that7e303cf2014-03-06 07:57:43 +0100208
Matt Mowercdd3b332014-03-27 14:38:48 -0500209 if(property_list(copy_property_to_legacy, 0) != 0)
210 return -1;
211
212 return 0;
213}