Directory: | cvmfs/ |
---|---|
File: | cvmfs/clientctx.cc |
Date: | 2025-02-02 02:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 65 | 76 | 85.5% |
Branches: | 22 | 40 | 55.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | #include "clientctx.h" | ||
6 | |||
7 | #include <cassert> | ||
8 | |||
9 | #include "interrupt.h" | ||
10 | #include "util/concurrency.h" | ||
11 | #include "util/smalloc.h" | ||
12 | |||
13 | using namespace std; // NOLINT | ||
14 | |||
15 | ClientCtx *ClientCtx::instance_ = NULL; | ||
16 | |||
17 | |||
18 | 78 | void ClientCtx::CleanupInstance() { | |
19 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 12 times.
|
78 | delete instance_; |
20 | 78 | instance_ = NULL; | |
21 | 78 | } | |
22 | |||
23 | |||
24 | 68 | ClientCtx::ClientCtx() { | |
25 | 68 | lock_tls_blocks_ = reinterpret_cast<pthread_mutex_t *>( | |
26 | 68 | smalloc(sizeof(pthread_mutex_t))); | |
27 | 68 | int retval = pthread_mutex_init(lock_tls_blocks_, NULL); | |
28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | assert(retval == 0); |
29 | 68 | } | |
30 | |||
31 | |||
32 | 66 | ClientCtx::~ClientCtx() { | |
33 | 66 | pthread_mutex_destroy(lock_tls_blocks_); | |
34 | 66 | free(lock_tls_blocks_); | |
35 | |||
36 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 66 times.
|
75 | for (unsigned i = 0; i < tls_blocks_.size(); ++i) { |
37 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | delete tls_blocks_[i]; |
38 | } | ||
39 | |||
40 | 66 | int retval = pthread_key_delete(thread_local_storage_); | |
41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | assert(retval == 0); |
42 | 66 | } | |
43 | |||
44 | |||
45 | 206 | ClientCtx *ClientCtx::GetInstance() { | |
46 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 138 times.
|
206 | if (instance_ == NULL) { |
47 | 68 | instance_ = new ClientCtx(); | |
48 | int retval = | ||
49 | 68 | pthread_key_create(&instance_->thread_local_storage_, TlsDestructor); | |
50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | assert(retval == 0); |
51 | } | ||
52 | |||
53 | 206 | return instance_; | |
54 | } | ||
55 | |||
56 | |||
57 | 13 | void ClientCtx::Get(uid_t *uid, gid_t *gid, pid_t *pid, InterruptCue **ic) { | |
58 | ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>( | ||
59 | 13 | pthread_getspecific(thread_local_storage_)); | |
60 |
4/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 11 times.
|
13 | if ((tls == NULL) || !tls->is_set) { |
61 | 2 | *uid = -1; | |
62 | 2 | *gid = -1; | |
63 | 2 | *pid = -1; | |
64 | 2 | *ic = NULL; | |
65 | } else { | ||
66 | 11 | *uid = tls->uid; | |
67 | 11 | *gid = tls->gid; | |
68 | 11 | *pid = tls->pid; | |
69 | 11 | *ic = tls->interrupt_cue; | |
70 | } | ||
71 | 13 | } | |
72 | |||
73 | |||
74 | 92 | bool ClientCtx::IsSet() { | |
75 | ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>( | ||
76 | 92 | pthread_getspecific(thread_local_storage_)); | |
77 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 42 times.
|
92 | if (tls == NULL) |
78 | 50 | return false; | |
79 | |||
80 | 42 | return tls->is_set; | |
81 | } | ||
82 | |||
83 | |||
84 | 42 | void ClientCtx::Set(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic) { | |
85 | ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>( | ||
86 | 42 | pthread_getspecific(thread_local_storage_)); | |
87 | |||
88 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33 times.
|
42 | if (tls == NULL) { |
89 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | tls = new ThreadLocalStorage(uid, gid, pid, ic); |
90 | 9 | int retval = pthread_setspecific(thread_local_storage_, tls); | |
91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | assert(retval == 0); |
92 | 9 | MutexLockGuard lock_guard(lock_tls_blocks_); | |
93 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | tls_blocks_.push_back(tls); |
94 | 9 | } else { | |
95 | 33 | tls->uid = uid; | |
96 | 33 | tls->gid = gid; | |
97 | 33 | tls->pid = pid; | |
98 | 33 | tls->interrupt_cue = ic; | |
99 | 33 | tls->is_set = true; | |
100 | } | ||
101 | 42 | } | |
102 | |||
103 | |||
104 | ✗ | void ClientCtx::TlsDestructor(void *data) { | |
105 | ✗ | ThreadLocalStorage *tls = static_cast<ClientCtx::ThreadLocalStorage *>(data); | |
106 | ✗ | delete tls; | |
107 | |||
108 | ✗ | assert(instance_); | |
109 | ✗ | MutexLockGuard lock_guard(instance_->lock_tls_blocks_); | |
110 | ✗ | for (vector<ThreadLocalStorage *>::iterator i = | |
111 | ✗ | instance_->tls_blocks_.begin(), iEnd = instance_->tls_blocks_.end(); | |
112 | ✗ | i != iEnd; ++i) | |
113 | { | ||
114 | ✗ | if ((*i) == tls) { | |
115 | ✗ | instance_->tls_blocks_.erase(i); | |
116 | ✗ | break; | |
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
121 | |||
122 | 36 | void ClientCtx::Unset() { | |
123 | ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>( | ||
124 | 36 | pthread_getspecific(thread_local_storage_)); | |
125 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (tls != NULL) { |
126 | 36 | tls->is_set = false; | |
127 | 36 | tls->uid = -1; | |
128 | 36 | tls->gid = -1; | |
129 | 36 | tls->pid = -1; | |
130 | 36 | tls->interrupt_cue = NULL; | |
131 | } | ||
132 | 36 | } | |
133 |