Git Product home page Git Product logo

c-app-override-syslog-at-run-time's Introduction

This repo contains two applications to demonstrate the use of LD_PRELOAD to override the syslog method in a simple c app.

Pre-requisites

  • Docker
  • Docker dev container The apps are compiled and run in a docker container. The docker container is based on the debian image and contains the necessary tools to compile and run the apps.

The Simple app

The simple app is a simple c application that uses syslog to log messages to the syslog server. The app is compiled with the shared library zlog.so to override the syslog method at runtime. The shared library zlog.so intercepts the syslog calls and writes the log messages to the file system using the zlog library.

The Shared Library

The shared library zlog.so is a shared library that uses the zlog library to write log messages to the file system. The shared library is injected into the simple app at runtime using the LD_PRELOAD environment variable to override the syslog method.

Install the zlog library

To compile the shared library, you need to install the zlog library. The zlog library is a simple logging library that provides a simple API to write log messages to the file system. The zlog library is used by the shared library to write log messages to the file system. https://github.com/HardySimpson/zlog

  1. Download the zlog library from the github repository https://github.com/HardySimpson/zlog/releases

  2. Extract and install the zlog library to the /usr/local directory

tar -zxvf zlog-latest-stable.tar.gz
cd zlog-latest-stable/
make 
sudo make install
  1. Refresh the dynamic linker
sudo ldconfig

How to compile the apps

  1. Compile the simple app
gcc src/main.c -o app
  1. Compile the shared library
gcc -fPIC -shared zlog/zlog.c -o zlog.so -L /usr/local/lib -lzlog
  1. Run the app with the shared library to override the syslog method

The application takes its log configuration from the zlog.conf file. The zlog.conf file is located in /workspaces/debian/config/zlog.conf. The zlog.conf file contains the log configuration for the zlog library. The zlog library uses the log configuration to write log messages to the file system. Make the necessary changes to the location of the zlog.conf file in the zlog/zlog.c file.

LD_PRELOAD=./zlog.so ./app

you should see the log messages written to the file system in the /workspaces/debian/logs/simpleapp.log file.

2024-05-01 18:56:12 INFO [42253:zlog/zlog.c:24] zlog initialized
2024-05-01 18:56:12 INFO [42253:zlog/zlog.c:38] Hello 1 from syslog

2024-05-01 18:56:12 INFO [42253:zlog/zlog.c:38] Hello 2 from syslog

2024-05-01 18:56:12 INFO [42253:zlog/zlog.c:30] finishing zlog

c-app-override-syslog-at-run-time's People

Watchers

Rida KADDIR avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.