commit d929fe8470e99f4dcbbd889e7aa87e147d0d5b48 Author: Ingo Franzki Date: Fri Feb 12 11:25:21 2021 +0100 Externalize linked list functions Externalize the linked list functions (dlist_xxx), so that they can also be used on pkcsslotd. Signed-off-by: Ingo Franzki diff --git a/usr/lib/cca_stdll/cca_stdll.mk b/usr/lib/cca_stdll/cca_stdll.mk index bd230b9f..c5e86fa7 100644 --- a/usr/lib/cca_stdll/cca_stdll.mk +++ b/usr/lib/cca_stdll/cca_stdll.mk @@ -35,7 +35,8 @@ opencryptoki_stdll_libpkcs11_cca_la_SOURCES = \ usr/lib/common/mech_ssl3.c usr/lib/common/verify_mgr.c \ usr/lib/common/p11util.c usr/lib/common/sw_crypt.c \ usr/lib/common/shared_memory.c usr/lib/common/profile_obj.c \ - usr/lib/cca_stdll/cca_specific.c usr/lib/common/attributes.c + usr/lib/cca_stdll/cca_specific.c usr/lib/common/attributes.c \ + usr/lib/common/dlist.c if ENABLE_LOCKS opencryptoki_stdll_libpkcs11_cca_la_SOURCES += \ diff --git a/usr/lib/common/dlist.c b/usr/lib/common/dlist.c new file mode 100644 index 00000000..1fee1ea9 --- /dev/null +++ b/usr/lib/common/dlist.c @@ -0,0 +1,218 @@ +/* + * COPYRIGHT (c) International Business Machines Corp. 2021 + * + * This program is provided under the terms of the Common Public License, + * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this + * software constitutes recipient's acceptance of CPL-1.0 terms which can be + * found in the file LICENSE file or at + * https://opensource.org/licenses/cpl1.0.php + */ + +#include +#include +#include +#include +#include +#include + +#include "dlist.h" +#include "host_defs.h" +#include "h_extern.h" + + +// Function: dlist_add_as_first() +// +// Adds the specified node to the start of the list +// +// Returns: pointer to the start of the list +// +DL_NODE *dlist_add_as_first(DL_NODE *list, void *data) +{ + DL_NODE *node = NULL; + + if (!data) + return list; + + node = (DL_NODE *) malloc(sizeof(DL_NODE)); + if (!node) + return NULL; + + node->data = data; + node->prev = NULL; + node->next = list; + if (list) + list->prev = node; + + return node; +} + +// Function: dlist_add_as_last() +// +// Adds the specified node to the end of the list +// +// Returns: pointer to the start of the list +// +DL_NODE *dlist_add_as_last(DL_NODE *list, void *data) +{ + DL_NODE *node = NULL; + + if (!data) + return list; + + node = (DL_NODE *) malloc(sizeof(DL_NODE)); + if (!node) + return NULL; + + node->data = data; + node->next = NULL; + + if (!list) { + node->prev = NULL; + return node; + } else { + DL_NODE *temp = dlist_get_last(list); + temp->next = node; + node->prev = temp; + + return list; + } +} + +// Function: dlist_find() +// +DL_NODE *dlist_find(DL_NODE *list, void *data) +{ + DL_NODE *node = list; + + while (node && node->data != data) + node = node->next; + + return node; +} + +// Function: dlist_get_first() +// +// Returns the last node in the list or NULL if list is empty +// +DL_NODE *dlist_get_first(DL_NODE *list) +{ + DL_NODE *temp = list; + + if (!list) + return NULL; + + while (temp->prev != NULL) + temp = temp->prev; + + return temp; +} + +// Function: dlist_get_last() +// +// Returns the last node in the list or NULL if list is empty +// +DL_NODE *dlist_get_last(DL_NODE *list) +{ + DL_NODE *temp = list; + + if (!list) + return NULL; + + while (temp->next != NULL) + temp = temp->next; + + return temp; +} + +// +// +CK_ULONG dlist_length(DL_NODE *list) +{ + DL_NODE *temp = list; + CK_ULONG len = 0; + + while (temp) { + len++; + temp = temp->next; + } + + return len; +} + +// +// +DL_NODE *dlist_next(DL_NODE *node) +{ + if (!node) + return NULL; + + return node->next; +} + +// +// +DL_NODE *dlist_prev(DL_NODE *node) +{ + if (!node) + return NULL; + + return node->prev; +} + +// +// +void dlist_purge(DL_NODE *list) +{ + DL_NODE *node; + + if (!list) + return; + + do { + node = list->next; + free(list); + list = node; + } while (list); +} + +// Function: dlist_remove_node() +// +// Attempts to remove the specified node from the list. The caller is +// responsible for freeing the data associated with the node prior to +// calling this routine +// +DL_NODE *dlist_remove_node(DL_NODE *list, DL_NODE *node) +{ + DL_NODE *temp = list; + + if (!list || !node) + return NULL; + + // special case: removing head of the list + // + if (list == node) { + temp = list->next; + if (temp) + temp->prev = NULL; + + free(list); + return temp; + } + // we have no guarantee that the node is in the list + // so search through the list to find it + // + while ((temp != NULL) && (temp->next != node)) + temp = temp->next; + + if (temp != NULL) { + DL_NODE *next = node->next; + + temp->next = next; + if (next) + next->prev = temp; + + free(node); + } + + return list; +} diff --git a/usr/lib/common/dlist.h b/usr/lib/common/dlist.h new file mode 100644 index 00000000..eda4af9c --- /dev/null +++ b/usr/lib/common/dlist.h @@ -0,0 +1,32 @@ +/* + * COPYRIGHT (c) International Business Machines Corp. 2021 + * + * This program is provided under the terms of the Common Public License, + * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this + * software constitutes recipient's acceptance of CPL-1.0 terms which can be + * found in the file LICENSE file or at + * https://opensource.org/licenses/cpl1.0.php + */ + + + +#ifndef _DLIST_H_ +#define _DLIST_H_ + +#include "pkcs11types.h" +#include "defs.h" + +// linked-list routines +// +DL_NODE *dlist_add_as_first(DL_NODE *list, void *data); +DL_NODE *dlist_add_as_last(DL_NODE *list, void *data); +DL_NODE *dlist_find(DL_NODE *list, void *data); +DL_NODE *dlist_get_first(DL_NODE *list); +DL_NODE *dlist_get_last(DL_NODE *list); +CK_ULONG dlist_length(DL_NODE *list); +DL_NODE *dlist_next(DL_NODE *list); +DL_NODE *dlist_prev(DL_NODE *list); +void dlist_purge(DL_NODE *list); +DL_NODE *dlist_remove_node(DL_NODE *list, DL_NODE *node); + +#endif diff --git a/usr/lib/common/h_extern.h b/usr/lib/common/h_extern.h index 63aff79f..5e251d95 100644 --- a/usr/lib/common/h_extern.h +++ b/usr/lib/common/h_extern.h @@ -24,6 +24,7 @@ #define _H_EXTERN_H #include +#include "dlist.h" // global variables // @@ -1759,19 +1760,6 @@ int ec_point_from_public_data(const CK_BYTE *data, CK_ULONG data_len, CK_BBOOL *allocated, CK_BYTE **ec_point, CK_ULONG *ec_point_len); -// linked-list routines -// -DL_NODE *dlist_add_as_first(DL_NODE *list, void *data); -DL_NODE *dlist_add_as_last(DL_NODE *list, void *data); -DL_NODE *dlist_find(DL_NODE *list, void *data); -DL_NODE *dlist_get_first(DL_NODE *list); -DL_NODE *dlist_get_last(DL_NODE *list); -CK_ULONG dlist_length(DL_NODE *list); -DL_NODE *dlist_next(DL_NODE *list); -DL_NODE *dlist_prev(DL_NODE *list); -void dlist_purge(DL_NODE *list); -DL_NODE *dlist_remove_node(DL_NODE *list, DL_NODE *node); - CK_RV attach_shm(STDLL_TokData_t *tokdata, CK_SLOT_ID slot_id); CK_RV detach_shm(STDLL_TokData_t *tokdata, CK_BBOOL ignore_ref_count); diff --git a/usr/lib/common/utility.c b/usr/lib/common/utility.c index 38d8d959..b2c6ee50 100644 --- a/usr/lib/common/utility.c +++ b/usr/lib/common/utility.c @@ -40,203 +40,6 @@ #include #include -// Function: dlist_add_as_first() -// -// Adds the specified node to the start of the list -// -// Returns: pointer to the start of the list -// -DL_NODE *dlist_add_as_first(DL_NODE *list, void *data) -{ - DL_NODE *node = NULL; - - if (!data) - return list; - - node = (DL_NODE *) malloc(sizeof(DL_NODE)); - if (!node) - return NULL; - - node->data = data; - node->prev = NULL; - node->next = list; - if (list) - list->prev = node; - - return node; -} - -// Function: dlist_add_as_last() -// -// Adds the specified node to the end of the list -// -// Returns: pointer to the start of the list -// -DL_NODE *dlist_add_as_last(DL_NODE *list, void *data) -{ - DL_NODE *node = NULL; - - if (!data) - return list; - - node = (DL_NODE *) malloc(sizeof(DL_NODE)); - if (!node) - return NULL; - - node->data = data; - node->next = NULL; - - if (!list) { - node->prev = NULL; - return node; - } else { - DL_NODE *temp = dlist_get_last(list); - temp->next = node; - node->prev = temp; - - return list; - } -} - -// Function: dlist_find() -// -DL_NODE *dlist_find(DL_NODE *list, void *data) -{ - DL_NODE *node = list; - - while (node && node->data != data) - node = node->next; - - return node; -} - -// Function: dlist_get_first() -// -// Returns the last node in the list or NULL if list is empty -// -DL_NODE *dlist_get_first(DL_NODE *list) -{ - DL_NODE *temp = list; - - if (!list) - return NULL; - - while (temp->prev != NULL) - temp = temp->prev; - - return temp; -} - -// Function: dlist_get_last() -// -// Returns the last node in the list or NULL if list is empty -// -DL_NODE *dlist_get_last(DL_NODE *list) -{ - DL_NODE *temp = list; - - if (!list) - return NULL; - - while (temp->next != NULL) - temp = temp->next; - - return temp; -} - -// -// -CK_ULONG dlist_length(DL_NODE *list) -{ - DL_NODE *temp = list; - CK_ULONG len = 0; - - while (temp) { - len++; - temp = temp->next; - } - - return len; -} - -// -// -DL_NODE *dlist_next(DL_NODE *node) -{ - if (!node) - return NULL; - - return node->next; -} - -// -// -DL_NODE *dlist_prev(DL_NODE *node) -{ - if (!node) - return NULL; - - return node->prev; -} - -// -// -void dlist_purge(DL_NODE *list) -{ - DL_NODE *node; - - if (!list) - return; - - do { - node = list->next; - free(list); - list = node; - } while (list); -} - -// Function: dlist_remove_node() -// -// Attempts to remove the specified node from the list. The caller is -// responsible for freeing the data associated with the node prior to -// calling this routine -// -DL_NODE *dlist_remove_node(DL_NODE *list, DL_NODE *node) -{ - DL_NODE *temp = list; - - if (!list || !node) - return NULL; - - // special case: removing head of the list - // - if (list == node) { - temp = list->next; - if (temp) - temp->prev = NULL; - - free(list); - return temp; - } - // we have no guarantee that the node is in the list - // so search through the list to find it - // - while ((temp != NULL) && (temp->next != node)) - temp = temp->next; - - if (temp != NULL) { - DL_NODE *next = node->next; - - temp->next = next; - if (next) - next->prev = temp; - - free(node); - } - - return list; -} - CK_RV CreateXProcLock(char *tokname, STDLL_TokData_t *tokdata) { char lockfile[PATH_MAX]; diff --git a/usr/lib/ep11_stdll/ep11_stdll.mk b/usr/lib/ep11_stdll/ep11_stdll.mk index bc617124..b5574d9e 100644 --- a/usr/lib/ep11_stdll/ep11_stdll.mk +++ b/usr/lib/ep11_stdll/ep11_stdll.mk @@ -36,7 +36,7 @@ opencryptoki_stdll_libpkcs11_ep11_la_SOURCES = \ usr/lib/common/utility.c usr/lib/common/trace.c \ usr/lib/common/mech_list.c usr/lib/common/shared_memory.c \ usr/lib/common/attributes.c usr/lib/common/sw_crypt.c \ - usr/lib/common/profile_obj.c \ + usr/lib/common/profile_obj.c usr/lib/common/dlist.c \ usr/lib/common/pkey_utils.c \ usr/lib/ep11_stdll/new_host.c usr/lib/ep11_stdll/ep11_specific.c diff --git a/usr/lib/ica_s390_stdll/ica_s390_stdll.mk b/usr/lib/ica_s390_stdll/ica_s390_stdll.mk index d8448486..8f467e11 100644 --- a/usr/lib/ica_s390_stdll/ica_s390_stdll.mk +++ b/usr/lib/ica_s390_stdll/ica_s390_stdll.mk @@ -34,7 +34,7 @@ opencryptoki_stdll_libpkcs11_ica_la_SOURCES = \ usr/lib/common/verify_mgr.c usr/lib/common/trace.c \ usr/lib/common/mech_list.c usr/lib/common/shared_memory.c \ usr/lib/common/profile_obj.c usr/lib/common/attributes.c \ - usr/lib/ica_s390_stdll/ica_specific.c + usr/lib/ica_s390_stdll/ica_specific.c usr/lib/common/dlist.c if ENABLE_LOCKS opencryptoki_stdll_libpkcs11_ica_la_SOURCES += \ diff --git a/usr/lib/icsf_stdll/icsf_stdll.mk b/usr/lib/icsf_stdll/icsf_stdll.mk index 788478c2..21c64f9a 100644 --- a/usr/lib/icsf_stdll/icsf_stdll.mk +++ b/usr/lib/icsf_stdll/icsf_stdll.mk @@ -43,7 +43,7 @@ opencryptoki_stdll_libpkcs11_icsf_la_SOURCES = \ usr/lib/common/mech_ssl3.c usr/lib/common/verify_mgr.c \ usr/lib/common/mech_list.c usr/lib/common/shared_memory.c \ usr/lib/common/attributes.c usr/lib/icsf_stdll/new_host.c \ - usr/lib/common/profile_obj.c \ + usr/lib/common/profile_obj.c usr/lib/common/dlist.c \ usr/lib/icsf_stdll/pbkdf.c usr/lib/icsf_stdll/icsf_specific.c \ usr/lib/icsf_stdll/icsf_config_parse.y \ usr/lib/icsf_stdll/icsf_config_lexer.l \ diff --git a/usr/lib/soft_stdll/soft_stdll.mk b/usr/lib/soft_stdll/soft_stdll.mk index cea802b5..ac401539 100644 --- a/usr/lib/soft_stdll/soft_stdll.mk +++ b/usr/lib/soft_stdll/soft_stdll.mk @@ -32,7 +32,8 @@ opencryptoki_stdll_libpkcs11_sw_la_SOURCES = \ usr/lib/common/utility.c usr/lib/common/verify_mgr.c \ usr/lib/common/trace.c usr/lib/common/mech_list.c \ usr/lib/common/shared_memory.c usr/lib/common/profile_obj.c \ - usr/lib/soft_stdll/soft_specific.c usr/lib/common/attributes.c + usr/lib/soft_stdll/soft_specific.c usr/lib/common/attributes.c \ + usr/lib/common/dlist.c if ENABLE_LOCKS opencryptoki_stdll_libpkcs11_sw_la_SOURCES += \ diff --git a/usr/lib/tpm_stdll/tpm_stdll.mk b/usr/lib/tpm_stdll/tpm_stdll.mk index f199a103..0e0eb024 100644 --- a/usr/lib/tpm_stdll/tpm_stdll.mk +++ b/usr/lib/tpm_stdll/tpm_stdll.mk @@ -34,7 +34,8 @@ opencryptoki_stdll_libpkcs11_tpm_la_SOURCES = \ usr/lib/common/verify_mgr.c usr/lib/common/mech_list.c \ usr/lib/common/shared_memory.c usr/lib/common/profile_obj.c \ usr/lib/tpm_stdll/tpm_specific.c usr/lib/common/attributes.c \ - usr/lib/tpm_stdll/tpm_openssl.c usr/lib/tpm_stdll/tpm_util.c + usr/lib/tpm_stdll/tpm_openssl.c usr/lib/tpm_stdll/tpm_util.c \ + usr/lib/common/dlist.c if ENABLE_LOCKS opencryptoki_stdll_libpkcs11_tpm_la_SOURCES += \ diff --git a/usr/sbin/pkcscca/pkcscca.mk b/usr/sbin/pkcscca/pkcscca.mk index a223265f..cc40f819 100644 --- a/usr/sbin/pkcscca/pkcscca.mk +++ b/usr/sbin/pkcscca/pkcscca.mk @@ -36,7 +36,7 @@ usr_sbin_pkcscca_pkcscca_SOURCES = \ usr/lib/common/p11util.c usr/lib/common/sw_crypt.c \ usr/lib/common/shared_memory.c usr/lib/common/profile_obj.c \ usr/lib/common/attributes.c usr/lib/common/mech_rng.c \ - usr/lib/common/pkcs_utils.c \ + usr/lib/common/pkcs_utils.c usr/lib/common/dlist.c \ usr/sbin/pkcscca/pkcscca.c diff --git a/usr/sbin/pkcsslotd/pkcsslotd.mk b/usr/sbin/pkcsslotd/pkcsslotd.mk index 4f0e3c56..2d36b4a9 100644 --- a/usr/sbin/pkcsslotd/pkcsslotd.mk +++ b/usr/sbin/pkcsslotd/pkcsslotd.mk @@ -21,5 +21,6 @@ usr_sbin_pkcsslotd_pkcsslotd_SOURCES = \ usr/sbin/pkcsslotd/socket_server.c nodist_usr_sbin_pkcsslotd_pkcsslotd_SOURCES = \ - usr/lib/common/parser.h usr/lib/common/parser.c usr/lib/common/lexer.c + usr/lib/common/parser.h usr/lib/common/parser.c usr/lib/common/lexer.c \ + usr/lib/common/dlist.c usr/sbin/pkcsslotd/slotmgr.$(OBJEXT): usr/lib/common/parser.h