CernVM-FS  2.9.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 
25 class ClientCtx {
26  public:
28  ThreadLocalStorage(uid_t u, gid_t g, pid_t p)
29  : uid(u), gid(g), pid(p), is_set(true) { }
30  uid_t uid;
31  gid_t gid;
32  pid_t pid;
33  bool is_set;
34  };
35 
36  static ClientCtx *GetInstance();
37  static void CleanupInstance();
38  ~ClientCtx();
39 
40  void Set(uid_t uid, gid_t gid, pid_t pid);
41  void Unset();
42  void Get(uid_t *uid, gid_t *gid, pid_t *pid);
43  bool IsSet();
44 
45  private:
47  static void TlsDestructor(void *data);
48 
49  ClientCtx();
50 
51  pthread_key_t thread_local_storage_;
52  pthread_mutex_t *lock_tls_blocks_;
53  std::vector<ThreadLocalStorage *> tls_blocks_;
54 };
55 
63  public:
64  ClientCtxGuard(uid_t uid, gid_t gid, pid_t pid)
65  : set_on_construction_(false)
66  , old_uid_(-1)
67  , old_gid_(-1)
68  , old_pid_(-1)
69  {
70  // Implementation guarantees old_ctx is not null.
71  ClientCtx *old_ctx = ClientCtx::GetInstance();
72  assert(old_ctx);
73  if (old_ctx->IsSet()) {
74  set_on_construction_ = true;
75  old_ctx->Get(&old_uid_, &old_gid_, &old_pid_);
76  }
77  old_ctx->Set(uid, gid, pid);
78  }
79 
83  ctx->Set(old_uid_, old_gid_, old_pid_);
84  } else {
85  ctx->Unset();
86  }
87  }
88 
89  private:
91  uid_t old_uid_;
92  gid_t old_gid_;
93  pid_t old_pid_;
94 };
95 
96 #endif // CVMFS_CLIENTCTX_H_
bool set_on_construction_
Definition: clientctx.h:90
ClientCtxGuard(uid_t uid, gid_t gid, pid_t pid)
Definition: clientctx.h:64
struct cvmcache_context * ctx
pid_t old_pid_
Definition: clientctx.h:93
bool is_set
either not yet set or deliberately unset
Definition: clientctx.h:33
uid_t old_uid_
Definition: clientctx.h:91
std::vector< ThreadLocalStorage * > tls_blocks_
Definition: clientctx.h:53
static ClientCtx * instance_
Definition: clientctx.h:46
ThreadLocalStorage(uid_t u, gid_t g, pid_t p)
Definition: clientctx.h:28
assert((mem||(size==0))&&"Out Of Memory")
bool IsSet()
Definition: clientctx.cc:71
pthread_key_t thread_local_storage_
Definition: clientctx.h:51
void Set(uid_t uid, gid_t gid, pid_t pid)
Definition: clientctx.cc:81
static void TlsDestructor(void *data)
Definition: clientctx.cc:100
pthread_mutex_t * lock_tls_blocks_
Definition: clientctx.h:52
void Unset()
Definition: clientctx.cc:118
~ClientCtx()
Definition: clientctx.cc:31
void Get(uid_t *uid, gid_t *gid, pid_t *pid)
Definition: clientctx.cc:56
static void CleanupInstance()
Definition: clientctx.cc:17
gid_t old_gid_
Definition: clientctx.h:92
static ClientCtx * GetInstance()
Definition: clientctx.cc:44