Skip to content

Commit

Permalink
Finish GOTO to celix_auto conversion for rsa_shm.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Jul 25, 2023
1 parent 4973b13 commit b0125af
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 174 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "remote_constants.h"
#include "celix_log_helper.h"
#include "celix_api.h"
#include "celix_stdlib_cleanup.h"
#include <string.h>
#include <assert.h>

Expand All @@ -41,12 +42,11 @@ static void importRegistration_removeRpcFac(void *handle, void *svc);
celix_status_t importRegistration_create(celix_bundle_context_t *context,
celix_log_helper_t *logHelper, endpoint_description_t *endpointDesc,
long reqSenderSvcId, import_registration_t **importOut) {
celix_status_t status = CELIX_SUCCESS;
if (context == NULL || logHelper == NULL || endpointDescription_isInvalid(endpointDesc)
|| reqSenderSvcId < 0 || importOut == NULL) {
return CELIX_ILLEGAL_ARGUMENT;
}
import_registration_t *import = (import_registration_t *)calloc(1, sizeof(*import));
celix_autofree import_registration_t *import = (import_registration_t *)calloc(1, sizeof(*import));
if (import == NULL) {
return CELIX_ENOMEM;
}
Expand All @@ -55,9 +55,9 @@ celix_status_t importRegistration_create(celix_bundle_context_t *context,

import->endpointDesc = endpointDescription_clone(endpointDesc);
if (import->endpointDesc == NULL) {
status = CELIX_ENOMEM;
goto clone_ep_err;
return CELIX_ENOMEM;
}
celix_autoptr(endpoint_description_t) epDesc = import->endpointDesc;

import->reqSenderSvcId = reqSenderSvcId;
import->rpcFac = NULL;
Expand All @@ -67,11 +67,10 @@ celix_status_t importRegistration_create(celix_bundle_context_t *context,
OSGI_RSA_SERVICE_IMPORTED_CONFIGS, NULL);
if (serviceImportedConfigs == NULL) {
celix_logHelper_error(logHelper,"RSA import reg: service.imported.configs property is not exist.");
status = CELIX_ILLEGAL_ARGUMENT;
goto imported_configs_err;
return CELIX_ILLEGAL_ARGUMENT;
}
char *rsaRpcType = NULL;
char *icCopy = strdup(serviceImportedConfigs);
celix_autofree char *icCopy = strdup(serviceImportedConfigs);
const char delimiter[2] = ",";
char *token, *savePtr;
token = strtok_r(icCopy, delimiter, &savePtr);
Expand All @@ -85,16 +84,14 @@ celix_status_t importRegistration_create(celix_bundle_context_t *context,
}
if (rsaRpcType == NULL) {
celix_logHelper_error(logHelper,"RSA import reg: %s property is not exist.", RSA_RPC_TYPE_KEY);
status = CELIX_ILLEGAL_ARGUMENT;
goto rpc_type_err;
return CELIX_ILLEGAL_ARGUMENT;
}

char filter[128] = {0};
int bytes = snprintf(filter, sizeof(filter), "(%s=%s)", RSA_RPC_TYPE_KEY, rsaRpcType);
if (bytes >= sizeof(filter)) {
celix_logHelper_error(logHelper,"RSA import reg: The value(%s) of %s is too long.", rsaRpcType, RSA_RPC_TYPE_KEY);
status = CELIX_ILLEGAL_ARGUMENT;
goto rpc_type_filter_err;
return CELIX_ILLEGAL_ARGUMENT;
}
celix_service_tracking_options_t opts = CELIX_EMPTY_SERVICE_TRACKING_OPTIONS;
opts.filter.filter = filter;
Expand All @@ -105,25 +102,14 @@ celix_status_t importRegistration_create(celix_bundle_context_t *context,
opts.remove = importRegistration_removeRpcFac;
import->rpcSvcTrkId = celix_bundleContext_trackServicesWithOptionsAsync(context, &opts);
if (import->rpcSvcTrkId < 0) {
celix_logHelper_error(logHelper,"RSA import reg: Error Tracking service for %s.", RSA_RPC_FACTORY_NAME);
status = CELIX_SERVICE_EXCEPTION;
goto tracker_err;
celix_logHelper_error(logHelper,"RSA import reg: Error Tracking service for %s.", RSA_RPC_FACTORY_NAME);
return CELIX_SERVICE_EXCEPTION;
}

*importOut = import;

free(icCopy);
celix_steal_ptr(epDesc);
*importOut = celix_steal_ptr(import);

return CELIX_SUCCESS;
tracker_err:
rpc_type_filter_err:
rpc_type_err:
free(icCopy);
imported_configs_err:
endpointDescription_destroy(import->endpointDesc);
clone_ep_err:
free(import);
return status;
}

static void importRegistration_stopRpcSvcTrkDone(void *data) {
Expand Down Expand Up @@ -189,25 +175,25 @@ static void importRegistration_removeRpcFac(void *handle, void *svc) {

//LCOV_EXCL_START

celix_status_t importRegistration_getException(import_registration_t *registration) {
celix_status_t importRegistration_getException(import_registration_t *registration CELIX_UNUSED) {
celix_status_t status = CELIX_SUCCESS;
//It is stub and will not be called at present.
return status;
}

celix_status_t importRegistration_getImportReference(import_registration_t *registration, import_reference_t **reference) {
celix_status_t importRegistration_getImportReference(import_registration_t *registration CELIX_UNUSED, import_reference_t **reference CELIX_UNUSED) {
celix_status_t status = CELIX_SUCCESS;
//It is stub and will not be called at present.
return status;
}

celix_status_t importReference_getImportedEndpoint(import_reference_t *reference) {
celix_status_t importReference_getImportedEndpoint(import_reference_t *reference CELIX_UNUSED) {
celix_status_t status = CELIX_SUCCESS;
//It is stub and will not be called at present.
return status;
}

celix_status_t importReference_getImportedService(import_reference_t *reference) {
celix_status_t importReference_getImportedService(import_reference_t *reference CELIX_UNUSED) {
celix_status_t status = CELIX_SUCCESS;
//It is stub and will not be called at present.
return status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include "rsa_shm_constants.h"
#include "shm_cache.h"
#include "celix_log_helper.h"
#include "celix_stdlib_cleanup.h"
#include "celix_build_assert.h"
#include "celix_api.h"
#include "celix_unistd_cleanup.h"
#include <thpool.h>
#include <sys/un.h>
#include <sys/socket.h>
Expand All @@ -38,6 +40,9 @@

#define MAX_RSA_SHM_SERVER_HANDLE_MSG_THREADS_NUM 5

// move to thpool.h once it is reused in other places
CELIX_DEFINE_AUTO_CLEANUP_FREE_FUNC(threadpool, thpool_destroy, NULL)

struct rsa_shm_server {
celix_bundle_context_t *ctx;
char *name;
Expand Down Expand Up @@ -71,7 +76,7 @@ celix_status_t rsaShmServer_create(celix_bundle_context_t *ctx, const char *name
return CELIX_ILLEGAL_ARGUMENT;
}

rsa_shm_server_t *server = (rsa_shm_server_t *)calloc(1, sizeof(rsa_shm_server_t));
celix_autofree rsa_shm_server_t *server = (rsa_shm_server_t *)calloc(1, sizeof(rsa_shm_server_t));
if (server == NULL) {
return CELIX_ENOMEM;
}
Expand All @@ -81,15 +86,14 @@ celix_status_t rsaShmServer_create(celix_bundle_context_t *ctx, const char *name
RSA_SHM_MSG_TIMEOUT_KEY, RSA_SHM_MSG_TIMEOUT_DEFAULT_IN_S);
server->name = celix_utils_strdup(name);
if (server->name == NULL) {
status = CELIX_ENOMEM;
goto failed_to_dup_name;
return CELIX_ENOMEM;
}
celix_autofree char* serverName = server->name;
server->loghelper = loghelper;
int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
celix_auto(celix_fd_t) sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sfd == -1) {
celix_logHelper_error(loghelper, "RsaShmServer: create socket fd err, errno is %d.", errno);
status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
goto sfd_err;
return CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
}
server->sfd = sfd;
struct sockaddr_un svaddr;
Expand All @@ -98,47 +102,38 @@ celix_status_t rsaShmServer_create(celix_bundle_context_t *ctx, const char *name
strncpy(&svaddr.sun_path[1], name, sizeof(svaddr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *) &svaddr, sizeof(struct sockaddr_un)) == -1) {
celix_logHelper_error(loghelper, "RsaShmServer: bind socket fd err, errno is %d.", errno);
status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
goto sfd_bind_err;
return CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
}

shm_cache_t *shmCache = NULL;
celix_autoptr(shm_cache_t) shmCache = NULL;
status = shmCache_create(false, &shmCache);
if (status != CELIX_SUCCESS) {
celix_logHelper_logTssErrors(loghelper, CELIX_LOG_LEVEL_ERROR);
celix_logHelper_error(loghelper, "RsaShmServer: create shm cache err; error code is %d.", status);
goto create_shm_cache_err;
return status;
}
server->shmCache = shmCache;

server->threadPool = thpool_init(MAX_RSA_SHM_SERVER_HANDLE_MSG_THREADS_NUM);
if (server->threadPool == NULL) {
celix_logHelper_error(loghelper, "RsaShmServer: create thread pool err.");
status = CELIX_ILLEGAL_STATE;
goto create_thpool_err;
return CELIX_ILLEGAL_STATE;
}
celix_auto(threadpool) thpool = server->threadPool;
server->revCB = receiveCB;
server->revCBHandle = revHandle;
server->revMsgThreadActive = true;
status = celixThread_create(&server->revMsgThread, NULL, rsaShmServer_receiveMsgThread, server);
if (status != CELIX_SUCCESS) {
celix_logHelper_error(loghelper, "RsaShmServer: create receive msg thread err.");
goto create_rev_msg_thread_err;
return status;
}
*shmServerOut = server;
celix_steal_ptr(thpool);
celix_steal_ptr(shmCache);
celix_steal_fd(&sfd);
celix_steal_ptr(serverName);
*shmServerOut = celix_steal_ptr(server);
return CELIX_SUCCESS;
create_rev_msg_thread_err:
thpool_destroy(server->threadPool);
create_thpool_err:
shmCache_destroy(shmCache);
create_shm_cache_err:
sfd_bind_err:
close(sfd);
sfd_err:
free(server->name);
failed_to_dup_name:
free(server);
return status;
}

void rsaShmServer_destroy(rsa_shm_server_t *server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "celix_cleanup.h"
#include <celix_errno.h>
#include <sys/types.h>
#include <stddef.h>
Expand All @@ -48,6 +49,8 @@ celix_status_t shmCache_create(bool shmRdOnly, shm_cache_t **shmCache);
*/
void shmCache_destroy(shm_cache_t *shmCache);

CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(shm_cache_t, shmCache_destroy)

/**
* @brief It will be called when shared memory is closed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ celix_status_t endpointDescription_create(celix_properties_t *properties, endpoi
return CELIX_ILLEGAL_ARGUMENT;
}

endpoint_description_t *ep = calloc(1,sizeof(*ep));
endpoint_description_t *ep = calloc(1,sizeof(*ep));
if (ep == NULL) {
return CELIX_ENOMEM;
}
Expand Down
3 changes: 3 additions & 0 deletions libs/utils/include/celix_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdbool.h>

#include "celix_cleanup.h"
#include "celix_compiler.h"
#include "celix_errno.h"
#include "celix_utils_export.h"
Expand Down Expand Up @@ -53,6 +54,8 @@ CELIX_UTILS_EXPORT celix_properties_t* celix_properties_create(void);

CELIX_UTILS_EXPORT void celix_properties_destroy(celix_properties_t *properties);

CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(celix_properties_t, celix_properties_destroy)

CELIX_UTILS_EXPORT celix_properties_t* celix_properties_load(const char *filename);

CELIX_UTILS_EXPORT celix_properties_t* celix_properties_loadWithStream(FILE *stream);
Expand Down
4 changes: 4 additions & 0 deletions libs/utils/private/test/properties_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,7 @@ TEST(properties, sizeAndIteratorTest) {

celix_properties_destroy(props);
}

TEST(properties, autocleanup) {
celix_autoptr(celix_properties_t) props = celix_properties_create();
}

0 comments on commit b0125af

Please sign in to comment.