-
Notifications
You must be signed in to change notification settings - Fork 71
Add a compile-time option for a daemon binary #46
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[Unit] | ||
Description=PARSEC Service | ||
Documentation=https://github.com/parallaxsecond/parsec | ||
|
||
[Service] | ||
Type=notify | ||
NonBlocking=true | ||
ExecStart=/home/parsec/.cargo/bin/parsec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[Unit] | ||
Description=PARSEC Socket | ||
Documentation=https://github.com/parallaxsecond/parsec | ||
|
||
[Socket] | ||
ListenStream=/home/parsec/parsec.sock | ||
|
||
[Install] | ||
WantedBy=sockets.target |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it reasonable that we'll reach this point? if not =>
unreachable!();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we reach this point, it is because the
accept
method failed and we check if the failure comes from the fact that it was given a bad file descriptor. We gave it3
as a file descriptor but if the process was not launched by systemd,3
will map to nothing and trigger this error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also possible that the user wants to run the application as a service instead of socket activation. TBH, using fd 3 directly feels a bit dirty.
According to http://0pointer.de/blog/projects/socket-activation.html,
sd_listen_fds
would be a better solution, but mysd-notify
crate doesn't support that (yet?).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @lnicola!
Currently we only have the compile-time flag
systemd-daemon
to differentiate between a binary to be launched by systemd or by the command line. If compiled as a daemon and launched on the command line by the user, it will trigger this error.I agree that a better way would be to do it at runtime, the way shown in your link below and assuming that we can also do runtime checks on other daemon-dependant parts of our application and remove the compile-time flag.
For example, what would be the effect of
sd-notify
if not run as a daemon?The crate
systemd
has Rust wrappers around this function and other very useful functions that would allow us to do that. It is licensed under LGPL-2.1+ so we would have to check for compatibility with our Apache 2.0 license.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That shouldn't be needed in general, IMHO.
Do you mean
sd_notify::notify
? I haven't checked the implementation ofsd_listen_fds
, but readiness notification works like this:systemd
or a compatible service manager starts the daemon, it sets an well-known environment variable (NOTIFY_SOCKET
) with a Unix Domain Socket addresssystemd
by writing to that socketIn the case of
notify
, if the program is not launched bysystemd
, the variable won't be set, and the function will returnOk(())
, so the whole thing is a no-op.I don't think they're compatible. I can look into adding more functionality to the
sd-notify
crate. If you want an alternative tosd-notify
, there's https://github.com/lucab/libsystemd-rs, which is licensed under MIT/Apache 2.0. The reason I startedsd-notify
is lucab/libsystemd-rs#24 (i.e. having ~15 transitive dependencies, includingserde
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree now with this philosophy to make one binary that works both when run as an user or by
systemd
but something I forgot as well: ideally (for now) we would likeparsec
to work on as many OSes as possible. The compile-time flag is a way to make it work also on OSes that do not havesystemd
. And just having conditional compilation and target Linux only might not be enough if some Linux distributions do not havesystemd
?Thanks for the explanation, that is very good to know!
Even if we end up keeping the compile-time flag, it would be more robust and less dirty to call
sd_listen_fds
as done on the blog post. Like this, even if the flag is set the binary can be launched both ways.I checked the docs of
libsystemd-rs
and it does not seem to have the function.If you were to add a
sd_listen_fds
function to your crate, we would use it for sure 😋 !!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's totally fine to run such a binary on a non-
systemd
distro -- unless you're linking to the Clibsystemd
, which might prove problematic (and you also want to avoid it because of the licensing issue?).No promises, but I'll take a look to see what it would imply. I didn't implement FD passing in
sd-notify
because it's quite complex, and it's probably using the same mechanism.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, just got it, I did not know you could implement the function in Rust only without any
systemd
dependency, as you did fornotify
. In that case, the only conditional compilation checks would be on the OS which is good!Great, please ping me if you ever do 😃 !