Skip to content

Commit

Permalink
[dlopen] compress: fix load/unload of libraries and add extensive test
Browse files Browse the repository at this point in the history
each handle should only unload libraries that has loaded or referenced
on exit, attempt to close all libraries from that handle
on compress_fini close only the one that's open

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Sep 14, 2017
1 parent 3902348 commit 115b403
Show file tree
Hide file tree
Showing 6 changed files with 831 additions and 16 deletions.
44 changes: 31 additions & 13 deletions libknet/compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ static int compress_load_lib(knet_handle_t knet_h, int cmp_model, int rate_limit
}

compress_modules_cmds[cmp_model].libref++;
knet_h->compress_activated[cmp_model] = 1;

return 0;
}
Expand All @@ -237,6 +238,7 @@ int compress_init(
return -1;
}

memset(knet_h->compress_activated, 0, KNET_MAX_COMPRESS_METHODS);
memset(&last_load_failure, 0, sizeof(struct timespec));

return 0;
Expand Down Expand Up @@ -310,6 +312,9 @@ int compress_cfg(
err = -1;
goto out_unlock;
}
} else {
compress_modules_cmds[cmp_model].libref++;
knet_h->compress_activated[cmp_model] = 1;
}

if (val_level(knet_h, cmp_model, knet_handle_compress_cfg->compress_level) < 0) {
Expand All @@ -327,14 +332,17 @@ int compress_cfg(
if (!err) {
knet_h->compress_model = cmp_model;
knet_h->compress_level = knet_handle_compress_cfg->compress_level;
} else {
knet_h->compress_model = 0;
}

errno = savederrno;
return err;
}

void compress_fini(
knet_handle_t knet_h)
knet_handle_t knet_h,
int all)
{
int savederrno = 0;
int idx = 0;
Expand All @@ -349,19 +357,24 @@ void compress_fini(
while (compress_modules_cmds[idx].model_name != NULL) {
if ((compress_modules_cmds[idx].built_in == 1) &&
(compress_modules_cmds[idx].loaded == 1) &&
(compress_modules_cmds[idx].model_id > 0) &&
(knet_h->compress_activated[compress_modules_cmds[idx].model_id] == 1) &&
(idx < KNET_MAX_COMPRESS_METHODS)) {
if (compress_modules_cmds[idx].fini != NULL) {
compress_modules_cmds[idx].fini(knet_h, idx);
} else {
knet_h->compress_int_data[idx] = NULL;
}
compress_modules_cmds[idx].libref--;

if ((compress_modules_cmds[idx].libref == 0) &&
(compress_modules_cmds[idx].loaded == 1)) {
log_debug(knet_h, KNET_SUB_COMPRESS, "Unloading %s library", compress_modules_cmds[idx].model_name);
compress_modules_cmds[idx].unload_lib(knet_h);
compress_modules_cmds[idx].loaded = 0;
if ((all) || (compress_modules_cmds[idx].model_id == knet_h->compress_model)) {
if (compress_modules_cmds[idx].fini != NULL) {
compress_modules_cmds[idx].fini(knet_h, idx);
} else {
knet_h->compress_int_data[idx] = NULL;
}
compress_modules_cmds[idx].libref--;
knet_h->compress_activated[compress_modules_cmds[idx].model_id] = 0;

if ((compress_modules_cmds[idx].libref == 0) &&
(compress_modules_cmds[idx].loaded == 1)) {
log_debug(knet_h, KNET_SUB_COMPRESS, "Unloading %s library", compress_modules_cmds[idx].model_name);
compress_modules_cmds[idx].unload_lib(knet_h);
compress_modules_cmds[idx].loaded = 0;
}
}
}
idx++;
Expand Down Expand Up @@ -436,6 +449,11 @@ int decompress(
strerror(savederrno));
goto out_unlock;
}
} else {
if (!knet_h->compress_activated[compress_model]) {
compress_modules_cmds[compress_model].libref++;
knet_h->compress_activated[compress_model] = 1;
}
}

err = compress_modules_cmds[compress_model].decompress(knet_h, buf_in, buf_in_len, buf_out, buf_out_len);
Expand Down
3 changes: 2 additions & 1 deletion libknet/compress.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ int compress_init(
knet_handle_t knet_h);

void compress_fini(
knet_handle_t knet_h);
knet_handle_t knet_h,
int all);

int compress(
knet_handle_t knet_h,
Expand Down
4 changes: 2 additions & 2 deletions libknet/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ int knet_handle_free(knet_handle_t knet_h)
_destroy_buffers(knet_h);
_close_socks(knet_h);
crypto_fini(knet_h);
compress_fini(knet_h);
compress_fini(knet_h, 1);
_destroy_locks(knet_h);

exit_nolock:
Expand Down Expand Up @@ -1405,7 +1405,7 @@ int knet_handle_compress(knet_handle_t knet_h, struct knet_handle_compress_cfg *
return -1;
}

compress_fini(knet_h);
compress_fini(knet_h, 0);
err = compress_cfg(knet_h, knet_handle_compress_cfg);
savederrno = errno;

Expand Down
1 change: 1 addition & 0 deletions libknet/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ struct knet_handle {
int compress_level;
uint32_t compress_threshold;
void *compress_int_data[KNET_MAX_COMPRESS_METHODS]; /* for compress method private data */
uint8_t compress_activated[KNET_MAX_COMPRESS_METHODS]; /* track active compression library used by this handle */
unsigned char *recv_from_links_buf_decompress;
unsigned char *send_to_links_buf_compress;
seq_num_t tx_seq_num;
Expand Down
4 changes: 4 additions & 0 deletions libknet/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ check_PROGRAMS = \
$(fun_checks)

int_checks = \
int_lib_load_unload \
int_crypto_test \
int_timediff_test

Expand Down Expand Up @@ -61,6 +62,9 @@ check-api-test-coverage:

pckt_test_SOURCES = pckt_test.c

int_lib_load_unload_SOURCES = int_lib_load_unload.c \
test-common.c

int_crypto_test_SOURCES = int_crypto.c \
../common.c \
../crypto.c \
Expand Down

0 comments on commit 115b403

Please sign in to comment.