Skip to content

Commit

Permalink
Introduce constants for application name and directories (#74)
Browse files Browse the repository at this point in the history
* Introduce constants for application name and directories

* Update Makefile

---------

Co-authored-by: Mattias Axelsson <mattiaax@axis.com>
Co-authored-by: Madelen Andersson <madelen.andersson@axis.com>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent 36d93a0 commit cb35047
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
5 changes: 3 additions & 2 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ DOCKS = docker dockerd docker-compose docker-init docker-proxy
CFLAGS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --cflags $(PKGS))
LDLIBS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs $(PKGS))

FLAGS += -W -Wformat=2 -Wpointer-arith -Wbad-function-cast -Wstrict-prototypes \
CFLAGS += -W -Wformat=2 -Wpointer-arith -Wbad-function-cast -Wstrict-prototypes \
-Wmissing-prototypes -Winline -Wdisabled-optimization -Wfloat-equal -Wall -Werror \
-Wno-unused-variable
-Wno-unused-variable \
-D APP_NAME=\"$(PROG1)\"

all: $(PROG1)

Expand Down
49 changes: 22 additions & 27 deletions app/dockerdwrapperwithcompose.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <syslog.h>
#include <unistd.h>

#define APP_DIRECTORY "/usr/local/packages/" APP_NAME
#define APP_LOCALDATA APP_DIRECTORY "/localdata"

struct settings {
char *data_root;
bool use_tls;
Expand Down Expand Up @@ -61,8 +64,7 @@ static const char *ax_parameters[] = {"IPCSocket",
"TCPSocket",
"UseTLS"};

static const char *tls_cert_path =
"/usr/local/packages/dockerdwrapperwithcompose/";
static const char *tls_cert_path = APP_DIRECTORY;

static const char *tls_certs[] = {"ca.pem",
"server-cert.pem",
Expand Down Expand Up @@ -137,8 +139,7 @@ static char *
get_parameter_value(const char *parameter_name)
{
GError *error = NULL;
AXParameter *ax_parameter =
ax_parameter_new("dockerdwrapperwithcompose", &error);
AXParameter *ax_parameter = ax_parameter_new(APP_NAME, &error);
char *parameter_value = NULL;

if (ax_parameter == NULL) {
Expand Down Expand Up @@ -320,9 +321,9 @@ get_and_verify_tls_selection(bool *use_tls_ret)
const bool use_tls = is_parameter_yes("UseTLS");
{
if (use_tls) {
char *ca_path = g_strdup_printf("%s%s", tls_cert_path, tls_certs[0]);
char *cert_path = g_strdup_printf("%s%s", tls_cert_path, tls_certs[1]);
char *key_path = g_strdup_printf("%s%s", tls_cert_path, tls_certs[2]);
char *ca_path = g_strdup_printf("%s/%s", tls_cert_path, tls_certs[0]);
char *cert_path = g_strdup_printf("%s/%s", tls_cert_path, tls_certs[1]);
char *key_path = g_strdup_printf("%s/%s", tls_cert_path, tls_certs[2]);

bool ca_exists = access(ca_path, F_OK) == 0;
bool cert_exists = access(cert_path, F_OK) == 0;
Expand Down Expand Up @@ -432,13 +433,11 @@ start_dockerd(const struct settings *settings, struct app_state *app_state)
guint args_offset = 0;
gchar **args_split = NULL;

args_offset += g_snprintf(
args + args_offset,
args_len - args_offset,
"%s %s",
"dockerd",
"--config-file "
"/usr/local/packages/dockerdwrapperwithcompose/localdata/daemon.json");
args_offset += g_snprintf(args + args_offset,
args_len - args_offset,
"%s %s",
"dockerd",
"--config-file " APP_LOCALDATA "/daemon.json");

g_strlcpy(msg, "Starting dockerd", msg_len);

Expand Down Expand Up @@ -466,12 +465,9 @@ start_dockerd(const struct settings *settings, struct app_state *app_state)
" -H tcp://0.0.0.0:%d",
port);
if (use_tls) {
const char *ca_path =
"/usr/local/packages/dockerdwrapperwithcompose/ca.pem";
const char *cert_path =
"/usr/local/packages/dockerdwrapperwithcompose/server-cert.pem";
const char *key_path =
"/usr/local/packages/dockerdwrapperwithcompose/server-key.pem";
const char *ca_path = APP_DIRECTORY "/ca.pem";
const char *cert_path = APP_DIRECTORY "/server-cert.pem";
const char *key_path = APP_DIRECTORY "/server-key.pem";
args_offset += g_snprintf(args + args_offset,
args_len - args_offset,
" %s %s %s %s %s %s %s",
Expand Down Expand Up @@ -629,7 +625,7 @@ parameter_changed_callback(const gchar *name,
const gchar *value,
__attribute__((unused)) gpointer data)
{
const gchar *parname = name += strlen("root.dockerdwrapperwithcompose.");
const gchar *parname = name += strlen("root." APP_NAME ".");

for (size_t i = 0; i < sizeof(ax_parameters) / sizeof(ax_parameters[0]);
++i) {
Expand All @@ -645,17 +641,16 @@ setup_axparameter(void)
{
bool success = false;
GError *error = NULL;
AXParameter *ax_parameter =
ax_parameter_new("dockerdwrapperwithcompose", &error);
AXParameter *ax_parameter = ax_parameter_new(APP_NAME, &error);
if (ax_parameter == NULL) {
syslog(LOG_ERR, "Error when creating AXParameter: %s", error->message);
goto end;
}

for (size_t i = 0; i < sizeof(ax_parameters) / sizeof(ax_parameters[0]);
++i) {
char *parameter_path = g_strdup_printf(
"%s.%s", "root.dockerdwrapperwithcompose", ax_parameters[i]);
char *parameter_path =
g_strdup_printf("root.%s.%s", APP_NAME, ax_parameters[i]);
gboolean geresult = ax_parameter_register_callback(
ax_parameter, parameter_path, parameter_changed_callback, NULL, &error);
free(parameter_path);
Expand Down Expand Up @@ -719,8 +714,8 @@ main(void)
if (ax_parameter != NULL) {
for (size_t i = 0; i < sizeof(ax_parameters) / sizeof(ax_parameters[0]);
++i) {
char *parameter_path = g_strdup_printf(
"%s.%s", "root.dockerdwrapperwithcompose", ax_parameters[i]);
char *parameter_path =
g_strdup_printf("root.%s.%s", APP_NAME, ax_parameters[i]);
ax_parameter_unregister_callback(ax_parameter, parameter_path);
free(parameter_path);
}
Expand Down

0 comments on commit cb35047

Please sign in to comment.