LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2015, 02:02 PM   #1
cohmstede
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Rep: Reputation: Disabled
Linux log out / reboot / shutdown signal(s)


Is there a common signal issued to all programs on a log out / reboot / shutdown. I'm currently using SIGHUP but this signal may or may not be issued depending on the Linux configuration (Ubuntu issues SIGHUPs by default, Fedora does not.) Is there a signal I've missed?

Thanks
cohmstede
 
Old 09-17-2015, 10:26 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
logout is irrelevant for the running processes; at halt (or shutdown) they first get SIGTERM then SIGKILL
 
Old 09-17-2015, 10:55 PM   #3
cohmstede
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hmmm. I need to know both logout and shutdown so my application can delete it's temp files and flush it's logs before it closes. I'm not seeing a SIGTERM at logout or shutdown. I only get a SIGTERM when it is specifically sent to my application (via System Monitor for example.) I'll give SIGKILL a try.

Thanks
cohmstede
 
Old 09-17-2015, 11:33 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. logout: What do you mean by 'logout'?
2. SIGTERM: How did you test? Did you create signal-handler for SIGTERM? What is in the handler?
 
Old 09-18-2015, 12:07 AM   #5
cohmstede
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
1. Logout the linux user (closes all applications and brings up the user selection for logging in.)

2. I want to make sure you understand that this code works fine in Ubuntu but not in Fedora. Here are the significant parts where the signals are set up in the derive of the wxApp.

Code:
WhyCantIConnectApp::WhyCantIConnectApp() : wxApp(), m_frame(NULL)
{
#ifdef __UNIX__
	SetupLinuxSignals();
#endif
}

// The m_frame pointer is set from the creation of the main window.

#ifdef __UNIX__
void WhyCantIConnectApp::OnCleanupSignal()
{
	if(m_frame)
		m_frame->SystemQuickDestroyMainFrame();
}

void WhyCantIConnectApp::SetupLinuxSignals()
{
	struct sigaction sigHup;

   	sigHup.sa_handler = CleanupSignal;
   	sigemptyset(&sigHup.sa_mask);
   	sigHup.sa_flags = 0;
   	sigHup.sa_flags |= SA_RESTART;
   	sigaction(SIGHUP, &sigHup, 0);

   	struct sigaction sigTerm;

   	sigTerm.sa_handler = CleanupSignal;
   	sigemptyset(&sigTerm.sa_mask);
   	sigTerm.sa_flags = 0;
   	sigTerm.sa_flags |= SA_RESTART;
   	sigaction(SIGTERM, &sigTerm, 0);
}

void CleanupSignal(int sig)
{
	wxGetApp().OnCleanupSignal();
}
#endif
Thanks
cohmstede
 
Old 09-18-2015, 02:57 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. Oh, I see what's the misunderstanding: unix is a multiasking-multiuser system, countless users can be logged on in the same time, their logging in and out hardly matters.

2. Doesn't look proper C-code to me. Try something much simpler.
Code:
/* sigtest.c */
 
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static void handler (int signo);
static volatile int interrupted= 0;
int main (void)
{
    signal (SIGTERM, handler);

    printf ("Try \"kill %ld\"\n", (long)getpid());
    fflush (stdout);

    while (!interrupted) sleep (60);

    printf ("Exiting, interrupted=%d\n", interrupted);
    fflush (stdout);

    return 0;
}

static void handler (int signo)
{
    char msg[128];
    size_t msglen;

    interrupted= signo;

    msglen= sprintf (msg, ">>> Interrupted by signal %d <<<\n", signo);
    write (STDOUT_FILENO, msg, msglen);
}
Of course if you are testing reboot, you should redirect it into a file.

Last edited by NevemTeve; 09-18-2015 at 02:59 AM. Reason: %percent-mark disappeared
 
Old 09-18-2015, 01:26 PM   #7
cohmstede
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
First off, thank you for your help NevemTeve

I believe I have found my issue. This is a known systemd problem.

Bug #1448259
https://bugs.launchpad.net/bugs/1448259

It appears that SIGKILL is issued immediately after SIGTERM which does not allow enough time for my application to react.

The bug report supplies a simple application to test with (pretty much the same as yours NevemTeve.)

Code:
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void myFuncTerm();

int main(void)
{
    if (signal(SIGTERM, myFuncTerm) == SIG_ERR) {
        printf("SIGTERM error.\n");
    }
    printf("READY\n");
    while (1) {
        sleep(2);
    }
}

void myFuncTerm()
{
    FILE *f;
    int retVal;
    time_t current_time;
    char* c_time_string;
    current_time = time(NULL);
    c_time_string = ctime(&current_time);
    f = fopen("testing.txt", "a");
    if (f == NULL) {
        printf("File open error!\n");
        return;
    }
    retVal = fprintf(f, "SIGTERM! - %s\n", c_time_string);
    if (retVal < 0) {
        printf("File write error!\n");
        return;
    }
    fflush(f);
    fclose(f);
}
During my testing with this app, the majority of the time the file was never created. The few times the file was created, it was never written to.

systemd allows you to configure if SIGHUP signals should be sent. The SIGHUP is issued well before the shutdown so I'll use these until I see a resolution to this problem.

Thanks
cohmstede
 
Old 09-18-2015, 02:32 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(afaik, SNU is short for systemd is not unix -- same things that are true for unix might not be true for systemd)
 
Old 09-18-2015, 02:57 PM   #9
cohmstede
LQ Newbie
 
Registered: Nov 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
I suppose, but most Linux distros already use systemd or are planning to in the future. I need to support it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FC18: running a command on shutdown (or reboot) before every other shutdown operation P5music Fedora 3 04-23-2013 03:56 PM
Where I can locate Shutdown log for Linux Shutdown (RHL 5.1-2.6.18-53.el5PAE) msmallya Linux - General 2 04-23-2010 01:56 AM
Unable to shutdown/reboot server, no shutdown process running dctw Linux - Server 5 03-31-2010 05:46 AM
[FYI] "last -f <old wtmp> -x reboot shutdown" incorrect for last reboot and shutdown catkin Linux - General 1 03-25-2010 11:52 PM
Why do I loose Linux when I shutdown or reboot ? wertaylors Linux - General 4 04-06-2007 02:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration