]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Create a context
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 10:54:40 +0000 (10:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 10:54:40 +0000 (10:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/ctx.c [new file with mode: 0644]
src/daemon/ctx.h [new file with mode: 0644]
src/daemon/main.c

index fc274102e93c1f0cbfa2215132125edcb0ec94c3..0d01cb4417f6dc498708c02700788342598bf568 100644 (file)
@@ -89,6 +89,8 @@ bin_PROGRAMS += \
        collectyd
 
 dist_collectyd_SOURCES = \
+       src/daemon/ctx.c \
+       src/daemon/ctx.h \
        src/daemon/main.c
 
 # ------------------------------------------------------------------------------
diff --git a/src/daemon/ctx.c b/src/daemon/ctx.c
new file mode 100644 (file)
index 0000000..8437989
--- /dev/null
@@ -0,0 +1,63 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include "ctx.h"
+
+struct collecty_ctx {
+       int nrefs;
+};
+
+static void collecty_ctx_free(collecty_ctx* ctx) {
+       return;
+}
+
+int collecty_ctx_create(collecty_ctx** ctx) {
+       struct collecty_ctx* self = NULL;
+
+       // Allocate some memory
+       self = calloc(1, sizeof(*self));
+       if (!self)
+               return -errno;
+
+       // Initialize the reference counter
+       self->nrefs = 1;
+
+       // Return the pointer
+       *ctx = self;
+
+       return 0;
+}
+
+collecty_ctx* collecty_ctx_ref(collecty_ctx* ctx) {
+       ++ctx->nrefs;
+
+       return ctx;
+}
+
+collecty_ctx* collecty_ctx_unref(collecty_ctx* ctx) {
+       if (--ctx->nrefs > 0)
+               return ctx;
+
+       collecty_ctx_free(ctx);
+       return NULL;
+}
diff --git a/src/daemon/ctx.h b/src/daemon/ctx.h
new file mode 100644 (file)
index 0000000..454c996
--- /dev/null
@@ -0,0 +1,31 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef COLLECTY_CTX_H
+#define COLLECTY_CTX_H
+
+typedef struct collecty_ctx collecty_ctx;
+
+int collecty_ctx_create(collecty_ctx** ctx);
+
+collecty_ctx* collecty_ctx_ref(collecty_ctx* ctx);
+collecty_ctx* collecty_ctx_unref(collecty_ctx* ctx);
+
+#endif /* COLLECTY_CTX_H */
index f9836f08480b39c88edf136a58cad37c0aa509eb..01e1a78883ee72bcbc39776ea19a2c31024b25f8 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <stddef.h>
+#include <stdlib.h>
+
+#include "ctx.h"
+
 int main(int argc, char* argv[]) {
-       return 0;
+       collecty_ctx* ctx = NULL;
+       int r;
+
+       // Allocate a new context
+       r = collecty_ctx_create(&ctx);
+       if (r < 0)
+               goto ERROR;
+
+       // TODO
+
+ERROR:
+       if (ctx)
+               collecty_ctx_unref(ctx);
+
+       return (r) ? EXIT_FAILURE : EXIT_SUCCESS;
 }