From 04a3a6beeaa199dc1a80a711f26de3a41d98ff74 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 24 Mar 2016 00:52:05 +0100 Subject: started rewrite in go diff --git a/.gitignore b/.gitignore index 171da9e..23950cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,3 @@ -src/*.d -src/*.o -src/include.mk -src/switchctl -src/serialclient -src/stdioclient -src/heartbeatclient -src/luaclient +bin/ +pkg/ +src/github.com diff --git a/LICENSE b/LICENSE index 9bad8f0..525d296 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ /* * rhctl * - * Copyright (C) 2009-2015 Christian Pointner + * Copyright (C) 2009-2016 Christian Pointner * * This file is part of rhctl. * diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2b06bd5 --- /dev/null +++ b/Makefile @@ -0,0 +1,59 @@ +## +## rhctl +## +## Copyright (C) 2009-2016 Christian Pointner +## +## This file is part of rhctl. +## +## rhctl 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 +## any later version. +## +## rhctl 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 rhctl. If not, see . +## + +curdir:= $(shell pwd) +GOCMD := GOPATH=$(curdir) go + +EXECUTEABLE := rhctl + +LIBS := "github.com/schleibinger/sio" + + +.PHONY: getlibs updatelibs vet format build clean distclean +all: build + + +getlibs: + @$(foreach lib,$(LIBS), echo "fetching lib: $(lib)"; $(GOCMD) get $(lib);) + +updatelibs: + @$(foreach lib,$(LIBS), echo "updating lib: $(lib)"; $(GOCMD) get -u $(lib);) + +vet: + @echo "vetting: $(EXECUTEABLE)" + @$(GOCMD) vet $(EXECUTEABLE) + +format: + @echo "formating: $(EXECUTEABLE)" + @$(GOCMD) fmt $(EXECUTEABLE) + +build: getlibs + @echo "installing: $(EXECUTEABLE)" + @$(GOCMD) install $(EXECUTEABLE) + + +clean: + rm -rf pkg/*/$(EXECUTEABLE) + rm -rf bin + +distclean: clean + @$(foreach dir,$(shell ls src/),$(if $(subst $(EXECUTEABLE),,$(dir)),$(shell rm -rf src/$(dir)))) + rm -rf pkg diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index 2751eb7..0000000 --- a/src/Makefile +++ /dev/null @@ -1,122 +0,0 @@ -## -## rhctl -## -## Copyright (C) 2009-2015 Christian Pointner -## -## This file is part of rhctl. -## -## rhctl 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 -## any later version. -## -## rhctl 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 rhctl. If not, see . -## - -ifneq ($(MAKECMDGOALS),distclean) -include include.mk -endif - -EXE_SWITCHCTL := switchctl -EXE_SERIALCLIENT := serialclient -EXE_STDIOCLIENT := stdioclient -EXE_HEARTBEATCLIENT := heartbeatclient -EXE_LUACLIENT := luaclient - -COMMONOBJ := log.o \ - sig_handler.o \ - string_list.o \ - key_value_storage.o \ - utils.o - -SWITCHCTLOBJ := command_queue.o \ - client_list.o \ - opt-switchctl.o \ - switchctl.o - -SERIALCLIENTOBJ := opt-serialclient.o \ - serialclient.o - -STDIOCLIENTOBJ := opt-stdioclient.o \ - stdioclient.o - -HEARTBEATCLIENTOBJ := opt-heartbeatclient.o \ - heartbeatclient.o - -LUACLIENTOBJ := opt-luaclient.o \ - l_log.o \ - l_sig_handler.o \ - l_cmd.o \ - luaclient.o - - -SRC := $(COMMONOBJ:%.o=%.c) $(SWITCHCTLOBJ:%.o=%.c) $(SERIALCLIENTOBJ:%.o=%.c) $(STDIOCLIENTOBJ:%.o=%.c) $(HEARTBEATCLIENTOBJ:%.o=%.c) $(LUACLIENTOBJ:%.o=%.c) options.c - -.PHONY: clean distclean - -all: $(EXE_SWITCHCTL) $(EXE_SERIALCLIENT) $(EXE_STDIOCLIENT) $(EXE_HEARTBEATCLIENT) $(EXE_LUACLIENT) - -%.d: %.c - @set -e; rm -f $@; \ - $(CC) -MM $(CFLAGS) $< > $@.$$$$; \ - sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ - rm -f $@.$$$$; echo '(re)building $@' - -ifneq ($(MAKECMDGOALS),distclean) --include $(SRC:%.c=%.d) -endif - -$(EXE_SWITCHCTL): $(COMMONOBJ) $(SWITCHCTLOBJ) - $(CC) $(COMMONOBJ) $(SWITCHCTLOBJ) -o $@ $(LDFLAGS) - -$(EXE_SERIALCLIENT): $(COMMONOBJ) $(SERIALCLIENTOBJ) - $(CC) $(COMMONOBJ) $(SERIALCLIENTOBJ) -o $@ $(LDFLAGS) - -$(EXE_STDIOCLIENT): $(COMMONOBJ) $(STDIOCLIENTOBJ) - $(CC) $(COMMONOBJ) $(STDIOCLIENTOBJ) -o $@ $(LDFLAGS) - -$(EXE_HEARTBEATCLIENT): $(COMMONOBJ) $(HEARTBEATCLIENTOBJ) - $(CC) $(COMMONOBJ) $(HEARTBEATCLIENTOBJ) -o $@ $(LDFLAGS) - -$(EXE_LUACLIENT): $(COMMONOBJ) $(LUACLIENTOBJ) - $(CC) $(COMMONOBJ) $(LUACLIENTOBJ) -o $@ $(LDFLAGS) $(LUA_LDFLAGS) - -opt-switchctl.o: options.c - $(CC) $(CFLAGS) -DOPT_SWITCHCTL -o $@ -c $< - -opt-serialclient.o: options.c - $(CC) $(CFLAGS) -DOPT_SERIALCLIENT -o $@ -c $< - -opt-stdioclient.o: options.c - $(CC) $(CFLAGS) -DOPT_STDIOCLIENT -o $@ -c $< - -opt-heartbeatclient.o: options.c - $(CC) $(CFLAGS) -DOPT_HEARTBEATCLIENT -o $@ -c $< - -opt-luaclient.o: options.c - $(CC) $(CFLAGS) -DOPT_LUACLIENT -o $@ -c $< - -%.o: %.c - $(CC) $(CFLAGS) -c $< - - -distclean: clean - find . -name *.o -exec rm -f {} \; - find . -name "*.\~*" -exec rm -rf {} \; - rm -f include.mk - -clean: - rm -f *.o - rm -f *.d - rm -f *.d.* - rm -f $(EXE_SWITCHCTL) - rm -f $(EXE_SERIALCLIENT) - rm -f $(EXE_STDIOCLIENT) - rm -f $(EXE_HEARTBEATCLIENT) - rm -f $(EXE_LUACLIENT) diff --git a/src/client_list.c b/src/client_list.c deleted file mode 100644 index f21aea3..0000000 --- a/src/client_list.c +++ /dev/null @@ -1,137 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include -#include - -#include "client_list.h" -#include "datatypes.h" - -char* client_type_tostring(client_type_t type) -{ - switch(type) - { - case MASTER: return "master"; - case STANDBY: return "standby"; - case HB_MASTER: return "hb_master"; - case HB_STANDBY: return "hb_standby"; - case DEFAULT: return "unspecified"; - } - return ""; -} - -client_t* client_get_last(client_t* first) -{ - if(!first) - return NULL; - - while(first->next) { - first = first->next; - } - - return first; -} - -int client_add(client_t** first, int fd) -{ - if(!first) - return -1; - - client_t* new_client = malloc(sizeof(client_t)); - if(!new_client) - return -2; - - new_client->fd = fd; - new_client->type = DEFAULT; - new_client->request_listener = 0; - new_client->mode_listener = 0; - new_client->status_listener = 0; - new_client->gpi_listener = 0; - new_client->oc_listener = 0; - new_client->relay_listener = 0; - new_client->silence_listener = 0; - new_client->health_listener = 0; - new_client->next = NULL; - new_client->buffer.offset = 0; - - if(!(*first)) { - *first = new_client; - return 0; - } - - client_get_last(*first)->next = new_client; - - return 0; -} - -void client_remove(client_t** first, int fd) -{ - if(!first || !(*first)) - return; - - client_t* deletee = *first; - if((*first)->fd == fd) { - *first = (*first)->next; - close(deletee->fd); - free(deletee); - return; - } - - client_t* prev = deletee; - deletee = deletee->next; - while(deletee) { - if(deletee->fd == fd) { - prev->next = deletee->next; - close(deletee->fd); - free(deletee); - return; - } - prev = deletee; - deletee = deletee->next; - } -} - -client_t* client_find(client_t* first, int fd) -{ - if(!first) - return NULL; - - while(first) { - if(first->fd == fd) - return first; - - first = first->next; - } - return NULL; -} - -void client_clear(client_t** first) -{ - if(!first || !(*first)) - return; - - while(*first) { - client_t* deletee = *first; - *first = (*first)->next; - close(deletee->fd); - free(deletee); - } -} diff --git a/src/client_list.h b/src/client_list.h deleted file mode 100644 index d0b6d3e..0000000 --- a/src/client_list.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_client_list_h_INCLUDED -#define RHCTL_client_list_h_INCLUDED - -#include "datatypes.h" - -enum client_type_enum { DEFAULT, MASTER, STANDBY, HB_MASTER, HB_STANDBY }; -typedef enum client_type_enum client_type_t; -char* client_type_tostring(client_type_t); - -struct client_struct { - int fd; - client_type_t type; - int request_listener; - int mode_listener; - int status_listener; - int gpi_listener; - int oc_listener; - int relay_listener; - int silence_listener; - int health_listener; - struct client_struct* next; - read_buffer_t buffer; -}; -typedef struct client_struct client_t; - -int client_add(client_t** first, int fd); -void client_remove(client_t** first, int fd); -client_t* client_find(client_t* first, int fd); -void client_clear(client_t** first); - -#endif diff --git a/src/command_queue.c b/src/command_queue.c deleted file mode 100644 index d59f7ef..0000000 --- a/src/command_queue.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include -#include - -#include "command_queue.h" -#include "datatypes.h" - -cmd_t* cmd_get_last(cmd_t* first) -{ - if(!first) - return NULL; - - while(first->next) { - first = first->next; - } - - return first; -} - -int cmd_push(cmd_t** first, int fd, cmd_id_t cmd, const char* param) -{ - if(!first) - return -1; - - cmd_t* new_cmd = malloc(sizeof(cmd_t)); - if(!new_cmd) - return -2; - - new_cmd->fd = fd; - new_cmd->cmd = cmd; - if(param) { - new_cmd->param = strdup(param); - if(!new_cmd->param) { - free(new_cmd); - return -2; - } - } - else - new_cmd->param = NULL; - new_cmd->sent = 0; - new_cmd->tv_start.tv_sec = 0; - new_cmd->tv_start.tv_usec = 0; - new_cmd->next = NULL; - - if(!(*first)) { - *first = new_cmd; - return 0; - } - - cmd_get_last(*first)->next = new_cmd; - - return 0; -} - -void cmd_sent(cmd_t* cmd) -{ - if(!cmd) - return; - - cmd->sent = 1; - gettimeofday(&cmd->tv_start, NULL); -} - -int cmd_has_expired(cmd_t cmd) -{ - struct timeval now; - timerclear(&now); - gettimeofday(&now, NULL); - cmd.tv_start.tv_sec++; - - return timercmp(&cmd.tv_start, &now, <); -} - -void cmd_pop(cmd_t** first) -{ - if(!first || !(*first)) - return; - - cmd_t* deletee = *first; - *first = (*first)->next; - if(deletee->param) - free(deletee->param); - free(deletee); -} - -void cmd_clear(cmd_t** first) -{ - if(!first || !(*first)) - return; - - while(*first) { - cmd_t* deletee = *first; - *first = (*first)->next; - if(deletee->param) - free(deletee->param); - free(deletee); - } -} diff --git a/src/command_queue.h b/src/command_queue.h deleted file mode 100644 index 3e5cdb2..0000000 --- a/src/command_queue.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_command_queue_h_INCLUDED -#define RHCTL_command_queue_h_INCLUDED - -#include - -enum cmd_id_enum { SWITCH, CHANNEL, TYPE, MODE, HEARTBEAT, STATUS, HEALTH, LOG, LISTEN }; -typedef enum cmd_id_enum cmd_id_t; - -struct cmd_struct { - int fd; - cmd_id_t cmd; - char* param; - int sent; - struct timeval tv_start; - struct cmd_struct* next; -}; -typedef struct cmd_struct cmd_t; - -int cmd_push(cmd_t** first, int fd, cmd_id_t cmd, const char* param); -void cmd_sent(cmd_t* cmd); -int cmd_has_expired(cmd_t cmd); -void cmd_pop(cmd_t** first); -void cmd_clear(cmd_t** first); - -#endif diff --git a/src/configure b/src/configure deleted file mode 100755 index 6b8bdf6..0000000 --- a/src/configure +++ /dev/null @@ -1,194 +0,0 @@ -#!/bin/sh -# -# rhctl -# -# Copyright (C) 2009-2015 Christian Pointner -# -# This file is part of rhctl. -# -# rhctl 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 -# any later version. -# -# rhctl 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 rhctl. If not, see . -# - -TARGET=`uname -s` - -USE_CLANG=0 - -LUA_LDFLAGS='-lm' -LUA_DIR='' -LUA='' -LUA_VER='' - - -print_usage() { - echo "configure --help print this" - echo " --target= build target i.e. Linux (default: autodetect)" - echo " --with-lua= use this lua tree instead of system default" - echo " --lua-version=nnn set fixed Lua version for automatic detection (501 -> 5.1, 502 -> 5.2)" - echo " --use-clang use clang/llvm as compiler/linker" -} - -for arg -do - case $arg in - --target=*) - TARGET=${arg#--target=} - ;; - --use-clang) - USE_CLANG=1 - ;; - --with-lua=*) - LUA_DIR=${arg#--with-lua=} - ;; - --lua-version=*) - LUA_VER=${arg#--lua-version=} - ;; - --help) - print_usage - exit 0 - ;; - *) - echo "Unknown argument: $arg" - print_usage - exit 1 - ;; - esac -done - -if [ $USE_CLANG -eq 0 ]; then - CFLAGS='-g -Wall -O2' - LDFLAGS='-g -Wall -O2' - COMPILER='gcc' -else - CFLAGS='-g -O2' - LDFLAGS='-g -O2' - COMPILER='clang' -fi - -rm -f include.mk -case $TARGET in - Linux) - echo "Linux specific build options" - LUA_LDFLAGS=$LUA_LDFLAGS' -ldl' - ;; - OpenBSD|FreeBSD|NetBSD) - echo "BSD specific build options" - CFLAGS=$CFLAGS' -I/usr/local/include' - LDFLAGS=$LDFLAGS' -L/usr/local/lib' - ;; - *) - echo "Plattform not supported" - exit 1; - ;; -esac - -test_lua_version() -{ - LUA_VERSION_MAJ=`cat $1 | grep "#define LUA_VERSION_MAJOR[ ]" | cut -f2- | tr -d '"'` - LUA_VERSION_MIN=`cat $1 | grep "#define LUA_VERSION_MINOR[ ]" | cut -f2- | tr -d '"'` - LUA_VERSION_REL=`cat $1 | grep "#define LUA_VERSION_RELEASE[ ]" | cut -f2- | tr -d '"'` - - LUA_VERSION="$LUA_VERSION_MAJ.$LUA_VERSION_MIN" - LUA_RELEASE="$LUA_VERSION_MAJ.$LUA_VERSION_MIN.$LUA_VERSION_REL" - - if [ -z "$LUA_VERSION_MAJ" ]; then - LUA_VERSION=`cat $1 | grep "#define LUA_VERSION[ ]" | cut -f2- | tr -d '"' | sed -e 's/Lua \([0-9][0-9.]*\)/\1/'` - LUA_RELEASE=`cat $1 | grep "#define LUA_RELEASE[ ]" | cut -f2- | tr -d '"' | sed -e 's/Lua //'` - fi - - LUA_VERSION_NUM=`cat $1 | grep "#define LUA_VERSION_NUM" | awk '{ print $3 }'` - - if [ -n "$LUA_VER" ]; then - if [ "$LUA_VER" -eq $LUA_VERSION_NUM ]; then - return 1 - else - return 0 - fi - else - if [ $LUA_VERSION_NUM -ge 501 ]; then - return 1 - else - return 0 - fi - fi -} - -if [ -z "$LUA_DIR" ]; then - for prefix in /usr /usr/local; do - if [ -e $prefix/include/lua.h ]; then - test_lua_version $prefix/include/lua.h - if [ $? -eq 1 ]; then - echo "using Lua $LUA_VERSION ($LUA_RELEASE) found at $prefix/include" - CFLAGS="$CFLAGS -I'$prefix/include'" - LUA_LDFLAGS="$LUA_LDFLAGS -L'$prefix/lib' -llua" - LUA=$prefix/lua - LUAC=$prefix/luac - break - fi - else - for dir in `ls -r -d $prefix/include/lua* 2> /dev/null`; do - if [ -e $dir/lua.h ]; then - test_lua_version $dir/lua.h - if [ $? -eq 1 ]; then - echo "using Lua $LUA_VERSION ($LUA_RELEASE) found at $dir" - CFLAGS="$CFLAGS -I$dir" - if [ -x "$prefix/bin/lua$LUA_VERSION" ]; then - LUA_LDFLAGS="$LUA_LDFLAGS -L'$prefix/lib' -llua$LUA_VERSION" - LUA=$prefix/bin/lua$LUA_VERSION - LUAC=$prefix/bin/luac$LUA_VERSION - elif [ -x "$prefix/bin/lua-$LUA_VERSION" ]; then - LUA_LDFLAGS="$LUA_LDFLAGS -L'$prefix/lib' -llua-$LUA_VERSION" - LUA=$prefix/bin/lua-$LUA_VERSION - LUAC=$prefix/bin/luac-$LUA_VERSION - else - echo "ERROR: found lua.h at $dir/lua.h but no matching lua and luac" - return 1 - fi - break - fi - fi - done - if [ -n "$LUAC" ]; then - break - fi - fi - done - - if [ -z "$LUAC" ]; then - echo "ERROR: no suitable lua found .. please install lua 5.1 or higher or use --with-lua" - return 1 - fi - -else - CFLAGS="$CFLAGS -I'$LUA_DIR/include'" - LUA_LDFLAGS="$LUA_LDFLAGS '$LUA_DIR/lib/liblua.a'" - LUA=$LUA_DIR/bin/lua - LUAC=$LUA_DIR/bin/luac -fi - - -cat > include.mk < - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_daemon_h_INCLUDED -#define RHCTL_daemon_h_INCLUDED - -#include -#include -#include -#include -#include -#include -#include - -struct priv_info_struct { - struct passwd* pw_; - struct group* gr_; -}; -typedef struct priv_info_struct priv_info_t; - -int priv_init(priv_info_t* priv, const char* username, const char* groupname) -{ - if(!priv) - return -1; - - priv->pw_ = NULL; - priv->gr_ = NULL; - - priv->pw_ = getpwnam(username); - if(!priv->pw_) { - log_printf(ERROR, "unknown user %s", username); - return -1; - } - - if(groupname) - priv->gr_ = getgrnam(groupname); - else - priv->gr_ = getgrgid(priv->pw_->pw_gid); - - if(!priv->gr_) { - log_printf(ERROR, "unknown group %s", groupname); - return -1; - } - - return 0; -} - -int priv_drop(priv_info_t* priv) -{ - if(!priv || !priv->pw_ || !priv->gr_) { - log_printf(ERROR, "privileges not initialized properly"); - return -1; - } - - if(setgid(priv->gr_->gr_gid)) { - log_printf(ERROR, "setgid('%s') failed: %s", priv->gr_->gr_name, strerror(errno)); - return -1; - } - - gid_t gr_list[1]; - gr_list[0] = priv->gr_->gr_gid; - if(setgroups (1, gr_list)) { - log_printf(ERROR, "setgroups(['%s']) failed: %s", priv->gr_->gr_name, strerror(errno)); - return -1; - } - - if(setuid(priv->pw_->pw_uid)) { - log_printf(ERROR, "setuid('%s') failed: %s", priv->pw_->pw_name, strerror(errno)); - return -1; - } - - log_printf(NOTICE, "dropped privileges to %s:%s", priv->pw_->pw_name, priv->gr_->gr_name); - return 0; -} - - -int do_chroot(const char* chrootdir) -{ - if(getuid() != 0) { - log_printf(ERROR, "this program has to be run as root in order to run in a chroot"); - return -1; - } - - if(chroot(chrootdir)) { - log_printf(ERROR, "can't chroot to %s: %s", chrootdir, strerror(errno)); - return -1; - } - log_printf(NOTICE, "we are in chroot jail (%s) now", chrootdir); - if(chdir("/")) { - log_printf(ERROR, "can't change to /: %s", strerror(errno)); - return -1; - } - - return 0; -} - -void daemonize() -{ - pid_t pid; - - pid = fork(); - if(pid < 0) { - log_printf(ERROR, "daemonizing failed at fork(): %s, exitting", strerror(errno)); - exit(-1); - } - if(pid) exit(0); - - umask(0); - - if(setsid() < 0) { - log_printf(ERROR, "daemonizing failed at setsid(): %s, exitting", strerror(errno)); - exit(-1); - } - - pid = fork(); - if(pid < 0) { - log_printf(ERROR, "daemonizing failed at fork(): %s, exitting", strerror(errno)); - exit(-1); - } - if(pid) exit(0); - - if ((chdir("/")) < 0) { - log_printf(ERROR, "daemonizing failed at chdir(): %s, exitting", strerror(errno)); - exit(-1); - } - - int fd; - for (fd=0;fd<=2;fd++) // close all file descriptors - close(fd); - fd = open("/dev/null",O_RDWR); // stdin - if(fd == -1) - log_printf(WARNING, "can't open stdin (chroot and no link to /dev/null?)"); - else { - if(dup(fd) == -1) // stdout - log_printf(WARNING, "can't open stdout"); - if(dup(fd) == -1) // stderr - log_printf(WARNING, "can't open stderr"); - } - umask(027); -} - -#endif - diff --git a/src/datatypes.h b/src/datatypes.h deleted file mode 100644 index e6715b1..0000000 --- a/src/datatypes.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_datatypes_h_INCLUDED -#define RHCTL_datatypes_h_INCLUDED - -#define _GNU_SOURCE -#include -#include - -typedef uint8_t bool; -#define FALSE 0 -#define TRUE 1 - -typedef uint8_t u_int8_t; -typedef uint16_t u_int16_t; -typedef uint32_t u_int32_t; -typedef uint64_t u_int64_t; -/* typedef int8_t int8_t; */ -/* typedef int16_t int16_t; */ -/* typedef int32_t int32_t; */ -/* typedef int64_t int64_t; */ - -struct buffer_struct { - u_int32_t length_; - u_int8_t* buf_; -}; -typedef struct buffer_struct buffer_t; - -struct read_buffer_struct { - u_int32_t offset; - u_int8_t buf[100]; -}; -typedef struct read_buffer_struct read_buffer_t; - -#endif diff --git a/src/health-watch.lua b/src/health-watch.lua deleted file mode 100644 index 2c2c32a..0000000 --- a/src/health-watch.lua +++ /dev/null @@ -1,107 +0,0 @@ --- --- rhctl --- --- Copyright (C) 2009-2015 Christian Pointner --- --- This file is part of rhctl. --- --- rhctl 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 --- any later version. --- --- rhctl 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 rhctl. If not, see . --- - -package.path = package.path .. ';/usr/share/rhctl/?.lua' - -socket = require("socket") -utils = require("utils") - -old_state = {} -current_state = {} - -function state_changed() - for k,v in pairs(old_state) do - if v ~= current_state[k] then - return true - end - end - for k,v in pairs(current_state) do - if old_state[k] == nil then - return true - end - end - return false -end - -function process_cmd(message) - local exps = { hs = "^Health: (%a+)$", mc = "^Master: (%a+)$", sc = "^Standby: (%a+)$", hbm = "^Hearbeat Master: (%a+, %a+)$", hbs = "^Hearbeat Standby: (%a+, %a+)$" } - for key, exp in pairs(exps) do - local state = string.match(message, exp) - if(state) then - if(key == 'hs') then - current_state = {} - end - current_state[key] = state - end - end - - if(current_state['hbs'] ~= nil) then - if(state_changed()) then - body = "RHCTL Health Output:\n\n" - body = body .. "Health: " .. current_state['hs'] .. "\n" - body = body .. "Master: " .. current_state['mc'] .. "\n" - body = body .. "Standby: " .. current_state['sc'] .. "\n" - body = body .. "Master Hearbeat: " .. current_state['hbm'] .. "\n" - body = body .. "Standby Hearbeat: " .. current_state['hbs'] .. "\n" - utils.send_mail("logs@helsinki.at", "[RHCTL] health: " .. current_state['hs'], body) - old_state = current_state - end - end - - return 0 -end - -function main_loop(opt) - log.printf(log.NOTICE, "main_loop started") - local sig = signal.init() - local cmdfd = cmd.init() - - cmd.send_string("listen health"); - cmd.send_string("health"); - - local return_value = 0 - while return_value == 0 do - local readable, _, err = socket.select({ sig , cmdfd }, nil) - if(err) then - log.printf(log.ERROR, "select returned with error: %s", err) - return_value = -1 - else - for _, input in ipairs(readable) do - if(input == sig) then - return_value = signal.handle() - if(return_value == 1) then break end - else - if(input == cmdfd) then - return_value = cmd.recv_data(process_cmd) - if(return_value ~= 0) then break end - else - log.printf(log.ERROR, "select returned invalid handle??") - return_value = -1 - break; - end - end - end - end - end - - signal.stop() - return return_value -end diff --git a/src/heartbeatclient.c b/src/heartbeatclient.c deleted file mode 100644 index 975581f..0000000 --- a/src/heartbeatclient.c +++ /dev/null @@ -1,428 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include "datatypes.h" - -#include -#include -#include -#include - -#include "log.h" -#include "sig_handler.h" -#include "options.h" - -#include "daemon.h" -#include "utils.h" - -int process_serial(read_buffer_t* buffer, int serial_fd, int led_pipe_fd, bool *state) -{ - int ret = 0; - struct timeval tv; - fd_set fds; - FD_ZERO(&fds); - FD_SET(serial_fd, &fds); - - for(;;) { - tv.tv_sec = 0; - tv.tv_usec = 0; - ret = select(serial_fd+1, &fds, NULL, NULL, &tv); - if(!ret) - return 0; - else if(ret < 0) - return ret; - - ret = read(serial_fd, &buffer->buf[buffer->offset], 1); - if(!ret) - return 2; - if(ret == -1 && errno == EAGAIN) - return 0; - else if(ret < 0) - break; - - if(buffer->buf[buffer->offset] == '\n') { - buffer->buf[buffer->offset] = 0; - - if(buffer->offset > 0 && buffer->buf[buffer->offset-1] == '\r') - buffer->buf[buffer->offset-1] = 0; - - if(strlen((char *)(buffer->buf))) { - *state = TRUE; - if(led_pipe_fd >= 0) { - char buf = '1'; - ret = write(led_pipe_fd, &buf, 1); - if(ret == -1 && errno != EAGAIN) - log_printf(WARNING, "write to led process pipe returned with error: %s", strerror(errno)); - } - } - - buffer->offset = 0; - return 0; - } - - buffer->offset++; - if(buffer->offset >= sizeof(buffer->buf)) { - log_printf(DEBUG, "string too long (fd=%d)", serial_fd); - buffer->offset = 0; - return 0; - } - } - - return ret; -} - -int main_loop(int serial_fd, int cmd_fd, int led_pipe_fd, options_t* opt) -{ - log_printf(NOTICE, "entering main loop"); - - fd_set readfds, tmpfds; - FD_ZERO(&readfds); - FD_SET(serial_fd, &readfds); - FD_SET(cmd_fd, &readfds); - int max_fd = serial_fd > cmd_fd ? serial_fd : cmd_fd; - - int sig_fd = signal_init(); - if(sig_fd < 0) - return -1; - FD_SET(sig_fd, &readfds); - max_fd = (max_fd < sig_fd) ? sig_fd : max_fd; - - int return_value = 0; - return_value = send_string(cmd_fd, "type heartbeat\n"); - if(return_value <= 0) { - log_printf(ERROR, "error setting client type"); - return_value = -1; - } - else { - log_printf(NOTICE, "connecting as type heartbeat"); - return_value = 0; - } - - bool state = FALSE; - send_string(cmd_fd, "heartbeat 0\n"); - - read_buffer_t serial_buffer; - serial_buffer.offset = 0; - struct timeval timeout; - u_int32_t time = 0; - while(!return_value) { - memcpy(&tmpfds, &readfds, sizeof(tmpfds)); - - timeout.tv_sec = 0; - timeout.tv_usec = 100000; - int ret = select(max_fd+1, &tmpfds, NULL, NULL, &timeout); - if(ret == -1 && errno != EINTR) { - log_printf(ERROR, "select returned with error: %s", strerror(errno)); - return_value = -1; - break; - } - if(ret == -1) - continue; - if(!ret) { - time++; - if(time >= opt->timeout_) { - if(state == TRUE) { - log_printf(WARNING, "heartbeat timeout"); - send_string(cmd_fd, "heartbeat 0\n"); - state = FALSE; - } - time = 0; - } - continue; - } - - if(FD_ISSET(sig_fd, &tmpfds)) - if(signal_handle()) - return_value = 1; - - if(FD_ISSET(serial_fd, &tmpfds)) { - bool old_state = state; - return_value = process_serial(&serial_buffer, serial_fd, led_pipe_fd, &state); - if(return_value) - break; - if(state) { - if(!old_state) { - log_printf(WARNING, "heartbeat returned"); - send_string(cmd_fd, "heartbeat 1\n"); - } - time = 0; - } - } - - if(FD_ISSET(cmd_fd, &tmpfds)) { - u_int8_t buf[100]; - int ret = recv(cmd_fd, buf, 100, 0); - if(!ret) - return_value = 3; - if(ret == -1 && errno == EAGAIN) - return_value = 0; - else if(ret < 0) - return_value = ret; - } - } - - signal_stop(); - return return_value; -} - -int led_process(int pipe_fd, int led_fd) -{ - struct timeval timeout; - char buf; - int ret; - - log_printf(INFO, "led_process: starting up"); - for(;;) { - ret = read(pipe_fd, &buf, 1); - if(ret == -1 && errno == EAGAIN) - continue; - else if(!ret || ret < 0) { - log_printf(ERROR, "led_process: read returned with error: %s", strerror(errno)); - break; - } - - buf = '1'; - ret = write(led_fd, &buf, 1); - if(ret == -1 && errno != EAGAIN) { - log_printf(ERROR, "led_process: write returned with error: %s", strerror(errno)); - break; - } - - timeout.tv_sec = 0; - timeout.tv_usec = 100000; - ret = select(0, NULL, NULL, NULL, &timeout); - if(ret == -1 && errno != EINTR) { - log_printf(ERROR, "led_process: select returned with error: %s", strerror(errno)); - break; - } - - buf = '0'; - ret = write(led_fd, &buf, 1); - if(ret == -1 && errno != EAGAIN) { - log_printf(ERROR, "led_process: write returned with error: %s", strerror(errno)); - break; - } - } - log_printf(INFO, "led_process: stopped"); - return -1; -} - -int start_led_process(options_t* opt) -{ - int pipefd[2]; - int led_fd; - pid_t cpid; - - led_fd = open(opt->led_filename_, O_WRONLY); - if(led_fd < 0) { - log_printf(ERROR, "led_process: open() failed: %s", strerror(errno)); - return -2; - } - - if (pipe(pipefd) == -1) { - log_printf(ERROR, "led_process: pipe() failed: %s", strerror(errno)); - close(led_fd); - return -2; - } - - cpid = fork(); - if (cpid == -1) { - log_printf(ERROR, "led_process: fork() failed: %s", strerror(errno)); - close(pipefd[0]); - close(pipefd[1]); - close(led_fd); - return -2; - } - - if (cpid == 0) { - close(pipefd[1]); - return led_process(pipefd[0], led_fd); - } - - close(pipefd[0]); - close(led_fd); - return pipefd[1]; -} - -int main(int argc, char* argv[]) -{ - log_init(); - - options_t opt; - int ret = options_parse(&opt, argc, argv); - if(ret) { - if(ret > 0) { - fprintf(stderr, "syntax error near: %s\n\n", argv[ret]); - } - if(ret == -2) { - fprintf(stderr, "memory error on options_parse, exiting\n"); - } - if(ret == -5) { - fprintf(stderr, "syntax error: invalid baudrate\n"); - } - - if(ret != -2) - options_print_usage(); - - options_clear(&opt); - log_close(); - exit(ret); - } - string_list_element_t* tmp = opt.log_targets_.first_; - if(!tmp) { - log_add_target("syslog:3,heartbeatclient,daemon"); - } - else { - while(tmp) { - ret = log_add_target(tmp->string_); - if(ret) { - switch(ret) { - case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break; - case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break; - case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break; - default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break; - } - - options_clear(&opt); - log_close(); - exit(ret); - } - tmp = tmp->next_; - } - } - log_printf(NOTICE, "just started..."); - if(options_parse_post(&opt)) { - options_clear(&opt); - log_close(); - exit(-1); - } - - priv_info_t priv; - if(opt.username_) - if(priv_init(&priv, opt.username_, opt.groupname_)) { - options_clear(&opt); - log_close(); - exit(-1); - } - - FILE* pid_file = NULL; - if(opt.pid_file_) { - pid_file = fopen(opt.pid_file_, "w"); - if(!pid_file) { - log_printf(WARNING, "unable to open pid file: %s", strerror(errno)); - } - } - - if(opt.chroot_dir_) - if(do_chroot(opt.chroot_dir_)) { - options_clear(&opt); - log_close(); - exit(-1); - } - if(opt.username_) - if(priv_drop(&priv)) { - options_clear(&opt); - log_close(); - exit(-1); - } - - if(opt.daemonize_) { - pid_t oldpid = getpid(); - daemonize(); - log_printf(INFO, "running in background now (old pid: %d)", oldpid); - } - - if(pid_file) { - pid_t pid = getpid(); - fprintf(pid_file, "%d", pid); - fclose(pid_file); - } - - int led_pipe_fd = -1; - if(opt.led_filename_) { - log_printf(NOTICE, "starting led blink process"); - led_pipe_fd = start_led_process(&opt); - if(led_pipe_fd == -1) { - options_clear(&opt); - log_close(); - exit(0); - } - if(led_pipe_fd < -1) { - options_clear(&opt); - log_close(); - exit(-1); - } - } - - int cmd_fd = 0; - int serial_fd = 0; - for(;;) { - cmd_fd = connect_command_socket(opt.command_sock_); - if(cmd_fd < 0) - ret = 3; - else { - serial_fd = open(opt.serial_dev_, O_RDWR | O_NOCTTY); - if(serial_fd < 0) { - log_printf(ERROR, "open(%s): %s", opt.serial_dev_, strerror(errno)); - ret = 2; - } - else { - ret = setup_tty(serial_fd, opt.baudrate_); - if(ret) - ret = 2; - else - ret = main_loop(serial_fd, cmd_fd, led_pipe_fd, &opt); - } - } - - if(ret == 2 || ret == 3) { - if(ret == 2) - log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.serial_dev_); - if(ret == 3) - log_printf(ERROR, "socket error, trying to reconnect in 5 seconds.."); - - if(cmd_fd > 0) - close(cmd_fd); - if(serial_fd > 0) - close(serial_fd); - sleep(5); - } - else - break; - } - - if(cmd_fd > 0) - close(cmd_fd); - if(serial_fd > 0) - close(serial_fd); - - if(!ret) - log_printf(NOTICE, "normal shutdown"); - else if(ret < 0) - log_printf(NOTICE, "shutdown after error (code %d)", ret); - else - log_printf(NOTICE, "shutdown after signal"); - - options_clear(&opt); - log_close(); - - return ret; -} diff --git a/src/key_value_storage.c b/src/key_value_storage.c deleted file mode 100644 index 31f48d9..0000000 --- a/src/key_value_storage.c +++ /dev/null @@ -1,92 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include -#include - -#include "key_value_storage.h" - -void key_value_storage_init(key_value_storage_t* stor) -{ - if(!stor) - return; - - string_list_init(&(stor->keys_)); - string_list_init(&(stor->values_)); -} - -void key_value_storage_clear(key_value_storage_t* stor) -{ - if(!stor) - return; - - string_list_clear(&(stor->keys_)); - string_list_clear(&(stor->values_)); -} - -int key_value_storage_add(key_value_storage_t* stor, const char* key, const char* value) -{ - if(!stor || !key || !value) - return -1; - - int ret = string_list_add(&(stor->keys_), key); - if(ret!=0) - return ret; - - ret = string_list_add(&(stor->values_), value); - if(ret!=0) - return ret; - - return 0; -} - -char* key_value_storage_find(key_value_storage_t* stor, const char* key) -{ - if(!stor || !key) - return NULL; - - string_list_element_t* k = stor->keys_.first_; - string_list_element_t* v = stor->values_.first_; - while(v && k) { - if(!strcmp(k->string_, key)) - return v->string_; - - k = k->next_; - v = v->next_; - } - - return NULL; -} - -void key_value_storage_print(key_value_storage_t* stor, const char* head, const char* sep, const char* tail) -{ - if(!stor) - return; - - string_list_element_t* k = stor->keys_.first_; - string_list_element_t* v = stor->values_.first_; - while(v && k) { - printf("%s%s%s%s%s", head, k->string_, sep, v->string_, tail); - k = k->next_; - v = v->next_; - } - printf("\n"); -} diff --git a/src/key_value_storage.h b/src/key_value_storage.h deleted file mode 100644 index 1a58a60..0000000 --- a/src/key_value_storage.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_key_value_storage_h_INCLUDED -#define RHCTL_key_value_storage_h_INCLUDED - -#include "string_list.h" - -struct key_value_storage_struct { - string_list_t keys_; - string_list_t values_; -}; -typedef struct key_value_storage_struct key_value_storage_t; - -void key_value_storage_init(key_value_storage_t* stor); -void key_value_storage_clear(key_value_storage_t* stor); -int key_value_storage_add(key_value_storage_t* stor, const char* key, const char* value); -char* key_value_storage_find(key_value_storage_t* stor, const char* key); - -void key_value_storage_print(key_value_storage_t* stor, const char* head, const char* sep, const char* tail); - - -#endif diff --git a/src/l_cmd.c b/src/l_cmd.c deleted file mode 100644 index 374535e..0000000 --- a/src/l_cmd.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include -#include - -#include -#include -#include - -#include "datatypes.h" - -#include "l_cmd.h" - -#include "options.h" -#include "command_queue.h" -#include "client_list.h" -#include "log.h" -#include "utils.h" - -int cmd_fd; -read_buffer_t cmd_buffer; - -static int l_cmd_getfd(lua_State *L) -{ - if(!lua_istable(L, -1)) - luaL_error(L, "can't retreive signal fd"); - - lua_pushliteral(L, "fd"); - lua_gettable(L, -2); - return 1; -} - -static int l_cmd_dirty(lua_State *L) -{ - lua_pushboolean(L, 0); - return 1; -} - -static int l_cmd_init(lua_State *L) -{ - lua_newtable(L); - lua_pushliteral(L, "fd"); - lua_pushinteger(L, cmd_fd); - lua_settable(L, -3); - lua_pushliteral(L, "getfd"); - lua_pushcfunction(L, l_cmd_getfd); - lua_settable(L, -3); - lua_pushliteral(L, "dirty"); - lua_pushcfunction(L, l_cmd_dirty); - lua_settable(L, -3); - return 1; -} - -// gets called by nonblock_recvline for every complete message -static int cmd_recv_data(lua_State* L) -{ - int ret = 0; - for(;;) { - ret = recv(cmd_fd, &cmd_buffer.buf[cmd_buffer.offset], 1, 0); - if(!ret) - return 2; - if(ret == -1 && errno == EAGAIN) - return 0; - else if(ret < 0) - break; - - if(cmd_buffer.buf[cmd_buffer.offset] == '\n') { - cmd_buffer.buf[cmd_buffer.offset] = 0; - - lua_pushstring(L, (char *)(cmd_buffer.buf)); - - ret = lua_pcall(L, 1, 1, 0); - if(ret) { - const char* err_str = luaL_checkstring(L, -1); - switch(ret) { - case LUA_ERRRUN: log_printf(ERROR, "lua_pcall(cmd_callback) runtime error: %s", err_str); break; - case LUA_ERRMEM: log_printf(ERROR, "lua_pcall(cmd_callback) malloc error: %s", err_str); break; - case LUA_ERRERR: log_printf(ERROR, "lua_pcall(cmd_callback) error at error handler function: %s", err_str); break; - } - return -1; - } - - ret = lua_tointeger(L, 1); - cmd_buffer.offset = 0; - break; - } - - cmd_buffer.offset++; - if(cmd_buffer.offset >= sizeof(cmd_buffer.buf)) { - log_printf(DEBUG, "string too long cmd_fd"); - cmd_buffer.offset = 0; - return 0; - } - } - - return ret; -} - -static int l_cmd_recv_data(lua_State* L) -{ - int ret = cmd_recv_data(L); - lua_pushinteger(L, ret); - return 1; -} - -static int l_cmd_send_string(lua_State* L) -{ - int ret = send_string(cmd_fd, luaL_checkstring(L,1)); - if (ret > 0) - do { - ret = write(cmd_fd, "\n", 1); - } while(!ret || (ret == -1 && errno == EINTR)); - - if(ret > 0) - ret = 0; - - lua_pushinteger(L, ret); - return 1; -} - -static const struct luaL_Reg cmd_funcs [] = { - { "init", l_cmd_init }, - { "recv_data", l_cmd_recv_data }, - { "send_string", l_cmd_send_string }, - { NULL, NULL } -}; - -LUALIB_API int luaopen_cmd(lua_State *L) -{ -#if LUA_VERSION_NUM > 501 - lua_newtable(L); - luaL_setfuncs(L, cmd_funcs, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, LUA_CMDLIBNAME); -#else - luaL_register(L, LUA_CMDLIBNAME, cmd_funcs); -#endif - return 1; -} diff --git a/src/l_cmd.h b/src/l_cmd.h deleted file mode 100644 index a23f79a..0000000 --- a/src/l_cmd.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_l_cmd_h_INCLUDED -#define RHCTL_l_cmd_h_INCLUDED - -#include - -extern int cmd_fd; - -#define LUA_CMDLIBNAME "cmd" -LUALIB_API int luaopen_cmd(lua_State *L); - -#endif diff --git a/src/l_log.c b/src/l_log.c deleted file mode 100644 index 05111c3..0000000 --- a/src/l_log.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include -#include - -#include - -#include "datatypes.h" -#include "log.h" - -#include "l_log.h" - -static int l_log_init(lua_State *L) -{ - log_init(); - return 0; -} - -static int l_log_close(lua_State *L) -{ - log_close(); - return 0; -} - -static int l_log_add_target(lua_State *L) -{ - int ret = log_add_target(luaL_checkstring(L,1)); - if(ret) { - lua_pushboolean(L, 0); - switch(ret) { - case -2: lua_pushstring(L, "memory error at log_add_target"); break; - case -3: lua_pushstring(L, "unknown log target"); break; - case -4: lua_pushstring(L, "this log target is only allowed once"); break; - default: lua_pushstring(L, "syntax error"); break; - } - return 2; - } - - lua_pushboolean(L, 1); - return 1; -} - -static int l_log_printf(lua_State *L) -{ - int numargs = lua_gettop(L); - if(numargs < 2) - return luaL_error(L, "log.printf too few arguments"); - - if(numargs > 2) { - lua_getglobal(L, "string"); - lua_pushliteral(L, "format"); - lua_gettable(L, -2); - lua_insert(L, 2); - lua_remove(L, -1); - lua_call(L, numargs - 1, 1); - } - - log_prio_t prio = luaL_checkinteger(L,1); - log_printf(prio, "%s", luaL_checkstring(L, 2)); - return 0; -} - -static const struct luaL_Reg log_funcs [] = { - { "init", l_log_init }, - { "close", l_log_close }, - { "add_target", l_log_add_target }, - { "printf", l_log_printf }, - { NULL, NULL } -}; - - -LUALIB_API int luaopen_log(lua_State *L) -{ -#if LUA_VERSION_NUM > 501 - lua_newtable(L); - luaL_setfuncs(L, log_funcs, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, LUA_LOGLIBNAME); -#else - luaL_register(L, LUA_LOGLIBNAME, log_funcs); -#endif - lua_pushliteral(L, "ERROR"); - lua_pushinteger(L, ERROR); - lua_settable(L, -3); - lua_pushliteral(L, "WARNING"); - lua_pushinteger(L, WARNING); - lua_settable(L, -3); - lua_pushliteral(L, "NOTICE"); - lua_pushinteger(L, NOTICE); - lua_settable(L, -3); - lua_pushliteral(L, "INFO"); - lua_pushinteger(L, INFO); - lua_settable(L, -3); - lua_pushliteral(L, "DEBUG"); - lua_pushinteger(L, DEBUG); - lua_settable(L, -3); - return 1; -} diff --git a/src/l_log.h b/src/l_log.h deleted file mode 100644 index 169b591..0000000 --- a/src/l_log.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_l_log_h_INCLUDED -#define RHCTL_l_log_h_INCLUDED - -#include - -#define LUA_LOGLIBNAME "log" -LUALIB_API int luaopen_log(lua_State *L); - -#endif diff --git a/src/l_sig_handler.c b/src/l_sig_handler.c deleted file mode 100644 index f0616b8..0000000 --- a/src/l_sig_handler.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include -#include - -#include "sig_handler.h" - -#include "l_sig_handler.h" - -static int l_signal_getfd(lua_State *L) -{ - if(!lua_istable(L, -1)) - luaL_error(L, "can't retreive signal fd"); - - lua_pushliteral(L, "fd"); - lua_gettable(L, -2); - return 1; -} - -static int l_signal_dirty(lua_State *L) -{ - lua_pushboolean(L, 0); - return 1; -} - -static int l_signal_init(lua_State *L) -{ - int sig_fd = signal_init(); - if(sig_fd < 0) - luaL_error(L, "error at signal init"); - - lua_newtable(L); - lua_pushliteral(L, "fd"); - lua_pushinteger(L, sig_fd); - lua_settable(L, -3); - lua_pushliteral(L, "getfd"); - lua_pushcfunction(L, l_signal_getfd); - lua_settable(L, -3); - lua_pushliteral(L, "dirty"); - lua_pushcfunction(L, l_signal_dirty); - lua_settable(L, -3); - return 1; -} - -static int l_signal_stop(lua_State *L) -{ - signal_stop(); - return 0; -} - -static int l_signal_handle(lua_State *L) -{ - int ret = signal_handle(); - lua_pushinteger(L, ret); - return 1; -} - -static const struct luaL_Reg signal_funcs [] = { - { "init", l_signal_init }, - { "stop", l_signal_stop }, - { "handle", l_signal_handle }, - { NULL, NULL } -}; - - -LUALIB_API int luaopen_signal(lua_State *L) -{ -#if LUA_VERSION_NUM > 501 - lua_newtable(L); - luaL_setfuncs(L, signal_funcs, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, LUA_SIGNALLIBNAME); -#else - luaL_register(L, LUA_SIGNALLIBNAME, signal_funcs); -#endif - return 1; -} diff --git a/src/l_sig_handler.h b/src/l_sig_handler.h deleted file mode 100644 index 310414a..0000000 --- a/src/l_sig_handler.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_l_signal_handler_h_INCLUDED -#define RHCTL_l_signal_handler_h_INCLUDED - -#include - -#define LUA_SIGNALLIBNAME "signal" -LUALIB_API int luaopen_signal(lua_State *L); - -#endif diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 721d7c0..0000000 --- a/src/log.c +++ /dev/null @@ -1,246 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#include "datatypes.h" - -#include -#include -#include -#include -#include - -#define SYSLOG_NAMES -#include - -#include "log.h" - -log_t stdlog; - -#include "log_targets.h" - -const char* log_prio_to_string(log_prio_t prio) -{ - switch(prio) { - case ERROR: return "ERROR"; - case WARNING: return "WARNING"; - case NOTICE: return "NOTICE"; - case INFO: return "INFO"; - case DEBUG: return "DEBUG"; - } - return "UNKNOWN"; -} - -log_target_type_t log_target_parse_type(const char* conf) -{ - if(!conf) - return TARGET_UNKNOWN; - - if(!strncmp(conf, "syslog", 6)) return TARGET_SYSLOG; - if(!strncmp(conf, "file", 4)) return TARGET_FILE; - if(!strncmp(conf, "stdout", 6)) return TARGET_STDOUT; - if(!strncmp(conf, "stderr", 6)) return TARGET_STDERR; - - return TARGET_UNKNOWN; -} - -int log_targets_target_exists(log_targets_t* targets, log_target_type_t type) -{ - if(!targets && !targets->first_) - return 0; - - log_target_t* tmp = targets->first_; - while(tmp) { - if(tmp->type_ == type) - return 1; - tmp = tmp->next_; - } - return 0; -} - -int log_targets_add(log_targets_t* targets, const char* conf) -{ - if(!targets) - return -1; - - log_target_t* new_target = NULL; - int duplicates_allowed = 0; - switch(log_target_parse_type(conf)) { - case TARGET_SYSLOG: new_target = log_target_syslog_new(); break; - case TARGET_FILE: new_target = log_target_file_new(); duplicates_allowed = 1; break; - case TARGET_STDOUT: new_target = log_target_stdout_new(); break; - case TARGET_STDERR: new_target = log_target_stderr_new(); break; - default: return -3; - } - if(!new_target) - return -2; - - if(!duplicates_allowed && log_targets_target_exists(targets, new_target->type_)) { - free(new_target); - return -4; - } - - const char* prioptr = strchr(conf, ':'); - if(!prioptr || prioptr[1] == 0) { - free(new_target); - return -1; - } - prioptr++; - if(!isdigit(prioptr[0]) || (prioptr[1] != 0 && prioptr[1] != ',')) { - free(new_target); - return -1; - } - new_target->max_prio_ = prioptr[0] - '0'; - if(new_target->max_prio_ > 0) - new_target->enabled_ = 1; - - if(new_target->init != NULL) { - const char* confptr = NULL; - if(prioptr[1] != 0) - confptr = prioptr+2; - - int ret = (*new_target->init)(new_target, confptr); - if(ret) { - free(new_target); - return ret; - } - } - - if(new_target->open != NULL) - (*new_target->open)(new_target); - - - if(!targets->first_) { - targets->first_ = new_target; - } - else { - log_target_t* tmp = targets->first_; - while(tmp->next_) - tmp = tmp->next_; - - tmp->next_ = new_target; - } - return 0; -} - -void log_targets_log(log_targets_t* targets, log_prio_t prio, const char* msg) -{ - if(!targets) - return; - - log_target_t* tmp = targets->first_; - while(tmp) { - if(tmp->log != NULL && tmp->enabled_ && tmp->max_prio_ >= prio) - (*tmp->log)(tmp, prio, msg); - - tmp = tmp->next_; - } -} - -void log_targets_clear(log_targets_t* targets) -{ - if(!targets) - return; - - while(targets->first_) { - log_target_t* tmp = targets->first_; - targets->first_ = tmp->next_; - if(tmp->close != NULL) - (*tmp->close)(tmp); - if(tmp->clear != NULL) - (*tmp->clear)(tmp); - free(tmp); - } -} - - -void log_init() -{ - stdlog.max_prio_ = 0; - stdlog.targets_.first_ = NULL; -} - -void log_close() -{ - log_targets_clear(&stdlog.targets_); -} - -void update_max_prio() -{ - log_target_t* tmp = stdlog.targets_.first_; - while(tmp) { - if(tmp->enabled_ && tmp->max_prio_ > stdlog.max_prio_) - stdlog.max_prio_ = tmp->max_prio_; - - tmp = tmp->next_; - } -} - -int log_add_target(const char* conf) -{ - if(!conf) - return -1; - - int ret = log_targets_add(&stdlog.targets_, conf); - if(!ret) update_max_prio(); - return ret; -} - -void log_printf(log_prio_t prio, const char* fmt, ...) -{ - if(stdlog.max_prio_ < prio) - return; - - static char msg[MSG_LENGTH_MAX]; - va_list args; - - va_start(args, fmt); - vsnprintf(msg, MSG_LENGTH_MAX, fmt, args); - va_end(args); - - log_targets_log(&stdlog.targets_, prio, msg); -} - -void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len) -{ - if(stdlog.max_prio_ < prio) - return; - - static char msg[MSG_LENGTH_MAX]; - - if(!buf) { - snprintf(msg, MSG_LENGTH_MAX, "(NULL)"); - } - else { - u_int32_t i; - int offset = snprintf(msg, MSG_LENGTH_MAX, "dump(%d): ", len); - if(offset < 0) - return; - char* ptr = &msg[offset]; - - for(i=0; i < len; i++) { - if(((i+1)*3) >= (MSG_LENGTH_MAX - offset)) - break; - snprintf(ptr, 3, "%02X ", buf[i]); - ptr+=3; - } - } - log_targets_log(&stdlog.targets_, prio, msg); -} diff --git a/src/log.h b/src/log.h deleted file mode 100644 index 872e1ee..0000000 --- a/src/log.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_log_h_INCLUDED -#define RHCTL_log_h_INCLUDED - -#define MSG_LENGTH_MAX 150 - -enum log_prio_enum { ERROR = 1, WARNING = 2, NOTICE = 3, - INFO = 4, DEBUG = 5 }; -typedef enum log_prio_enum log_prio_t; - -const char* log_prio_to_string(log_prio_t prio); - -enum log_target_type_enum { TARGET_SYSLOG , TARGET_STDOUT, TARGET_STDERR, TARGET_FILE , TARGET_UNKNOWN }; -typedef enum log_target_type_enum log_target_type_t; - -struct log_target_struct { - log_target_type_t type_; - int (*init)(struct log_target_struct* self, const char* conf); - void (*open)(struct log_target_struct* self); - void (*log)(struct log_target_struct* self, log_prio_t prio, const char* msg); - void (*close)(struct log_target_struct* self); - void (*clear)(struct log_target_struct* self); - int opened_; - int enabled_; - log_prio_t max_prio_; - void* param_; - struct log_target_struct* next_; -}; -typedef struct log_target_struct log_target_t; - - -struct log_targets_struct { - log_target_t* first_; -}; -typedef struct log_targets_struct log_targets_t; - -int log_targets_target_exists(log_targets_t* targets, log_target_type_t type); -int log_targets_add(log_targets_t* targets, const char* conf); -void log_targets_log(log_targets_t* targets, log_prio_t prio, const char* msg); -void log_targets_clear(log_targets_t* targets); - - -struct log_struct { - log_prio_t max_prio_; - log_targets_t targets_; -}; -typedef struct log_struct log_t; - -void log_init(); -void log_close(); -void update_max_prio(); -int log_add_target(const char* conf); -void log_printf(log_prio_t prio, const char* fmt, ...); -void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len); - -#endif diff --git a/src/log_targets.h b/src/log_targets.h deleted file mode 100644 index d0a85b1..0000000 --- a/src/log_targets.h +++ /dev/null @@ -1,349 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2015 Christian Pointner - * - * This file is part of rhctl. - * - * rhctl 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 - * any later version. - * - * rhctl 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 rhctl. If not, see . - */ - -#ifndef RHCTL_log_targets_h_INCLUDED -#define RHCTL_log_targets_h_INCLUDED - -#include - -static char* get_time_formatted() -{ - char* time_string; - time_t t = time(NULL); - if(t < 0) - time_string = "