sigaltstackのヘルプ・マニュアル
日本語 英語
sigaltstack --help
man sigaltstack
SIGALTSTACK(2) Linux Programmer’s Manual SIGALTSTACK(2)
名前
sigaltstack - シグナルスタックのコンテキストを設定・取得する
書式
#include
int sigaltstack(const stack_t *ss, stack_t *oss);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
sigaltstack(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
説明
sigaltstack() を使うと、プロセスは新しい代替シグナルスタックを定義した
り、既存の代替シグナルスタックの状態を取得できる。シグナルハンドラが 代
替 シグナルスタックを要求するように設定されていると (sigaction(2) 参照)
、ハンドラの実行中はそのシグナルスタックが使われる。
代替シグナルスタックを使う際の一般的な手順は、以下の通りである:
1. 代替シグナルスタックで使うメモリ領域を確保する。
2. sigaltstack() を使って、代替シグナルスタックの存在と場所をシステムに
知らせる。
3. sigaction(2) を使ってシグナルハンドラを確立する際、 SA_ONSTACK フラ
グを指定することにより、そのシグナルハンドラを代替シグナルスタック上
で実行することをシステムに知らせる。
ss 引き数は、新しいシグナルスタックを指定するために使う。また oss 引き
数は、現在確立されているシグナルスタックの情報を取得するために使う。 こ
の操作のうち 1 つだけを実行させるには、使用しない引き数を NULL に指定す
ればよい。引き数となる構造体は、以下のような型である:
typedef struct {
void *ss_sp; /* スタックのベースアドレス */
int ss_flags; /* フラグ */
size_t ss_size; /* スタックのバイト数 */
} stack_t;
新規の代替シグナルスタックを確立するには、 ss.ss_flags を 0 に設定し 、
ss.ss_sp と ss.ss_size にスタックの開始アドレスとスタックサイズを指定す
る。定数 SIGSTKSZ は、代替シグナルスタックが通常必要するサイズよりも 充
分大きく定義されている。また定数 MINSIGSTKSZ は、シグナルハンドラの実行
に必要な最小サイズに定義されている。
代替スタックでシグナルハンドラが起動された場合には、カーネルにより自 動
的に、ss.ss_sp で指定されたアドレスは動作しているハードウェアアーキテク
チャに適したアドレス境界に調整される。
既存のスタックを無効にするには、 ss.ss_flags を SS_DISABLE に指定する。
この場合、ss の他のフィールドは無視される。
oss が NULL 以外の場合、 oss に代替シグナルスタックの情報が返される。こ
れは (実質的に) sigaltstack() の呼び出しより先に行われる。 oss.ss_sp と
oss.ss_size フィールドにスタックの開始アドレスとスタックサイズが返され
る。 oss.ss_flags には以下のどちらかの値が返される:
SS_ONSTACK
プロセスが代替シグナルスタック上で実行されている (プロセスが既に
そのシグナルスタック上で実行されている場合は、それと同じシグナル
スタックには変更できない点に注意すること)。
SS_DISABLE
代替シグナルスタックが現在無効になっている。
返り値
sigaltstack() は成功した場合 0 を返す。失敗した場合は -1 を返して、エラ
ーを示す値に errno を設定する。
エラー
EFAULT ss または oss のどちらが、NULL 以外で、かつプロセスのアドレス空
間の外を指している。
EINVAL ss が NULL 以外で、ss_flags フィールドが SS_DISABLE 以外の 0 で
ない値になっている。
ENOMEM 新しい代替シグナルスタック (ss.ss_size) に指定したサイズが MIN-
STKSZ より小さい。
EPERM 代替シグナルスタックが有効であるときに変更を行おうとした (つまり
、プロセスが既に現在の代替シグナルスタック上で実行されていた)。
準拠
SUSv2, SVr4, POSIX.1-2001.
注意
代替シグナルスタックを使用する最もよくある場面は、 SIGSEGV シグナルを扱
うときである。 SIGSEGV はプロセスの通常のスタックが利用できる空間が使い
果たされた際に生成されるシグナルである。この場合には、 SIGSEGV 用のシグ
ナルハンドラをプロセスのスタック上では起動することができない。そのた め
、 このシグナルを扱おうとする場合には、代替シグナルスタックを使用しなけ
ればならない。
プロセスが標準のシグナルスタックを使い果たすことが予想される場合は、 代
替 シグナルスタックを確立すると便利である。例えば、スタックが最上位アド
レスから下位アドレス方向に非常にたくさん積まれてしまうことで、最下位 ア
ド レ ス から上位アドレス方向に積まれるヒープとぶつかってしまう場合や、
setrlimit(RLIMIT_STACK, &rlim) の呼び出しで確立された制限に達してしまっ
た 場合に、この様な事が起こる。標準のスタックを使い果たしてしまうと、カ
ーネルはプロセスに SIGSEGV シグナルを送る。このような状況では、代替シグ
ナルスタック上でしかシグナルをキャッチできない。
Linux がサポートする多くのハードウェアアーキテクチャでは、スタックは下
位アドレス方向に積まれる。 sigaltstack() はスタックが積まれる方向を自動
的に決定する。
代 替シグナルスタック上で実行されているシグナルハンドラから呼ばれる関数
も、代替シグナルハンドラを使う (プロセスが代替シグナルスタック上で実 行
さ れている場合、他のシグナルで呼び出されるハンドラもこの代替シグナルハ
ンドラを使う)。標準のスタックとは異なり、システムは代替シグナルスタック
を 自動的に拡張しない。代替シグナルスタック用に確保したサイズを越えた場
合、結果は予想できない。
execve(2) の呼び出しが成功すると、既存の全ての代替シグナルスタックが 削
除 される。 fork() 経由で作成された子プロセスは、親プロセスの代替シグナ
ルスタックの設定のコピーを継承する。
sigaltstack() は以前の sigstack() を置き換えるものである。過去プログ ラ
ム との互換性のため、glibc では sigstack() も提供している。新しいのアプ
リケーションは全て sigaltstack() を使って書くべきである。
歴史
4.2BSD には sigstack() システムコールがあった。この関数は少し異なった構
造 体を使っており、呼び出した側がスタックの積まれる方向を知っていなけれ
ばならないという大きな欠点があった。
例
以下のコードで sigaltstack() の使用法の一部を示す:
stack_t ss;
ss.ss_sp = malloc(SIGSTKSZ);
if (ss.ss_sp == NULL)
/* ハンドルエラー */;
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) == -1)
/* ハンドルエラー */;
関連項目
execve(2), setrlimit(2), sigaction(2), siglongjmp(3), sigsetjmp(3),
signal(7)
Linux 2008-10-04 SIGALTSTACK(2)
SIGALTSTACK(2) Linux Programmer’s Manual SIGALTSTACK(2)
NAME
sigaltstack - set and/or get signal stack context
SYNOPSIS
#include
int sigaltstack(const stack_t *ss, stack_t *oss);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sigaltstack(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
DESCRIPTION
sigaltstack() allows a process to define a new alternate signal stack
and/or retrieve the state of an existing alternate signal stack. An
alternate signal stack is used during the execution of a signal handler
if the establishment of that handler (see sigaction(2)) requested it.
The normal sequence of events for using an alternate signal stack is
the following:
1. Allocate an area of memory to be used for the alternate signal
stack.
2. Use sigaltstack() to inform the system of the existence and location
of the alternate signal stack.
3. When establishing a signal handler using sigaction(2), inform the
system that the signal handler should be executed on the alternate
signal stack by specifying the SA_ONSTACK flag.
The ss argument is used to specify a new alternate signal stack, while
the oss argument is used to retrieve information about the currently
established signal stack. If we are interested in 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 stack */
} stack_t;
To establish a new alternate signal stack, ss.ss_flags is set to zero,
and ss.ss_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 stack, and
the constant MINSIGSTKSZ defines the minimum size required to execute a
signal handler.
When a signal handler is invoked on the alternate stack, the kernel
automatically aligns the address given in ss.ss_sp to a suitable
address boundary for the underlying hardware architecture.
To disable an existing stack, specify ss.ss_flags as SS_DISABLE. In
this case, the remaining fields in ss are ignored.
If oss is not NULL, then it is used to return information about the
alternate signal stack which was in 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:
SS_ONSTACK
The process is currently executing on the alternate signal
stack. (Note that it is not possible to change the alternate
signal stack if the process is currently executing on it.)
SS_DISABLE
The alternate signal stack is currently disabled.
RETURN VALUE
sigaltstack() returns 0 on success, or -1 on failure with errno set to
indicate the error.
ERRORS
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 stack
(ss.ss_size) was less than MINSTKSZ.
EPERM An attempt was made to change the alternate signal stack while
it was active (i.e., the process was already executing on the
current alternate signal stack).
CONFORMING TO
SUSv2, SVr4, POSIX.1-2001.
NOTES
The most common usage of an alternate signal stack is to handle the
SIGSEGV signal that is generated if the space available for the normal
process stack is exhausted: in this case, a signal handler for SIGSEGV
cannot be invoked on the process stack; if we wish to handle it, we
must use an alternate signal stack.
Establishing an alternate signal stack is useful if 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 is on an alternate signal 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 handler executing on an alternate signal
stack will also use the alternate signal stack. (This also applies to
any handlers invoked for other signals while the process is executing
on the alternate signal stack.) Unlike the standard stack, the system
does not automatically extend the alternate signal stack. Exceeding
the allocated size of the alternate signal stack will lead to unpre-
dictable results.
A successful call to execve(2) removes any existing alternate signal
stack. A child process created via fork() inherits a copy of its par-
ent’s alternate signal stack settings.
sigaltstack() supersedes the older sigstack() call. For backwards com-
patibility, glibc also provides sigstack(). All new applications
should be written using sigaltstack().
History
4.2BSD had a sigstack() system call. It used a slightly different
struct, and had the major disadvantage that the caller had to know the
direction of stack growth.
EXAMPLE
The following code segment demonstrates the use of sigaltstack():
stack_t ss;
ss.ss_sp = malloc(SIGSTKSZ);
if (ss.ss_sp == NULL)
/* Handle error */;
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) == -1)
/* Handle error */;
SEE ALSO
execve(2), setrlimit(2), sigaction(2), siglongjmp(3), sigsetjmp(3),
signal(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-10-04 SIGALTSTACK(2)