sg_init(3)

πŸ“¦
version 0.92.1 Β· 2 versions
, in alpine 3.24, arch rolling, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ NAME πŸŽ€

okay so this is sg_init(3) from libstatgrab, and the NAME squad is literally sg_init, sg_snapshot, sg_shutdown, and sg_drop_privileges β€” the whole vibe is initialise libstatgrab. also yep, sg_log_init shows up in the synopsis and description, bestie, keep scrolling 🫢

✨ SYNOPSIS ✨

       #include <statgrab.h>

       void sg_log_init (const char *properties_pfx, const char *env_name,
                        const char *argv0);

       sg_error sg_init (int ignore_init_errors);

       sg_error sg_snapshot (void);

       sg_error sg_shutdown (void);

       sg_error sg_drop_privileges (void);

πŸ’… DESCRIPTION πŸ’…

okay so sg_init() is the main character here: it either initialises the statgrab library and sets the initialisation counter to 1, if that hasn’t already happened, or it increases the initialisation counter. if it’s called for the first time (in fact: if it’s called when the initialisation counter is 0), the constructor (initialisation routine) of each registered component gets invoked, some global variables get initialised, requested mutual exclusion semaphores get initialised, and so on. sg_init() is protected by the mutex named "statgrab". no cap, that’s the whole system πŸ’…

sg_log_init() lets you do some application-individual logging configuration. it’s intended for projects with several applications or commands which might have different appenders. mind that sg_log_init must be called before sg_init, but after your application initialized logging framework. timing is everything, bestie ⏰

sg_snapshot() is Win32 only and will probably disappear. it’s giving legacy era, ngl πŸ’€

sg_shutdown() decrements the initialisation counter. if the counter reaches zero, components’ destructors are called, mutual exclusion semaphores (except "statgrab") are destroyed etc. sg_shutdown() is protected by the mutex named "statgrab". clean exit energy, periodt ✨

sg_drop_privileges() drops elevated privileges. safety slay πŸ›‘οΈ

🫢 RETURN VALUES 🫢

all functions return a statgrab error code. either SG_ERROR_NONE when everything was ok, or the appropriate error code from an constructor/destructor. that’s the whole return-value tea β˜•

🌸 EXAMPLE 🌸

typical initialization/deinitialization sequence when using with log4cplus:

       static void *l4cplus_initializer;

       static void
       cleanup_logging(void)
       {
              log4cplus_deinitialize(l4cplus_initializer);
       }

       int
       main(int argc, char const *argv[])
       {
           l4cplus_initializer = log4cplus_initialize();
           atexit((void (*)(void))cleanup_logging);

           sg_log_init("saidar", "SAIDAR_LOG_PROPERTIES", argv[0]);
           sg_init(1);
           if(sg_drop_privileges() != 0) {
            die("Failed to drop setuid/setgid privileges");
           }

           do_something();

           sg_shutdown();

           return 0;
       }

πŸ”₯ SEE ALSO πŸ”₯

statgrab(3) β€” go read that next, bestie, it’s giving required reading πŸ“š

🌈 WEBSITE 🌈

⟨https://libstatgrab.org/⟩ β€” the iconic little corner of the internet for this one πŸ¦‹


πŸ’– okay that’s the whole sg_init(3) page, and it comes from libstatgrab, dated 2019-10-03 β€” mwah, go init responsibly and remember to drop those privileges πŸ’