<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: i cannot trap the SIGTERM when someone kills my process in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031311#M93825</link>
    <description>Can you use "tusc -fp" on the demon to see what is happening?</description>
    <pubDate>Tue, 03 Jul 2007 08:58:58 GMT</pubDate>
    <dc:creator>Dennis Handly</dc:creator>
    <dc:date>2007-07-03T08:58:58Z</dc:date>
    <item>
      <title>i cannot trap the SIGTERM when someone kills my process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031310#M93824</link>
      <description>&lt;BR /&gt;i am using &lt;BR /&gt;&lt;BR /&gt;kill 9897&lt;BR /&gt;kill -15 7897&lt;BR /&gt;&lt;BR /&gt;and its not catching the SIGTERM signal &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;this is the function daemonize :&lt;BR /&gt;&lt;BR /&gt;static void daemonize( const char *lockfile )&lt;BR /&gt;{&lt;BR /&gt;    pid_t pid, sid, parent;&lt;BR /&gt;    int lfp = -1; &lt;BR /&gt;    char * sstring="";&lt;BR /&gt;    &lt;BR /&gt;    log_message(LOG_FILE,"iniciar al demonio");&lt;BR /&gt;       &lt;BR /&gt;    /* already a daemon */&lt;BR /&gt;    if ( getppid() == 1 ) return;&lt;BR /&gt;&lt;BR /&gt;    /* Create the lock file as the current user */&lt;BR /&gt;    if ( lockfile &amp;amp;&amp;amp; lockfile[0] ) {&lt;BR /&gt;        lfp = open(lockfile,O_RDWR|O_CREAT,0640);&lt;BR /&gt;        if ( lfp &amp;lt; 0 ) {               &lt;BR /&gt;            sscanf( sstring, "incapaz crear el archivo de la cerradura  %s, code=%d (%s)", lockfile, errno, strerror(errno)); &lt;BR /&gt;            log_message(LOG_FILE,sstring);            &lt;BR /&gt;            exit(EXIT_FAILURE);            &lt;BR /&gt;        }&lt;BR /&gt;    }    &lt;BR /&gt;   /* Drop user if there is one, and we were run as root */&lt;BR /&gt;    if ( getuid() == 0 || geteuid() == 0 ) {&lt;BR /&gt;        struct passwd *pw = getpwnam(RUN_AS_USER);&lt;BR /&gt;        if ( pw ) {&lt;BR /&gt;            log_message(LOG_FILE, "fijar a usuario a " RUN_AS_USER);    &lt;BR /&gt;           &lt;BR /&gt;            setuid( pw-&amp;gt;pw_uid );&lt;BR /&gt;        }&lt;BR /&gt;    }    &lt;BR /&gt;    /* Trap signals that we expect to recieve */ &lt;BR /&gt;    signal(SIGCHLD,child_handler);&lt;BR /&gt;    signal(SIGUSR1,child_handler);&lt;BR /&gt;    signal(SIGALRM,child_handler);&lt;BR /&gt;    signal(SIGHUP, child_handler); &lt;BR /&gt;    signal(SIGTERM,child_handler);&lt;BR /&gt;    &lt;BR /&gt;    /* Fork off the parent process */&lt;BR /&gt;    pid = fork();&lt;BR /&gt;    if (pid &amp;lt; 0) {   &lt;BR /&gt;        sscanf( sstring, "incapaz bifurcar demonio, código  =%d (%s)", errno, strerror(errno)); &lt;BR /&gt;        log_message(LOG_FILE,sstring);  &lt;BR /&gt;        exit(EXIT_FAILURE);&lt;BR /&gt;    }&lt;BR /&gt;    &lt;BR /&gt;   /* If we got a good PID, then we can exit the parent process. */&lt;BR /&gt;    if (pid &amp;gt; 0) {&lt;BR /&gt;        /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or&lt;BR /&gt;           for two seconds to elapse (SIGALRM).  pause() should not return. */&lt;BR /&gt;        alarm(2);&lt;BR /&gt;        pause();&lt;BR /&gt;        exit(EXIT_FAILURE);&lt;BR /&gt;    }&lt;BR /&gt;    &lt;BR /&gt;   /* At this point we are executing as the child process */&lt;BR /&gt;    parent = getppid();&lt;BR /&gt;&lt;BR /&gt;    /* Cancel certain signals */    &lt;BR /&gt;    signal(SIGCHLD,SIG_DFL); /* A child process dies */&lt;BR /&gt;    signal(SIGTSTP,SIG_IGN); /* Various TTY signals */&lt;BR /&gt;    signal(SIGTTOU,SIG_IGN);&lt;BR /&gt;    signal(SIGTTIN,SIG_IGN);&lt;BR /&gt;    signal(SIGHUP, child_handler); &lt;BR /&gt;    signal(SIGTERM,child_handler);&lt;BR /&gt;   // signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */&lt;BR /&gt;    //signal(SIGTERM,SIG_DFL); /* Die on SIGTERM */&lt;BR /&gt;    &lt;BR /&gt;    /* Change the file mode mask */&lt;BR /&gt;    umask(0);&lt;BR /&gt;&lt;BR /&gt;    /* Create a new SID for the child process */&lt;BR /&gt;    sid = setsid();&lt;BR /&gt;    if (sid &amp;lt; 0) {    &lt;BR /&gt;        sscanf( sstring, "incapaz crear una nueva sesión, código  %d (%s)", errno, strerror(errno)); &lt;BR /&gt;        log_message(LOG_FILE,sstring);  &lt;BR /&gt;        exit(EXIT_FAILURE);&lt;BR /&gt;    }    &lt;BR /&gt;    /*Change the current working directory.  This prevents the current directory from being locked; hence not being able to remove it.*/&lt;BR /&gt;    if ((chdir("/")) &amp;lt; 0) {    &lt;BR /&gt;        sscanf( sstring, "incapaz cambiar el directorio a  %s, code %d (%s)","/", errno, strerror(errno) ); &lt;BR /&gt;        log_message(LOG_FILE,sstring); &lt;BR /&gt;        exit(EXIT_FAILURE);&lt;BR /&gt;    }&lt;BR /&gt;    &lt;BR /&gt;    /* Redirect standard files to /dev/null */&lt;BR /&gt;    freopen( "/dev/null", "r", stdin);    &lt;BR /&gt;    freopen( "/dev/null", "w", stdout);&lt;BR /&gt;    freopen( "/dev/null", "w", stderr);&lt;BR /&gt;&lt;BR /&gt;    /* Tell the parent process that we are A-okay */&lt;BR /&gt;    kill( parent, SIGUSR1 );  &lt;BR /&gt;    &lt;BR /&gt;   log_message(LOG_FILE,"el salir daemonize la función");&lt;BR /&gt;          &lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;this is the signal handler :&lt;BR /&gt;&lt;BR /&gt;static void child_handler(int signum)&lt;BR /&gt;{&lt;BR /&gt;    switch(signum) &lt;BR /&gt;   {&lt;BR /&gt;     case SIGALRM: exit(EXIT_FAILURE); break;&lt;BR /&gt;     case SIGUSR1: exit(EXIT_SUCCESS); break;&lt;BR /&gt;     case SIGCHLD: exit(EXIT_FAILURE); break;      &lt;BR /&gt;     case SIGHUP:      &lt;BR /&gt;        log_message(LOG_FILE,"a Terminar el demonio::::::::SIGHUP");&lt;BR /&gt;      exit(EXIT_SUCCESS);&lt;BR /&gt;        break;&lt;BR /&gt;     case SIGTERM: &lt;BR /&gt;        log_message(LOG_FILE,"a Terminar el demonio::::::::SIGTERM");&lt;BR /&gt;      exit(0);&lt;BR /&gt;        break;            &lt;BR /&gt;    }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;this is the main ()&lt;BR /&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt; daemonize();&lt;BR /&gt; while(1) sleep(1); /* run */&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 03 Jul 2007 06:09:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031310#M93824</guid>
      <dc:creator>Pedro Dinis</dc:creator>
      <dc:date>2007-07-03T06:09:42Z</dc:date>
    </item>
    <item>
      <title>Re: i cannot trap the SIGTERM when someone kills my process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031311#M93825</link>
      <description>Can you use "tusc -fp" on the demon to see what is happening?</description>
      <pubDate>Tue, 03 Jul 2007 08:58:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031311#M93825</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-07-03T08:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: i cannot trap the SIGTERM when someone kills my process</title>
      <link>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031312#M93826</link>
      <description>hi &lt;BR /&gt;&lt;BR /&gt;thanks for replying&lt;BR /&gt;&lt;BR /&gt;it is working now, i had a problem with the log function, now it is logging when the daemon gets a SIGTERM.</description>
      <pubDate>Wed, 04 Jul 2007 14:44:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/i-cannot-trap-the-sigterm-when-someone-kills-my-process/m-p/4031312#M93826</guid>
      <dc:creator>Pedro Dinis</dc:creator>
      <dc:date>2007-07-04T14:44:18Z</dc:date>
    </item>
  </channel>
</rss>

