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:
@@ -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)
|
||||
{
|
||||
|
@@ -21,6 +21,22 @@ enum subid_status {
|
||||
SUBID_STATUS_ERROR = 3,
|
||||
};
|
||||
|
||||
/*
|
||||
* libsubid_init: initialize libsubid
|
||||
*
|
||||
* @progname: Name to display as program. If NULL, then "(libsubid)" will be
|
||||
* shown in error messages.
|
||||
* @logfd: Open file pointer to pass error messages to. If NULL, then
|
||||
* /dev/null will be opened and messages will be sent there. The
|
||||
* default if libsubid_init() is not called is stderr (2).
|
||||
*
|
||||
* This function does not need to be called. If not called, then the defaults
|
||||
* will be used.
|
||||
*
|
||||
* Returns false if an error occurred.
|
||||
*/
|
||||
bool libsubid_init(const char *progname, FILE *logfd);
|
||||
|
||||
/*
|
||||
* get_subuid_ranges: return a list of UID ranges for a user
|
||||
*
|
||||
|
Reference in New Issue
Block a user