Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] libsubid: don't print error messages on stderr by default #335

Merged
merged 1 commit into from
May 17, 2021

Conversation

hallyn
Copy link
Member

@hallyn hallyn commented May 8, 2021

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 [email protected]

@hallyn hallyn requested a review from brauner May 8, 2021 22:47
@hallyn
Copy link
Member Author

hallyn commented May 8, 2021

Drat, I meant this to be a 'draft' pull request.

Only compile-tested so far.

@hallyn hallyn changed the title libsubid: don't print error messages on stderr by default [WIP] libsubid: don't print error messages on stderr by default May 8, 2021
@hallyn hallyn marked this pull request as draft May 8, 2021 22:53
@hallyn
Copy link
Member Author

hallyn commented May 8, 2021

oh neato, a new 'convert to draft' button.

/*
* libsubid_init: initialize libsubid
*
* @progname: Name to display as program. If NULL, then "(libsubid)" will be

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would mention pointer is kept (not memory copy). Thus caller should not release memory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, actually I'm doing an strdup now instead. Prefer not to risk segfaults due to an accidental caller garbage collection.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

libsubid/api.c Outdated
shadow_logfd = logfd;
return;
}
shadow_logfd = fopen("/dev/null", O_RDWR);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"w" mode should be enough.

* 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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have "/dev/null" as a default? I think it makes perfect sense for a lib.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's more difficult in this approach. Would have to use an __init, which I'd prefer not to. When we switch to a proper log subsystem, we can make that change.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

}
shadow_logfd = fopen("/dev/null", O_RDWR);
if (!shadow_logfd) {
fprintf(stderr, "ERROR opening /dev/null for error messages. Using stderr.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is expected if caller explicitly requested "dev/null".
Perhaps error return code for init() function would make more sense here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll return false on error to help the library figure out what's going on.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update with error code.

I think return false should be enough here.
IMO, fprintf() in this case is unnecessary and maybe even undesirable (taking into account "/dev/null" was requested explicitly).

libsubid/api.c Outdated
@@ -38,6 +38,25 @@
#include "idmapping.h"
#include "subid.h"

char *Prog = "(libsubid)";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const char *

@alexey-tikhonov
Copy link

Hi @hallyn,

since your draft touches every single fprintf line anyway, wouldn't it be more convenient to just introduce a dedicated log() function that would just return in case To == /dev/null?
This would help to avoid opening "/dev/null" at all and hence avoid having any code to handle open() fail...

But general approach is up to you, of course. I don't object keeping it this way.
I would only ask to make "/dev/null" a default for libsubid.

@hallyn
Copy link
Member Author

hallyn commented May 15, 2021

@alexey-tikhonov I agree with your feeling that a proper log subsystem would be nicer, and I intend to do that "next". But this was doable with mostly just a 'sed' and no scary complications. As I think you're under a time pressure to release the package, this should suffice for now, right?

Closes shadow-maint#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 <[email protected]>
@hallyn hallyn force-pushed the 2021-05-08/stderr branch from d029cf7 to 2b22a69 Compare May 15, 2021 17:40
if (progname)
Prog = progname;
else
fprintf(stderr, "Out of memory");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false?

@alexey-tikhonov
Copy link

Yes, this would suffice. Thank you, @hallyn.

(Please take a note I didn't review changes other than in libsubid)

@hallyn hallyn marked this pull request as ready for review May 17, 2021 01:41
@hallyn hallyn merged commit f9831a4 into shadow-maint:master May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libsubid shouldn't log to stderr
2 participants