CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clientctx.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_CLIENTCTX_H_
6 #define CVMFS_CLIENTCTX_H_
7 
8 #include <pthread.h>
9 #include <unistd.h>
10 
11 #include <cassert>
12 #include <vector>
13 
14 class InterruptCue;
15 
31 class ClientCtx {
32  public:
34  ThreadLocalStorage(uid_t u, gid_t g, pid_t p, InterruptCue *ic)
35  : uid(u), gid(g), pid(p), interrupt_cue(ic), is_set(true) { }
36  uid_t uid;
37  gid_t gid;
38  pid_t pid;
40  bool is_set;
41  };
42 
43  static ClientCtx *GetInstance();
44  static void CleanupInstance();
45  ~ClientCtx();
46 
47  void Set(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic);
48  void Unset();
49  void Get(uid_t *uid, gid_t *gid, pid_t *pid, InterruptCue **ic);
50  bool IsSet();
51 
52  private:
54  static void TlsDestructor(void *data);
55 
56  ClientCtx();
57 
58  pthread_key_t thread_local_storage_;
59  pthread_mutex_t *lock_tls_blocks_;
60  std::vector<ThreadLocalStorage *> tls_blocks_;
61 };
62 
70  public:
71  ClientCtxGuard(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic)
72  : set_on_construction_(false)
73  , old_uid_(-1)
74  , old_gid_(-1)
75  , old_pid_(-1)
76  , old_interrupt_cue_(NULL)
77  {
78  // Implementation guarantees old_ctx is not null.
79  ClientCtx *old_ctx = ClientCtx::GetInstance();
80  assert(old_ctx);
81  if (old_ctx->IsSet()) {
82  set_on_construction_ = true;
84  }
85  old_ctx->Set(uid, gid, pid, ic);
86  }
87 
92  } else {
93  ctx->Unset();
94  }
95  }
96 
97  private:
99  uid_t old_uid_;
100  gid_t old_gid_;
101  pid_t old_pid_;
103 };
104 
105 #endif // CVMFS_CLIENTCTX_H_
bool set_on_construction_
Definition: clientctx.h:98
ClientCtxGuard(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic)
Definition: clientctx.h:71
struct cvmcache_context * ctx
pid_t old_pid_
Definition: clientctx.h:101
bool is_set
either not yet set or deliberately unset
Definition: clientctx.h:40
void Get(uid_t *uid, gid_t *gid, pid_t *pid, InterruptCue **ic)
Definition: clientctx.cc:57
uid_t old_uid_
Definition: clientctx.h:99
std::vector< ThreadLocalStorage * > tls_blocks_
Definition: clientctx.h:60
static ClientCtx * instance_
Definition: clientctx.h:53
InterruptCue * old_interrupt_cue_
Definition: clientctx.h:102
assert((mem||(size==0))&&"Out Of Memory")
ThreadLocalStorage(uid_t u, gid_t g, pid_t p, InterruptCue *ic)
Definition: clientctx.h:34
bool IsSet()
Definition: clientctx.cc:74
InterruptCue * interrupt_cue
A non-owning pointer.
Definition: clientctx.h:39
pthread_key_t thread_local_storage_
Definition: clientctx.h:58
static void TlsDestructor(void *data)
Definition: clientctx.cc:104
pthread_mutex_t * lock_tls_blocks_
Definition: clientctx.h:59
void Unset()
Definition: clientctx.cc:122
~ClientCtx()
Definition: clientctx.cc:32
void Set(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic)
Definition: clientctx.cc:84
static void CleanupInstance()
Definition: clientctx.cc:18
gid_t old_gid_
Definition: clientctx.h:100
static ClientCtx * GetInstance()
Definition: clientctx.cc:45