Skip to content

Commit 8eb063e

Browse files
committed
src/bindings: add keep_on_reload field to struct pidns_store
Let's add keep_on_reload field to struct pidns_store. The idea behind it is that if this flag is set to true, then pidns_store entry won't be considered as a cache item which can be dropped. But instead, it will be kept across liblxcfs reloads and droped only if a pid namespace it refers die. Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
1 parent 3322ab4 commit 8eb063e

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/bindings.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ static void remove_initpid(struct pidns_store *entry)
224224
}
225225
}
226226

227+
static bool keep_pidns_entry(struct pidns_store *entry)
228+
{
229+
return (entry->version >= 1) && entry->keep_on_reload &&
230+
initpid_still_valid(entry);
231+
}
232+
227233
#define PURGE_SECS 5
228234
/* Must be called under store_lock */
229235
static void prune_initpid_store(void)
@@ -250,10 +256,11 @@ static void prune_initpid_store(void)
250256

251257
for (int i = 0; i < PIDNS_HASH_SIZE; i++) {
252258
for (struct pidns_store *entry = pidns_hash_table[i], *prev = NULL; entry;) {
253-
if (entry->lastcheck < threshold) {
254-
struct pidns_store *cur = entry;
259+
struct pidns_store *cur = entry;
255260

256-
lxcfs_debug("Removed cache entry for pid %d to init pid cache", cur->initpid);
261+
if ((entry->lastcheck < threshold) &&
262+
!keep_pidns_entry(cur)) {
263+
lxcfs_debug("Removed cache entry for pid %d from init pid cache", cur->initpid);
257264

258265
if (prev)
259266
prev->next = entry->next;
@@ -263,6 +270,8 @@ static void prune_initpid_store(void)
263270
close_prot_errno_disarm(cur->init_pidfd);
264271
free_disarm(cur);
265272
} else {
273+
lxcfs_debug("Kept cache entry for pid %d in init pid cache", cur->initpid);
274+
266275
prev = entry;
267276
entry = entry->next;
268277
}
@@ -277,15 +286,25 @@ static void clear_initpid_store(void)
277286

278287
store_lock();
279288
for (int i = 0; i < PIDNS_HASH_SIZE; i++) {
280-
for (struct pidns_store *entry = pidns_hash_table[i]; entry;) {
289+
for (struct pidns_store *entry = pidns_hash_table[i], *prev = NULL; entry;) {
281290
struct pidns_store *cur = entry;
282291

283-
lxcfs_debug("Removed cache entry for pid %d to init pid cache", cur->initpid);
292+
if (keep_pidns_entry(cur)) {
293+
lxcfs_debug("Kept cache entry for pid %d in init pid cache", cur->initpid);
294+
295+
prev = entry;
296+
entry = entry->next;
297+
} else {
298+
lxcfs_debug("Removed cache entry for pid %d from init pid cache", cur->initpid);
284299

285-
pidns_hash_table[i] = entry->next;
286-
entry = entry->next;
287-
close_prot_errno_disarm(cur->init_pidfd);
288-
free_disarm(cur);
300+
if (prev)
301+
prev->next = entry->next;
302+
else
303+
pidns_hash_table[i] = entry->next;
304+
entry = entry->next;
305+
close_prot_errno_disarm(cur->init_pidfd);
306+
free_disarm(cur);
307+
}
289308
}
290309
}
291310
store_unlock();
@@ -320,13 +339,14 @@ static void save_initpid(ino_t pidns_inode, pid_t pid)
320339

321340
ino_hash = HASH(pidns_inode);
322341
*entry = (struct pidns_store){
323-
.version = 0,
342+
.version = 1,
324343
.ino = pidns_inode,
325344
.initpid = pid,
326345
.ctime = st.st_ctime,
327346
.next = pidns_hash_table[ino_hash],
328347
.lastcheck = time(NULL),
329348
.init_pidfd = move_fd(pidfd),
349+
.keep_on_reload = false,
330350
};
331351
pidns_hash_table[ino_hash] = move_ptr(entry);
332352

src/bindings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ struct pidns_store {
136136
int init_pidfd;
137137
int64_t ctime; /* the time at which /proc/$initpid was created */
138138
int64_t lastcheck;
139+
140+
/* Do not free on liblxcfs reload (contains useful persistent data) */
141+
bool keep_on_reload;
139142
};
140143

141144
/* lol - look at how they are allocated in the kernel */

0 commit comments

Comments
 (0)