CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clientctx.cc
Go to the documentation of this file.
1 
5 #include "clientctx.h"
6 
7 #include <cassert>
8 
9 #include "smalloc.h"
10 #include "util_concurrency.h"
11 
12 using namespace std; // NOLINT
13 
15 
16 
18  delete instance_;
19  instance_ = NULL;
20 }
21 
22 
24  lock_tls_blocks_ = reinterpret_cast<pthread_mutex_t *>(
25  smalloc(sizeof(pthread_mutex_t)));
26  int retval = pthread_mutex_init(lock_tls_blocks_, NULL);
27  assert(retval == 0);
28 }
29 
30 
32  pthread_mutex_destroy(lock_tls_blocks_);
33  free(lock_tls_blocks_);
34 
35  for (unsigned i = 0; i < tls_blocks_.size(); ++i) {
36  delete tls_blocks_[i];
37  }
38 
39  int retval = pthread_key_delete(thread_local_storage_);
40  assert(retval == 0);
41 }
42 
43 
45  if (instance_ == NULL) {
46  instance_ = new ClientCtx();
47  int retval =
48  pthread_key_create(&instance_->thread_local_storage_, TlsDestructor);
49  assert(retval == 0);
50  }
51 
52  return instance_;
53 }
54 
55 
56 void ClientCtx::Get(uid_t *uid, gid_t *gid, pid_t *pid) {
57  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
58  pthread_getspecific(thread_local_storage_));
59  if ((tls == NULL) || !tls->is_set) {
60  *uid = -1;
61  *gid = -1;
62  *pid = -1;
63  } else {
64  *uid = tls->uid;
65  *gid = tls->gid;
66  *pid = tls->pid;
67  }
68 }
69 
70 
72  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
73  pthread_getspecific(thread_local_storage_));
74  if (tls == NULL)
75  return false;
76 
77  return tls->is_set;
78 }
79 
80 
81 void ClientCtx::Set(uid_t uid, gid_t gid, pid_t pid) {
82  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
83  pthread_getspecific(thread_local_storage_));
84 
85  if (tls == NULL) {
86  tls = new ThreadLocalStorage(uid, gid, pid);
87  int retval = pthread_setspecific(thread_local_storage_, tls);
88  assert(retval == 0);
89  MutexLockGuard lock_guard(lock_tls_blocks_);
90  tls_blocks_.push_back(tls);
91  } else {
92  tls->uid = uid;
93  tls->gid = gid;
94  tls->pid = pid;
95  tls->is_set = true;
96  }
97 }
98 
99 
100 void ClientCtx::TlsDestructor(void *data) {
101  ThreadLocalStorage *tls = static_cast<ClientCtx::ThreadLocalStorage *>(data);
102  delete tls;
103 
104  assert(instance_);
105  MutexLockGuard lock_guard(instance_->lock_tls_blocks_);
106  for (vector<ThreadLocalStorage *>::iterator i =
107  instance_->tls_blocks_.begin(), iEnd = instance_->tls_blocks_.end();
108  i != iEnd; ++i)
109  {
110  if ((*i) == tls) {
111  instance_->tls_blocks_.erase(i);
112  break;
113  }
114  }
115 }
116 
117 
119  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
120  pthread_getspecific(thread_local_storage_));
121  if (tls != NULL) {
122  tls->is_set = false;
123  tls->uid = -1;
124  tls->gid = -1;
125  tls->pid = -1;
126  }
127 }
bool is_set
either not yet set or deliberately unset
Definition: clientctx.h:33
static ClientCtx * instance_
Definition: clientctx.h:46
assert((mem||(size==0))&&"Out Of Memory")
bool IsSet()
Definition: clientctx.cc:71
void Set(uid_t uid, gid_t gid, pid_t pid)
Definition: clientctx.cc:81
static void TlsDestructor(void *data)
Definition: clientctx.cc:100
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
static ClientCtx * GetInstance()
Definition: clientctx.cc:44