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