1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef LIBRARIES_NACL_IO_KERNEL_HANDLE_H_ 6 #define LIBRARIES_NACL_IO_KERNEL_HANDLE_H_ 7 8 #include <fcntl.h> 9 #include <pthread.h> 10 #include <ppapi/c/pp_resource.h> 11 12 #include "nacl_io/error.h" 13 #include "nacl_io/mount.h" 14 #include "nacl_io/mount_node.h" 15 #include "nacl_io/ossocket.h" 16 #include "nacl_io/ostypes.h" 17 18 #include "sdk_util/macros.h" 19 #include "sdk_util/ref_object.h" 20 #include "sdk_util/scoped_ref.h" 21 #include "sdk_util/simple_lock.h" 22 23 namespace nacl_io { 24 25 class MountNodeSocket; 26 27 // HandleAttr struct is passed the MountNode in calls 28 // to Read and Write. It contains handle specific state 29 // such as the file offset and the open flags. 30 struct HandleAttr { 31 HandleAttr() : offs(0), flags(0) {} 32 bool IsBlocking() const { return !(flags & O_NONBLOCK); } 33 34 size_t offs; 35 int flags; 36 }; 37 38 // KernelHandle provides a reference counted container for the open 39 // file information, such as it's mount, node, access type and offset. 40 // KernelHandle can only be referenced when the KernelProxy lock is held. 41 class KernelHandle : public sdk_util::RefObject { 42 public: 43 44 KernelHandle(); 45 KernelHandle(const ScopedMount& mnt, const ScopedMountNode& node); 46 ~KernelHandle(); 47 48 Error Init(int open_flags); 49 50 Error Accept(PP_Resource* new_sock, struct sockaddr* addr, socklen_t* len); 51 Error Connect(const struct sockaddr* addr, socklen_t len); 52 Error Fcntl(int request, int* result, ...); 53 Error VFcntl(int request, int* result, va_list args); 54 Error GetDents(struct dirent* pdir, size_t count, int* bytes_written); 55 Error Read(void* buf, size_t nbytes, int* bytes_read); 56 Error Recv(void* buf, size_t len, int flags, int* out_len); 57 Error RecvFrom(void* buf, 58 size_t len, 59 int flags, 60 struct sockaddr* src_addr, 61 socklen_t* addrlen, 62 int* out_len); 63 // Assumes |out_offset| is non-NULL. 64 Error Seek(off_t offset, int whence, off_t* out_offset); 65 Error Send(const void* buf, size_t len, int flags, int* out_len); 66 Error SendTo(const void* buf, 67 size_t len, 68 int flags, 69 const struct sockaddr* dest_addr, 70 socklen_t addrlen, 71 int* out_len); 72 Error Write(const void* buf, size_t nbytes, int* bytes_written); 73 74 const ScopedMountNode& node() { return node_; } 75 const ScopedMount& mount() { return mount_; } 76 77 const HandleAttr& Attr() { return handle_attr_; } 78 79 int OpenMode() { return handle_attr_.flags & 3; } 80 81 private: 82 // Returns the MountNodeSocket* if this node is a socket otherwise returns 83 // NULL. 84 MountNodeSocket* socket_node(); 85 86 ScopedMount mount_; 87 ScopedMountNode node_; 88 sdk_util::SimpleLock handle_lock_; 89 HandleAttr handle_attr_; 90 91 friend class KernelProxy; 92 DISALLOW_COPY_AND_ASSIGN(KernelHandle); 93 }; 94 95 typedef sdk_util::ScopedRef<KernelHandle> ScopedKernelHandle; 96 97 } // namespace nacl_io 98 99 #endif // LIBRARIES_NACL_IO_KERNEL_HANDLE_H_ 100