From 7ebfbd91f6d317893cff5a9c78e1f0a26e396116 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 22 Mar 2018 09:14:13 +0100 Subject: [PATCH] structering core --- CMakeLists.txt | 54 +++++++++++++++++----------- core/core.c | 81 ++---------------------------------------- core/inotify.c | 1 + core/lsyncd.h | 9 ----- core/smem.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/smem.h | 19 ++++++++++ 6 files changed, 151 insertions(+), 108 deletions(-) create mode 100644 core/smem.c create mode 100644 core/smem.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e27c31e..4e6e610 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,22 +1,26 @@ -# preamble +# Preamble project( Lsyncd ) cmake_minimum_required( VERSION 2.8 ) set( LSYNCD_VERSION 3.0.0-devel ) set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/" ) -# finding Lua +# Finding Lua find_package( Lua REQUIRED ) include_directories ( ${LUA_INCLUDE_DIR} ) +# Setting Lsyncd sources. +# Order here doesn't matter as being linked together. +set( LSYNCD_SRC + core/core.c + core/smem.c + luacode.c +) -# setting Lsyncd sources -set( LSYNCD_SRC core/core.c luacode.c ) - -# tell systemd via the sd-daemon library about the status of Lsyncd +# Tell systemd via the sd-daemon library about the status of Lsyncd. option( WITH_SYSTEMD "Communicate Lsyncd status to systemd" ON ) -# selecting the file notification mechanisms to compile against +# Selecting the file notification mechanisms to compile against. option( WITH_INOTIFY "Compile with inotify file notifications (Linux)" ON ) if( WITH_INOTIFY ) @@ -24,7 +28,7 @@ if( WITH_INOTIFY ) endif( WITH_INOTIFY ) -# generating the config.h file +# Generating the config.h file. configure_file ( "${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h" @@ -32,8 +36,11 @@ configure_file ( include_directories("${PROJECT_BINARY_DIR}") -# building and compiling the part of lsyncd written in Lua - +# Building and compiling the part of lsyncd written in Lua +# +# Order here matters as the scripts lua will be executed in this order +# on initialization of Lsyncd. +# set( LUA_CODE ${PROJECT_SOURCE_DIR}/mantle/array.lua ${PROJECT_SOURCE_DIR}/mantle/counter.lua @@ -61,31 +68,37 @@ set( LUA_CODE ${PROJECT_SOURCE_DIR}/mantle/userenv.lua ) -add_custom_command( OUTPUT luacode.c - COMMAND ${CMAKE_COMMAND} -E echo "Generating luacode linkable" - COMMAND ${LUA_EXECUTABLE} ${PROJECT_SOURCE_DIR}/bin2carray.lua luacode.out luacode luacode.c - DEPENDS luacode.out -) - +# Creates the Lua bytecode from Lua source add_custom_command( OUTPUT luacode.out COMMAND ${CMAKE_COMMAND} -E echo "Compiling luacode" COMMAND ${LUA_COMPILER} -o luacode.out ${LUA_CODE} DEPENDS ${LUA_CODE} ) -# the manpage +# Transforms the Lua bytecode into c constants +# It may be possible to use the linker directly to link binary +# Code into the resulting ELF-file, but this proofed to be more +# portable and easier. +add_custom_command( OUTPUT luacode.c + COMMAND ${CMAKE_COMMAND} -E echo "Generating luacode linkable" + COMMAND ${LUA_EXECUTABLE} ${PROJECT_SOURCE_DIR}/bin2carray.lua luacode.out luacode luacode.c + DEPENDS luacode.out +) + +# The manpage add_custom_target( manpage COMMAND ${CMAKE_COMMAND} -E echo "Updating the manpage" COMMAND a2x --format=manpage doc/manpage/lsyncd.1.txt DEPENDS doc/manpage/lsyncd.1.txt ) -# the test suite +# The test suite. add_custom_target( tests COMMAND echo "Running the tests" COMMAND echo "Note you are expected to:" COMMAND echo " * have lua-posix installed" COMMAND echo " * have a passwordless ssh access to localhost" + COMMAND echo "/tmp will be used as testbase" COMMAND ${LUA_EXECUTABLE} tests/schedule.lua COMMAND ${LUA_EXECUTABLE} tests/l4rsyncdata.lua COMMAND ${LUA_EXECUTABLE} tests/filter-rsync.lua @@ -94,13 +107,14 @@ add_custom_target( tests COMMAND ${LUA_EXECUTABLE} tests/churn-rsync.lua COMMAND ${LUA_EXECUTABLE} tests/churn-rsyncssh.lua COMMAND ${LUA_EXECUTABLE} tests/churn-direct.lua - COMMAND echo "Finished all successfull!" + COMMAND echo "Finished all successfully!" ) -# compiling and linking it all together +# Compiling and linking it all together. add_executable( lsyncd ${LSYNCD_SRC} ) target_link_libraries( lsyncd ${LUA_LIBRARIES} ) +# Installing. install( TARGETS lsyncd RUNTIME DESTINATION bin ) install( FILES doc/manpage/lsyncd.1 DESTINATION man ) diff --git a/core/core.c b/core/core.c index 09eeea8..b9f27ba 100644 --- a/core/core.c +++ b/core/core.c @@ -45,6 +45,8 @@ #include #include +#include "smem.h" + /* | The Lua part of Lsyncd */ @@ -394,85 +396,6 @@ printlogf0(lua_State *L, } -/*:::::::::::::::::::::::::::. -:: Simple memory management -'::::::::::::::::::::::::::::*/ - - -// FIXME call the Lua garbace collector in case of out of memory - -/* -| "Secured" calloc -*/ -extern void * -s_calloc( size_t nmemb, size_t size ) -{ - void * r = calloc( nmemb, size ); - - if( r == NULL ) - { - logstring0( LOG_ERR, "Error", "Out of memory!" ); - exit( -1 ); - } - - return r; -} - - -/* -| "Secured" malloc -*/ -extern void * -s_malloc( size_t size ) -{ - void * r = malloc( size ); - - if( r == NULL ) - { - logstring0( LOG_ERR, "Error", "Out of memory!" ); - exit( -1 ); - } - - return r; -} - - -/* -| "Secured" realloc -*/ -extern void * -s_realloc( void * ptr, size_t size ) -{ - void * r = realloc( ptr, size ); - - if( r == NULL ) - { - logstring0( LOG_ERR, "Error", "Out of memory!" ); - exit( -1 ); - } - - return r; -} - - -/* -| "Secured" strdup -*/ -extern char * -s_strdup( const char *src ) -{ - char *s = strdup( src ); - - if( s == NULL ) - { - logstring0( LOG_ERR, "Error", "Out of memory!" ); - exit( -1 ); - } - - return s; -} - - /*:::::::::::::::::::::. :: Pipes Management '::::::::::::::::::::::*/ diff --git a/core/inotify.c b/core/inotify.c index 8d6dfa4..de44c77 100644 --- a/core/inotify.c +++ b/core/inotify.c @@ -35,6 +35,7 @@ #include #include +#include "smem.h" /* | Event types. diff --git a/core/lsyncd.h b/core/lsyncd.h index 348e885..0cbd5f1 100644 --- a/core/lsyncd.h +++ b/core/lsyncd.h @@ -78,15 +78,6 @@ extern volatile sig_atomic_t hup; extern volatile sig_atomic_t term; -/** -* wrappers for heap management, they exit if out-of-memory. -*/ -extern void * s_calloc(size_t nmemb, size_t size); -extern void * s_malloc(size_t size); -extern void * s_realloc(void *ptr, size_t size); -extern char * s_strdup(const char *src); - - /* * Logging */ diff --git a/core/smem.c b/core/smem.c new file mode 100644 index 0000000..f2847fc --- /dev/null +++ b/core/smem.c @@ -0,0 +1,95 @@ +/* +| smem.c +| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +| +| +| Simple "secured" memory management. +| +| In future it might be an idea to call the lua garbage collecter in case memory allocation +| fails. However on Linux it's a mood point since practically a NULL is only returned +| when requesting a way too large memory block the system can ever handle, if the kernel +| runs out of memory it goes instead into oom-killer mode. +| +| +| This code assumes you have a 100 character wide display to view it (when tabstop is 4) +| +| License: GPLv2 (see COPYING) or any later version +| Authors: Axel Kittenberger +*/ + +#include +#include +#include "lsyncd.h" + + +/* +| "Secured" calloc +*/ +extern void * +s_calloc( size_t nmemb, size_t size ) +{ + void * r = calloc( nmemb, size ); + + if( r == NULL ) + { + logstring0( LOG_ERR, "Error", "Out of memory!" ); + exit( -1 ); + } + + return r; +} + + +/* +| "Secured" malloc +*/ +extern void * +s_malloc( size_t size ) +{ + void * r = malloc( size ); + + if( r == NULL ) + { + logstring0( LOG_ERR, "Error", "Out of memory!" ); + exit( -1 ); + } + + return r; +} + + +/* +| "Secured" realloc +*/ +extern void * +s_realloc( void * ptr, size_t size ) +{ + void * r = realloc( ptr, size ); + + if( r == NULL ) + { + logstring0( LOG_ERR, "Error", "Out of memory!" ); + exit( -1 ); + } + + return r; +} + + +/* +| "Secured" strdup +*/ +extern char * +s_strdup( const char *src ) +{ + char *s = strdup( src ); + + if( s == NULL ) + { + logstring0( LOG_ERR, "Error", "Out of memory!" ); + exit( -1 ); + } + + return s; +} + diff --git a/core/smem.h b/core/smem.h new file mode 100644 index 0000000..d6197a7 --- /dev/null +++ b/core/smem.h @@ -0,0 +1,19 @@ +/* +| smem.h +| +| Simple "secured" memory management. +| +| License: GPLv2 (see COPYING) or any later version +| Authors: Axel Kittenberger +*/ + +#ifndef LSYNCD_SMEM_H +#define LSYNCD_SMEM_H + +extern void * s_calloc(size_t nmemb, size_t size); +extern void * s_malloc(size_t size); +extern void * s_realloc(void *ptr, size_t size); +extern char * s_strdup(const char *src); + +#endif +