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