Initial commit

This commit is contained in:
Wolfgang Hottgenroth 2020-06-17 09:06:02 +02:00
commit 9552a54c3d
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
12 changed files with 185 additions and 0 deletions

5
.bash_history Normal file
View File

@ -0,0 +1,5 @@
ls
ls
make
gcc
ls

15
Dockerfile Normal file
View File

@ -0,0 +1,15 @@
FROM registry.hottis.de/dockerized/c-build-env AS builder
LABEL Maintainer="Wolfgang Hottgenoth <woho@hottis.de>"
LABEL ImageName="registry.hottis.de/dockerized/mqttauditing"
LABEL AlternativeImageName="wollud1969/mqttauditing"

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
CC=gcc
CFLAGS=-Wall
LDFLAGS=-lconfig
INST_DIR=/opt/sbin
REFCNT:=$(shell git rev-list --all --count)
VERSION:=$(shell cat VERSION)
.PHONY: all
all: mqttauditing
mqttauditing: mqttauditing.o logging.o ringbuffer.o version.o
$(CC) -o $@ $(LDFLAGS) $^
version.o: version.c VERSION
$(CC) -DD_REFCNT=$(REFCNT) -DD_VERSION=\"$(VERSION)\" -c $<
.c.o:
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean:
-rm -f *.o mqttauditing

1
VERSION Normal file
View File

@ -0,0 +1 @@
0.1

13
logging.c Normal file
View File

@ -0,0 +1,13 @@
#include <syslog.h>
#include <stdarg.h>
void logmsg(int prio, const char* format, ...) {
va_list vl;
openlog("mqttauditing", 0, LOG_LOCAL0);
va_start(vl, format);
vsyslog(prio, format, vl);
va_end(vl);
closelog();
}

8
logging.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _LOGGING_H_
#define _LOGGING_H_
#include <syslog.h>
void logmsg(int prio, const char* format, ...);
#endif // _LOGGING_H_

42
mqttauditing.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <libconfig.h>
#include <math.h>
#include "ringbuffer.h"
#include "logging.h"
extern char VERSION[];
extern uint32_t REFCNT;
config_t cfg;
void readConfig() {
config_init(&cfg);
if (! config_read_file(&cfg, "/opt/etc/mqttauditing.cfg")) {
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg));
config_destroy(&cfg);
exit(-1);
}
}
int main (void) {
fprintf(stderr, "VERSION: %s, REFCNT: %u\n", VERSION, REFCNT);
readConfig();
fprintf(stderr, "started.\n");
// will never be reached
config_destroy(&cfg);
}

2
mqttauditing.cfg Normal file
View File

@ -0,0 +1,2 @@
# mqttauditing file

53
ringbuffer.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdint.h>
#include <pthread.h>
#define BUFFER_SIZE 256
uint32_t buffer[BUFFER_SIZE+5];
uint32_t bufferReadIdx = 0;
uint32_t bufferWriteIdx = 0;
pthread_mutex_t eventMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t eventSignal = PTHREAD_COND_INITIALIZER;
void ringbufferPut(uint32_t f) {
if (bufferWriteIdx == (BUFFER_SIZE - 1)) {
while (bufferReadIdx == BUFFER_SIZE);
} else {
while (bufferReadIdx == (bufferWriteIdx + 1));
}
buffer[bufferWriteIdx] = f;
bufferWriteIdx++;
if (bufferWriteIdx > BUFFER_SIZE) {
bufferWriteIdx = 0;
}
pthread_mutex_lock(&eventMutex);
pthread_cond_signal(&eventSignal);
pthread_mutex_unlock(&eventMutex);
}
uint32_t ringbufferGet() {
if (bufferReadIdx == bufferWriteIdx) {
pthread_mutex_lock(&eventMutex);
pthread_cond_wait(&eventSignal, &eventMutex);
pthread_mutex_unlock(&eventMutex);
}
double res = buffer[bufferReadIdx];
bufferReadIdx++;
if (bufferReadIdx > BUFFER_SIZE) {
bufferReadIdx = 0;
}
return res;
}

9
ringbuffer.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _RINGBUFFER_H_
#define _RINGBUFFER_H_
#include <stdint.h>
void ringbufferPut(uint32_t f);
uint32_t ringbufferGet();
#endif // _RINGBUFFER_H_

6
startBuildEnv.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
IMAGE=registry.hottis.de/dockerized/c-build-env:latest
docker pull $IMAGE
docker run -it --rm -v $PWD:/work $IMAGE bash

6
version.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdint.h>
const uint32_t REFCNT = D_REFCNT;
const char VERSION[] = D_VERSION;