[Bug 218266] Need article about Linux shutdown process

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--- Comment #6 from Rajesh (r.pandian@xxxxxxxxx) ---
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.




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux