Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:20:30+00:00 2026-06-06T20:20:30+00:00

Alright so I’m working on a program written in c++ that is running as

  • 0

Alright so I’m working on a program written in c++ that is running as a daemon. It is primarily aiming Linux users, but I wish to include Windows (running as service) and Mac users as well.

I want the daemon to log whenever it is manually shut down. However it should not log shutdowns made by the system because of system halt or reboot.

Currently I’ve masked all signals and implemented some sort of handling using sigaction(). Before logging the shutdown, a function is also checking the system’s runlevel, and if it is 0, 1 0r 6 the logging is omitted. The way to check the runlevel is to run the command “runlevel” and process the output.

My problem is that the runlevel is not always what I would expect it to. I’m running Ubuntu, and when logged in as usual I’m in runlevel 2, and that’s the same when rebooting. When halting I sometimes get nothing as output from “runlevel”. Different Linux distros are using their own runlevels, so it’s not optimal for portability.

So is there a better way to determine if the system is halting? Also, is there a better way to catch interruptions, e.g. through exception handling etc?

I’ll paste a snippet of the code, if it is of any help here. Written in c++, using the Poco C++ libraries.

void MainApplication::signalHandler(int sig) {
#if defined(POCO_OS_FAMILY_UNIX)
    switch(sig) {
        case -1:
            struct sigaction act;
            act.sa_handler = signalHandler;
            sigemptyset(&act.sa_mask);
            act.sa_flags = 0;
            sigaction(SIGINT, &act, 0); //Better way to do this?
            sigaction(SIGQUIT, &act, 0);
            sigaction(SIGKILL, &act, 0);
            sigaction(SIGTERM, &act, 0);
            sigaction(SIGHUP, &act, 0);
            sigaction(SIGSTOP, &act, 0);
            sigaction(SIGTSTP, &act, 0);
            sigaction(SIGCONT, &act, 0);
            sigaction(SIGUSR1, &act, 0);
            sigaction(SIGUSR2, &act, 0);
            break;
        case SIGINT:
        case SIGQUIT:
        case SIGTSTP:
        case SIGHUP:
        case SIGKILL:
        case SIGTERM:
            //Log Shutdown!
            if (!isHalting())
                _instance->_database->logBypass(BYPASS_SHUTDOWN);
            terminateNicely(true);
            break;
        case SIGCONT:
            //Continued, means stopped
            break;
        case SIGUSR1:
            //Resetting Net Responsibility
            _instance->uninitialize();
            _instance->initialize(*_instance);
            break;
        case SIGUSR2:
            //Other action or just mask it
            break;
        default:
            //Caught signal
            break;
    }
#endif
}



bool MainApplication::isHalting() {
#if defined(POCO_OS_FAMILY_UNIX)
    string cmd("runlevel");
    vector<string> args;
    Poco::Pipe outPipe;
    ProcessHandle ph = Process::launch(cmd, args, 0, &outPipe, 0);
    ph.wait();
    Poco::PipeInputStream istr(outPipe);
    stringstream ss;
    Poco::StreamCopier::copyStream(istr, ss);
    int runlevel;
    ss.seekg(-2, ios::end);
    ss >> runlevel;
    return (runlevel == 0 || runlevel == 1 || runlevel == 6);
#else
    return false;
#endif
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:20:33+00:00Added an answer on June 6, 2026 at 8:20 pm

    I ended up going another route to solve the issue. Now I’m reading the computer’s boot history and compares it to the history of the software to see if the computer booted or halted without the software. That’s enough and will probably be even more accurate.

    The command used to determine the boot history in Linux is

    $ last reboot
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, this is driving me nuts because my regex is working on Rubular, but
Alright, So im working on a faily good sized program and im wanting to
Alright, so I'm writing this program that essentially batch runs other java programs for
Alright, quick question. A server is running in Eastern Time. PHP program needs to
Alright so my question is, how can I make a program that basically executes
Alright, so I'm about 90% sure that this isn't possible, but I thought I'd
Alright I don't see why this isnt working. It seems pretty simple. Here is
Alright so i need to open a .txt file that will be created in
Alright so I have no idea how to even begin doing this But basically
Alright so this is a bizarre cross platform thing that I'm experiencing with text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.