Seth Woolley's Man Viewer

sigaltstack(2) - sigaltstack, sigaltstack - set and/or get signal stack context - man 2 sigaltstack

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

SIGALTSTACK(2)             Linux Programmer's Manual            SIGALTSTACK(2)

       sigaltstack - set(7,n,1 builtins) and/or get signal(2,7) stack context

       #include <signal.h>

       int sigaltstack(const stack_t *ss, stack_t *oss);

       sigaltstack  allows  a  process  to define a new alternate signal(2,7) stack
       and/or retrieve the state of an existing alternate  signal(2,7)  stack.   An
       alternate signal(2,7) stack is used during the execution of a signal(2,7) handler
       if(3,n) the establishment of that handler (see sigaction(2)) requested it.

       The normal sequence of events for using an alternate  signal(2,7)  stack  is
       the following:

       1.     Allocate  an  area of memory to be used for the alternate signal(2,7)

       2.     Use sigaltstack to inform the system of the existence and  loca-
              tion of the alternate signal(2,7) stack.

       3.     When  establishing  a signal(2,7) handler using sigaction, inform the
              system that the signal(2,7) handler should be executed on the  alter-
              nate signal(2,7) stack by specifying the SA_ONSTACK flag.

       The  ss argument is used to specify a new alternate signal(2,7) stack, while
       the oss argument is used to retrieve information  about  the  currently
       established  signal(2,7) stack.  If we are interested in(1,8) performing just one
       of these tasks then the other argument can be specified as NULL.   Each
       of these arguments is a structure of the following type:

              typedef struct {
                  void  *ss_sp;     /* Base address of stack */
                  int    ss_flags;  /* Flags */
                  size_t ss_size;   /* Number of bytes in(1,8) stack */
              } stack_t;

       To  establish a new alternate signal(2,7) stack, ss.ss_flags is set(7,n,1 builtins) to zero,
       and ss.sp_sp and ss.ss_size specify the starting address  and  size  of
       the  stack.   The  constant  SIGSTKSZ  is defined to be large enough to
       cover the usual size requirements for an alternate  signal(2,7)  stack,  and
       the constant MINSIGSTKSZ defines the minimum size required to execute a
       signal(2,7) handler.

       To disable an existing stack, specify ss.ss_flags  as  SS_DISABLE.   In
       this case, the remaining fields in(1,8) ss are ignored.

       If  oss  is  not  NULL, then it is used to return information about the
       alternate signal(2,7) stack which was in(1,8) effect prior to the call to sigalt-
       stack.   The  oss.ss_sp  and  oss.ss_size  fields  return  the starting
       address and size of that stack.  The oss.ss_flags may return either  of
       the following values:

              The  process  is  currently  executing  on  the alternate signal(2,7)
              stack.  (Note that it is not possible to  change  the  alternate
              signal(2,7) stack if(3,n) the process is currently executing on it.)

              The alternate signal(2,7) stack is currently disabled.

       sigaltstack  returns  0  on success, or -1 on failure with errno set(7,n,1 builtins) to
       indicate the error.

       EFAULT Either ss or oss is not NULL and points to an  area  outside  of
              the process's address space.

       EINVAL ss  is not NULL and the ss_flags field contains a non-zero value
              other than SS_DISABLE.

       ENOMEM The  specified  size  of  the   new   alternate   signal(2,7)   stack
              (ss.ss_size) was less(1,3) than MINSTKSZ.

       EPERM  An  attempt  was made to change the alternate signal(2,7) stack while
              it was active (i.e., the process was already  executing  on  the
              current alternate signal(2,7) stack).

       The following code segment demonstrates the use of sigaltstack:

              stack_t ss;

              ss.ss_sp = malloc(SIGSTKSZ);
              if(3,n) (ss.ss_sp == NULL)
                  /* Handle error(8,n) */;
              ss.ss_size = SIGSTKSZ;
              ss.ss_flags = 0;
              if(3,n) (sigaltstack(&ss, NULL) == -1)
                  /* Handle error(8,n) */;

       Establishing  an  alternate signal(2,7) stack is useful if(3,n) a process expects
       that it may exhaust its standard stack.  This may occur,  for  example,
       because  the stack grows so large that it encounters the upwardly grow-
       ing heap, or it  reaches  a  limit  established  by  a  call  to  setr-
       limit(RLIMIT_STACK,  &rlim).   If  the standard stack is exhausted, the
       kernel sends the process a SIGSEGV signal.  In these circumstances  the
       only way to catch this signal(2,7) is on an alternate signal(2,7) stack.

       On  most  hardware  architectures supported by Linux, stacks grow down-
       wards.  sigaltstack automatically takes account  of  the  direction  of
       stack growth.

       Functions called from a signal(2,7) handler executing on an alternate signal(2,7)
       stack will also use the alternate signal(2,7) stack.  (This also applies  to
       any  handlers  invoked for other signals while the process is executing
       on the alternate signal(2,7) stack.)  Unlike the standard stack, the  system
       does  not  automatically  extend the alternate signal(2,7) stack.  Exceeding
       the allocated size of the alternate signal(2,7) stack will  lead  to  unpre-
       dictable results.

       A  successful  call  to  execve  removes  any existing alternate signal(2,7)

       sigaltstack supersedes the older sigstack call.  For backwards compati-
       bility,  glibc  also provides sigstack.  All new applications should be
       written using sigaltstack.

       BSD 4.2 had a sigstack() system call.  It  used  a  slightly  different
       struct,  and  had as major disadvantage that the caller had to know the
       direction of stack growth.

       SUSv2, SVr4, POSIX 1003.1-2001.

       execve(2),  setrlimit(2),  sigaction(2),  siglongjmp(3),  sigsetjmp(3),

Linux 2.4                         2001-09-27                    SIGALTSTACK(2)

References for this manual (incoming links)