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

cmd: unify die() across C programs #7362

Merged
merged 4 commits into from Sep 5, 2019
Merged

Conversation

zyga
Copy link
Collaborator

@zyga zyga commented Aug 27, 2019

Historically we inherited die() from ubuntu-core-launcher. It underwent a few
small changes but otherwise retained its function for print-and-quit helper.
This was true until system-shutdown was first implemented. There die existed as
a function with the same purpose but different implementation. The changes were
motivated by desire to fit the environment in which system-shutdown executes:
it runs as the last PID of the system and should not just exit. Therefore the
implementation called sync and reboot. The output of the message was also
formatted differently to fit other messages printed by the system at the same
time.

At some point, system-shutdown started to use all of libsnap-confine-private,
not just a single module. This presented a problem because now die() was
defined twice. Oddly, this went unnoticed until
#7358 was opened, trying to resolve the
problem.

After some inspection I decided to implement the following solution. There is
only one die(), it always does what it says on the tin. Die is now implemented
with a new function, sc_panic(), which is just like die but can be tweaked in
two ways: the way the error is printed and the way the process is exited. This
is exactly what system-shutdown needed to change.

The panic module comes with simple tests and documentation.

When die() dies, it doesn't give us much choice as to how to go.  While
reviewing a pull request snapcore#7358 we
realized that system-shutdown was defining die twice, once from
libsnap-confine-private, using the regular die-and-exit code, and once
internally with custom die-with-reboot.

This isn't great. We should just have one function and allow a process
to say how it wants to customize death.

This is what sc_panic() is. It's a drop-in replacement to die() (in
fact, die will be a thin wrapper around it). On top of die it offers a
pair of customization points: one for how to exit (e.g. you can call
reboot) and one for how to say something failed (you can print to
whatever is appropriate, e.g. to a file).

This patch contains just the new panic function along with documentation
and tests.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
The die() function is our common print-and-quit helper. With the
introduction of sc_panic() we have a flexible way to achieve that.
Since die() is so widespread it's easier to make it a wrapper for
sc_panic(). This patch does just that.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
Now that libsnap-confine-private offers a way to customize the behavior
of die() we can drop or implementation and configure the behavior of
sc_panic(), that backs the implementation of die()

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
static sc_panic_exit_fn panic_exit_fn = NULL;
static sc_panic_msg_fn panic_msg_fn = NULL;

void sc_panic(const char *fmt, ...) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Quick drive-by: maybe some else than "panic"? Panic smells a lot like a go panic() which will behave differently AFAICT (print stacktrace etc).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, the fact that it prints a stack trace isn't the essential feature. We could also print one if that would make you feel better. I'm somewhat -1 on this precisely because of error + panic parity that C and Go can have.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not arguing in favour of adding a stacktrace to the C code. Just that its confusing to use the same name for something different without any advantage in doing so (at least I don't see one).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I argue that the principal function of sc_panic is the same as panic, to abort execution without retaining regular control flow.

@mvo5
Copy link
Contributor

mvo5 commented Aug 29, 2019

Thanks for the PR - the solution looks interesting. But I think its likely that the shutdown utility will go away with UC20 and its also really rarely touched (last real change ~1y ago). So I would prefer if we would go with the simpler fix in #7358. I think the use-case(s) we have does not justfiy the extra code/API.

@zyga
Copy link
Collaborator Author

zyga commented Aug 30, 2019

@mvo5 that fix is incorrect, we should reboot, not exit in system-shutdown

@mvo5
Copy link
Contributor

mvo5 commented Sep 2, 2019

One critical piece of information that I was missing when I suggested to go with 7358 is that the helpers in "libsnapconfine-private" may call "die()" which will cause a program exit. But when die() is called in the context of system-shutdown we need it to behave differently. So I need to recheck this with fresh eyes - sorry that I did not realize this before.

Copy link
Collaborator

@bboozzoo bboozzoo left a comment

Choose a reason for hiding this comment

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

Can't really comment on naming. Otherwise LGTM.

cmd/libsnap-confine-private/panic.c Show resolved Hide resolved
@mvo5 mvo5 added this to the 2.42 milestone Sep 5, 2019
Copy link
Contributor

@stolowski stolowski left a comment

Choose a reason for hiding this comment

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

Looks good to me, and I'm ok with the naming and semantics. Two small comments.

cmd/libsnap-confine-private/panic.c Show resolved Hide resolved
cmd/system-shutdown/system-shutdown.c Show resolved Hide resolved
cmd/system-shutdown/system-shutdown.c Outdated Show resolved Hide resolved
cmd/system-shutdown: rename sync_and_reboot to sync_and_halt
@zyga zyga merged commit a5806f0 into snapcore:master Sep 5, 2019
zyga added a commit to zyga/snapd that referenced this pull request Sep 30, 2019
cmd: unify die() across C programs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants