Merge "Move RevocableFileDescriptor creation outside the locked section." into rvc-dev am: 51540fe05e am: 5a78574e55 am: f81117f0d8

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11744166

Change-Id: I85278455a3d1954853e67fda011df3fcbc6d292c
This commit is contained in:
Sudheer Shanka
2020-06-13 01:57:11 +00:00
committed by Automerger Merge Worker
3 changed files with 60 additions and 37 deletions

View File

@@ -44,7 +44,7 @@ import java.util.Objects;
/**
* Class for representing how a blob can be shared.
*
* Note that this class is not thread-safe, callers need to take of synchronizing access.
* Note that this class is not thread-safe, callers need to take care of synchronizing access.
*/
class BlobAccessMode {
@Retention(RetentionPolicy.SOURCE)

View File

@@ -61,6 +61,8 @@ import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.XmlUtils;
import com.android.server.blob.BlobStoreManagerService.DumpArgs;
import libcore.io.IoUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@@ -349,14 +351,16 @@ class BlobMetadata {
} catch (ErrnoException e) {
throw e.rethrowAsIOException();
}
synchronized (mMetadataLock) {
return createRevocableFdLocked(fd, callingPackage);
try {
return createRevocableFd(fd, callingPackage);
} catch (IOException e) {
IoUtils.closeQuietly(fd);
throw e;
}
}
@GuardedBy("mMetadataLock")
@NonNull
private ParcelFileDescriptor createRevocableFdLocked(FileDescriptor fd,
private ParcelFileDescriptor createRevocableFd(FileDescriptor fd,
String callingPackage) throws IOException {
final RevocableFileDescriptor revocableFd =
new RevocableFileDescriptor(mContext, fd);

View File

@@ -59,6 +59,8 @@ import com.android.internal.util.XmlUtils;
import com.android.server.blob.BlobStoreManagerService.DumpArgs;
import com.android.server.blob.BlobStoreManagerService.SessionStateChangeListener;
import libcore.io.IoUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@@ -207,27 +209,37 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
throw new IllegalStateException("Not allowed to write in state: "
+ stateToString(mState));
}
}
try {
return openWriteLocked(offsetBytes, lengthBytes);
} catch (IOException e) {
throw ExceptionUtils.wrap(e);
FileDescriptor fd = null;
try {
fd = openWriteInternal(offsetBytes, lengthBytes);
final RevocableFileDescriptor revocableFd = new RevocableFileDescriptor(mContext, fd);
synchronized (mSessionLock) {
if (mState != STATE_OPENED) {
IoUtils.closeQuietly(fd);
throw new IllegalStateException("Not allowed to write in state: "
+ stateToString(mState));
}
trackRevocableFdLocked(revocableFd);
return revocableFd.getRevocableFileDescriptor();
}
} catch (IOException e) {
IoUtils.closeQuietly(fd);
throw ExceptionUtils.wrap(e);
}
}
@GuardedBy("mSessionLock")
@NonNull
private ParcelFileDescriptor openWriteLocked(@BytesLong long offsetBytes,
private FileDescriptor openWriteInternal(@BytesLong long offsetBytes,
@BytesLong long lengthBytes) throws IOException {
// TODO: Add limit on active open sessions/writes/reads
FileDescriptor fd = null;
try {
final File sessionFile = getSessionFile();
if (sessionFile == null) {
throw new IllegalStateException("Couldn't get the file for this session");
}
fd = Os.open(sessionFile.getPath(), O_CREAT | O_RDWR, 0600);
final FileDescriptor fd = Os.open(sessionFile.getPath(), O_CREAT | O_RDWR, 0600);
if (offsetBytes > 0) {
final long curOffset = Os.lseek(fd, offsetBytes, SEEK_SET);
if (curOffset != offsetBytes) {
@@ -238,10 +250,10 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
if (lengthBytes > 0) {
mContext.getSystemService(StorageManager.class).allocateBytes(fd, lengthBytes);
}
return fd;
} catch (ErrnoException e) {
e.rethrowAsIOException();
throw e.rethrowAsIOException();
}
return createRevocableFdLocked(fd);
}
@Override
@@ -253,29 +265,40 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
throw new IllegalStateException("Not allowed to read in state: "
+ stateToString(mState));
}
}
try {
return openReadLocked();
} catch (IOException e) {
throw ExceptionUtils.wrap(e);
FileDescriptor fd = null;
try {
fd = openReadInternal();
final RevocableFileDescriptor revocableFd = new RevocableFileDescriptor(mContext, fd);
synchronized (mSessionLock) {
if (mState != STATE_OPENED) {
IoUtils.closeQuietly(fd);
throw new IllegalStateException("Not allowed to read in state: "
+ stateToString(mState));
}
trackRevocableFdLocked(revocableFd);
return revocableFd.getRevocableFileDescriptor();
}
} catch (IOException e) {
IoUtils.closeQuietly(fd);
throw ExceptionUtils.wrap(e);
}
}
@GuardedBy("mSessionLock")
@NonNull
private ParcelFileDescriptor openReadLocked() throws IOException {
FileDescriptor fd = null;
private FileDescriptor openReadInternal() throws IOException {
try {
final File sessionFile = getSessionFile();
if (sessionFile == null) {
throw new IllegalStateException("Couldn't get the file for this session");
}
fd = Os.open(sessionFile.getPath(), O_RDONLY, 0);
final FileDescriptor fd = Os.open(sessionFile.getPath(), O_RDONLY, 0);
return fd;
} catch (ErrnoException e) {
e.rethrowAsIOException();
throw e.rethrowAsIOException();
}
return createRevocableFdLocked(fd);
}
@Override
@@ -396,7 +419,7 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
}
mState = state;
revokeAllFdsLocked();
revokeAllFds();
if (sendCallback) {
mListener.onStateChanged(this);
@@ -433,20 +456,17 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
}
}
@GuardedBy("mSessionLock")
private void revokeAllFdsLocked() {
for (int i = mRevocableFds.size() - 1; i >= 0; --i) {
mRevocableFds.get(i).revoke();
private void revokeAllFds() {
synchronized (mRevocableFds) {
for (int i = mRevocableFds.size() - 1; i >= 0; --i) {
mRevocableFds.get(i).revoke();
}
mRevocableFds.clear();
}
mRevocableFds.clear();
}
@GuardedBy("mSessionLock")
@NonNull
private ParcelFileDescriptor createRevocableFdLocked(FileDescriptor fd)
throws IOException {
final RevocableFileDescriptor revocableFd =
new RevocableFileDescriptor(mContext, fd);
private void trackRevocableFdLocked(RevocableFileDescriptor revocableFd) {
synchronized (mRevocableFds) {
mRevocableFds.add(revocableFd);
}
@@ -455,7 +475,6 @@ class BlobStoreSession extends IBlobStoreSession.Stub {
mRevocableFds.remove(revocableFd);
}
});
return revocableFd.getRevocableFileDescriptor();
}
@Nullable