tags:

views:

217

answers:

4

When an assert() call fails, what is the exit code used, and where is it documented?

+1  A: 

It's implementation-specific. You could do this:

int main()
{
    assert(0);
}

Then run it:

> ./a.out
> echo $?
1 (<- or whatever)

This'll at least tell you what to expect for your setup. I'm gettitng 134 on a couple linux boxes with both gcc and g++.

You should always back your answers with a specific reference to the standard.
wilhelmtell
+11  A: 

The c99 standard states that assert calls abort and the abort stuff states this about the return code:

An implementation-defined form of the status unsuccessful termination is returned to the host environment by means of the function call raise(SIGABRT).

It's documented in section 7.2.1.1 (assert) and 7.20.4.1 (abort) of the c99 standard here.

Many UNIX systems will return 128 plus the signal number (SIGABRT is signal number 6) so you may get 134. Whatever you get, it should be documented by the C implementation.

For example, see here for gcc. Although it's quite silent on what gets returned to the calling environment. From the specific sections here:

Some choices are made by the library and operating system (or other environment when compiling for a freestanding environment); refer to their documentation for details.

And here:

The behavior of most of these points are dependent on the implementation of the C library, and are not defined by GCC itself.

So is the glibc doco here on program termination (specifically the exit status bit). It mentions conventions but no firm rules.

paxdiablo
This is a fantastic answer, and 134 is exactly what I am getting.
Matt Joiner
The linked to documentation for GCC doesn't appear to contain anything of interest, nor can I find the quote you've made.
Matt Joiner
You might change the glibc doco link to point directly to here unless I've missed something? http://www.gnu.org/s/libc/manual/html_node/Exit-Status.html#Exit-Status
Matt Joiner
@Matt, they were links underneath that link I gave. I've added the specific links to the quotes. The link I gave is the top level which gives implementation details for quite a lot of GCC so it's still quite handy, allowing you to drill down to lots of different parts (c, c++, etc).
paxdiablo
A: 

I was working with posix message queues i have got the same error mq_open was failed with errono 38 (ENOSYS).

The work arround is to rebuild the kenel with POSIX MESSGE QUEUE enabled in the kernel configuration.

This will build the kernel with POSIX message queue support and it worked for me.

Thank

Giridhara
I think you meant to put this answer on another question.
Matt Joiner
A: 

I can't find the actual spec for it (POSIX is kind of hard to search), but I did find a reference.

A thrown assertion results in an exit status that is the same as whatever EXIT_FAILURE expands to be (reference). Since you are dealing with Linux, you are also dealing with POSIX, which further defines the behavior from c99.

Tim Post