Home | History | Annotate | Download | only in nacl_io
      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_OBJECT_H_
      6 #define LIBRARIES_NACL_IO_KERNEL_OBJECT_H_
      7 
      8 #include <pthread.h>
      9 
     10 #include <map>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "nacl_io/error.h"
     15 #include "nacl_io/kernel_handle.h"
     16 #include "nacl_io/mount.h"
     17 #include "nacl_io/mount_node.h"
     18 #include "nacl_io/path.h"
     19 
     20 #include "sdk_util/simple_lock.h"
     21 
     22 namespace nacl_io {
     23 
     24 // KernelObject provides basic functionality for threadsafe access to kernel
     25 // objects such as the CWD, mount points, file descriptors and file handles.
     26 // All Kernel locks are private to ensure the lock order.
     27 //
     28 // All calls are assumed to be a relative path.
     29 class KernelObject {
     30  public:
     31   typedef std::vector<ScopedKernelHandle> HandleMap_t;
     32   typedef std::map<std::string, ScopedMount> MountMap_t;
     33 
     34   KernelObject();
     35   virtual ~KernelObject();
     36 
     37   // Attach the given Mount object at the specified path.
     38   Error AttachMountAtPath(const ScopedMount& mnt, const std::string& path);
     39 
     40   // Unmap the Mount object from the specified path and release it.
     41   Error DetachMountAtPath(const std::string& path);
     42 
     43   // Find the mount for the given path, and acquires it and return a
     44   // path relative to the mount.
     45   // Assumes |out_mount| and |rel_path| are non-NULL.
     46   Error AcquireMountAndRelPath(const std::string& path,
     47                                ScopedMount* out_mount,
     48                                Path* rel_path);
     49 
     50   // Find the mount and node for the given path, acquiring/creating it as
     51   // specified by the |oflags|.
     52   // Assumes |out_mount| and |out_node| are non-NULL.
     53   Error AcquireMountAndNode(const std::string& path,
     54                             int oflags,
     55                             ScopedMount* out_mount,
     56                             ScopedMountNode* out_node);
     57 
     58   // Convert from FD to KernelHandle, and acquire the handle.
     59   // Assumes |out_handle| is non-NULL.
     60   Error AcquireHandle(int fd, ScopedKernelHandle* out_handle);
     61 
     62   // Allocate a new fd and assign the handle to it, while
     63   // ref counting the handle and associated mount.
     64   // Assumes |handle| is non-NULL;
     65   int AllocateFD(const ScopedKernelHandle& handle);
     66 
     67   // Assumes |handle| is non-NULL;
     68   void FreeAndReassignFD(int fd, const ScopedKernelHandle& handle);
     69   void FreeFD(int fd);
     70 
     71   // Returns or sets CWD
     72   std::string GetCWD();
     73   Error SetCWD(const std::string& path);
     74 
     75   // Returns parts of the absolute path for the given relative path
     76   Path GetAbsParts(const std::string& path);
     77 
     78 private:
     79   std::string cwd_;
     80   std::vector<int> free_fds_;
     81   HandleMap_t handle_map_;
     82   MountMap_t mounts_;
     83 
     84   // Lock to protect free_fds_ and handle_map_.
     85   sdk_util::SimpleLock handle_lock_;
     86 
     87   // Lock to protect handle_map_.
     88   sdk_util::SimpleLock mount_lock_;
     89 
     90   // Lock to protect cwd_.
     91   sdk_util::SimpleLock cwd_lock_;
     92 
     93   DISALLOW_COPY_AND_ASSIGN(KernelObject);
     94 };
     95 
     96 }  // namespace nacl_io
     97 
     98 #endif  // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_
     99