structering core

This commit is contained in:
Axel Kittenberger 2018-03-22 09:14:13 +01:00
parent 6c0884a102
commit 7ebfbd91f6
6 changed files with 151 additions and 108 deletions

View File

@ -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 )

View File

@ -45,6 +45,8 @@
#include <lualib.h>
#include <lauxlib.h>
#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
'::::::::::::::::::::::*/

View File

@ -35,6 +35,7 @@
#include <lualib.h>
#include <lauxlib.h>
#include "smem.h"
/*
| Event types.

View File

@ -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
*/

95
core/smem.c Normal file
View File

@ -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 <axkibe@gmail.com>
*/
#include <stdlib.h>
#include <syslog.h>
#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;
}

19
core/smem.h Normal file
View File

@ -0,0 +1,19 @@
/*
| smem.h
|
| Simple "secured" memory management.
|
| License: GPLv2 (see COPYING) or any later version
| Authors: Axel Kittenberger <axkibe@gmail.com>
*/
#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