Add syslog support, actual pin signals and mock support

This commit is contained in:
Chris Josten 2020-12-28 16:39:13 +01:00
parent ae24bac0bc
commit f2b96ece0d
3 changed files with 98 additions and 52 deletions

View file

@ -1,4 +1,4 @@
set(CMAKE_MINIMUM_VERSION_REQUIRED 3.0)
cmake_minimum_required(VERSION 3.0)
project(kerstezel-klant)
if (MSVC)
@ -7,9 +7,17 @@ else()
add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()
add_compile_definitions(LOG_PREFIX="${CMAKE_SOURCE_DIR}")
if(SPOOF)
add_compile_definitions(SPOOF)
endif()
set(KERSTEZEL_SOURCES src/kerstezel.c)
set(KERSTEZEL_HEADERS src/kerstezel.h)
add_executable(kerstezel ${KERSTEZEL_SOURCES} ${KERSTEZEL_HEADERS})
if(NOT SPOOF)
target_link_libraries(kerstezel -lbcm2835)
endif()
install(TARGETS kerstezel DESTINATION sbin)

42
init Normal file
View file

@ -0,0 +1,42 @@
#!/bin/sh
### BEGIN INIT INFO
# Provides: kerstezel
# Required-Start: $network
# Required-Stop:
# Default-Start: S
# Default-Stop:
# Short-Description: Set hostname based on /etc/hostname
### END INIT INFO
. /etc/init.d/functions
address=santaofiapc.rocks
port=9305
dapid=0
case $1 in
start)
( while true; do /sbin/kerstezel $address $port; sleep 10; done; ) &
dapid=$!
;;
stop)
if [ $dapid -ne 0 ]; then
kill $dapid
dapid=0
else
echo "Not running"
exit 1
done
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 start|stop|restart"
exit 1
;;
esac
exit 0

View file

@ -11,11 +11,14 @@
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>
#include <linux/seccomp.h>
#ifndef SPOOF
#include <bcm2835.h>
#endif //SPOOF
#define LOGF(x, ...) do { logf_impl(x, __FILE__, __LINE__, __func__, __VA_ARGS__); } while(0);
#define LOG(x) do { log_impl(x, __FILE__, __LINE__, __func__); } while(0);
@ -25,6 +28,8 @@
#define TROMMEL_PIN 17
const char *SOCKET_ERROR = "Socket-fout";
const char *LOG_FMT = "[%s:%d %s]: %s";
int log_prefix_length = 0;
typedef struct mbuf {
/// Internal buffer
@ -38,34 +43,25 @@ typedef struct mbuf {
} mbuf_t;
const char *pid_path = "/run/kerstezel.pid";
const char *log_path = "/var/log/kerstman.log";
FILE *log_file = NULL;
void log_impl(const char *message, const char *file, int line, const char *func) {
fprintf(log_file, "%s:%d:%s: %s\n", file, line, func, message);
fflush(log_file);
syslog(LOG_INFO, LOG_FMT, file + log_prefix_length, line, func, message);
}
void logf_impl(const char *message, const char *file, int line, const char *func, ...) {
va_list args;
va_start(args, func);
fprintf(log_file, "%s:%d %s: ", file, line, func);
vfprintf(log_file, message, args);
fprintf(log_file, "\n");
int c = snprintf(NULL, 0, LOG_FMT, file + log_prefix_length, line, func, message);
char *msg = malloc(c + 1);
snprintf(msg, c + 1, LOG_FMT, file + log_prefix_length, line, func, message);
vsyslog(LOG_INFO, msg, args);
va_end(args);
fflush(log_file);
}
int quit_if_fail(int err, const char *failMessage) {
if (err < 0) {
int no = errno;
size_t stringSize = strlen(failMessage) + strlen(strerror(no)) + 2;
char *msg = (char *) malloc(stringSize);
strcpy(msg, failMessage);
strcat(msg, ": ");
strcat(msg, strerror(no));
LOG(msg);
free(msg);
LOGF( "%s: %s", failMessage, strerror(no));
exit(EXIT_FAILURE);
}
return err;
@ -97,7 +93,7 @@ int mbuf_read_until(mbuf_t *buf, char c, char *out, size_t max_size) {
if (buf->rpos == buf->wpos) {
size_t wpos = buf->wpos % MBUF_SIZE;
if ((n = read(buf->sock, buf->buffer + wpos, MBUF_SIZE - wpos)) < 0) {
if (errno == EINTR) continue;
//if (errno == EINTR) continue;
return -1;
} else if (n == 0) {
return 0;
@ -120,7 +116,11 @@ int mbuf_read_until(mbuf_t *buf, char c, char *out, size_t max_size) {
}
void do_trommel() {
//TODO: implement
#ifndef SPOOF
bcm2835_gpio_write(TROMMEL_PIN, HIGH);
bcm2835_delay(250);
bcm2835_gpio_write(TROMMEL_PIN, LOW);
#endif //SPOOF
}
int do_handshake(mbuf_t *mbuf) {
@ -132,6 +132,7 @@ int do_handshake(mbuf_t *mbuf) {
return -1;
}
// oed\0
size_t response_size = reslen + 4;
char *response = (char *) malloc(response_size);
response[0] = '\0';
@ -140,7 +141,7 @@ int do_handshake(mbuf_t *mbuf) {
strcat(response, "go");
}
strcat(response, "ed\n");
if (write(mbuf->sock, response, response_size) < 0) return -1;
if (write(mbuf->sock, response, response_size - 1) < 0) return -1;
free(response);
return 0;
}
@ -154,9 +155,7 @@ int do_command_loop(mbuf_t *mbuf) {
if (len == 0) return 0;
LOGF("COMMANDO: %s", buf);
if (strcmp(buf, "trommel") == 0) {
bcm2835_gpio_write(TROMMEL_PIN, HIGH);
bcm2835_delay(250);
bcm2835_gpio_write(TROMMEL_PIN, LOW);
do_trommel();
const char *ok = "oké\n";
write(mbuf->sock, ok, strlen(ok) * sizeof(char));
} else if (strcmp(buf, "ping") == 0) {
@ -171,53 +170,44 @@ int do_command_loop(mbuf_t *mbuf) {
}
int main(int argc, char *argv[]) {
log_file = stderr;
#ifdef LOG_PREFIX
log_prefix_length = strlen(LOG_PREFIX);
#endif
openlog(argv[0], LOG_ODELAY, LOG_DAEMON);
if (argc < 3) {
const char *format = "GEBRUIK: %s [ADRES] [POORT]";
size_t length = strlen(argv[0]) + strlen(format);
char *msg = (char *) malloc(length);
snprintf(msg, length, format, argv[0]);
LOG(msg);
free(msg);
LOGF("GEBRUIK: %s ADRES POORT", argv[0]);
return EXIT_FAILURE;
}
LOG("Kerstezel starten");
// Daemonize
pid_t pid;
pid = quit_if_fail(fork(), "Kon niet vorken");
if (pid > 0) exit(EXIT_SUCCESS);
//pid_t pid;
//pid = quit_if_fail(fork(), "Kon niet vorken");
//if (pid > 0) exit(EXIT_SUCCESS);
quit_if_fail(chdir("/"), "Kon werkmap niet veranderen");
quit_if_fail(setsid(), "Kon sid niet veranderen");
//quit_if_fail(setsid(), "Kon sid niet veranderen");
// Zorg ervoor dat de daemon geen TTY kan bemachtigen
pid = quit_if_fail(fork(), "Kon niet vorken");
if (pid > 0) exit(EXIT_SUCCESS);
log_file = fopen(log_path, "a");
if (log_file == NULL) {
// Kan niet loggen lol
exit(EXIT_FAILURE);
}
//pid = quit_if_fail(fork(), "Kon niet vorken");
//if (pid > 0) exit(EXIT_SUCCESS);
// WRITE PIDFILE
FILE *fd = fopen(pid_path, "w");
/*FILE *fd = fopen(pid_path, "w");
if (fd != NULL) {
fprintf(fd, "%d", getpid());
fclose(fd);
}
}*/
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
#ifndef SPOOF
bcm2835_init();
bcm2835_gpio_fsel(TROMMEL_PIN, BCM2835_GPIO_FSEL_OUTP);
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT) < 0) {
LOGF("Kon seccomp niet aanroepen: %s", strerror(errno));
}
LOG("bcm2835 geinitialiseerd")
#endif //SPOOF
struct addrinfo hints, *servinfo, *p;
@ -259,11 +249,17 @@ int main(int argc, char *argv[]) {
quit_if_fail(res, "Kon niet verbinden");
//(void) quit_if_fail(connect(buf->sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)), "Kon niet verbinden");
/*if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT) < 0) {
LOGF("Kon seccomp niet aanroepen: %s", strerror(errno));
}*/
(void) quit_if_fail(do_handshake(buf), "Handenschudden mislukt");
LOG("Handjeschudden voltooid")
(void) quit_if_fail(do_command_loop(buf), "Fout");
LOG("De andere kant heeft de verbinding verbroken");
free(buf);
#ifndef SPOOF
bcm2835_close();
#endif //SPOOF
return 0;
}