blob: 89d3276b8923ce13f1a9058f8da5f5ff918c75bc [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 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 <stdarg.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <errno.h>
Colin Cross504bc512010-04-13 19:35:09 -070024#include <time.h>
Nick Kralevichae76f6d2013-07-11 15:38:26 -070025#include <ftw.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Stephen Smalleye46f9d52012-01-13 08:48:47 -050027#include <selinux/label.h>
Stephen Smalleydbd37f22014-01-28 10:34:09 -050028#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050029
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/file.h>
36#include <android-base/strings.h>
Dan Albertc007bc32015-03-16 10:08:46 -070037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038/* for ANDROID_SOCKET_* */
39#include <cutils/sockets.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/stringprintf.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42#include <private/android_filesystem_config.h>
43
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070045#include "log.h"
Tom Cherryb7349902015-08-26 11:43:36 -070046#include "property_service.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070047#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049/*
50 * android_name_to_id - returns the integer uid/gid associated with the given
Nick Kralevichd2104df2015-06-18 17:46:54 -070051 * name, or UINT_MAX on error.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052 */
53static unsigned int android_name_to_id(const char *name)
54{
Edwin Vanede7f1ad2012-07-26 14:09:13 -040055 const struct android_id_info *info = android_ids;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 unsigned int n;
57
58 for (n = 0; n < android_id_count; n++) {
59 if (!strcmp(info[n].name, name))
60 return info[n].aid;
61 }
62
Nick Kralevichd2104df2015-06-18 17:46:54 -070063 return UINT_MAX;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064}
65
Nick Kralevichd2104df2015-06-18 17:46:54 -070066static unsigned int do_decode_uid(const char *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067{
68 unsigned int v;
69
70 if (!s || *s == '\0')
Nick Kralevichd2104df2015-06-18 17:46:54 -070071 return UINT_MAX;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 if (isalpha(s[0]))
73 return android_name_to_id(s);
74
75 errno = 0;
76 v = (unsigned int) strtoul(s, 0, 0);
77 if (errno)
Nick Kralevichd2104df2015-06-18 17:46:54 -070078 return UINT_MAX;
79 return v;
80}
81
82/*
83 * decode_uid - decodes and returns the given string, which can be either the
84 * numeric or name representation, into the integer uid or gid. Returns
85 * UINT_MAX on error.
86 */
87unsigned int decode_uid(const char *s) {
88 unsigned int v = do_decode_uid(s);
89 if (v == UINT_MAX) {
90 ERROR("decode_uid: Unable to find UID for '%s'. Returning UINT_MAX\n", s);
91 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 return v;
93}
94
95/*
96 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
97 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
98 * daemon. We communicate the file descriptor's value via the environment
99 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
100 */
Stephen Smalley8348d272013-05-13 12:37:04 -0400101int create_socket(const char *name, int type, mode_t perm, uid_t uid,
102 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103{
104 struct sockaddr_un addr;
Nick Kralevichab5629c2016-02-24 15:50:52 -0800105 int fd, ret, savederrno;
Stephen Smalley8348d272013-05-13 12:37:04 -0400106 char *filecon;
107
Nick Kralevich83ccb1c2015-11-23 16:26:42 -0800108 if (socketcon) {
109 if (setsockcreatecon(socketcon) == -1) {
110 ERROR("setsockcreatecon(\"%s\") failed: %s\n", socketcon, strerror(errno));
111 return -1;
112 }
113 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114
115 fd = socket(PF_UNIX, type, 0);
116 if (fd < 0) {
117 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
118 return -1;
119 }
120
Stephen Smalley8348d272013-05-13 12:37:04 -0400121 if (socketcon)
122 setsockcreatecon(NULL);
123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 memset(&addr, 0 , sizeof(addr));
125 addr.sun_family = AF_UNIX;
126 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
127 name);
128
129 ret = unlink(addr.sun_path);
130 if (ret != 0 && errno != ENOENT) {
131 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
132 goto out_close;
133 }
134
Stephen Smalley8348d272013-05-13 12:37:04 -0400135 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500136 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400137 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500138 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400139 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500140 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
Nick Kralevichab5629c2016-02-24 15:50:52 -0800143 savederrno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500145 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400146 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500147
Nick Kralevichab5629c2016-02-24 15:50:52 -0800148 if (ret) {
149 ERROR("Failed to bind socket '%s': %s\n", name, strerror(savederrno));
150 goto out_unlink;
151 }
152
153 ret = lchown(addr.sun_path, uid, gid);
154 if (ret) {
155 ERROR("Failed to lchown socket '%s': %s\n", addr.sun_path, strerror(errno));
156 goto out_unlink;
157 }
158 ret = fchmodat(AT_FDCWD, addr.sun_path, perm, AT_SYMLINK_NOFOLLOW);
159 if (ret) {
160 ERROR("Failed to fchmodat socket '%s': %s\n", addr.sun_path, strerror(errno));
161 goto out_unlink;
162 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
164 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
165 addr.sun_path, perm, uid, gid);
166
167 return fd;
168
169out_unlink:
170 unlink(addr.sun_path);
171out_close:
172 close(fd);
173 return -1;
174}
175
Elliott Hughesf682b472015-02-06 12:19:48 -0800176bool read_file(const char* path, std::string* content) {
177 content->clear();
178
179 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
180 if (fd == -1) {
181 return false;
182 }
183
184 // For security reasons, disallow world-writable
185 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800186 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800187 if (fstat(fd, &sb) == -1) {
188 ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
189 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800190 }
191 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800192 ERROR("skipping insecure file '%s'\n", path);
193 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800194 }
195
Dan Albertc007bc32015-03-16 10:08:46 -0700196 bool okay = android::base::ReadFdToString(fd, content);
Elliott Hughes9fc83432015-05-15 19:16:40 -0700197 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800198 return okay;
199}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Elliott Hughesf682b472015-02-06 12:19:48 -0800201int write_file(const char* path, const char* content) {
202 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
203 if (fd == -1) {
Nick Kralevicheedbe812015-04-25 14:10:03 -0700204 NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
205 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800206 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700207 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
208 if (result == -1) {
209 NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
210 }
Elliott Hughes9fc83432015-05-15 19:16:40 -0700211 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800212 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213}
214
Colin Crossf24ed8c2010-04-12 18:51:06 -0700215#define MAX_MTD_PARTITIONS 16
216
217static struct {
218 char name[16];
219 int number;
220} mtd_part_map[MAX_MTD_PARTITIONS];
221
222static int mtd_part_count = -1;
223
224static void find_mtd_partitions(void)
225{
226 int fd;
227 char buf[1024];
228 char *pmtdbufp;
229 ssize_t pmtdsize;
230 int r;
231
Nick Kralevich45a884f2015-02-02 14:37:22 -0800232 fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
Colin Crossf24ed8c2010-04-12 18:51:06 -0700233 if (fd < 0)
234 return;
235
236 buf[sizeof(buf) - 1] = '\0';
237 pmtdsize = read(fd, buf, sizeof(buf) - 1);
238 pmtdbufp = buf;
239 while (pmtdsize > 0) {
240 int mtdnum, mtdsize, mtderasesize;
241 char mtdname[16];
242 mtdname[0] = '\0';
243 mtdnum = -1;
244 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
245 &mtdnum, &mtdsize, &mtderasesize, mtdname);
246 if ((r == 4) && (mtdname[0] == '"')) {
247 char *x = strchr(mtdname + 1, '"');
248 if (x) {
249 *x = 0;
250 }
251 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
252 if (mtd_part_count < MAX_MTD_PARTITIONS) {
253 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
254 mtd_part_map[mtd_part_count].number = mtdnum;
255 mtd_part_count++;
256 } else {
257 ERROR("too many mtd partitions\n");
258 }
259 }
260 while (pmtdsize > 0 && *pmtdbufp != '\n') {
261 pmtdbufp++;
262 pmtdsize--;
263 }
264 if (pmtdsize > 0) {
265 pmtdbufp++;
266 pmtdsize--;
267 }
268 }
269 close(fd);
270}
271
272int mtd_name_to_number(const char *name)
273{
274 int n;
275 if (mtd_part_count < 0) {
276 mtd_part_count = 0;
277 find_mtd_partitions();
278 }
279 for (n = 0; n < mtd_part_count; n++) {
280 if (!strcmp(name, mtd_part_map[n].name)) {
281 return mtd_part_map[n].number;
282 }
283 }
284 return -1;
285}
Colin Cross504bc512010-04-13 19:35:09 -0700286
Elliott Hughesda40c002015-03-27 23:20:44 -0700287time_t gettime() {
288 timespec now;
289 clock_gettime(CLOCK_MONOTONIC, &now);
290 return now.tv_sec;
291}
Colin Cross504bc512010-04-13 19:35:09 -0700292
Elliott Hughesda40c002015-03-27 23:20:44 -0700293uint64_t gettime_ns() {
294 timespec now;
295 clock_gettime(CLOCK_MONOTONIC, &now);
296 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700297}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700298
299int mkdir_recursive(const char *pathname, mode_t mode)
300{
301 char buf[128];
302 const char *slash;
303 const char *p = pathname;
304 int width;
305 int ret;
306 struct stat info;
307
308 while ((slash = strchr(p, '/')) != NULL) {
309 width = slash - pathname;
310 p = slash + 1;
311 if (width < 0)
312 break;
313 if (width == 0)
314 continue;
315 if ((unsigned int)width > sizeof(buf) - 1) {
316 ERROR("path too long for mkdir_recursive\n");
317 return -1;
318 }
319 memcpy(buf, pathname, width);
320 buf[width] = 0;
321 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400322 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700323 if (ret && errno != EEXIST)
324 return ret;
325 }
326 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400327 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700328 if (ret && errno != EEXIST)
329 return ret;
330 return 0;
331}
332
Johan Redestig93ca79b2012-04-18 16:41:19 +0200333/*
334 * replaces any unacceptable characters with '_', the
335 * length of the resulting string is equal to the input string
336 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700337void sanitize(char *s)
338{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200339 const char* accept =
340 "abcdefghijklmnopqrstuvwxyz"
341 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
342 "0123456789"
343 "_-.";
344
Colin Crossb0ab94b2010-04-08 16:16:20 -0700345 if (!s)
346 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200347
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400348 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200349 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400350 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200351 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700352}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200353
Chris Fries79f33842013-09-05 13:19:21 -0500354void make_link_init(const char *oldpath, const char *newpath)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700355{
356 int ret;
357 char buf[256];
358 char *slash;
359 int width;
360
361 slash = strrchr(newpath, '/');
362 if (!slash)
363 return;
364 width = slash - newpath;
365 if (width <= 0 || width > (int)sizeof(buf) - 1)
366 return;
367 memcpy(buf, newpath, width);
368 buf[width] = 0;
369 ret = mkdir_recursive(buf, 0755);
370 if (ret)
371 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
372
373 ret = symlink(oldpath, newpath);
374 if (ret && errno != EEXIST)
375 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
376}
377
378void remove_link(const char *oldpath, const char *newpath)
379{
380 char path[256];
381 ssize_t ret;
382 ret = readlink(newpath, path, sizeof(path) - 1);
383 if (ret <= 0)
384 return;
385 path[ret] = 0;
386 if (!strcmp(path, oldpath))
387 unlink(newpath);
388}
Colin Crosscd0f1732010-04-19 17:10:24 -0700389
390int wait_for_file(const char *filename, int timeout)
391{
392 struct stat info;
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700393 uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000);
Colin Crosscd0f1732010-04-19 17:10:24 -0700394 int ret = -1;
395
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700396 while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0))
Colin Crosscd0f1732010-04-19 17:10:24 -0700397 usleep(10000);
398
399 return ret;
400}
Colin Crossf83d0b92010-04-21 12:04:20 -0700401
402void open_devnull_stdio(void)
403{
Nick Kraleviche34577c2015-04-25 16:24:53 -0700404 // Try to avoid the mknod() call if we can. Since SELinux makes
405 // a /dev/null replacement available for free, let's use it.
406 int fd = open("/sys/fs/selinux/null", O_RDWR);
407 if (fd == -1) {
408 // OOPS, /sys/fs/selinux/null isn't available, likely because
409 // /sys/fs/selinux isn't mounted. Fall back to mknod.
410 static const char *name = "/dev/__null__";
411 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
412 fd = open(name, O_RDWR);
413 unlink(name);
414 }
415 if (fd == -1) {
416 exit(1);
Colin Crossf83d0b92010-04-21 12:04:20 -0700417 }
418 }
419
Nick Kraleviche34577c2015-04-25 16:24:53 -0700420 dup2(fd, 0);
421 dup2(fd, 1);
422 dup2(fd, 2);
423 if (fd > 2) {
424 close(fd);
425 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700426}
427
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700428void import_kernel_cmdline(bool in_qemu,
429 std::function<void(const std::string&, const std::string&, bool)> fn) {
430 std::string cmdline;
431 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700432
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700433 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
434 std::vector<std::string> pieces = android::base::Split(entry, "=");
435 if (pieces.size() == 2) {
436 fn(pieces[0], pieces[1], in_qemu);
437 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700438 }
439}
Stephen Smalleye096e362012-06-11 13:37:39 -0400440
441int make_dir(const char *path, mode_t mode)
442{
443 int rc;
444
Stephen Smalleye096e362012-06-11 13:37:39 -0400445 char *secontext = NULL;
446
447 if (sehandle) {
448 selabel_lookup(sehandle, &secontext, path, mode);
449 setfscreatecon(secontext);
450 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400451
452 rc = mkdir(path, mode);
453
Stephen Smalleye096e362012-06-11 13:37:39 -0400454 if (secontext) {
455 int save_errno = errno;
456 freecon(secontext);
457 setfscreatecon(NULL);
458 errno = save_errno;
459 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700460
Stephen Smalleye096e362012-06-11 13:37:39 -0400461 return rc;
462}
463
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500464int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400465{
Stephen Smalley27a93652014-02-07 09:14:13 -0500466 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700467}
468
469int restorecon_recursive(const char* pathname)
470{
Stephen Smalley27a93652014-02-07 09:14:13 -0500471 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700472}
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700473
Jeff Sharkey1635afe2016-07-15 16:21:34 -0600474int restorecon_recursive_skipce(const char* pathname)
475{
476 return selinux_android_restorecon(pathname,
477 SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIPCE);
478}
479
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700480/*
481 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
482 */
483std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
484 std::string hex("0x");
485 for (size_t i = 0; i < bytes_len; i++)
486 android::base::StringAppendF(&hex, "%02x", bytes[i]);
487 return hex;
488}
Lee Campbellf13b1b32015-07-24 16:57:14 -0700489
490/*
491 * Returns true is pathname is a directory
492 */
493bool is_dir(const char* pathname) {
494 struct stat info;
495 if (stat(pathname, &info) == -1) {
496 return false;
497 }
498 return S_ISDIR(info.st_mode);
499}
Tom Cherryb7349902015-08-26 11:43:36 -0700500
501bool expand_props(const std::string& src, std::string* dst) {
502 const char* src_ptr = src.c_str();
503
504 if (!dst) {
505 return false;
506 }
507
508 /* - variables can either be $x.y or ${x.y}, in case they are only part
509 * of the string.
510 * - will accept $$ as a literal $.
511 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
512 * bad things will happen
Mark Salyzyn109d7d22016-06-07 08:49:01 -0700513 * - ${x.y:-default} will return default value if property empty.
Tom Cherryb7349902015-08-26 11:43:36 -0700514 */
515 while (*src_ptr) {
516 const char* c;
517
518 c = strchr(src_ptr, '$');
519 if (!c) {
520 dst->append(src_ptr);
521 return true;
522 }
523
524 dst->append(src_ptr, c);
525 c++;
526
527 if (*c == '$') {
528 dst->push_back(*(c++));
529 src_ptr = c;
530 continue;
531 } else if (*c == '\0') {
532 return true;
533 }
534
535 std::string prop_name;
Mark Salyzyn109d7d22016-06-07 08:49:01 -0700536 std::string def_val;
Tom Cherryb7349902015-08-26 11:43:36 -0700537 if (*c == '{') {
538 c++;
539 const char* end = strchr(c, '}');
540 if (!end) {
541 // failed to find closing brace, abort.
542 ERROR("unexpected end of string in '%s', looking for }\n", src.c_str());
543 return false;
544 }
545 prop_name = std::string(c, end);
546 c = end + 1;
Mark Salyzyn109d7d22016-06-07 08:49:01 -0700547 size_t def = prop_name.find(":-");
548 if (def < prop_name.size()) {
549 def_val = prop_name.substr(def + 2);
550 prop_name = prop_name.substr(0, def);
551 }
Tom Cherryb7349902015-08-26 11:43:36 -0700552 } else {
553 prop_name = c;
554 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
555 c);
556 c += prop_name.size();
557 }
558
559 if (prop_name.empty()) {
560 ERROR("invalid zero-length prop name in '%s'\n", src.c_str());
561 return false;
562 }
563
564 std::string prop_val = property_get(prop_name.c_str());
565 if (prop_val.empty()) {
Mark Salyzyn109d7d22016-06-07 08:49:01 -0700566 if (def_val.empty()) {
567 ERROR("property '%s' doesn't exist while expanding '%s'\n",
568 prop_name.c_str(), src.c_str());
569 return false;
570 }
571 prop_val = def_val;
Tom Cherryb7349902015-08-26 11:43:36 -0700572 }
573
574 dst->append(prop_val);
575 src_ptr = c;
576 }
577
578 return true;
579}