libsubid: don't print error messages on stderr by default

Closes #325

Add a new subid_init() function which can be used to specify the
stream on which error messages should be printed.  (If you want to
get fancy you can redirect that to memory :)  If subid_init() is
not called, use stderr.  If NULL is passed, then /dev/null will
be used.

This patch also fixes up the 'Prog', which previously had to be
defined by any program linking against libsubid.  Now, by default
in libsubid it will show (subid).  Once subid_init() is called,
it will use the first variable passed to subid_init().

Signed-off-by: Serge Hallyn <serge@hallyn.com>
This commit is contained in:
Serge Hallyn
2021-05-08 17:42:14 -05:00
parent 3ac8d97825
commit 2b22a6909d
75 changed files with 311 additions and 191 deletions

View File

@ -32,12 +32,39 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <stdbool.h>
#include "subordinateio.h"
#include "idmapping.h"
#include "subid.h"
const char *Prog = "(libsubid)";
extern FILE * shadow_logfd;
bool libsubid_init(const char *progname, FILE * logfd)
{
if (progname) {
progname = strdup(progname);
if (progname)
Prog = progname;
else
fprintf(stderr, "Out of memory");
}
if (logfd) {
shadow_logfd = logfd;
return true;
}
shadow_logfd = fopen("/dev/null", "w");
if (!shadow_logfd) {
fprintf(stderr, "ERROR opening /dev/null for error messages. Using stderr.");
shadow_logfd = stderr;
return false;
}
return true;
}
static
int get_subid_ranges(const char *owner, enum subid_type id_type, struct subordinate_range ***ranges)
{