use of android.system.ErrnoException in project platform_frameworks_base by android.
the class ZygoteConnection method handleChildProc.
/**
* Handles post-fork setup of child proc, closing sockets as appropriate,
* reopen stdio as appropriate, and ultimately throwing MethodAndArgsCaller
* if successful or returning if failed.
*
* @param parsedArgs non-null; zygote args
* @param descriptors null-ok; new file descriptors for stdio if available.
* @param pipeFd null-ok; pipe for communication back to Zygote.
* @param newStderr null-ok; stream to use for stderr until stdio
* is reopened.
*
* @throws Zygote.MethodAndArgsCaller on success to
* trampoline to code that invokes static main.
*/
private void handleChildProc(Arguments parsedArgs, FileDescriptor[] descriptors, FileDescriptor pipeFd, PrintStream newStderr) throws Zygote.MethodAndArgsCaller {
/**
* By the time we get here, the native code has closed the two actual Zygote
* socket connections, and substituted /dev/null in their place. The LocalSocket
* objects still need to be closed properly.
*/
closeSocket();
if (descriptors != null) {
try {
Os.dup2(descriptors[0], STDIN_FILENO);
Os.dup2(descriptors[1], STDOUT_FILENO);
Os.dup2(descriptors[2], STDERR_FILENO);
for (FileDescriptor fd : descriptors) {
IoUtils.closeQuietly(fd);
}
newStderr = System.err;
} catch (ErrnoException ex) {
Log.e(TAG, "Error reopening stdio", ex);
}
}
if (parsedArgs.niceName != null) {
Process.setArgV0(parsedArgs.niceName);
}
// End of the postFork event.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
if (parsedArgs.invokeWith != null) {
WrapperInit.execApplication(parsedArgs.invokeWith, parsedArgs.niceName, parsedArgs.targetSdkVersion, VMRuntime.getCurrentInstructionSet(), pipeFd, parsedArgs.remainingArgs);
} else {
ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, null);
}
}
use of android.system.ErrnoException in project platform_frameworks_base by android.
the class ExifInterfaceTest method testSaveAttributes_withFileDescriptor.
private void testSaveAttributes_withFileDescriptor(File imageFile, ExpectedValue expectedValue) throws IOException {
String verboseTag = imageFile.getName();
FileDescriptor fd = null;
try {
fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDWR, 0600);
ExifInterface exifInterface = new ExifInterface(fd);
exifInterface.saveAttributes();
Os.lseek(fd, 0, OsConstants.SEEK_SET);
exifInterface = new ExifInterface(fd);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
// Test for modifying one attribute.
String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
exifInterface.saveAttributes();
Os.lseek(fd, 0, OsConstants.SEEK_SET);
exifInterface = new ExifInterface(fd);
assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
// Restore the backup value.
exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
exifInterface.saveAttributes();
Os.lseek(fd, 0, OsConstants.SEEK_SET);
exifInterface = new ExifInterface(fd);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
} catch (ErrnoException e) {
throw e.rethrowAsIOException();
} finally {
IoUtils.closeQuietly(fd);
}
}
use of android.system.ErrnoException in project platform_frameworks_base by android.
the class ExifInterfaceTest method testExifInterfaceCommon.
private void testExifInterfaceCommon(File imageFile, ExpectedValue expectedValue) throws IOException {
String verboseTag = imageFile.getName();
// Creates via path.
ExifInterface exifInterface = new ExifInterface(imageFile.getAbsolutePath());
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
// Creates from an asset file.
InputStream in = null;
try {
in = mContext.getAssets().open(imageFile.getName());
exifInterface = new ExifInterface(in);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
} finally {
IoUtils.closeQuietly(in);
}
// Creates via InputStream.
in = null;
try {
in = new BufferedInputStream(new FileInputStream(imageFile.getAbsolutePath()));
exifInterface = new ExifInterface(in);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
} finally {
IoUtils.closeQuietly(in);
}
// Creates via FileDescriptor.
FileDescriptor fd = null;
try {
fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDONLY, 0600);
exifInterface = new ExifInterface(fd);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
} catch (ErrnoException e) {
throw e.rethrowAsIOException();
} finally {
IoUtils.closeQuietly(fd);
}
}
use of android.system.ErrnoException in project platform_frameworks_base by android.
the class UserManagerService method getSerialNumber.
/**
* Return serial number stored in user directory inode.
*
* @return parsed serial number, or -1 if not set
*/
private static int getSerialNumber(File file) throws IOException {
try {
final byte[] buf = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL);
final String serial = new String(buf);
try {
return Integer.parseInt(serial);
} catch (NumberFormatException e) {
throw new IOException("Bad serial number: " + serial);
}
} catch (ErrnoException e) {
if (e.errno == OsConstants.ENODATA) {
return -1;
} else {
throw e.rethrowAsIOException();
}
}
}
use of android.system.ErrnoException in project platform_frameworks_base by android.
the class UsbMidiDevice method openLocked.
private boolean openLocked() {
// FIXME - support devices with different number of input and output ports
FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
if (fileDescriptors == null) {
Log.e(TAG, "nativeOpen failed");
return false;
}
mFileDescriptors = fileDescriptors;
int inputStreamCount = fileDescriptors.length;
// last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
// in our input thread
int outputStreamCount = fileDescriptors.length - 1;
mPollFDs = new StructPollfd[inputStreamCount];
mInputStreams = new FileInputStream[inputStreamCount];
for (int i = 0; i < inputStreamCount; i++) {
FileDescriptor fd = fileDescriptors[i];
StructPollfd pollfd = new StructPollfd();
pollfd.fd = fd;
pollfd.events = (short) OsConstants.POLLIN;
mPollFDs[i] = pollfd;
mInputStreams[i] = new FileInputStream(fd);
}
mOutputStreams = new FileOutputStream[outputStreamCount];
mEventSchedulers = new MidiEventScheduler[outputStreamCount];
for (int i = 0; i < outputStreamCount; i++) {
mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
MidiEventScheduler scheduler = new MidiEventScheduler();
mEventSchedulers[i] = scheduler;
mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
}
final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
// Create input thread which will read from all output ports of the physical device
new Thread("UsbMidiDevice input thread") {
@Override
public void run() {
byte[] buffer = new byte[BUFFER_SIZE];
try {
while (true) {
// Record time of event immediately after waking.
long timestamp = System.nanoTime();
synchronized (mLock) {
if (!mIsOpen)
break;
// look for a readable FileDescriptor
for (int index = 0; index < mPollFDs.length; index++) {
StructPollfd pfd = mPollFDs[index];
if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
break;
} else if ((pfd.revents & OsConstants.POLLIN) != 0) {
// clear readable flag
pfd.revents = 0;
if (index == mInputStreams.length - 1) {
// last file descriptor is used only for unblocking Os.poll()
break;
}
int count = mInputStreams[index].read(buffer);
outputReceivers[index].send(buffer, 0, count, timestamp);
}
}
}
// wait until we have a readable port or we are signalled to close
Os.poll(mPollFDs, -1);
}
} catch (IOException e) {
Log.d(TAG, "reader thread exiting");
} catch (ErrnoException e) {
Log.d(TAG, "reader thread exiting");
}
Log.d(TAG, "input thread exit");
}
}.start();
// Create output thread for each input port of the physical device
for (int port = 0; port < outputStreamCount; port++) {
final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
final FileOutputStream outputStreamF = mOutputStreams[port];
final int portF = port;
new Thread("UsbMidiDevice output thread " + port) {
@Override
public void run() {
while (true) {
MidiEvent event;
try {
event = (MidiEvent) eventSchedulerF.waitNextEvent();
} catch (InterruptedException e) {
// try again
continue;
}
if (event == null) {
break;
}
try {
outputStreamF.write(event.data, 0, event.count);
} catch (IOException e) {
Log.e(TAG, "write failed for port " + portF);
}
eventSchedulerF.addEventToPool(event);
}
Log.d(TAG, "output thread exit");
}
}.start();
}
mIsOpen = true;
return true;
}
Aggregations