
src.com.android.server.os.NativeTombstoneManager Maven / Gradle / Ivy
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.os;
import static android.app.ApplicationExitInfo.REASON_CRASH_NATIVE;
import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import android.annotation.AppIdInt;
import android.annotation.CurrentTimeMillisLong;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ApplicationExitInfo;
import android.app.IParcelFileDescriptorRetriever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.FileObserver;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.system.ErrnoException;
import android.system.Os;
import android.system.StructStat;
import android.util.Slog;
import android.util.SparseArray;
import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoParseException;
import com.android.internal.annotations.GuardedBy;
import com.android.server.BootReceiver;
import com.android.server.ServiceThread;
import com.android.server.os.TombstoneProtos.Cause;
import com.android.server.os.TombstoneProtos.Tombstone;
import libcore.io.IoUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* A class to manage native tombstones.
*/
public final class NativeTombstoneManager {
private static final String TAG = NativeTombstoneManager.class.getSimpleName();
private static final File TOMBSTONE_DIR = new File("/data/tombstones");
private final Context mContext;
private final Handler mHandler;
private final TombstoneWatcher mWatcher;
private final Object mLock = new Object();
@GuardedBy("mLock")
private final SparseArray mTombstones;
NativeTombstoneManager(Context context) {
mTombstones = new SparseArray();
mContext = context;
final ServiceThread thread = new ServiceThread(TAG + ":tombstoneWatcher",
THREAD_PRIORITY_BACKGROUND, true /* allowIo */);
thread.start();
mHandler = thread.getThreadHandler();
mWatcher = new TombstoneWatcher();
mWatcher.startWatching();
}
void onSystemReady() {
registerForUserRemoval();
registerForPackageRemoval();
// Scan existing tombstones.
mHandler.post(() -> {
final File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
if (tombstoneFiles[i].isFile()) {
handleTombstone(tombstoneFiles[i]);
}
}
});
}
private void handleTombstone(File path) {
final String filename = path.getName();
if (!filename.startsWith("tombstone_")) {
return;
}
if (filename.endsWith(".pb")) {
handleProtoTombstone(path);
BootReceiver.addTombstoneToDropBox(mContext, path, true);
} else {
BootReceiver.addTombstoneToDropBox(mContext, path, false);
}
}
private void handleProtoTombstone(File path) {
final String filename = path.getName();
if (!filename.endsWith(".pb")) {
Slog.w(TAG, "unexpected tombstone name: " + path);
return;
}
final String suffix = filename.substring("tombstone_".length());
final String numberStr = suffix.substring(0, suffix.length() - 3);
int number;
try {
number = Integer.parseInt(numberStr);
if (number < 0 || number > 99) {
Slog.w(TAG, "unexpected tombstone name: " + path);
return;
}
} catch (NumberFormatException ex) {
Slog.w(TAG, "unexpected tombstone name: " + path);
return;
}
ParcelFileDescriptor pfd;
try {
pfd = ParcelFileDescriptor.open(path, MODE_READ_WRITE);
} catch (FileNotFoundException ex) {
Slog.w(TAG, "failed to open " + path, ex);
return;
}
final Optional parsedTombstone = TombstoneFile.parse(pfd);
if (!parsedTombstone.isPresent()) {
IoUtils.closeQuietly(pfd);
return;
}
synchronized (mLock) {
TombstoneFile previous = mTombstones.get(number);
if (previous != null) {
previous.dispose();
}
mTombstones.put(number, parsedTombstone.get());
}
}
/**
* Remove native tombstones matching a user and/or app.
*
* @param userId user id to filter by, selects all users if empty
* @param appId app id to filter by, selects all users if empty
*/
public void purge(Optional userId, Optional appId) {
mHandler.post(() -> {
synchronized (mLock) {
for (int i = mTombstones.size() - 1; i >= 0; --i) {
TombstoneFile tombstone = mTombstones.valueAt(i);
if (tombstone.matches(userId, appId)) {
tombstone.purge();
mTombstones.removeAt(i);
}
}
}
});
}
private void purgePackage(int uid, boolean allUsers) {
final int appId = UserHandle.getAppId(uid);
Optional userId;
if (allUsers) {
userId = Optional.empty();
} else {
userId = Optional.of(UserHandle.getUserId(uid));
}
purge(userId, Optional.of(appId));
}
private void purgeUser(int uid) {
purge(Optional.of(uid), Optional.empty());
}
private void registerForPackageRemoval() {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED);
filter.addDataScheme("package");
mContext.registerReceiverForAllUsers(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final int uid = intent.getIntExtra(Intent.EXTRA_UID, UserHandle.USER_NULL);
if (uid == UserHandle.USER_NULL) return;
final boolean allUsers = intent.getBooleanExtra(
Intent.EXTRA_REMOVED_FOR_ALL_USERS, false);
purgePackage(uid, allUsers);
}
}, filter, null, mHandler);
}
private void registerForUserRemoval() {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_REMOVED);
mContext.registerReceiverForAllUsers(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
if (userId < 1) return;
purgeUser(userId);
}
}, filter, null, mHandler);
}
/**
* Collect native tombstones.
*
* @param output list to append to
* @param callingUid POSIX uid to filter by
* @param pid pid to filter by, ignored if zero
* @param maxNum maximum number of elements in output
*/
public void collectTombstones(ArrayList output, int callingUid, int pid,
int maxNum) {
CompletableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy