### Install elogind using Ninja Source: https://github.com/elogind/elogind/blob/main/README.md Install the built elogind components to their designated system locations using the Ninja build system. ```bash sudo ninja install ``` -------------------------------- ### Install elogind with a custom destination directory Source: https://github.com/elogind/elogind/blob/main/README.md Install elogind to a specific destination directory using the DESTDIR variable with the Ninja install command. ```bash DESTDIR=... ninja install ``` -------------------------------- ### Locate and Set LD_LIBRARY_PATH for Clang Sanitizer DSOs Source: https://github.com/elogind/elogind/blob/main/docs/TESTING_WITH_SANITIZERS.md After compiling with clang and `-shared-libsan`, binaries might complain about missing `libclang_rt.asan*` libraries. This example shows how to find the runtime DSO and set `LD_LIBRARY_PATH` to make it accessible. ```bash # Optionally locate the respective runtime DSO $ ldd build/elogind | grep libclang_rt.asan libclang_rt.asan-x86_64.so => not found libclang_rt.asan-x86_64.so => not found $ find /usr/lib* /usr/local/lib* -type f -name libclang_rt.asan-x86_64.so 2>/dev/null /usr/lib64/clang/7.0.1/lib/libclang_rt.asan-x86_64.so # Set the LD_LIBRARY_PATH accordingly export LD_LIBRARY_PATH=/usr/lib64/clang/7.0.1/lib/ # If the path is correct, the "not found" message should change to an actual path $ ldd build/elogind | grep libclang_rt.asan libclang_rt.asan-x86_64.so => /usr/lib64/clang/7.0.1/lib/libclang_rt.asan-x86_64.so (0x00007fa9752fc000) ``` -------------------------------- ### Function Parameter Naming Convention Example Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Illustrates the naming convention for function parameters, distinguishing between input, mutable, success return, and failure return parameters. Use 'ret_' prefix for success returns and 'reterr_' for failure returns. ```c static int foobar_frobnicate( Foobar *object, /* the associated mutable object */ const char *input, /* immutable input parameter */ char **ret_frobnicated, /* return parameter on success */ unsigned *reterr_line, /* return parameter on failure */ unsigned *reterr_column) { /* ditto */ … return 0; } ``` -------------------------------- ### Incorrect Order of Return Parameters Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Shows an example of an incorrect order for function parameters, where failure and success return parameters are not placed according to the specified convention. ```c static int do_something( const char *reterr_on_failure, const char *ret_on_success, const char *input); ``` -------------------------------- ### Build elogind using Makefile wrapper Source: https://github.com/elogind/elogind/blob/main/README.md Use the Makefile for IDE compatibility (like CLion or VSCode). For maximum control, direct Meson/Ninja usage is recommended. ```bash make DEBUG=YES ``` -------------------------------- ### Build elogind using Meson and Ninja Source: https://github.com/elogind/elogind/blob/main/README.md Standard build command for elogind. It configures the build environment and then proceeds with the build using Ninja. ```bash meson build/ && ninja -C build ``` -------------------------------- ### Run all tests with Ninja Source: https://github.com/elogind/elogind/blob/main/README.md Execute all defined test suites for the elogind project using the Ninja build system. ```bash ninja test ``` -------------------------------- ### Run Unit Tests Source: https://github.com/elogind/elogind/blob/main/docs/CODE_QUALITY.md Execute unit tests using Meson. Consider running with sudo for tests requiring privileges, or with specific flags for unsafe or slow tests. Flags can be configured for existing build trees. ```bash meson test -C build sudo meson test -C build meson setup -Dtests=unsafe meson setup -Dslow-tests=true meson configure -C build -D... ``` -------------------------------- ### Change elogind configuration options Source: https://github.com/elogind/elogind/blob/main/README.md After initial configuration, use this command to modify build arguments. Running `meson configure` without arguments lists available options. ```bash meson configure -Darg=value... build/ ``` -------------------------------- ### Run Integration Tests Source: https://github.com/elogind/elogind/blob/main/docs/CODE_QUALITY.md Execute the full integration test suite, which involves building OS images and running tests. ```bash ./test/run-integration-tests.sh ``` -------------------------------- ### Configure mkosi for Sanitizers Source: https://github.com/elogind/elogind/blob/main/docs/TESTING_WITH_SANITIZERS.md Add this configuration to `mkosi.local.conf` to enable Address and Undefined Behavior Sanitizers when building with mkosi. This passes the sanitizers option to meson's `b_sanitize`. ```ini [Content] Environment=SANITIZERS=address,undefined ``` -------------------------------- ### Configure 'video' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'video' group. Use this option to specify a custom GID. ```meson # option('video-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "video" group') ``` -------------------------------- ### Debugging systemd-homed with Environment Variables Source: https://github.com/elogind/elogind/blob/main/docs/ENVIRONMENT.md This snippet demonstrates how to run an additional copy of systemd-homed for debugging or development purposes by setting specific environment variables. These variables control debug suffixes, homework paths, root directories, and record directories. ```sh SYSTEMD_HOME_DEBUG_SUFFIX=foo \ SYSTEMD_HOMEWORK_PATH=/home/lennart/projects/systemd/build/systemd-homework \ SYSTEMD_HOME_ROOT=/home.foo/ \ SYSTEMD_HOME_RECORD_DIR=/var/lib/systemd/home.foo/ \ /home/lennart/projects/systemd/build/systemd-homed ``` -------------------------------- ### Check API Documentation Source: https://github.com/elogind/elogind/blob/main/docs/CODE_QUALITY.md Compare exported symbols of shared libraries with man pages to identify undocumented symbols. This command requires a Meson-built tree. ```bash ninja -C build check-api-docs ``` -------------------------------- ### Update Hardware Database Source: https://github.com/elogind/elogind/blob/main/docs/CODE_QUALITY.md Automatically download and import PCI, USB, and OUI databases, as well as autosuspend quirks, into the hardware database. This command requires a Meson-built tree. ```bash ninja -C build update-hwdb ninja -C build update-hwdb-autosuspend ``` -------------------------------- ### Run a specific target with Ninja Source: https://github.com/elogind/elogind/blob/main/README.md Execute a particular build target defined in the Ninja build system. ```bash ninja -v some/target ``` -------------------------------- ### Use EXIT_SUCCESS and EXIT_FAILURE for main return Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md When returning a status code from the main() function, prefer using EXIT_SUCCESS and EXIT_FAILURE as defined by libc for clarity and portability. ```c return EXIT_SUCCESS; ``` -------------------------------- ### Configure 'systemd-resolve' user UID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the systemd-resolve user. Use this option to specify a custom UID. ```meson # option('systemd-resolve-uid', type : 'integer', value : 0, # description : 'soft-static allocation for the systemd-resolve user') ``` -------------------------------- ### Build a static version of libelogind Source: https://github.com/elogind/elogind/blob/main/README.md Add this Meson option during configuration to build a static library alongside the shared library. Options include 'pic', 'true', or 'no-pic'. ```bash -Dstatic-libelogind=pic ``` -------------------------------- ### Configure 'tape' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'tape' group. Use this option to specify a custom GID. ```meson # option('tape-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "tape" group') ``` -------------------------------- ### Create an elogind tarball Source: https://github.com/elogind/elogind/blob/main/README.md Command to create a compressed tar archive of the elogind source code from a git repository. ```bash git archive --format=tar --prefix=elogind-241/ v241 | xz > elogind-241.tar.xz ``` -------------------------------- ### Configure 'systemd-timesync' user UID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the systemd-timesync user. Use this option to specify a custom UID. ```meson # option('systemd-timesync-uid', type : 'integer', value : 0, # description : 'soft-static allocation for the systemd-timesync user') ``` -------------------------------- ### Configure 'utmp' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'utmp' group. Use this option to specify a custom GID. ```meson # option('utmp-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "utmp" group') ``` -------------------------------- ### Configure 'systemd-network' user UID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the systemd-network user. Use this option to specify a custom UID. ```meson # option('systemd-network-uid', type : 'integer', value : 0, # description : 'soft-static allocation for the systemd-network user') ``` -------------------------------- ### Configure 'lp' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'lp' group. Use this option to specify a custom GID. ```meson # option('lp-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "lp" group') ``` -------------------------------- ### Configure 'users' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'users' group. Use this option to specify a custom GID. ```meson # option('users-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "users" group') ``` -------------------------------- ### Update Man Page Rules Source: https://github.com/elogind/elogind/blob/main/docs/CODE_QUALITY.md Update the Meson rules for automatically building man pages from docbook XML files. This command requires a Meson-built tree. ```bash ninja -C build update-man-rules ``` -------------------------------- ### Configure 'render' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'render' group. Use this option to specify a custom GID. ```meson # option('render-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "render" group') ``` -------------------------------- ### Configure elogind with a specific cgroup controller Source: https://github.com/elogind/elogind/blob/main/README.md Use this option when the target init system and cgroup controller are not yet active during the build process. It informs elogind which controller will be in charge. ```bash --with-cgroup-controller=openrc ``` -------------------------------- ### Correct Order of Return Parameters Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Demonstrates the preferred order for function parameters, placing failure return parameters (`reterr_`) last and success return parameters (`ret_`) immediately before them. ```c static int do_something( const char *input, const char *ret_on_success, const char *reterr_on_failure); ``` -------------------------------- ### Runtime Sanitizer Options for GCC/Clang Source: https://github.com/elogind/elogind/blob/main/docs/TESTING_WITH_SANITIZERS.md Set these environment variables to configure the runtime behavior of Address Sanitizer and Undefined Behavior Sanitizer for more useful debugging output. These are used after compiling with `-Db_sanitize=address,undefined`. ```bash ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1 UBSAN_OPTIONS=print_stacktrace=1:print_summary=1:halt_on_error=1 ``` -------------------------------- ### Function Definition Style Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Prefer defining functions with the opening brace on the same line as the function signature. ```c void foo() { } ``` -------------------------------- ### Configure 'systemd-journal' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the systemd-journal group. Use this option to specify a custom GID. ```meson # option('systemd-journal-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the systemd-journal group') ``` -------------------------------- ### C Structure Initialization with Initializers Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use C99 structure initializers for initializing stack-allocated structs instead of memzero() or memset(). This is shorter, cleaner, and faster. ```c struct foobar t = { .foo = 7, .bar = "bazz", }; ``` ```c struct foobar t; zero(t); t.foo = 7; t.bar = "bazz"; ``` -------------------------------- ### Configure 'sgx' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'sgx' group. Use this option to specify a custom GID. ```meson # option('sgx-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "sgx" group') ``` -------------------------------- ### Configure 'wheel' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the soft-static allocation for the 'wheel' group. Use this option to specify a custom GID. ```meson # option('wheel-gid', type : 'integer', value : 0, # description : 'soft-static allocation for the "wheel" group') ``` -------------------------------- ### Using Safe Memory Allocation Wrappers Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Prefer safe wrappers like alloca_safe(), strdupa_safe(), and strndupa_safe() over their direct counterparts. These wrappers include safety assertions to prevent stack overruns. ```c alloca_safe() ``` ```c strdupa_safe() ``` ```c strndupa_safe() ``` -------------------------------- ### Else Block Formatting Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Place the 'else' keyword on the same line as the closing brace of the preceding 'if' block. ```c if (foobar) { find(); waldo(); } else dont_find_waldo(); ``` -------------------------------- ### C Variable Declaration and Initialization Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Declare local variables at the top of a block or where they can be initialized. Avoid large declaration lists. 'int r' is an exception for state variables. ```c { uint64_t a; int r; r = frobnicate(&a); if (r < 0) … uint64_t b = a + 1, c; r = foobarify(a, b, &c); if (r < 0) … const char *pretty = prettify(a, b, c); … } ``` -------------------------------- ### Combined Log and Return Operation Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use this pattern for operations that should log errors and return the error code. It simplifies error handling by combining the operation, check, and logging into a single statement. ```c r = operation(...); if (r < 0) return log_(error|warning|notice|...)_errno(r, "Failed to ...: %m"); ``` -------------------------------- ### Compile with Shared Sanitizer Libraries for Clang Source: https://github.com/elogind/elogind/blob/main/docs/TESTING_WITH_SANITIZERS.md When using clang, pass `-shared-libasan` to CFLAGS and CXXFLAGS to ensure sanitizer libraries are linked dynamically. This is crucial for integration tests when an instrumented library is loaded by a standard application. ```bash CFLAGS=-shared-libasan CXXFLAGS=-shared-libasan ``` -------------------------------- ### Run Coccinelle Semantic Patch Scripts Source: https://github.com/elogind/elogind/blob/main/docs/CODE_QUALITY.md Execute all provided Coccinelle semantic patch scripts. Note that the output may contain false positives. ```bash ./coccinelle/run-coccinelle.sh ``` -------------------------------- ### Safe Stack String Allocation Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md When allocating fixed-size strings on the stack, use standard macros like LINE_MAX or count the maximum size carefully. Avoid arbitrary small buffer sizes like char buf[256]. ```c char buf[256] ``` -------------------------------- ### Enum for Public API Size Enforcement Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use the _SD_ENUM_FORCE_S64() macro in public API enum definitions to ensure a consistent 64-bit size and maintain ABI compatibility. ```c #define _SD_ENUM_FORCE_S64() // Forces the size of the enum to be signed 64-bit wide. ``` -------------------------------- ### C Yoda Comparison Style Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Prefer readable comparisons like 'if (a == 7)' over less readable Yoda comparisons like 'if (7 == a)'. ```c if (a == 7) ``` ```c if (7 == a) ``` -------------------------------- ### String Concatenation with strjoina or strjoin Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md For concatenating multiple strings, use strjoina() or strjoin() instead of asprintf() for better performance, especially in loops. Note that strjoina() is not suitable for inner loops. ```c strjoina() ``` ```c strjoin() ``` -------------------------------- ### Function Return Type Formatting Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Define function return types as a whole, attached to the function name, rather than on a separate line. ```c const char* foo(const char *input); ``` -------------------------------- ### C Boolean Conversion Check Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use C's downgrade-to-bool feature for expressions that are actually booleans. For numeric variables with more than two semantic values, explicitly check for non-zero using '!= 0' for clarity. ```c if (b) ``` ```c if (b != 0) ``` -------------------------------- ### Code Block Separation and Error Handling Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use single empty lines to separate code blocks. Avoid empty lines between function calls and their immediate error handling. ```c /* → empty line here is good */ r = some_function(…); /* → empty line here would be bad */ if (r < 0) return log_error_errno(r, "Some function failed: %m"); /* → empty line here is good */ ``` -------------------------------- ### Configure 'tty' group GID Source: https://github.com/elogind/elogind/blob/main/meson_options.txt Sets the numeric GID of the 'tty' group. The default value is 5. ```meson # option('tty-gid', type : 'integer', value : 5, # description : 'the numeric GID of the "tty" group') ``` -------------------------------- ### Convert libc error to elogind style Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use the RET_NERRNO() helper to convert libc-style error returns (-1 with errno) to elogind's negative error code style. This is useful for system calls like unlink() and open(). ```c r = RET_NERRNO(unlink(t)); ``` ```c r = RET_NERRNO(open("/some/file", O_RDONLY|O_CLOEXEC)); ``` -------------------------------- ### Function Declaration Formatting Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md When breaking a function declaration over multiple lines, use double indentation for the parameter list. ```c void some_function( int foo, bool bar, char baz) { int a, b, c; ``` -------------------------------- ### C Infinite Loop Construct Source: https://github.com/elogind/elogind/blob/main/docs/CODING_STYLE.md Use 'for (;;)' for endless loops instead of 'while (1)' or 'while (true)' to avoid discussions about the correct always-true expression. ```c for (;;) ```