Linux-man Archive mirror
 help / color / mirror / Atom feed
From: bugzilla-daemon@kernel.org
To: linux-man@vger.kernel.org
Subject: [Bug 218266] Need article about Linux shutdown process
Date: Mon, 25 Dec 2023 10:19:28 +0000	[thread overview]
Message-ID: <bug-218266-11311-BPIAt6Olg4@https.bugzilla.kernel.org/> (raw)
In-Reply-To: <bug-218266-11311@https.bugzilla.kernel.org/>

https://bugzilla.kernel.org/show_bug.cgi?id=218266

--- Comment #6 from Rajesh (r.pandian@gmail.com) ---
H(In reply to Alexander from comment #5)
> Thanks for the code, I tested it. On my machine it does not respond to
> system reboots. That is, the SIGTERM signal does not reach the program. In
> the log there is only "ok..". If I send a signal explicitly (killall -sTERM
> a.out) “Received signal is: 15” appears in the log. I tested it on different
> kernels: both distribution and self-assembled ones. Both with user rights
> for the program and with superuser rights by setting set uid root. Maybe
> it's a distribution feature (I'm using Debian sid) that the signal reaches
> systemd, but not other programs?

Hi Alexander,

Merry xmas and I come bearing gifts! 

So signal is not a posix standard and hence it's not working across different
flavours.

Sigaction is the posix standard. There is a lot to sigaction and hence I won't
be going down that road. Instead let me give a a dummy code which works and
also a hint.

Your process below needs to rely on systemd. If I run as a standalone process
it sometimes misses the signal. So the realiable way is to configure the below
as a systemd. I have given below the systemd config too for my below code that
runs as service "dummy.service". Refer to systemd docs on how to configure. 

Note: Tested the below in Ubuntu 22.04.4, deian trixie sid (test)


 /* Code for handling sigaction 
 * Create those directories that we are removing e.g rmdir("session");
 */

#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

volatile sig_atomic_t FLAG; 
volatile sig_atomic_t fd;

void signalHandler(int signal) {
   FLAG=0;
   write(fd,"Shutdown\n",9);
   rmdir("delete-this");
   rmdir("session");
   fsync(fd);
   _exit(0);
}

int main() {
   FLAG = 1;
   struct sigaction act;
   act.sa_handler = &signalHandler;
   sigaction(SIGTERM,&act,NULL); //calling sigaction to follow posix std
   fd = fopen("/home/duke/output.log",O_WRONLY|O_CREAT|O_TRUNC,0644);
   while (FLAG) {
      write(fd,"Write\n",6);
      sleep(1);
   }
}


/* Systemd config

Note that you this is just a sample and should never be used as is in a
prodcution as I didn't give much thought about running this in production as
this is a demo */


[Unit]
Description="test"

[Service]
Type=simple
User=root
WorkingDirectory=/home/duke
ExecStart=/home/rajesh/rebooter
Restart= always
RestartSec=3

[Install]
WantedBy=reboot.target


Now you run systemctl start dummy.service and reboot and your directories
should go away and an entry in the output.log

-- 
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.

      parent reply	other threads:[~2023-12-25 10:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-15  6:29 [Bug 218266] New: Need article about Linux shutdown process bugzilla-daemon
2023-12-15 10:27 ` [Bug 218266] " bugzilla-daemon
2023-12-15 11:24 ` bugzilla-daemon
2023-12-15 11:34 ` bugzilla-daemon
2023-12-22 14:09 ` bugzilla-daemon
2023-12-22 19:14 ` bugzilla-daemon
2023-12-25 10:19 ` bugzilla-daemon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-218266-11311-BPIAt6Olg4@https.bugzilla.kernel.org/ \
    --to=bugzilla-daemon@kernel.org \
    --cc=linux-man@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).