.\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992 .\" and Copyright (c) 2002, 2004, 2005, 2008, 2010 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Modified by Michael Haardt .\" Modified 1993-07-23 by Rik Faith .\" Modified 1996-01-13 by Arnt Gulbrandsen .\" Modified 1996-01-22 by aeb, following a remark by .\" Tigran Aivazian .\" Modified 1996-04-14 by aeb, following a remark by .\" Robert Bihlmeyer .\" Modified 1996-10-22 by Eric S. Raymond .\" Modified 2001-05-04 by aeb, following a remark by .\" Håvard Lygre .\" Modified 2001-04-17 by Michael Kerrisk .\" Modified 2002-06-13 by Michael Kerrisk .\" Added note on nonstandard behavior when SIGCHLD is ignored. .\" Modified 2002-07-09 by Michael Kerrisk .\" Enhanced descriptions of 'resource' values .\" Modified 2003-11-28 by aeb, added RLIMIT_CORE .\" Modified 2004-03-26 by aeb, added RLIMIT_AS .\" Modified 2004-06-16 by Michael Kerrisk .\" Added notes on CAP_SYS_RESOURCE .\" .\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included .\" coverage of getrusage(2), has been split, so that the latter .\" is now covered in its own getrusage.2. .\" .\" Modified 2004-11-16, mtk: A few other minor changes .\" Modified 2004-11-23, mtk .\" Added notes on RLIMIT_MEMLOCK, RLIMIT_NPROC, and RLIMIT_RSS .\" to "CONFORMING TO" .\" Modified 2004-11-25, mtk .\" Rewrote discussion on RLIMIT_MEMLOCK to incorporate kernel .\" 2.6.9 changes. .\" Added note on RLIMIT_CPU error in older kernels .\" 2004-11-03, mtk, Added RLIMIT_SIGPENDING .\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit. .\" 2005-07-28, mtk, Added descriptions of RLIMIT_NICE and RLIMIT_RTPRIO .\" 2008-05-07, mtk / Peter Zijlstra, Added description of RLIMIT_RTTIME .\" 2010-11-06, mtk: Added documentation of prlimit() .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated 1997-02-22, HANATAKA Shinya .\" Updated and Modified 2001-06-02, Yuichi SATO .\" Updated and Modified 2001-08-18, Yuichi SATO .\" Updated and Modified 2002-08-25, Yuichi SATO .\" Updated and Modified 2004-01-17, Yuichi SATO .\" Updated and Modified 2004-12-30, Yuichi SATO .\" Updated and Modified 2005-09-10, Yuichi SATO .\" Updated and Modified 2005-10-11, Akihiro MOTOKI .\" Updated and Modified 2005-10-27, Akihiro MOTOKI .\" Updated and Modified 2006-04-15, Akihiro MOTOKI, Catch up to LDP v2.29 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05 .\" Updated 2008-10-13, Akihiro MOTOKI, LDP v3.11 .\" Updated 2013-05-01, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" Updated 2013-07-22, Akihiro MOTOKI .\" .TH GETRLIMIT 2 2020\-11\-01 Linux "Linux Programmer's Manual" .SH 名前 getrlimit, setrlimit, prlimit \- 資源の制限を取得/設定する .SH 書式 \fB#include \fP .br \fB#include \fP .PP \fBint getrlimit(int \fP\fIresource\fP\fB, struct rlimit *\fP\fIrlim\fP\fB);\fP .br \fBint setrlimit(int \fP\fIresource\fP\fB, const struct rlimit *\fP\fIrlim\fP\fB);\fP .PP \fBint prlimit(pid_t \fP\fIpid\fP\fB, int \fP\fIresource\fP\fB, const struct rlimit *\fP\fInew_limit\fP\fB,\fP .br \fB struct rlimit *\fP\fIold_limit\fP\fB);\fP .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP \fBprlimit\fP(): _GNU_SOURCE .SH 説明 \fBgetrlimit\fP() と \fBsetrlimit\fP() は、資源 (resource) の制限 (limit) の設定と取得を行う。 各リソースには、それに対応するソフトリミットとハードリミットがあり、 \fIrlimit\fP 構造体で定義される: .PP .in +4n .EX struct rlimit { rlim_t rlim_cur; /* ソフトリミット */ rlim_t rlim_max; /* ハードリミット (rlim_cur より小さくない) */ }; .EE .in .PP ソフトリミットは、カーネルが対応するリソースに対して課す制限値である。 ハードリミットはソフトリミットの上限として働く。 特権を持たないプロセスは、ソフトリミットの値を 0 からハードリミットの範囲に設定することと、 ハードリミットを下げることのみができる (一度下げたハードリミットは上げられない)。 特権プロセス (Linux では初期ユーザー名前空間で \fBCAP_SYS_RESOURCE\fP ケーパビリティ (capability) を持つプロセス) は ソフトリミットとハードリミットを自由に変更できる。 .PP 値 \fBRLIM_INFINITY\fP はリソースに制限がないことを表す (この値は \fBgetrlimit\fP() が返す構造体と \fBsetrlimit\fP() に渡す構造体の両方で使用される)。 .PP \fIresource\fP 引数は次のいずれか 1 つである。 .TP \fBRLIMIT_AS\fP .\" since 2.0.27 / 2.1.12 This is the maximum size of the process's virtual memory (address space). The limit is specified in bytes, and is rounded down to the system page size. This limit affects calls to \fBbrk\fP(2), \fBmmap\fP(2), and \fBmremap\fP(2), which fail with the error \fBENOMEM\fP upon exceeding this limit. In addition, automatic stack expansion fails (and generates a \fBSIGSEGV\fP that kills the process if no alternate stack has been made available via \fBsigaltstack\fP(2)). Since the value is a \fIlong\fP, on machines with a 32\-bit \fIlong\fP either this limit is at most 2\ GiB, or this resource is unlimited. .TP \fBRLIMIT_CORE\fP This is the maximum size of a \fIcore\fP file (see \fBcore\fP(5)) in bytes that the process may dump. When 0 no core dump files are created. When nonzero, larger dumps are truncated to this size. .TP \fBRLIMIT_CPU\fP This is a limit, in seconds, on the amount of CPU time that the process can consume. When the process reaches the soft limit, it is sent a \fBSIGXCPU\fP signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program. If the process continues to consume CPU time, it will be sent \fBSIGXCPU\fP once per second until the hard limit is reached, at which time it is sent \fBSIGKILL\fP. (This latter point describes Linux behavior. Implementations vary in how they treat processes which continue to consume CPU time after reaching the soft limit. Portable applications that need to catch this signal should perform an orderly termination upon first receipt of \fBSIGXCPU\fP.) .TP \fBRLIMIT_DATA\fP .\" commits 84638335900f1995495838fe1bd4870c43ec1f67 .\" ("mm: rework virtual memory accounting"), .\" f4fcd55841fc9e46daac553b39361572453c2b88 .\" (mm: enable RLIMIT_DATA by default with workaround for valgrind). This is the maximum size of the process's data segment (initialized data, uninitialized data, and heap). The limit is specified in bytes, and is rounded down to the system page size. This limit affects calls to \fBbrk\fP(2), \fBsbrk\fP(2), and (since Linux 4.7) \fBmmap\fP(2), which fail with the error \fBENOMEM\fP upon encountering the soft limit of this resource. .TP \fBRLIMIT_FSIZE\fP これはプロセスが作成できるファイルサイズの最大値 (バイト単位) である。 このサイズを超えてファイルを拡張すると、 \fBSIGXFSZ\fP シグナルを送る。 デフォルトでは、このシグナルはプロセスを終了する。 プロセスをキャッチすることもできるが、 関連するシステムコール (\fBwrite\fP(2), \fBtruncate\fP(2) など) はエラー \fBEFBIG\fP で失敗する。 .TP \fBRLIMIT_LOCKS\fP (Linux 2.4.0 から 2.4.24 まで) .\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65 このプロセスが実行できる \fBflock\fP(2) ロック数と \fBfcntl\fP(2) リース数の合計値の上限である。 .TP \fBRLIMIT_MEMLOCK\fP This is the maximum number of bytes of memory that may be locked into RAM. This limit is in effect rounded down to the nearest multiple of the system page size. This limit affects \fBmlock\fP(2), \fBmlockall\fP(2), and the \fBmmap\fP(2) \fBMAP_LOCKED\fP operation. Since Linux 2.6.9, it also affects the \fBshmctl\fP(2) \fBSHM_LOCK\fP operation, where it sets a maximum on the total bytes in shared memory segments (see \fBshmget\fP(2)) that may be locked by the real user ID of the calling process. The \fBshmctl\fP(2) \fBSHM_LOCK\fP locks are accounted for separately from the per\-process memory locks established by \fBmlock\fP(2), \fBmlockall\fP(2), and \fBmmap\fP(2) \fBMAP_LOCKED\fP; a process can lock bytes up to this limit in each of these two categories. .IP In Linux kernels before 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process. Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock. .TP \fBRLIMIT_MSGQUEUE\fP (Linux 2.6.8 以降) This is a limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process. This limit is enforced for \fBmq_open\fP(3). Each message queue that the user creates counts (until it is removed) against this limit according to the formula: .IP Since Linux 3.5: .IP .EX bytes = attr.mq_maxmsg * sizeof(struct msg_msg) + min(attr.mq_maxmsg, MQ_PRIO_MAX) * sizeof(struct posix_msg_tree_node)+ /* オーバーヘッド分 */ attr.mq_maxmsg * attr.mq_msgsize; /* メッセージデータ分 */ .EE .IP Linux 3.4 and earlier: .IP .EX bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) + /* オーバーヘッド分 */ attr.mq_maxmsg * attr.mq_msgsize; /* メッセージデータ分 */ .EE .IP ここで \fIattr\fP は \fImq_attr\fP 構造体であり、 \fBmq_open\fP(3) の第 4 引数として指定される。 また、構造体 \fImsg_msg\fP と \fIposix_msg_tree_node\fP はカーネル内部の構造体である。 .IP 上記の式での「オーバーヘッド」加算分は、実装において必要となるオーバーヘッドを考慮したものである。 また、これにより、ユーザーが長さ 0 のメッセージを無制限に作れないことが保証される (このようなメッセージであっても、 記録のためのオーバーヘッドでシステムメモリーを消費する)。 .TP \fBRLIMIT_NICE\fP (Linux 2.6.12 以降, 下記の「バグ」の節も参照) This specifies a ceiling to which the process's nice value can be raised using \fBsetpriority\fP(2) or \fBnice\fP(2). The actual ceiling for the nice value is calculated as \fI20\ \-\ rlim_cur\fP. The useful range for this limit is thus from 1 (corresponding to a nice value of 19) to 40 (corresponding to a nice value of \-20). This unusual choice of range was necessary because negative numbers cannot be specified as resource limit values, since they typically have special meanings. For example, \fBRLIM_INFINITY\fP typically is the same as \-1. For more detail on the nice value, see \fBsched\fP(7). .TP \fBRLIMIT_NOFILE\fP このプロセスがオープンできるファイルディスクリプター数の最大値より 1 大きい値を指定する。 (\fBopen\fP(2), \fBpipe\fP(2), \fBdup\fP(2) などにより) この上限を超えようとした場合、エラー \fBEMFILE\fP が発生する (歴史的に、BSD ではこの上限は \fBRLIMIT_OFILE\fP という名前となっている)。 .IP Since Linux 4.5, this limit also defines the maximum number of file descriptors that an unprivileged process (one without the \fBCAP_SYS_RESOURCE\fP capability) may have "in flight" to other processes, by being passed across UNIX domain sockets. This limit applies to the \fBsendmsg\fP(2) system call. For further details, see \fBunix\fP(7). .TP \fBRLIMIT_NPROC\fP This is a limit on the number of extant process (or, more precisely on Linux, threads) for the real user ID of the calling process. So long as the current number of processes belonging to this process's real user ID is greater than or equal to this limit, \fBfork\fP(2) fails with the error \fBEAGAIN\fP. .IP The \fBRLIMIT_NPROC\fP limit is not enforced for processes that have either the \fBCAP_SYS_ADMIN\fP or the \fBCAP_SYS_RESOURCE\fP capability. .TP \fBRLIMIT_RSS\fP .\" As at kernel 2.6.12, this limit still does nothing in 2.6 though .\" talk of making it do something has surfaced from time to time in LKML .\" -- MTK, Jul 05 プロセスの resident set (RAM 上に存在する仮想ページの数) の 上限である (バイト単位)。 この制限は 2.4.30 より前でしか影響がなく、 \fBmadvise\fP(2) に \fBMADV_WILLNEED\fP を指定した関数コールにしか影響しない。 .TP \fBRLIMIT_RTPRIO\fP (Linux 2.6.12 以降, バグの節も参照) \fBsched_setscheduler\fP(2) や \fBsched_setparam\fP(2) を使って設定できる、そのプロセスのリアルタイム優先度の上限を指定する。 .IP For further details on real\-time scheduling policies, see \fBsched\fP(7) .TP \fBRLIMIT_RTTIME\fP (Linux 2.6.25 以降) リアルタイムスケジューリング方針でスケジューリングされるプロセスが ブロッキング型のシステムコールを呼び出さずに消費することのできる CPU 時間の合計についての上限を (マイクロ秒単位で) 指定する。 この上限の目的のため、プロセスがブロッキング型のシステムコールを 呼び出す度に、消費された CPU 時間のカウントは 0 にリセットされる。 プロセスが CPU を使い続けようとしたが他のプロセスに置き換えられた (preempted) 場合や、そのプロセスのタイムスライスが満了した場合、 そのプロセスが \fBsched_yield\fP(2) を呼び出した場合は、CPU 時間のカウントはリセットされない。 .IP ソフトリミットに達すると、そのプロセスに \fBSIGXCPU\fP シグナルが送られる。そのプロセスがこのシグナルを捕捉するか 無視して、CPU 時間を消費し続けた場合には、 ハードリミットに達するまで 1 秒に 1 回 \fBSIGXCPU\fP が生成され続けることになる。 ハードリミットに達した時点で、そのプロセスには \fBSIGKILL\fP シグナルが送られる。 .IP この上限を意図的に使用するのは、暴走したリアルタイムプロセスを 停止して、システムが動かなくなるのを避ける場合である。 .IP For further details on real\-time scheduling policies, see \fBsched\fP(7) .TP \fBRLIMIT_SIGPENDING\fP (Linux 2.6.8 以降) .\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit .\" that was present in kernels <= 2.6.7. MTK Dec 04 呼び出し元プロセスの実ユーザー ID に対して キューに入れられるシグナルの 数の制限を指定する。この制限をチェックするため、標準シグナルとリアルタ イムシグナルの両方がカウントされる。しかし、この制限は \fBsigqueue\fP(3) に対してのみ適用され、 \fBkill\fP(2) 使うことで、そのプロセスに対してま だキューに入れられていない シグナルのインスタンスをキューに入れることが できる。 .TP \fBRLIMIT_STACK\fP プロセススタックの最大サイズをバイト単位で指定する。 この上限に達すると、 \fBSIGSEGV\fP シグナルが生成される。 このシグナルを扱うためには、 プロセスは代りのシグナルスタック (\fBsigaltstack\fP(2)) を使用しなければならない。 .IP Linux 2.6.23 以降では、この制限はプロセスのコマンドライン引数と環境変数 に使用される空間の合計サイズの上限の決定にも使用される。詳細については \fBexecve\fP(2) を参照。 .SS prlimit() .\" commit c022a0acad534fd5f5d5f17280f6d4d135e74e81 .\" Author: Jiri Slaby .\" Date: Tue May 4 18:03:50 2010 +0200 .\" .\" rlimits: implement prlimit64 syscall .\" .\" commit 6a1d5e2c85d06da35cdfd93f1a27675bfdc3ad8c .\" Author: Jiri Slaby .\" Date: Wed Mar 24 17:06:58 2010 +0100 .\" .\" rlimits: add rlimit64 structure .\" Linux 固有の \fBprlimit\fP() システムコールは、 \fBsetrlimit\fP() と \fBgetrlimit\fP の機能を合わせて拡張したものである。 このシステムコールを使って、任意のプロセスのリソース上限の設定と取得を行うことができる。 .PP \fIresource\fP 引数は \fBsetrlimit\fP() や \fBgetrlimit\fP() と同じ意味である。 .PP \fInew_limit\fP 引数が NULL 以外の場合、 \fInew_limit\fP が指す \fIrlimit\fP 構造体を使って \fIresource\fP のソフトリミットとハードリミットの新しい値が設定される。 \fIold_limit\fP 引数が NULL 以外の場合、 \fBprlimit\fP() の呼び出しが成功すると、 \fIresource\fP の直前のソフトリミットとハードリミットが \fIold_limit\fP が指す \fIrlimit\fP 構造体に格納される。 .PP .\" FIXME . this permission check is strange .\" Asked about this on LKML, 7 Nov 2010 .\" "Inconsistent credential checking in prlimit() syscall" The \fIpid\fP argument specifies the ID of the process on which the call is to operate. If \fIpid\fP is 0, then the call applies to the calling process. To set or get the resources of a process other than itself, the caller must have the \fBCAP_SYS_RESOURCE\fP capability in the user namespace of the process whose resource limits are being changed, or the real, effective, and saved set user IDs of the target process must match the real user ID of the caller \fIand\fP the real, effective, and saved set group IDs of the target process must match the real group ID of the caller. .SH 返り値 成功した場合、これらのシステムコールは 0 を返す。 エラーの場合は \-1 が返され、 \fIerrno\fP が適切に設定される。 .SH エラー .TP \fBEFAULT\fP 場所を指すポインター引数がアクセス可能なアドレス空間外を指している。 .TP \fBEINVAL\fP \fIresource\fP で指定された値が有効でない。 または、 \fBsetrlimit\fP() や \fBprlimit\fP() で、 \fIrlim\->rlim_cur\fP が \fIrlim\->rlim_max\fP よりも大きかった。 .TP \fBEPERM\fP 非特権プロセスがハードリミットを増やそうとした。 この操作には \fBCAP_SYS_RESOURCE\fP ケーパビリティが必要である。 .TP \fBEPERM\fP 呼び出し元がハードリミット \fBRLIMIT_NOFILE\fP を \fI/proc/sys/fs/nr_open\fP (\fBproc\fP(5) 参照) で定義される最大値より大きな値に増やそうとした。 .TP \fBEPERM\fP (\fBprlimit\fP()) 呼び出し元のプロセスが \fIpid\fP で指定されたプロセスの上限を設定する許可を持っていなかった。 .TP \fBESRCH\fP \fIpid\fP で指定された ID のプロセスが見つからなかった。 .SH バージョン \fBprlimit\fP() システムコールは Linux 2.6.36 以降で利用できる。 ライブラリのサポートは glibc 2.13 以降で利用できる。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lbw35 lb lb l l l. インターフェース 属性 値 T{ \fBgetrlimit\fP(), \fBsetrlimit\fP(), \fBprlimit\fP() T} Thread safety MT\-Safe .TE .sp 1 .SH 準拠 \fBgetrlimit\fP(), \fBsetrlimit\fP(): POSIX.1\-2001, POSIX.1\-2008, SVr4, 4.3BSD. .PP \fBprlimit\fP(): Linux 固有。 .PP \fBRLIMIT_MEMLOCK\fP と \fBRLIMIT_NPROC\fP は BSD から派生し、 POSIX.1 には指定されていない。これらは BSD 系と Linux に存在するが、他の実装は少ない。 \fBRLIMIT_RSS\fP は BSD から派生し、POSIX.1 には指定されていない。それにも関わらず多くの実装で存在する。 \fBRLIMIT_MSGQUEUE\fP, \fBRLIMIT_NICE\fP, \fBRLIMIT_RTPRIO\fP, \fBRLIMIT_RTTIME\fP, \fBRLIMIT_SIGPENDING\fP は Linux 固有のものである。 .SH 注意 \fBfork\fP(2) で作成された作成された子プロセスは、 親プロセスのリソース制限を継承する。 \fBexecve\fP(2) の前後でリソース制限は保存される。 .PP Resource limits are per\-process attributes that are shared by all of the threads in a process. .PP リソースのソフトリミットをそのプロセスが現在のリソース使用量より小さい値に設定することはできる (但し、そのプロセスはそれ以降そのリソースの使用量を増やすことができなくなる)。 .PP シェルのリソース制限は、シェルの組み込みコマンドである \fIulimit\fP (\fBcsh\fP(1) では \fIlimit )\fP を使って設定することができる。 このシェルのリソース制限は、コマンドを実行してシェルが生成するプロセス に引き継がれる。 .PP Linux 2.6.24 以降では、 プロセスのリソース上限は \fI/proc/[pid]/limits\fP で知ることができる。 \fBproc\fP(5) 参照。 .PP 古いシステムでは、 \fBsetrlimit\fP() と同様の目的を持つ関数 \fBvlimit\fP() が提供されていた。 後方互換性のため、glibc でも \fBvlimit\fP() を提供している。 全ての新しいアプリケーションでは、 \fBsetrlimit\fP() を使用すべきである。 .SS "C ライブラリとカーネル ABI の違い" バージョン 2.13 以降では、 glibc の \fBgetrlimit\fP() と \fBsetrlimit\fP() のラッパー関数はもはや対応するシステムコールを呼び出さず、 代わりに「バグ」の節で説明されている理由から \fBprlimit\fP() を利用している。 .PP The name of the glibc wrapper function is \fBprlimit\fP(); the underlying system call is \fBprlimit64\fP(). .SH バグ 以前の Linux カーネルでは、プロセスがソフトまたはハード \fBRLIMIT_CPU\fP リミットに達した場合に送られる \fBSIGXCPU\fP と \fBSIGKILL\fP シグナルが、本来送られるべき時点の 1 (CPU) 秒後に送られてしまう。 これはカーネル 2.6.8 で修正された。 .PP .\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2 2.6.17 より前の 2.6.x カーネルでは、 \fBRLIMIT_CPU\fP リミットが 0 の場合、 (\fBRLIM_INFINITY\fP と同じように) 「制限なし」と間違って解釈されていた。 Linux 2.6.17 以降では、リミットを 0 に設定した場合にも 効果を持つようになっているが、実際にはリミットの値は 1 秒となる。 .PP .\" See https://lwn.net/Articles/145008/ カーネル 2.6.12 には、 \fBRLIMIT_RTPRIO\fP が動作しないというバグがある。この問題はカーネル 2.6.13 で修正されている。 .PP .\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2 カーネル 2.6.12 では、 \fBgetpriority\fP(2) と \fBRLIMIT_NICE\fP が返す優先度の範囲が一つずれていた。このため、nice 値の実際の上限が \fI19\ \-\ rlim_cur\fP になってしまうという影響があった。これはカーネル 2.6.13 で修正された。 .PP .\" The relevant patch, sent to LKML, seems to be .\" http://thread.gmane.org/gmane.linux.kernel/273462 .\" From: Roland McGrath redhat.com> .\" Subject: [PATCH 7/7] make RLIMIT_CPU/SIGXCPU per-process .\" Date: 2005-01-23 23:27:46 GMT .\" Tested Solaris 10, FreeBSD 9, OpenBSD 5.0 .\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=50951 Linux 2.6.12 以降では、 プロセスがその \fBRLIMIT_CPU\fP ソフトリミットに達し、 \fBSIGXCPU\fP に対してシグナルハンドラーが設定されている場合、 シグナルハンドラーを起動するだけでなく、 カーネルは 1 秒間ソフトリミットを増やす。 そのプロセスが CPU 時間を消費し続けている限り、 ハードリミットに達するまで、この動作が繰り返される。 ハードリミットに達すると、その時点でプロセスは kill される。 他の実装では、上記のような \fBRLIMIT_CPU\fP ソフトリミットの変更は行われず、 おそらく Linux の動作は標準に準拠していない。 移植性が必要なアプリケーションではこの Linux 固有の動作を前提にするのは避けるべきである。 Linux 固有の上限 \fBRLIMIT_RTTIME\fP でも、 ソフトリミットに達した場合に同じ動作となる。 .PP .\" d3561f78fd379a7110e46c87964ba7aa4120235c 2.4.22 より前のカーネルでは、 \fIrlim\->rlim_cur\fP が \fIrlim\->rlim_max\fP より大きかった場合、 \fBsetrlimit\fP() での \fBEINVAL\fP エラーを検出できない。 .PP .\" Linux doesn't return an error when an attempt to set \fBRLIMIT_CPU\fP has failed, for compatibility reasons. .SS "32 ビットプラットフォームにおける「大きな」リソース上限値の表現" .\" Linux still uses long for limits internally: .\" c022a0acad534fd5f5d5f17280f6d4d135e74e81 .\" kernel/sys.c:do_prlimit() still uses struct rlimit which .\" uses kernel_ulong_t for its members, i.e. 32-bit on 32-bit kernel. .\" https://bugzilla.kernel.org/show_bug.cgi?id=5042 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12201 glibc の \fBgetrlimit\fP() と \fBsetrlimit\fP() ラッパー関数は、32 ビットプラットフォームであっても 64 ビットの \fIrlim_t\fP データ型を使用する。 しかし、 \fBgetrlimit\fP() と \fBsetrlimit\fP() システムコールで使用される \fIrlim_t\fP データ型は (32 ビットの) \fIunsigned long\fP である。 さらに、 Linux では、 カーネルは 32 ビットプラットフォームではリソース上限を \fIunsigned long\fP として表現している。 しかしながら、 32 ビットデータ型は十分な大きさではない。 ここで最も関係がある上限値は \fBRLIMIT_FSIZE\fP である。 この上限はファイルサイズの最大値であり、実用性の面からは、 この上限をファイルオフセットを表現するのに使用されている型、 つまり 64 ビットの \fBoff_t\fP (\fI_FILE_OFFSET_BITS=64\fP でコンパイルしたプログラムの場合)、 と同じ幅を持つ型、を使って表現すべきである。 .PP カーネルのこの制限に対する対策として、 プログラムがリソース上限を 32 ビットの \fIunsigned long\fP で表現できる値よりも大きな値に設定しようとした際には、 glibc の \fBsetrlimit\fP() ラッパー関数はこの上限値を黙って \fBRLIM_INFINITY\fP に変換していた。 言い換えると、指定されたリソース上限値は黙って無視されていた。 .PP .\" https://www.sourceware.org/bugzilla/show_bug.cgi?id=12201 バージョン 2.13 以降の glibc では、 \fBgetrlimit\fP() と \fBsetrlimit\fP() システムコールの制限に対する回避手段として、 \fBsetrlimit\fP() と \fBgetrlimit\fP() を \fBprlimit\fP() を呼び出すラッパー関数として実装している。 .SH 例 以下のプログラムに \fBprlimit\fP() の使用例を示す。 .PP .EX #define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e } while (0) int main(int argc, char *argv[]) { struct rlimit old, new; struct rlimit *newp; pid_t pid; if (!(argc == 2 || argc == 4)) { fprintf(stderr, "Usage: %s [ " "]\en", argv[0]); exit(EXIT_FAILURE); } pid = atoi(argv[1]); /* PID of target process */ newp = NULL; if (argc == 4) { new.rlim_cur = atoi(argv[2]); new.rlim_max = atoi(argv[3]); newp = &new; } /* Set CPU time limit of target process; retrieve and display previous limit */ if (prlimit(pid, RLIMIT_CPU, newp, &old) == \-1) errExit("prlimit\-1"); printf("Previous limits: soft=%jd; hard=%jd\en", (intmax_t) old.rlim_cur, (intmax_t) old.rlim_max); /* Retrieve and display new CPU time limit */ if (prlimit(pid, RLIMIT_CPU, NULL, &old) == \-1) errExit("prlimit\-2"); printf("New limits: soft=%jd; hard=%jd\en", (intmax_t) old.rlim_cur, (intmax_t) old.rlim_max); exit(EXIT_SUCCESS); } .EE .SH 関連項目 \fBprlimit\fP(1), \fBdup\fP(2), \fBfcntl\fP(2), \fBfork\fP(2), \fBgetrusage\fP(2), \fBmlock\fP(2), \fBmmap\fP(2), \fBopen\fP(2), \fBquotactl\fP(2), \fBsbrk\fP(2), \fBshmctl\fP(2), \fBmalloc\fP(3), \fBsigqueue\fP(3), \fBulimit\fP(3), \fBcore\fP(5), \fBcapabilities\fP(7), \fBcgroups\fP(7), \fBcredentials\fP(7), \fBsignal\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。