blob: 7dca7c6422016a708d3442932ef7cd1893930cf3 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Andreas Gampe02d0de52015-11-11 20:43:16 -080017#include "utils.h"
Mike Lockwood94afecf2012-10-24 10:45:23 -070018
Andreas Gampe02d0de52015-11-11 20:43:16 -080019#include <errno.h>
20#include <fcntl.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070021#include <fts.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080022#include <stdlib.h>
23#include <sys/stat.h>
24#include <sys/wait.h>
Jeff Sharkey9a998f42016-07-14 18:16:22 -060025#include <sys/xattr.h>
Jeff Sharkeyed909ae2017-03-22 21:27:40 -060026#include <sys/statvfs.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080027
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080028#include <android-base/logging.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080029#include <android-base/stringprintf.h>
30#include <cutils/fs.h>
Jeff Sharkey871a8f22017-02-21 18:30:28 -070031#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080033#include <private/android_filesystem_config.h>
Jeff Sharkeyc03de092015-04-07 18:14:05 -070034
Andreas Gampe02d0de52015-11-11 20:43:16 -080035#include "globals.h" // extern variables.
36
37#ifndef LOG_TAG
38#define LOG_TAG "installd"
39#endif
Jeff Sharkey9a998f42016-07-14 18:16:22 -060040
Jeff Sharkey9a998f42016-07-14 18:16:22 -060041#define DEBUG_XATTRS 0
Mike Lockwood94afecf2012-10-24 10:45:23 -070042
Jeff Sharkeyc03de092015-04-07 18:14:05 -070043using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070044
Andreas Gampe02d0de52015-11-11 20:43:16 -080045namespace android {
46namespace installd {
47
Jeff Sharkeyc03de092015-04-07 18:14:05 -070048/**
49 * Check that given string is valid filename, and that it attempts no
50 * parent or child directory traversal.
51 */
Jeff Sharkey423e7462016-12-09 18:18:43 -070052bool is_valid_filename(const std::string& name) {
Jeff Sharkeyc03de092015-04-07 18:14:05 -070053 if (name.empty() || (name == ".") || (name == "..")
54 || (name.find('/') != std::string::npos)) {
55 return false;
56 } else {
57 return true;
58 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070059}
60
Calin Juravle6a1648e2016-02-01 12:12:16 +000061static void check_package_name(const char* package_name) {
62 CHECK(is_valid_filename(package_name));
Jeff Sharkey423e7462016-12-09 18:18:43 -070063 CHECK(is_valid_package_name(package_name));
Calin Juravle6a1648e2016-02-01 12:12:16 +000064}
65
Mike Lockwood94afecf2012-10-24 10:45:23 -070066/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -070067 * Create the path name where package app contents should be stored for
68 * the given volume UUID and package name. An empty UUID is assumed to
69 * be internal storage.
70 */
71std::string create_data_app_package_path(const char* volume_uuid,
72 const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000073 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070074 return StringPrintf("%s/%s",
75 create_data_app_path(volume_uuid).c_str(), package_name);
76}
77
78/**
Jeff Sharkeyc03de092015-04-07 18:14:05 -070079 * Create the path name where package data should be stored for the given
80 * volume UUID, package name, and user ID. An empty UUID is assumed to be
81 * internal storage.
Mike Lockwood94afecf2012-10-24 10:45:23 -070082 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -060083std::string create_data_user_ce_package_path(const char* volume_uuid,
Jeff Sharkeyd7921182015-04-30 15:58:19 -070084 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000085 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070086 return StringPrintf("%s/%s",
Jeff Sharkey2f720f72016-04-10 20:51:40 -060087 create_data_user_ce_path(volume_uuid, user).c_str(), package_name);
88}
89
Calin Juravle7d765462017-09-04 15:57:10 -070090/**
91 * Create the path name where package data should be stored for the given
92 * volume UUID, package name, and user ID. An empty UUID is assumed to be
93 * internal storage.
94 * Compared to create_data_user_ce_package_path this method always return the
95 * ".../user/..." directory.
96 */
97std::string create_data_user_ce_package_path_as_user_link(
98 const char* volume_uuid, userid_t userid, const char* package_name) {
99 check_package_name(package_name);
100 std::string data(create_data_path(volume_uuid));
101 return StringPrintf("%s/user/%u/%s", data.c_str(), userid, package_name);
102}
103
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600104std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user,
105 const char* package_name, ino_t ce_data_inode) {
106 // For testing purposes, rely on the inode when defined; this could be
107 // optimized to use access() in the future.
108 auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name);
109 if (ce_data_inode != 0) {
110 auto user_path = create_data_user_ce_path(volume_uuid, user);
111 DIR* dir = opendir(user_path.c_str());
112 if (dir == nullptr) {
113 PLOG(ERROR) << "Failed to opendir " << user_path;
114 return fallback;
115 }
116
117 struct dirent* ent;
118 while ((ent = readdir(dir))) {
119 if (ent->d_ino == ce_data_inode) {
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600120 auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600121#if DEBUG_XATTRS
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600122 if (resolved != fallback) {
123 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode
124 << " instead of " << fallback;
125 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600126#endif
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600127 closedir(dir);
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600128 return resolved;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600129 }
130 }
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600131 LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600132 closedir(dir);
133 return fallback;
134 } else {
135 return fallback;
136 }
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700137}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800139std::string create_data_user_de_package_path(const char* volume_uuid,
140 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +0000141 check_package_name(package_name);
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800142 return StringPrintf("%s/%s",
143 create_data_user_de_path(volume_uuid, user).c_str(), package_name);
144}
145
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700146std::string create_data_path(const char* volume_uuid) {
147 if (volume_uuid == nullptr) {
148 return "/data";
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700149 } else if (!strcmp(volume_uuid, "TEST")) {
150 CHECK(property_get_bool("ro.debuggable", false));
151 return "/data/local/tmp";
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700152 } else {
153 CHECK(is_valid_filename(volume_uuid));
154 return StringPrintf("/mnt/expand/%s", volume_uuid);
155 }
156}
157
Mike Lockwood94afecf2012-10-24 10:45:23 -0700158/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700159 * Create the path name for app data.
160 */
161std::string create_data_app_path(const char* volume_uuid) {
162 return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
163}
164
165/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700166 * Create the path name for user data for a certain userid.
cjbao75d4e572017-04-12 00:12:24 +0800167 * Keep same implementation as vold to minimize path walking overhead
Mike Lockwood94afecf2012-10-24 10:45:23 -0700168 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600169std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700170 std::string data(create_data_path(volume_uuid));
cjbao75d4e572017-04-12 00:12:24 +0800171 if (volume_uuid == nullptr && userid == 0) {
172 std::string legacy = StringPrintf("%s/data", data.c_str());
173 struct stat sb;
174 if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
175 /* /data/data is dir, return /data/data for legacy system */
176 return legacy;
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700177 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178 }
cjbao75d4e572017-04-12 00:12:24 +0800179 return StringPrintf("%s/user/%u", data.c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180}
181
182/**
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800183 * Create the path name for device encrypted user data for a certain userid.
184 */
185std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
186 std::string data(create_data_path(volume_uuid));
187 return StringPrintf("%s/user_de/%u", data.c_str(), userid);
188}
189
190/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700191 * Create the path name for media for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700193std::string create_data_media_path(const char* volume_uuid, userid_t userid) {
194 return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195}
196
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700197std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) {
198 return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name);
199}
200
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700201std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
202 const char* data_type, const char* package_name) {
203 return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(),
204 data_type, package_name);
205}
206
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600207std::string create_data_misc_legacy_path(userid_t userid) {
208 return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid);
209}
210
Calin Juravle114f0812017-03-08 19:05:07 -0800211std::string create_primary_cur_profile_dir_path(userid_t userid) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600212 return StringPrintf("%s/cur/%u", android_profiles_dir.c_str(), userid);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000213}
214
Calin Juravle114f0812017-03-08 19:05:07 -0800215std::string create_primary_current_profile_package_dir_path(userid_t user,
216 const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800217 check_package_name(package_name.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800218 return StringPrintf("%s/%s",
219 create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str());
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700220}
221
Calin Juravle114f0812017-03-08 19:05:07 -0800222std::string create_primary_ref_profile_dir_path() {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600223 return StringPrintf("%s/ref", android_profiles_dir.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000224}
225
Calin Juravle114f0812017-03-08 19:05:07 -0800226std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800227 check_package_name(package_name.c_str());
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600228 return StringPrintf("%s/ref/%s", android_profiles_dir.c_str(), package_name.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000229}
230
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700231std::string create_data_dalvik_cache_path() {
232 return "/data/dalvik-cache";
233}
234
Calin Juravle114f0812017-03-08 19:05:07 -0800235// Keep profile paths in sync with ActivityThread and LoadedApk.
236const std::string PROFILE_EXT = ".prof";
Calin Juravle3760ad32017-07-27 16:31:55 -0700237const std::string CURRENT_PROFILE_EXT = ".cur";
Calin Juravle114f0812017-03-08 19:05:07 -0800238const std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700239
Calin Juravle3760ad32017-07-27 16:31:55 -0700240// Gets the parent directory and the file name for the given secondary dex path.
241// Returns true on success, false on failure (if the dex_path does not have the expected
242// structure).
243static bool get_secondary_dex_location(const std::string& dex_path,
244 std::string* out_dir_name, std::string* out_file_name) {
245 size_t dirIndex = dex_path.rfind('/');
246 if (dirIndex == std::string::npos) {
247 return false;
248 }
249 if (dirIndex == dex_path.size() - 1) {
250 return false;
251 }
252 *out_dir_name = dex_path.substr(0, dirIndex);
253 *out_file_name = dex_path.substr(dirIndex + 1);
254
255 return true;
256}
257
Calin Juravle114f0812017-03-08 19:05:07 -0800258std::string create_current_profile_path(userid_t user, const std::string& location,
259 bool is_secondary_dex) {
260 if (is_secondary_dex) {
Calin Juravle3760ad32017-07-27 16:31:55 -0700261 // Secondary dex current profiles are stored next to the dex files under the oat folder.
262 std::string dex_dir;
263 std::string dex_name;
264 CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
265 << "Unexpected dir structure for secondary dex " << location;
266 return StringPrintf("%s/oat/%s%s%s",
267 dex_dir.c_str(), dex_name.c_str(), CURRENT_PROFILE_EXT.c_str(),
268 PROFILE_EXT.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800269 } else {
270 // Profiles for primary apks are under /data/misc/profiles/cur.
271 std::string profile_dir = create_primary_current_profile_package_dir_path(user, location);
272 return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
273 }
274}
275
276std::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) {
277 if (is_secondary_dex) {
278 // Secondary dex reference profiles are stored next to the dex files under the oat folder.
Calin Juravle3760ad32017-07-27 16:31:55 -0700279 std::string dex_dir;
280 std::string dex_name;
281 CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
Calin Juravle114f0812017-03-08 19:05:07 -0800282 << "Unexpected dir structure for secondary dex " << location;
Calin Juravle114f0812017-03-08 19:05:07 -0800283 return StringPrintf("%s/oat/%s%s",
284 dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str());
285 } else {
286 // Reference profiles for primary apks are stored in /data/misc/profile/ref.
287 std::string profile_dir = create_primary_reference_profile_package_dir_path(location);
288 return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
289 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700290}
291
Jeff Sharkeye3637242015-04-08 20:56:42 -0700292std::vector<userid_t> get_known_users(const char* volume_uuid) {
293 std::vector<userid_t> users;
294
295 // We always have an owner
296 users.push_back(0);
297
298 std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
299 DIR* dir = opendir(path.c_str());
300 if (dir == NULL) {
301 // Unable to discover other users, but at least return owner
302 PLOG(ERROR) << "Failed to opendir " << path;
303 return users;
304 }
305
306 struct dirent* ent;
307 while ((ent = readdir(dir))) {
308 if (ent->d_type != DT_DIR) {
309 continue;
310 }
311
312 char* end;
313 userid_t user = strtol(ent->d_name, &end, 10);
314 if (*end == '\0' && user != 0) {
315 LOG(DEBUG) << "Found valid user " << user;
316 users.push_back(user);
317 }
318 }
319 closedir(dir);
320
321 return users;
322}
323
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700324int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700325 int32_t include_gid, int32_t exclude_gid, bool exclude_apps) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700326 FTS *fts;
327 FTSENT *p;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700328 int64_t matchedSize = 0;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700329 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700330 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700331 if (errno != ENOENT) {
332 PLOG(ERROR) << "Failed to fts_open " << path;
333 }
334 return -1;
335 }
336 while ((p = fts_read(fts)) != NULL) {
337 switch (p->fts_info) {
338 case FTS_D:
339 case FTS_DEFAULT:
340 case FTS_F:
341 case FTS_SL:
342 case FTS_SLNONE:
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700343 int32_t uid = p->fts_statp->st_uid;
344 int32_t gid = p->fts_statp->st_gid;
345 int32_t user_uid = multiuser_get_app_id(uid);
346 int32_t user_gid = multiuser_get_app_id(gid);
347 if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END)
348 || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END)
349 || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) {
350 // Don't traverse inside or measure
351 fts_set(fts, p, FTS_SKIP);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700352 break;
353 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700354 if (include_gid != -1 && gid != include_gid) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700355 break;
356 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700357 if (exclude_gid != -1 && gid == exclude_gid) {
358 break;
359 }
360 matchedSize += (p->fts_statp->st_blocks * 512);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700361 break;
362 }
363 }
364 fts_close(fts);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700365#if MEASURE_DEBUG
366 if ((include_gid == -1) && (exclude_gid == -1)) {
367 LOG(DEBUG) << "Measured " << path << " size " << matchedSize;
368 } else {
369 LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid
370 << " exclude " << exclude_gid;
371 }
372#endif
373 *size += matchedSize;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700374 return 0;
375}
376
Mike Lockwood94afecf2012-10-24 10:45:23 -0700377/**
378 * Checks whether the package name is valid. Returns -1 on error and
379 * 0 on success.
380 */
Jeff Sharkey423e7462016-12-09 18:18:43 -0700381bool is_valid_package_name(const std::string& packageName) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700382 // This logic is borrowed from PackageParser.java
383 bool hasSep = false;
384 bool front = true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700385
Jeff Sharkey367ace22017-03-07 22:12:03 -0700386 auto it = packageName.begin();
387 for (; it != packageName.end() && *it != '-'; it++) {
388 char c = *it;
389 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
390 front = false;
391 continue;
392 }
393 if (!front) {
394 if ((c >= '0' && c <= '9') || c == '_') {
395 continue;
396 }
397 }
398 if (c == '.') {
399 hasSep = true;
400 front = true;
401 continue;
402 }
403 LOG(WARNING) << "Bad package character " << c << " in " << packageName;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700404 return false;
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700405 }
406
Jeff Sharkeyab7ac8d2017-03-08 12:39:46 -0700407 if (front) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700408 LOG(WARNING) << "Missing separator in " << packageName;
409 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700410 }
411
Jeff Sharkey367ace22017-03-07 22:12:03 -0700412 for (; it != packageName.end(); it++) {
413 char c = *it;
414 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
415 if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
416 LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
417 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700418 }
419
Jeff Sharkey423e7462016-12-09 18:18:43 -0700420 return true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700421}
422
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100423static int _delete_dir_contents(DIR *d,
424 int (*exclusion_predicate)(const char *name, const int is_dir))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700425{
426 int result = 0;
427 struct dirent *de;
428 int dfd;
429
430 dfd = dirfd(d);
431
432 if (dfd < 0) return -1;
433
434 while ((de = readdir(d))) {
435 const char *name = de->d_name;
436
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100437 /* check using the exclusion predicate, if provided */
438 if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
439 continue;
440 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700441
442 if (de->d_type == DT_DIR) {
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700443 int subfd;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700444 DIR *subdir;
445
446 /* always skip "." and ".." */
447 if (name[0] == '.') {
448 if (name[1] == 0) continue;
449 if ((name[1] == '.') && (name[2] == 0)) continue;
450 }
451
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700452 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700453 if (subfd < 0) {
454 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
455 result = -1;
456 continue;
457 }
458 subdir = fdopendir(subfd);
459 if (subdir == NULL) {
460 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
461 close(subfd);
462 result = -1;
463 continue;
464 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100465 if (_delete_dir_contents(subdir, exclusion_predicate)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700466 result = -1;
467 }
468 closedir(subdir);
469 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
470 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
471 result = -1;
472 }
473 } else {
474 if (unlinkat(dfd, name, 0) < 0) {
475 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
476 result = -1;
477 }
478 }
479 }
480
481 return result;
482}
483
Calin Juravleb06f98a2016-03-28 15:11:01 +0100484int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
485 return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700486}
487
Calin Juravleb06f98a2016-03-28 15:11:01 +0100488int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
489 return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700490}
491
Mike Lockwood94afecf2012-10-24 10:45:23 -0700492int delete_dir_contents(const char *pathname,
493 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100494 int (*exclusion_predicate)(const char*, const int),
495 bool ignore_if_missing)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700496{
497 int res = 0;
498 DIR *d;
499
500 d = opendir(pathname);
501 if (d == NULL) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100502 if (ignore_if_missing && (errno == ENOENT)) {
503 return 0;
504 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700505 ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
506 return -errno;
507 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100508 res = _delete_dir_contents(d, exclusion_predicate);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700509 closedir(d);
510 if (also_delete_dir) {
511 if (rmdir(pathname)) {
512 ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
513 res = -1;
514 }
515 }
516 return res;
517}
518
519int delete_dir_contents_fd(int dfd, const char *name)
520{
521 int fd, res;
522 DIR *d;
523
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700524 fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700525 if (fd < 0) {
526 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
527 return -1;
528 }
529 d = fdopendir(fd);
530 if (d == NULL) {
531 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
532 close(fd);
533 return -1;
534 }
535 res = _delete_dir_contents(d, 0);
536 closedir(d);
537 return res;
538}
539
Robin Lee60fd3fe2014-10-07 16:55:02 +0100540static int _copy_owner_permissions(int srcfd, int dstfd)
541{
542 struct stat st;
543 if (fstat(srcfd, &st) != 0) {
544 return -1;
545 }
546 if (fchmod(dstfd, st.st_mode) != 0) {
547 return -1;
548 }
549 return 0;
550}
551
552static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
553{
554 int result = 0;
555 if (_copy_owner_permissions(sdfd, ddfd) != 0) {
556 ALOGE("_copy_dir_files failed to copy dir permissions\n");
557 }
558 if (fchown(ddfd, owner, group) != 0) {
559 ALOGE("_copy_dir_files failed to change dir owner\n");
560 }
561
562 DIR *ds = fdopendir(sdfd);
563 if (ds == NULL) {
564 ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
565 return -1;
566 }
567 struct dirent *de;
568 while ((de = readdir(ds))) {
569 if (de->d_type != DT_REG) {
570 continue;
571 }
572
573 const char *name = de->d_name;
574 int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
575 int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
576 if (fsfd == -1 || fdfd == -1) {
577 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
578 } else {
579 if (_copy_owner_permissions(fsfd, fdfd) != 0) {
580 ALOGE("Failed to change file permissions\n");
581 }
582 if (fchown(fdfd, owner, group) != 0) {
583 ALOGE("Failed to change file owner\n");
584 }
585
586 char buf[8192];
587 ssize_t size;
588 while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
589 write(fdfd, buf, size);
590 }
591 if (size < 0) {
592 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
593 result = -1;
594 }
595 }
596 close(fdfd);
597 close(fsfd);
598 }
599
600 return result;
601}
602
603int copy_dir_files(const char *srcname,
604 const char *dstname,
605 uid_t owner,
606 uid_t group)
607{
608 int res = 0;
609 DIR *ds = NULL;
610 DIR *dd = NULL;
611
612 ds = opendir(srcname);
613 if (ds == NULL) {
614 ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
615 return -errno;
616 }
617
618 mkdir(dstname, 0600);
619 dd = opendir(dstname);
620 if (dd == NULL) {
621 ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
622 closedir(ds);
623 return -errno;
624 }
625
626 int sdfd = dirfd(ds);
627 int ddfd = dirfd(dd);
628 if (sdfd != -1 && ddfd != -1) {
629 res = _copy_dir_files(sdfd, ddfd, owner, group);
630 } else {
631 res = -errno;
632 }
633 closedir(dd);
634 closedir(ds);
635 return res;
636}
637
Jeff Sharkeya836c472017-04-02 23:29:30 -0600638int64_t data_disk_free(const std::string& data_path) {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600639 struct statvfs sfs;
640 if (statvfs(data_path.c_str(), &sfs) == 0) {
Jeff Sharkey4f7be172017-08-11 15:13:31 -0600641 return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700642 } else {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600643 PLOG(ERROR) << "Couldn't statvfs " << data_path;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700644 return -1;
645 }
646}
647
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600648int get_path_inode(const std::string& path, ino_t *inode) {
649 struct stat buf;
650 memset(&buf, 0, sizeof(buf));
651 if (stat(path.c_str(), &buf) != 0) {
652 PLOG(WARNING) << "Failed to stat " << path;
653 return -1;
654 } else {
655 *inode = buf.st_ino;
656 return 0;
657 }
658}
659
660/**
661 * Write the inode of a specific child file into the given xattr on the
662 * parent directory. This allows you to find the child later, even if its
663 * name is encrypted.
664 */
665int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
666 ino_t inode = 0;
667 uint64_t inode_raw = 0;
668 auto path = StringPrintf("%s/%s", parent.c_str(), name);
669
670 if (get_path_inode(path, &inode) != 0) {
671 // Path probably doesn't exist yet; ignore
672 return 0;
673 }
674
675 // Check to see if already set correctly
676 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
677 if (inode_raw == inode) {
678 // Already set correctly; skip writing
679 return 0;
680 } else {
681 PLOG(WARNING) << "Mismatched inode value; found " << inode
682 << " on disk but marked value was " << inode_raw << "; overwriting";
683 }
684 }
685
686 inode_raw = inode;
Jeff Sharkey4ed65072016-07-22 11:38:54 -0600687 if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600688 PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent;
689 return -1;
690 } else {
691 return 0;
692 }
693}
694
695/**
696 * Read the inode of a specific child file from the given xattr on the
697 * parent directory. Returns a currently valid path for that child, which
698 * might have an encrypted name.
699 */
700std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
701 ino_t inode = 0;
702 uint64_t inode_raw = 0;
703 auto fallback = StringPrintf("%s/%s", parent.c_str(), name);
704
705 // Lookup the inode value written earlier
706 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
707 inode = inode_raw;
708 }
709
710 // For testing purposes, rely on the inode when defined; this could be
711 // optimized to use access() in the future.
712 if (inode != 0) {
713 DIR* dir = opendir(parent.c_str());
714 if (dir == nullptr) {
715 PLOG(ERROR) << "Failed to opendir " << parent;
716 return fallback;
717 }
718
719 struct dirent* ent;
720 while ((ent = readdir(dir))) {
721 if (ent->d_ino == inode) {
722 auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name);
723#if DEBUG_XATTRS
724 if (resolved != fallback) {
725 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode
726 << " instead of " << fallback;
727 }
728#endif
729 closedir(dir);
730 return resolved;
731 }
732 }
733 LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback;
734 closedir(dir);
735 return fallback;
736 } else {
737 return fallback;
738 }
739}
740
Ryuki Nakamurac7342f82017-09-30 11:57:00 +0900741void remove_path_xattr(const std::string& path, const char* inode_xattr) {
742 if (removexattr(path.c_str(), inode_xattr) && errno != ENODATA) {
743 PLOG(ERROR) << "Failed to remove xattr " << inode_xattr << " at " << path;
744 }
745}
746
Mike Lockwood94afecf2012-10-24 10:45:23 -0700747/**
Calin Juravlec597b6d2014-08-19 17:43:05 +0100748 * Validate that the path is valid in the context of the provided directory.
749 * The path is allowed to have at most one subdirectory and no indirections
750 * to top level directories (i.e. have "..").
751 */
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600752static int validate_path(const std::string& dir, const std::string& path, int maxSubdirs) {
753 // Argument sanity checking
754 if (dir.find('/') != 0 || dir.rfind('/') != dir.size() - 1
755 || dir.find("..") != std::string::npos) {
756 LOG(ERROR) << "Invalid directory " << dir;
757 return -1;
758 }
759 if (path.find("..") != std::string::npos) {
760 LOG(ERROR) << "Invalid path " << path;
761 return -1;
Calin Juravlec597b6d2014-08-19 17:43:05 +0100762 }
763
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600764 if (path.compare(0, dir.size(), dir) != 0) {
765 // Common case, path isn't under directory
766 return -1;
767 }
768
769 // Count number of subdirectories
770 auto pos = path.find('/', dir.size());
771 int count = 0;
772 while (pos != std::string::npos) {
Jeff Sharkey172fac02017-10-06 13:09:46 -0600773 auto next = path.find('/', pos + 1);
774 if (next > pos + 1) {
775 count++;
776 }
777 pos = next;
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600778 }
779
780 if (count > maxSubdirs) {
781 LOG(ERROR) << "Invalid path depth " << path << " when tested against " << dir;
Calin Juravlec597b6d2014-08-19 17:43:05 +0100782 return -1;
783 }
784
785 return 0;
786}
787
788/**
Mike Lockwood94afecf2012-10-24 10:45:23 -0700789 * Checks whether a path points to a system app (.apk file). Returns 0
790 * if it is a system app or -1 if it is not.
791 */
792int validate_system_app_path(const char* path) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600793 std::string path_ = path;
794 for (const auto& dir : android_system_dirs) {
795 if (validate_path(dir, path, 1) == 0) {
796 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700797 }
798 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700799 return -1;
800}
801
Calin Juravle114f0812017-03-08 19:05:07 -0800802bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravle7d765462017-09-04 15:57:10 -0700803 const char* volume_uuid, int uid, int storage_flag) {
Calin Juravle42451c02017-01-17 14:43:25 -0800804 CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE);
805
Calin Juravle3760ad32017-07-27 16:31:55 -0700806 // Empty paths are not allowed.
807 if (dex_path.empty()) { return false; }
808 // First character should always be '/'. No relative paths.
809 if (dex_path[0] != '/') { return false; }
810 // The last character should not be '/'.
811 if (dex_path[dex_path.size() - 1] == '/') { return false; }
812 // There should be no '.' after the directory marker.
813 if (dex_path.find("/.") != std::string::npos) { return false; }
814 // The path should be at most PKG_PATH_MAX long.
815 if (dex_path.size() > PKG_PATH_MAX) { return false; }
816
Calin Juravle7d765462017-09-04 15:57:10 -0700817 // The dex_path should be under the app data directory.
818 std::string app_private_dir = storage_flag == FLAG_STORAGE_CE
Calin Juravledd42e272017-09-11 11:50:36 -0700819 ? create_data_user_ce_package_path(
820 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str())
821 : create_data_user_de_package_path(
822 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
Calin Juravle3760ad32017-07-27 16:31:55 -0700823
Calin Juravle7d765462017-09-04 15:57:10 -0700824 if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) {
825 // The check above might fail if the dex file is accessed via the /data/user/0 symlink.
826 // If that's the case, attempt to validate against the user data link.
827 std::string app_private_dir_symlink = create_data_user_ce_package_path_as_user_link(
828 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
829 if (strncmp(dex_path.c_str(), app_private_dir_symlink.c_str(),
830 app_private_dir_symlink.size()) != 0) {
Calin Juravledd42e272017-09-11 11:50:36 -0700831 return false;
832 }
Calin Juravle42451c02017-01-17 14:43:25 -0800833 }
Calin Juravle3760ad32017-07-27 16:31:55 -0700834
835 // If we got here we have a valid path.
836 return true;
Calin Juravle42451c02017-01-17 14:43:25 -0800837}
838
Mike Lockwood94afecf2012-10-24 10:45:23 -0700839/**
Narayan Kamathd845c962015-06-04 13:20:27 +0100840 * Check whether path points to a valid path for an APK file. The path must
841 * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
842 * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
843 * is encountered.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700844 */
Narayan Kamathd845c962015-06-04 13:20:27 +0100845static int validate_apk_path_internal(const char *path, int maxSubdirs) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600846 std::string path_ = path;
847 if (validate_path(android_app_dir, path_, maxSubdirs) == 0) {
848 return 0;
849 } else if (validate_path(android_app_private_dir, path_, maxSubdirs) == 0) {
850 return 0;
851 } else if (validate_path(android_app_ephemeral_dir, path_, maxSubdirs) == 0) {
852 return 0;
853 } else if (validate_path(android_asec_dir, path_, maxSubdirs) == 0) {
854 return 0;
855 } else if (validate_path(android_mnt_expand_dir, path_, std::max(maxSubdirs, 2)) == 0) {
856 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700857 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700858 return -1;
859 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700860}
861
Narayan Kamathd845c962015-06-04 13:20:27 +0100862int validate_apk_path(const char* path) {
863 return validate_apk_path_internal(path, 1 /* maxSubdirs */);
864}
865
866int validate_apk_path_subdirs(const char* path) {
867 return validate_apk_path_internal(path, 3 /* maxSubdirs */);
868}
869
Robin Lee095c7632014-04-25 15:05:19 +0100870int ensure_config_user_dirs(userid_t userid) {
Robin Lee095c7632014-04-25 15:05:19 +0100871 // writable by system, readable by any app within the same user
Robin Lee60fd3fe2014-10-07 16:55:02 +0100872 const int uid = multiuser_get_uid(userid, AID_SYSTEM);
873 const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
Robin Lee095c7632014-04-25 15:05:19 +0100874
875 // Ensure /data/misc/user/<userid> exists
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600876 auto path = create_data_misc_legacy_path(userid);
877 return fs_prepare_dir(path.c_str(), 0750, uid, gid);
Robin Lee095c7632014-04-25 15:05:19 +0100878}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800879
880int wait_child(pid_t pid)
881{
882 int status;
883 pid_t got_pid;
884
885 while (1) {
886 got_pid = waitpid(pid, &status, 0);
887 if (got_pid == -1 && errno == EINTR) {
888 printf("waitpid interrupted, retrying\n");
889 } else {
890 break;
891 }
892 }
893 if (got_pid != pid) {
894 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
895 (int) pid, (int) got_pid, strerror(errno));
896 return 1;
897 }
898
899 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
900 return 0;
901 } else {
902 return status; /* always nonzero */
903 }
904}
905
Calin Juravle42451c02017-01-17 14:43:25 -0800906/**
907 * Prepare an app cache directory, which offers to fix-up the GID and
908 * directory mode flags during a platform upgrade.
909 * The app cache directory path will be 'parent'/'name'.
910 */
911int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
912 uid_t uid, gid_t gid) {
913 auto path = StringPrintf("%s/%s", parent.c_str(), name);
914 struct stat st;
915 if (stat(path.c_str(), &st) != 0) {
916 if (errno == ENOENT) {
917 // This is fine, just create it
918 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) {
919 PLOG(ERROR) << "Failed to prepare " << path;
920 return -1;
921 } else {
922 return 0;
923 }
924 } else {
925 PLOG(ERROR) << "Failed to stat " << path;
926 return -1;
927 }
928 }
929
930 mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
931 if (st.st_uid != uid) {
932 // Mismatched UID is real trouble; we can't recover
933 LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid
934 << " but expected " << uid;
935 return -1;
936 } else if (st.st_gid == gid && actual_mode == target_mode) {
937 // Everything looks good!
938 return 0;
Jeff Sharkeye59c85c2017-04-02 21:53:14 -0600939 } else {
940 // Mismatched GID/mode is recoverable; fall through to update
941 LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid
Shubham Ajmeraec0afbf2017-09-14 11:07:33 -0700942 << "/" << actual_mode << " but expected " << gid << "/" << target_mode;
Calin Juravle42451c02017-01-17 14:43:25 -0800943 }
944
945 // Directory is owned correctly, but GID or mode mismatch means it's
946 // probably a platform upgrade so we need to fix them
947 FTS *fts;
948 FTSENT *p;
949 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700950 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Calin Juravle42451c02017-01-17 14:43:25 -0800951 PLOG(ERROR) << "Failed to fts_open " << path;
952 return -1;
953 }
954 while ((p = fts_read(fts)) != NULL) {
955 switch (p->fts_info) {
956 case FTS_DP:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600957 if (chmod(p->fts_path, target_mode) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800958 PLOG(WARNING) << "Failed to chmod " << p->fts_path;
959 }
960 // Intentional fall through to also set GID
961 case FTS_F:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600962 if (chown(p->fts_path, -1, gid) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800963 PLOG(WARNING) << "Failed to chown " << p->fts_path;
964 }
965 break;
966 case FTS_SL:
967 case FTS_SLNONE:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600968 if (lchown(p->fts_path, -1, gid) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800969 PLOG(WARNING) << "Failed to chown " << p->fts_path;
970 }
971 break;
972 }
973 }
974 fts_close(fts);
975 return 0;
976}
977
Andreas Gampe02d0de52015-11-11 20:43:16 -0800978} // namespace installd
979} // namespace android