Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting pointers to NULL after free #629

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions doc/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

## What is BlueChi?

BlueChi (formerly known as hirte) is a deterministic multi-node service manager
BlueChi (formerly known as hirte) is a deterministic multi-node service controller.

BlueChi can manage the states of different services across multiple nodes with a
focus on highly regulated industries, such as those requiring function safety.
focus on highly regulated industries, such as those requiring functional safety.
BlueChi integrates with systemd via its D-Bus API and relays D-Bus messages over
TCP for multi-nodes support.

On the main node a service called `bluechi-controller` is running. On startup, the manager
On the main node a service called `bluechi-controller` is running. On startup, the controller
loads the configuration files which describe all the involved systems
(called nodes) that are expected to be managed. Each node has a
unique name that is used to reference it in the manager.
unique name that is used to reference it in the controller.

On each managed node that is under control of BlueChi a service called `bluechi-agent`
is running. When the service starts, it connects (via D-Bus over TCP) to the manager
is running. When the service starts, it connects (via D-Bus over TCP) to the controller
and registers as available (optionally authenticating). It then receives requests
from the manager and reports local state changes to it.
from the controller and reports local state changes to it.

## Background

BlueChi is built around three components:

* `bluechi-controller` service, running on the primary node, is the primary orchestrator
* `bluechi-controller` service, running on the primary node, controls all connected nodes
* `bluechi-agent` services, with one running on each managed node, is the agent
talking locally to systemd to act on services
* `bluechictl` command line program, is meant to be used by administrators to test,
Expand Down
32 changes: 15 additions & 17 deletions src/agent/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ static void agent_job_op_unref(AgentJobOp *op) {
}

agent_unref(op->agent);
free(op->unit);
free(op->method);
free_and_null(op->unit);
free_and_null(op->method);
free(op);
}

Expand Down Expand Up @@ -290,9 +290,9 @@ static char *make_unit_path(const char *unit) {

static void unit_info_clear(void *item) {
AgentUnitInfo *info = item;
free(info->object_path);
free(info->unit);
free(info->substate);
free_and_null(info->object_path);
free_and_null(info->unit);
free_and_null(info->substate);
}

static uint64_t unit_info_hash(const void *item, uint64_t seed0, uint64_t seed1) {
Expand Down Expand Up @@ -475,11 +475,11 @@ void agent_unref(Agent *agent) {

hashmap_free(agent->unit_infos);

free(agent->name);
free(agent->host);
free(agent->orch_addr);
free(agent->api_bus_service_name);
free(agent->manager_address);
free_and_null(agent->name);
free_and_null(agent->host);
free_and_null(agent->orch_addr);
free_and_null(agent->api_bus_service_name);
free_and_null(agent->manager_address);

if (agent->event != NULL) {
sd_event_unrefp(&agent->event);
Expand Down Expand Up @@ -1766,7 +1766,7 @@ static void job_tracker_free(JobTracker *track) {
if (track->userdata && track->free_userdata) {
track->free_userdata(track->userdata);
}
free(track->job_object_path);
free_and_null(track->job_object_path);
free(track);
}

Expand Down Expand Up @@ -1953,7 +1953,7 @@ static int agent_match_unit_new(sd_bus_message *m, void *userdata, UNUSED sd_bus
*/
info->active_state = UNIT_INACTIVE;
if (info->substate != NULL) {
free(info->substate);
free_and_null(info->substate);
}
info->substate = strdup("dead");

Expand Down Expand Up @@ -1984,8 +1984,7 @@ static int agent_match_unit_removed(sd_bus_message *m, void *userdata, UNUSED sd

info->loaded = false;
info->active_state = _UNIT_ACTIVE_STATE_INVALID;
free(info->substate);
info->substate = NULL;
free_and_null(info->substate);

if (info->subscribed || agent->wildcard_subscription_active) {
/* Forward the event */
Expand Down Expand Up @@ -2088,7 +2087,7 @@ int agent_init_units(Agent *agent, sd_bus_message *m) {
info->loaded = true;
info->active_state = active_state_from_string(active_state);
if (info->substate != NULL) {
free(info->substate);
free_and_null(info->substate);
}
info->substate = strdup(sub_state);
}
Expand Down Expand Up @@ -2486,8 +2485,7 @@ static bool agent_reconnect(Agent *agent) {
// resolve FQDN again in case the system changed
// e.g. bluechi controller has been migrated to a different host
if (agent->orch_addr != NULL) {
free(agent->orch_addr);
agent->orch_addr = NULL;
free_and_null(agent->orch_addr);
}
if (!ensure_orch_address(agent)) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/agent/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ void proxy_service_unref(ProxyService *proxy) {
sd_bus_message_unrefp(&proxy->request_message);
sd_bus_slot_unrefp(&proxy->export_slot);

free(proxy->node_name);
free(proxy->unit_name);
free(proxy->local_service_name);
free(proxy->object_path);
free_and_null(proxy->node_name);
free_and_null(proxy->unit_name);
free_and_null(proxy->local_service_name);
free_and_null(proxy->object_path);
free(proxy);
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void client_unref(Client *client) {
sd_bus_unrefp(&client->api_bus);
}
if (client->object_path != NULL) {
free(client->object_path);
free_and_null(client->object_path);
}

free(client);
Expand Down
9 changes: 4 additions & 5 deletions src/client/method_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,10 @@ static void node_unref(Node *node) {
}

if (node->connection != NULL) {
free(node->connection->name);
free(node->connection->node_path);
free(node->connection->state);
free(node->connection);
node->connection = NULL;
free_and_null(node->connection->name);
free_and_null(node->connection->node_path);
free_and_null(node->connection->state);
free_and_null(node->connection);
}

if (node->nodes != NULL) {
Expand Down
20 changes: 10 additions & 10 deletions src/libbluechi/bus/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ void unit_unref(UnitInfo *unit) {
return;
}

free(unit->node);
free(unit->id);
free(unit->description);
free(unit->load_state);
free(unit->active_state);
free(unit->sub_state);
free(unit->following);
free(unit->unit_path);
free(unit->job_type);
free(unit->job_path);
free_and_null(unit->node);
free_and_null(unit->id);
free_and_null(unit->description);
free_and_null(unit->load_state);
free_and_null(unit->active_state);
free_and_null(unit->sub_state);
free_and_null(unit->following);
free_and_null(unit->unit_path);
free_and_null(unit->job_type);
free_and_null(unit->job_path);

free(unit);
}
Expand Down
18 changes: 9 additions & 9 deletions src/libbluechi/common/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ static int option_compare(const void *a, const void *b, UNUSED void *udata) {

static void option_destroy(void *item) {
struct config_option *option = item;
free(option->section);
free(option->name);
free(option->value);
free_and_null(option->section);
free_and_null(option->name);
free_and_null(option->value);
}

static uint64_t option_hash(const void *item, uint64_t seed0, uint64_t seed1) {
Expand Down Expand Up @@ -114,8 +114,8 @@ int cfg_copy_overwrite(struct config *src, struct config *dst) {

void cfg_dispose(struct config *config) {
hashmap_free(config->cfg_store);
free(config->default_section);
free(config);
free_and_null(config->default_section);
free_and_null(config);
}

int cfg_load_complete_configuration(
Expand Down Expand Up @@ -291,7 +291,7 @@ int cfg_set_default_section(struct config *config, const char *section_name) {
if (section_copy == NULL) {
return -ENOMEM;
}
free(config->default_section);
free_and_null(config->default_section);
config->default_section = section_copy;

return 0;
Expand Down Expand Up @@ -338,9 +338,9 @@ int cfg_s_set_value(
}

if (replaced != NULL) {
free(replaced->section);
free(replaced->name);
free(replaced->value);
free_and_null(replaced->section);
free_and_null(replaced->name);
free_and_null(replaced->value);
}
return 0;
}
Expand Down
6 changes: 6 additions & 0 deletions src/libbluechi/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ static inline void freep(void *p) {
free(*(void **) p);
}

// NOLINTBEGIN(bugprone-macro-parentheses)
#define free_and_null(ptr) \
free(ptr); \
ptr = NULL;
// NOLINTEND(bugprone-macro-parentheses)

typedef void (*free_func_t)(void *ptr);

#define malloc0(n) (calloc(1, (n) ?: 1))
Expand Down
6 changes: 3 additions & 3 deletions src/manager/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ void job_unref(Job *job) {

sd_bus_slot_unrefp(&job->export_slot);

free(job->object_path);
free(job->unit);
free(job->type);
free_and_null(job->object_path);
free_and_null(job->unit);
free_and_null(job->type);
free(job);
}

Expand Down
2 changes: 1 addition & 1 deletion src/manager/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void manager_unref(Manager *manager) {

sd_event_unrefp(&manager->event);

free(manager->api_bus_service_name);
free_and_null(manager->api_bus_service_name);

sd_event_source_unrefp(&manager->node_connection_source);

Expand Down
14 changes: 7 additions & 7 deletions src/manager/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ void subscription_unref(Subscription *subscription) {
SubscribedUnit *next_su = NULL;
LIST_FOREACH_SAFE(units, su, next_su, subscription->subscribed_units) {
LIST_REMOVE(units, subscription->subscribed_units, su);
free(su->name);
free(su);
free_and_null(su->name);
free_and_null(su);
}

free(subscription->node);
free(subscription);
free_and_null(subscription->node);
free_and_null(subscription);
}


Expand Down Expand Up @@ -163,9 +163,9 @@ void monitor_unref(Monitor *monitor) {
}

sd_bus_slot_unrefp(&monitor->export_slot);
free(monitor->object_path);
free(monitor->client);
free(monitor);
free_and_null(monitor->object_path);
free_and_null(monitor->client);
free_and_null(monitor);
}

bool monitor_export(Monitor *monitor) {
Expand Down
14 changes: 7 additions & 7 deletions src/manager/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ struct ProxyDependency {
};

static void proxy_dependency_free(struct ProxyDependency *dep) {
free(dep->unit_name);
free(dep);
free_and_null(dep->unit_name);
free_and_null(dep);
}

typedef struct UnitSubscription UnitSubscription;
Expand All @@ -110,8 +110,8 @@ typedef struct {

static void unit_subscriptions_clear(void *item) {
UnitSubscriptions *usubs = item;
free(usubs->unit);
free(usubs->substate);
free_and_null(usubs->unit);
free_and_null(usubs->substate);
assert(LIST_IS_EMPTY(usubs->subs));
}

Expand Down Expand Up @@ -201,8 +201,8 @@ void node_unref(Node *node) {
node_unset_agent_bus(node);
hashmap_free(node->unit_subscriptions);

free(node->name);
free(node->object_path);
free_and_null(node->name);
free_and_null(node->object_path);
free(node);
}

Expand Down Expand Up @@ -1669,7 +1669,7 @@ void node_unsubscribe(Node *node, Subscription *sub) {
}

LIST_REMOVE(subs, usubs->subs, found);
free(found);
free_and_null(found);

if (LIST_IS_EMPTY(usubs->subs)) {
/* Last subscription for this unit, tell agent */
Expand Down
4 changes: 2 additions & 2 deletions src/manager/proxy_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void proxy_monitor_unref(ProxyMonitor *monitor) {
subscription_unref(monitor->subscription);
}

free(monitor->unit_name);
free(monitor->proxy_object_path);
free_and_null(monitor->unit_name);
free_and_null(monitor->proxy_object_path);
free(monitor);
}

Expand Down