1 /* 2 * Copyright (C) 2024 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 package android.system; 17 18 import com.android.ravenwood.RavenwoodRuntimeNative; 19 import com.android.ravenwood.RavenwoodRuntimeState; 20 import com.android.ravenwood.common.JvmWorkaround; 21 22 import java.io.FileDescriptor; 23 import java.io.FileInputStream; 24 import java.io.IOException; 25 import java.io.InterruptedIOException; 26 import java.nio.ByteBuffer; 27 import java.nio.channels.AsynchronousCloseException; 28 29 /** 30 * OS class replacement used on Ravenwood. For now, we just implement APIs as we need them... 31 * TODO(b/340887115): Need a better integration with libcore. 32 */ 33 public final class Os { Os()34 private Os() {} 35 lseek(FileDescriptor fd, long offset, int whence)36 public static long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException { 37 return RavenwoodRuntimeNative.lseek(fd, offset, whence); 38 } 39 pipe2(int flags)40 public static FileDescriptor[] pipe2(int flags) throws ErrnoException { 41 return RavenwoodRuntimeNative.pipe2(flags); 42 } 43 44 /** Ravenwood version of the OS API. */ pipe()45 public static FileDescriptor[] pipe() throws ErrnoException { 46 return RavenwoodRuntimeNative.pipe2(0); 47 } 48 dup(FileDescriptor fd)49 public static FileDescriptor dup(FileDescriptor fd) throws ErrnoException { 50 return RavenwoodRuntimeNative.dup(fd); 51 } 52 fcntlInt(FileDescriptor fd, int cmd, int arg)53 public static int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException { 54 return RavenwoodRuntimeNative.fcntlInt(fd, cmd, arg); 55 } 56 fstat(FileDescriptor fd)57 public static StructStat fstat(FileDescriptor fd) throws ErrnoException { 58 return RavenwoodRuntimeNative.fstat(fd); 59 } 60 lstat(String path)61 public static StructStat lstat(String path) throws ErrnoException { 62 return RavenwoodRuntimeNative.lstat(path); 63 } 64 stat(String path)65 public static StructStat stat(String path) throws ErrnoException { 66 return RavenwoodRuntimeNative.stat(path); 67 } 68 69 /** Ravenwood version of the OS API. */ close(FileDescriptor fd)70 public static void close(FileDescriptor fd) throws ErrnoException { 71 try { 72 JvmWorkaround.getInstance().closeFd(fd); 73 } catch (IOException e) { 74 // The only valid error on Linux that can happen is EIO 75 throw new ErrnoException("close", OsConstants.EIO); 76 } 77 } 78 open(String path, int flags, int mode)79 public static FileDescriptor open(String path, int flags, int mode) throws ErrnoException { 80 return RavenwoodRuntimeNative.open(path, flags, mode); 81 } 82 83 /** Ravenwood version of the OS API. */ pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)84 public static int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, 85 long offset) throws ErrnoException, InterruptedIOException { 86 var channel = new FileInputStream(fd).getChannel(); 87 var buf = ByteBuffer.wrap(bytes, byteOffset, byteCount); 88 try { 89 return channel.read(buf, offset); 90 } catch (AsynchronousCloseException e) { 91 throw new InterruptedIOException(e.getMessage()); 92 } catch (IOException e) { 93 // Most likely EIO 94 throw new ErrnoException("pread", OsConstants.EIO, e); 95 } 96 } 97 setenv(String name, String value, boolean overwrite)98 public static void setenv(String name, String value, boolean overwrite) throws ErrnoException { 99 RavenwoodRuntimeNative.setenv(name, value, overwrite); 100 } 101 getpid()102 public static int getpid() { 103 return RavenwoodRuntimeState.sPid; 104 } 105 getuid()106 public static int getuid() { 107 return RavenwoodRuntimeState.sUid; 108 } 109 gettid()110 public static int gettid() { 111 return RavenwoodRuntimeNative.gettid(); 112 } 113 } 114