SD-DAEMON(3)

πŸ’… SD-DAEMON(3) β€” sd-daemon πŸ’…

okay so bestie, welcome to the sd-daemon page from systemd 261.3 β€” no cap, this is the glow-up guide for new-style daemons and their log-level era ✨


πŸ“› NAME πŸ“›

sd-daemon, SD_EMERG, SD_ALERT, SD_CRIT, SD_ERR, SD_WARNING, SD_NOTICE, SD_INFO, SD_DEBUG β€” literally the APIs for new-style daemons, periodt πŸ’…


✨ SYNOPSIS ✨

#include <systemd/sd-daemon.h>
pkg-config --cflags --libs libsystemd

🌸 DESCRIPTION 🌸

so sd-daemon.h is part of libsystemd(3) and it's giving APIs for new-style daemons, as implemented by the systemd(1) service manager. main character energy fr fr πŸ‘‘

for the deets on the actual functions, go check out sd_listen_fds(3), sd_notify(3), sd_booted(3), sd_is_fifo(3), sd_watchdog_enabled(3), and sd_pidfd_get_inode_id(3) β€” they're all linked below too, iykyk πŸ’–

and on top of those functions, there's also a whole set of logging prefixes defined as macros. slay:

#define SD_EMERG   "<0>"  /* system is unusable */
#define SD_ALERT   "<1>"  /* action must be taken immediately */
#define SD_CRIT    "<2>"  /* critical conditions */
#define SD_ERR     "<3>"  /* error conditions */
#define SD_WARNING "<4>"  /* warning conditions */
#define SD_NOTICE  "<5>"  /* normal but significant condition */
#define SD_INFO    "<6>"  /* informational */
#define SD_DEBUG   "<7>"  /* debug-level messages */

these prefixes are intended to be used with stderr-based logging (or stdout-based logging, bestie, both count) as implemented by systemd 🫢 if a systemd service definition file is configured with StandardError=journal or StandardError=kmsg (and similar with StandardOutput=), you can use these prefixes to encode a log level in the lines you print. it's giving kernel printk()-style logging vibe check β€” see klogctl(2) for more info πŸ’€

the log levels are identical to syslog(3)'s log level system, no cap. to use these prefixes, simply prefix every line with one of these strings. and a line that is not prefixed? it gets logged at the default log level SD_INFO. periodt 🌈

πŸŽ€ Example 1. Hello World πŸŽ€

a daemon may log with the log level NOTICE by issuing this call:

fprintf(stderr, SD_NOTICE "Hello World!\n");

iconic, honestly πŸ’–


πŸ¦‹ NOTES πŸ¦‹

the functions described here are available as a shared library, which you can compile against and link to with the libsystemd pkg-config(1) file β€” ez pz ✨

⚠️ okay so this part matters, bestie: the code described here uses getenv(3), which is declared to be not multi-thread-safe. that means the code calling the functions described here must not call setenv(3) from a parallel thread. it's recommended to only do calls to setenv() from an early phase of the program when no other threads have been started. fr fr, don't skip this one πŸ”₯

🌟 SEE ALSO 🌟

systemd(1), sd_listen_fds(3), sd_notify(3), sd_booted(3), sd_is_fifo(3), sd_watchdog_enabled(3), sd_pidfd_get_inode_id(3), daemon(7), systemd.service(5), systemd.socket(5), fprintf(3), pkg-config(1) πŸ’œ


and that's the whole vibe, bestie β€” this page is brought to you by systemd 261.3, dated the SD-DAEMON(3) era πŸŽ€ go forth and log responsibly, ily 🫢✨

πŸ“¦ where this page comes from