1sg_init(3) sg_init(3)
2
3
4
6 sg_init, sg_snapshot, sg_shutdown, sg_drop_privileges - initialise lib‐
7 statgrab
8
10 #include <statgrab.h>
11
12
13 void sg_log_init (const char *properties_pfx, const char *env_name,
14 const char *argv0);
15
16 sg_error sg_init (int ignore_init_errors);
17
18 sg_error sg_snapshot (void);
19
20 sg_error sg_shutdown (void);
21
22 sg_error sg_drop_privileges (void);
23
25 sg_init() either initialises the statgrab library and sets the initial‐
26 isation counter to 1, if not already done, or increases the initialisa‐
27 tion counter. If called for the first time (in fact: if called when
28 the initialisation counter is 0), the constructor (initialisation rou‐
29 tine) of each registered component is invoked, some global variables
30 are initialised, requested mutual exclusion semaphores are initialised
31 and so on. sg_init() is protected by the mutex named "statgrab".
32
33 sg_log_init() allows some application-individual logging configuration.
34 It's intended for projects with several applications or commands which
35 might have different appenders. Mind that sg_log_init must be called
36 before sg_init, but after your application initialized logging frame‐
37 work.
38
39 sg_snapshot() is Win32 only and will probably disappear.
40
41 sg_shutdown() decrements the initialisation counter. If the counter
42 reaches zero, components' destructors are called, mutual exclusion sem‐
43 aphores (except "statgrab") are destroyed etc. sg_shutdown() is pro‐
44 tected by the mutex named "statgrab".
45
46 sg_drop_privileges() drops elevated privileges.
47
49 All functions return a statgrab error code. Either SG_ERROR_NONE when
50 everything was ok or the appropriate error code from an constructor/de‐
51 structor.
52
54 Typical initialization/deinitialization sequence when using with
55 log4cplus:
56
57 static void *l4cplus_initializer;
58
59 static void
60 cleanup_logging(void)
61 {
62 log4cplus_deinitialize(l4cplus_initializer);
63 }
64
65 int
66 main(int argc, char const *argv[])
67 {
68 l4cplus_initializer = log4cplus_initialize();
69 atexit((void (*)(void))cleanup_logging);
70
71 sg_log_init("saidar", "SAIDAR_LOG_PROPERTIES", argv[0]);
72 sg_init(1);
73 if(sg_drop_privileges() != 0) {
74 die("Failed to drop setuid/setgid privileges");
75 }
76
77 do_something();
78
79 sg_shutdown();
80
81 return 0;
82 }
83
84
86 statgrab(3)
87
89 ⟨https://libstatgrab.org/⟩
90
91
92
93libstatgrab 2019-10-03 sg_init(3)