blob: ab6837d710054e336a9cd0c63e3ae064f5fe3ae5 [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
Mark Salyzyn62767fe2016-10-27 07:45:34 -070017#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <ftw.h>
21#include <pwd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdarg.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
Colin Cross504bc512010-04-13 19:35:09 -070026#include <time.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070027#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Stephen Smalleydbd37f22014-01-28 10:34:09 -050029#include <selinux/android.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070030#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050031
Mark Salyzyn62767fe2016-10-27 07:45:34 -070032#include <sys/socket.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <sys/stat.h>
34#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <sys/un.h>
36
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/file.h>
Elliott Hughesf86b5a62016-06-24 15:12:21 -070038#include <android-base/logging.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070039#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/strings.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041/* for ANDROID_SOCKET_* */
42#include <cutils/sockets.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
Nick Kralevichd2104df2015-06-18 17:46:54 -070049static unsigned int do_decode_uid(const char *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050{
51 unsigned int v;
52
53 if (!s || *s == '\0')
Nick Kralevichd2104df2015-06-18 17:46:54 -070054 return UINT_MAX;
William Roberts3792e6c2016-04-06 19:18:50 -070055
56 if (isalpha(s[0])) {
57 struct passwd* pwd = getpwnam(s);
58 if (!pwd)
59 return UINT_MAX;
60 return pwd->pw_uid;
61 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
63 errno = 0;
64 v = (unsigned int) strtoul(s, 0, 0);
65 if (errno)
Nick Kralevichd2104df2015-06-18 17:46:54 -070066 return UINT_MAX;
67 return v;
68}
69
70/*
71 * decode_uid - decodes and returns the given string, which can be either the
72 * numeric or name representation, into the integer uid or gid. Returns
73 * UINT_MAX on error.
74 */
75unsigned int decode_uid(const char *s) {
76 unsigned int v = do_decode_uid(s);
77 if (v == UINT_MAX) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070078 LOG(ERROR) << "decode_uid: Unable to find UID for '" << s << "'; returning UINT_MAX";
Nick Kralevichd2104df2015-06-18 17:46:54 -070079 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 return v;
81}
82
83/*
84 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
85 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
86 * daemon. We communicate the file descriptor's value via the environment
87 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
88 */
Stephen Smalley8348d272013-05-13 12:37:04 -040089int create_socket(const char *name, int type, mode_t perm, uid_t uid,
90 gid_t gid, const char *socketcon)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091{
92 struct sockaddr_un addr;
Nick Kralevich9bcfd642016-02-24 15:50:52 -080093 int fd, ret, savederrno;
Stephen Smalley8348d272013-05-13 12:37:04 -040094 char *filecon;
95
Nick Kralevich83ccb1c2015-11-23 16:26:42 -080096 if (socketcon) {
97 if (setsockcreatecon(socketcon) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070098 PLOG(ERROR) << "setsockcreatecon(\"" << socketcon << "\") failed";
Nick Kralevich83ccb1c2015-11-23 16:26:42 -080099 return -1;
100 }
101 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102
103 fd = socket(PF_UNIX, type, 0);
104 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700105 PLOG(ERROR) << "Failed to open socket '" << name << "'";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 return -1;
107 }
108
Stephen Smalley8348d272013-05-13 12:37:04 -0400109 if (socketcon)
110 setsockcreatecon(NULL);
111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 memset(&addr, 0 , sizeof(addr));
113 addr.sun_family = AF_UNIX;
114 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
115 name);
116
117 ret = unlink(addr.sun_path);
118 if (ret != 0 && errno != ENOENT) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700119 PLOG(ERROR) << "Failed to unlink old socket '" << name << "'";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 goto out_close;
121 }
122
Stephen Smalley8348d272013-05-13 12:37:04 -0400123 filecon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500124 if (sehandle) {
Stephen Smalley8348d272013-05-13 12:37:04 -0400125 ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500126 if (ret == 0)
Stephen Smalley8348d272013-05-13 12:37:04 -0400127 setfscreatecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500128 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800131 savederrno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500133 setfscreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400134 freecon(filecon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500135
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800136 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700137 errno = savederrno;
138 PLOG(ERROR) << "Failed to bind socket '" << name << "'";
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800139 goto out_unlink;
140 }
141
142 ret = lchown(addr.sun_path, uid, gid);
143 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700144 PLOG(ERROR) << "Failed to lchown socket '" << addr.sun_path << "'";
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800145 goto out_unlink;
146 }
147 ret = fchmodat(AT_FDCWD, addr.sun_path, perm, AT_SYMLINK_NOFOLLOW);
148 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700149 PLOG(ERROR) << "Failed to fchmodat socket '" << addr.sun_path << "'";
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800150 goto out_unlink;
151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700153 LOG(INFO) << "Created socket '" << addr.sun_path << "'"
154 << ", mode " << std::oct << perm << std::dec
155 << ", user " << uid
156 << ", group " << gid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
158 return fd;
159
160out_unlink:
161 unlink(addr.sun_path);
162out_close:
163 close(fd);
164 return -1;
165}
166
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700167/*
168 * create_file - opens and creates a file as dictated in init.rc.
169 * This file is inherited by the daemon. We communicate the file
170 * descriptor's value via the environment variable ANDROID_FILE_<basename>
171 */
172int create_file(const char *path, int flags, mode_t perm, uid_t uid,
173 gid_t gid, const char *filecon)
174{
175 char *secontext = NULL;
176 int ret;
177
178 if (filecon) {
179 if (setsockcreatecon(filecon) == -1) {
180 PLOG(ERROR) << "setsockcreatecon(\"" << filecon << "\") failed";
181 return -1;
182 }
183 } else if (sehandle) {
184 ret = selabel_lookup(sehandle, &secontext, path, perm);
185 if (ret != -1) {
186 ret = setfscreatecon(secontext);
187 if (ret == -1) {
188 freecon(secontext);
189 PLOG(ERROR) << "setfscreatecon(\"" << secontext << "\") failed";
190 return -1;
191 }
192 }
193 }
194
195 int fd = TEMP_FAILURE_RETRY(open(path, flags | O_NDELAY, perm));
196
197 if (filecon) {
198 setsockcreatecon(NULL);
199 lsetfilecon(path, filecon);
200 } else {
201 setfscreatecon(NULL);
202 freecon(secontext);
203 }
204
205 if (fd == -1) {
206 PLOG(ERROR) << "Failed to open/create file '" << path << "'";
207 goto out_close;
208 }
209
210 if (!(flags & O_NDELAY)) fcntl(fd, F_SETFD, flags);
211
212 ret = lchown(path, uid, gid);
213 if (ret) {
214 PLOG(ERROR) << "Failed to lchown file '" << path << "'";
215 goto out_close;
216 }
217 if (perm != static_cast<mode_t>(-1)) {
218 ret = fchmodat(AT_FDCWD, path, perm, AT_SYMLINK_NOFOLLOW);
219 if (ret) {
220 PLOG(ERROR) << "Failed to fchmodat file '" << path << "'";
221 goto out_close;
222 }
223 }
224
225 LOG(INFO) << "Created file '" << path << "'"
226 << ", mode " << std::oct << perm << std::dec
227 << ", user " << uid
228 << ", group " << gid;
229
230 return fd;
231
232out_close:
233 if (fd >= 0) close(fd);
234 return -1;
235}
236
Elliott Hughesf682b472015-02-06 12:19:48 -0800237bool read_file(const char* path, std::string* content) {
238 content->clear();
239
240 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
241 if (fd == -1) {
242 return false;
243 }
244
245 // For security reasons, disallow world-writable
246 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800247 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800248 if (fstat(fd, &sb) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700249 PLOG(ERROR) << "fstat failed for '" << path << "'";
Elliott Hughesf682b472015-02-06 12:19:48 -0800250 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800251 }
252 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700253 PLOG(ERROR) << "skipping insecure file '" << path << "'";
Elliott Hughesf682b472015-02-06 12:19:48 -0800254 return false;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800255 }
256
Dan Albertc007bc32015-03-16 10:08:46 -0700257 bool okay = android::base::ReadFdToString(fd, content);
Elliott Hughes9fc83432015-05-15 19:16:40 -0700258 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800259 return okay;
260}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
Elliott Hughesf682b472015-02-06 12:19:48 -0800262int write_file(const char* path, const char* content) {
263 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
264 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700265 PLOG(ERROR) << "write_file: Unable to open '" << path << "'";
Nick Kralevicheedbe812015-04-25 14:10:03 -0700266 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -0800267 }
Nick Kralevicheedbe812015-04-25 14:10:03 -0700268 int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
269 if (result == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700270 PLOG(ERROR) << "write_file: Unable to write to '" << path << "'";
Nick Kralevicheedbe812015-04-25 14:10:03 -0700271 }
Elliott Hughes9fc83432015-05-15 19:16:40 -0700272 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800273 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274}
275
Elliott Hughesda40c002015-03-27 23:20:44 -0700276time_t gettime() {
277 timespec now;
278 clock_gettime(CLOCK_MONOTONIC, &now);
279 return now.tv_sec;
280}
Colin Cross504bc512010-04-13 19:35:09 -0700281
Elliott Hughesda40c002015-03-27 23:20:44 -0700282uint64_t gettime_ns() {
283 timespec now;
284 clock_gettime(CLOCK_MONOTONIC, &now);
285 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Colin Cross504bc512010-04-13 19:35:09 -0700286}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700287
288int mkdir_recursive(const char *pathname, mode_t mode)
289{
290 char buf[128];
291 const char *slash;
292 const char *p = pathname;
293 int width;
294 int ret;
295 struct stat info;
296
297 while ((slash = strchr(p, '/')) != NULL) {
298 width = slash - pathname;
299 p = slash + 1;
300 if (width < 0)
301 break;
302 if (width == 0)
303 continue;
304 if ((unsigned int)width > sizeof(buf) - 1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700305 LOG(ERROR) << "path too long for mkdir_recursive";
Colin Crossb0ab94b2010-04-08 16:16:20 -0700306 return -1;
307 }
308 memcpy(buf, pathname, width);
309 buf[width] = 0;
310 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400311 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700312 if (ret && errno != EEXIST)
313 return ret;
314 }
315 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400316 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700317 if (ret && errno != EEXIST)
318 return ret;
319 return 0;
320}
321
Johan Redestig93ca79b2012-04-18 16:41:19 +0200322/*
323 * replaces any unacceptable characters with '_', the
324 * length of the resulting string is equal to the input string
325 */
Colin Crossb0ab94b2010-04-08 16:16:20 -0700326void sanitize(char *s)
327{
Johan Redestig93ca79b2012-04-18 16:41:19 +0200328 const char* accept =
329 "abcdefghijklmnopqrstuvwxyz"
330 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
331 "0123456789"
332 "_-.";
333
Colin Crossb0ab94b2010-04-08 16:16:20 -0700334 if (!s)
335 return;
Johan Redestig93ca79b2012-04-18 16:41:19 +0200336
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400337 while (*s) {
Johan Redestig93ca79b2012-04-18 16:41:19 +0200338 s += strspn(s, accept);
Christopher R. Palmer07f3fee2014-09-22 14:35:54 -0400339 if (*s) *s++ = '_';
Johan Redestig93ca79b2012-04-18 16:41:19 +0200340 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700341}
Johan Redestig93ca79b2012-04-18 16:41:19 +0200342
Colin Crosscd0f1732010-04-19 17:10:24 -0700343int wait_for_file(const char *filename, int timeout)
344{
345 struct stat info;
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700346 uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000);
Colin Crosscd0f1732010-04-19 17:10:24 -0700347 int ret = -1;
348
Thierry Strudel91cf41c2015-05-22 15:56:14 -0700349 while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0))
Colin Crosscd0f1732010-04-19 17:10:24 -0700350 usleep(10000);
351
352 return ret;
353}
Colin Crossf83d0b92010-04-21 12:04:20 -0700354
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700355void import_kernel_cmdline(bool in_qemu,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700356 const std::function<void(const std::string&, const std::string&, bool)>& fn) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700357 std::string cmdline;
358 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700359
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700360 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
361 std::vector<std::string> pieces = android::base::Split(entry, "=");
362 if (pieces.size() == 2) {
363 fn(pieces[0], pieces[1], in_qemu);
364 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700365 }
366}
Stephen Smalleye096e362012-06-11 13:37:39 -0400367
368int make_dir(const char *path, mode_t mode)
369{
370 int rc;
371
Stephen Smalleye096e362012-06-11 13:37:39 -0400372 char *secontext = NULL;
373
374 if (sehandle) {
375 selabel_lookup(sehandle, &secontext, path, mode);
376 setfscreatecon(secontext);
377 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400378
379 rc = mkdir(path, mode);
380
Stephen Smalleye096e362012-06-11 13:37:39 -0400381 if (secontext) {
382 int save_errno = errno;
383 freecon(secontext);
384 setfscreatecon(NULL);
385 errno = save_errno;
386 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700387
Stephen Smalleye096e362012-06-11 13:37:39 -0400388 return rc;
389}
390
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500391int restorecon(const char* pathname)
Stephen Smalleye096e362012-06-11 13:37:39 -0400392{
Stephen Smalley27a93652014-02-07 09:14:13 -0500393 return selinux_android_restorecon(pathname, 0);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700394}
395
396int restorecon_recursive(const char* pathname)
397{
Stephen Smalley27a93652014-02-07 09:14:13 -0500398 return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700399}
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700400
401/*
402 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
403 */
404std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
405 std::string hex("0x");
406 for (size_t i = 0; i < bytes_len; i++)
407 android::base::StringAppendF(&hex, "%02x", bytes[i]);
408 return hex;
409}
Lee Campbellf13b1b32015-07-24 16:57:14 -0700410
411/*
412 * Returns true is pathname is a directory
413 */
414bool is_dir(const char* pathname) {
415 struct stat info;
416 if (stat(pathname, &info) == -1) {
417 return false;
418 }
419 return S_ISDIR(info.st_mode);
420}
Tom Cherryb7349902015-08-26 11:43:36 -0700421
422bool expand_props(const std::string& src, std::string* dst) {
423 const char* src_ptr = src.c_str();
424
425 if (!dst) {
426 return false;
427 }
428
429 /* - variables can either be $x.y or ${x.y}, in case they are only part
430 * of the string.
431 * - will accept $$ as a literal $.
432 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
433 * bad things will happen
Mark Salyzyn4b561622016-06-07 08:49:01 -0700434 * - ${x.y:-default} will return default value if property empty.
Tom Cherryb7349902015-08-26 11:43:36 -0700435 */
436 while (*src_ptr) {
437 const char* c;
438
439 c = strchr(src_ptr, '$');
440 if (!c) {
441 dst->append(src_ptr);
442 return true;
443 }
444
445 dst->append(src_ptr, c);
446 c++;
447
448 if (*c == '$') {
449 dst->push_back(*(c++));
450 src_ptr = c;
451 continue;
452 } else if (*c == '\0') {
453 return true;
454 }
455
456 std::string prop_name;
Mark Salyzyn4b561622016-06-07 08:49:01 -0700457 std::string def_val;
Tom Cherryb7349902015-08-26 11:43:36 -0700458 if (*c == '{') {
459 c++;
460 const char* end = strchr(c, '}');
461 if (!end) {
462 // failed to find closing brace, abort.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700463 LOG(ERROR) << "unexpected end of string in '" << src << "', looking for }";
Tom Cherryb7349902015-08-26 11:43:36 -0700464 return false;
465 }
466 prop_name = std::string(c, end);
467 c = end + 1;
Mark Salyzyn4b561622016-06-07 08:49:01 -0700468 size_t def = prop_name.find(":-");
469 if (def < prop_name.size()) {
470 def_val = prop_name.substr(def + 2);
471 prop_name = prop_name.substr(0, def);
472 }
Tom Cherryb7349902015-08-26 11:43:36 -0700473 } else {
474 prop_name = c;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700475 LOG(ERROR) << "using deprecated syntax for specifying property '" << c << "', use ${name} instead";
Tom Cherryb7349902015-08-26 11:43:36 -0700476 c += prop_name.size();
477 }
478
479 if (prop_name.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700480 LOG(ERROR) << "invalid zero-length property name in '" << src << "'";
Tom Cherryb7349902015-08-26 11:43:36 -0700481 return false;
482 }
483
484 std::string prop_val = property_get(prop_name.c_str());
485 if (prop_val.empty()) {
Mark Salyzyn4b561622016-06-07 08:49:01 -0700486 if (def_val.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700487 LOG(ERROR) << "property '" << prop_name << "' doesn't exist while expanding '" << src << "'";
Mark Salyzyn4b561622016-06-07 08:49:01 -0700488 return false;
489 }
490 prop_val = def_val;
Tom Cherryb7349902015-08-26 11:43:36 -0700491 }
492
493 dst->append(prop_val);
494 src_ptr = c;
495 }
496
497 return true;
498}