lnicola / sd-notify

Apache License 2.0
45 stars 6 forks source link

Sending notification on behalf or another process #7

Open PradeepKiruvale opened 2 years ago

PradeepKiruvale commented 2 years ago

Hi,

I am using this crate for sending the notifications to systemd on behalf of another process. Below is my code snippet that is used for sending the notification. I collect the process ID of the process to be monitored by the watchdog program and send the notification to the systemd on behalf of that process. The code runs fine, without any errors. But the notifications are not reaching the system. So, the systemd keeps restarting the service after watchdog timeout. Please help me am I doing the right thing here or not.

fn notify_systemd(pid: u32) {

let result = sd_notify::notify(true, &[NotifyState::MainPid(pid)]);

dbg!(&result);

match result {

    Ok(()) => {

        println!("notify pid: {}", pid);
    }

    Err(e) => {
        println!("error: {:?}", e.to_string());
    }

}

Thanks, Pradeep

lnicola commented 2 years ago

Hello! You're using Type=notify, right?

I don't know what might be causing this, but it's an interesting problem since it involves the interaction between two different features. How are you enabling the watchdog, using WatchdogSec in the service file, or using the Watchdog notification?

PradeepKiruvale commented 2 years ago

I was not using Type=notify because the service start was hanging. I am using WatchdogSec to enable the watchdog feature. Also, I noticed from the documentation that sd_notify crate does not support sending notifications on behalf of another process. So, I feel it does not work for my use case.

lnicola commented 2 years ago

does not support sending notifications on behalf of another process

I wrote that a couple of years ago, I wonder what I meant :sweat_smile:. But MAINPID is not a notification on behalf of another process. It's a notification telling systemd to watch a different process instead of the current one.

And if you don't use Type=notify, I don't know if notifications do anything..

PradeepKiruvale commented 2 years ago

does not support sending notifications on behalf of another process

I wrote that a couple of years ago, I wonder what I meant 😅. But MAINPID is not a notification on behalf of another process. It's a notification telling systemd to watch a different process instead of the current one.

Ok, that means I can use these API's to send the notification on behalf of another process. Good to know that. But as I understand the sd-notification feature, the systemd creates a socket to communicate between the service it starts and systemd. Will it be possible to use the same socket endpoint to send the message on behalf of another service?. I read that its possible, provided if there are enough privileges. But how to set these privileges in the service file to allow another process to send the notification.

And if you don't use Type=notify, I don't know if notifications do anything..

Ok, I did not use because there is no way to tell the systemd that the ready state of a process using PID. So, the systemd hangs there till it receives the notification telling it's ready.

lnicola commented 2 years ago

Ok, that means I can use these API's to send the notification on behalf of another process. Good to know that.

You can't, because the docs say it's not supported. I was just saying that MAINPID is sent on behalf of the current, not another process.

PradeepKiruvale commented 2 years ago

Ok, that means I can use these API's to send the notification on behalf of another process. Good to know that.

You can't, because the docs say it's not supported. I was just saying that MAINPID is sent on behalf of the current, not another process.

Ha ok, thanks for the clarification.

lnicola commented 2 years ago

Yeah, so see sd_pid_notify, that's what I meant there.

PradeepKiruvale commented 2 years ago

Yeah, so see sd_pid_notify, that's what I meant there.

Yup, had a look. Needed some Rust crate that supports this. I mean pure Rust crate :).

lnicola commented 2 years ago

Well, we can implement it, but I wasn't sure about how the API should look (notify(Some(pid), ...) vs. notify_pid(pid, ...)).

PradeepKiruvale commented 2 years ago

I feel to be in line with C APIs we can implement notify and notify_pid as two separate APIs.

lnicola commented 2 years ago

So I just have to figure out how to send the PID...

maowen commented 2 years ago

One thing I learned is that I needed to set NotifyAccess=all so my child process was able to notify over the socket.