on_exit(3) bug in libc or manual page

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

 



Hello,

The http://man7.org/linux/man-pages/man3/on_exit.3.html tells:

The on_exit() function registers the given function to be called at
normal process termination, whether via exit(3) or via return from
the program's main().

When I compile and run the following code return from main() does
not seem to do what manual page is telling. Is this a bug in manual
or in glibc?

$ cat test.c
#include <stdio.h>
#include <stdlib.h>

struct data {
        int argc;
        char *argv;
};

static void run_on_exit(int exit_val __attribute__((__unused__)), void *arg)
{
        const struct data *ctl = (struct data *)arg;

        printf("on_exit: %d & %s\n", ctl->argc, ctl->argv);
}

int main(int argc, char **argv)
{
        struct data ctl = {
                .argc = argc,
                .argv = argv[0]
        };

        printf("argc: %d & %s\n", ctl.argc, ctl.argv);
        on_exit(run_on_exit, &ctl);

        if (1 < argc)
                exit(0);
        return 0;
}

$ ./a.out foobar
argc: 2 & ./a.out
on_exit: 2 & ./a.out

$ ./a.out
argc: 1 & ./a.out
on_exit: -168651984 & (null)

Above run is copy-paste from up to date (2019-03-31) Archlinux, that has
following glibc version.

$ pacman -Qi glibc
Name            : glibc
Version         : 2.28-5

-- 
Sami Kerola
http://www.iki.fi/kerolasa/



[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