• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
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 BASE_TEST_SCOPED_DEV_ZERO_FUCHSIA_H_
6 #define BASE_TEST_SCOPED_DEV_ZERO_FUCHSIA_H_
7 
8 #include <lib/fdio/namespace.h>
9 
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/threading/sequence_bound.h"
13 #include "base/threading/thread.h"
14 
15 namespace base {
16 
17 // An object that causes /dev/zero to exist during its lifetime. A reference to
18 // this class may be held by tests that require access to /dev/zero for the
19 // lifetime of that need.
20 class ScopedDevZero final : public RefCounted<ScopedDevZero> {
21  public:
22   REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
23 
24   // Returns a reference to the process-global /dev/zero. This must only be
25   // called, and the returned reference released, on the main thread. Returns
26   // null in case of failure to create the instance. It is good practice for
27   // tests to ASSERT the returned pointer.
28   static scoped_refptr<ScopedDevZero> Get();
29 
30   ScopedDevZero(const ScopedDevZero&) = delete;
31   ScopedDevZero operator=(const ScopedDevZero&) = delete;
32 
33  private:
34   friend class RefCounted<ScopedDevZero>;
35   class Server;
36 
37   ScopedDevZero();
38   ~ScopedDevZero();
39 
40   // Spins off the server thread and binds its pesudo-dir to /dev, returning
41   // true if all goes well, or false in case of any error.
42   bool Initialize();
43 
44   // A raw pointer to the process's single instance. Multiple references to this
45   // instance may be handed out to consumers.
46   static ScopedDevZero* instance_;
47   Thread io_thread_;
48   fdio_ns_t* global_namespace_ = nullptr;
49   SequenceBound<Server> server_;
50 };
51 
52 }  // namespace base
53 
54 #endif  // BASE_TEST_SCOPED_DEV_ZERO_FUCHSIA_H_
55