.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it) .\" and Copyright 2004, 2005 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 Tue Oct 22 17:53:56 1996 by Eric S. Raymond .\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer .\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer .\" Modified 20 Dec 2001, Michael Kerrisk .\" Modified 21 Dec 2001, aeb .\" Modified 27 May 2004, Michael Kerrisk .\" Added notes on CAP_IPC_OWNER requirement .\" Modified 17 Jun 2004, Michael Kerrisk .\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID .\" Modified, 11 Nov 2004, Michael Kerrisk .\" Language and formatting clean-ups .\" Rewrote semun text .\" Added semid_ds and ipc_perm structure definitions .\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions. .\" 2018-03-20, dbueso: Added SEM_STAT_ANY description. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997,1998 HANATAKA Shinya .\" all rights reserved. .\" Translated 1997-02-23, HANATAKA Shinya .\" Modified 1998-09-10, HANATAKA Shinya .\" Updated & Modified 2001-06-03, Yuichi SATO .\" Updated & Modified 2002-01-02, Yuichi SATO .\" Updated & Modified 2005-01-03, Yuichi SATO .\" Updated & Modified 2005-10-10, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" Updated 2013-07-24, Akihiro MOTOKI .\" .TH SEMCTL 2 2020\-12\-21 Linux "Linux Programmer's Manual" .SH 名前 semctl \- System V セマフォの制御操作を行なう .SH 書式 .nf \fB#include \fP \fB#include \fP \fB#include \fP .PP \fBint semctl(int \fP\fIsemid\fP\fB, int \fP\fIsemnum\fP\fB, int \fP\fIcmd\fP\fB, ...);\fP .fi .SH 説明 \fBsemctl\fP() は、 \fIsemid\fP で指定された System\ V セマフォ集合 (semaphore set) またはセマフォ集合の \fIsemnun\fP 番目のセマフォに対して、 \fIcmd\fP で指定された制御操作を行なう (集合内のセマフォの番号は 0 から始まる)。 .PP この関数は、 \fIcmd\fP の値に依存して、3 個または 4 個の引数を持つ。 引数が 4 個の場合、第 4 引数の型は \fIunion semun\fP である。 \fI呼び出し元プログラム\fPは、 この共用体 (union) を以下のように定義しなければならない。 .PP .in +4n .EX union semun { int val; /* SETVAL の値 */ struct semid_ds *buf; /* IPC_STAT, IPC_SET 用のバッファー */ unsigned short *array; /* GETALL, SETALL 用の配列 */ struct seminfo *__buf; /* IPC_INFO 用のバッファー (Linux 固有) */ }; .EE .in .PP \fIsemid_ds\fP データ構造体は \fI\fP で以下のように定義されている: .PP .in +4n .EX struct semid_ds { struct ipc_perm sem_perm; /* 所有権と許可 */ time_t sem_otime; /* 最後の semop の時刻 */ time_t sem_ctime; /* 作成時刻/semctl() により 最後に変更が行われた時刻 */ unsigned long sem_nsems; /* 集合内のセマフォの数 */ }; .EE .in .PP The fields of the \fIsemid_ds\fP structure are as follows: .TP 11 \fIsem_perm\fP This is an \fIipc_perm\fP structure (see below) that specifies the access permissions on the semaphore set. .TP \fIsem_otime\fP Time of last \fBsemop\fP(2) system call. .TP \fIsem_ctime\fP Time of creation of semaphore set or time of last \fBsemctl\fP() \fBIPCSET\fP, \fBSETVAL\fP, or \fBSETALL\fP operation. .TP \fIsem_nsems\fP Number of semaphores in the set. Each semaphore of the set is referenced by a nonnegative integer ranging from \fB0\fP to \fIsem_nsems\-1\fP. .PP \fIipc_perm\fP 構造体は以下のように定義されている (強調されたフィールドは \fBIPC_SET\fP を使って設定可能である): .PP .in +4n .EX struct ipc_perm { key_t __key; /* semget(2) に与えられるキー */ uid_t \fBuid\fP; /* 所有者 (owner) の実効 UID */ gid_t \fBgid\fP; /* 所有者の実効 GID */ uid_t cuid; /* 作成者 (creator) の実効 UID */ gid_t cgid; /* 作成者の実効 GID */ unsigned short \fBmode\fP; /* 許可 */ unsigned short __seq; /* シーケンス番号 */ }; .EE .in .PP The least significant 9 bits of the \fImode\fP field of the \fIipc_perm\fP structure define the access permissions for the shared memory segment. The permission bits are as follows: .TS l l. 0400 Read by user 0200 Write by user 0040 Read by group 0020 Write by group 0004 Read by others 0002 Write by others .TE .PP In effect, "write" means "alter" for a semaphore set. Bits 0100, 0010, and 0001 (the execute bits) are unused by the system. .PP \fIcmd\fP として有効な値は次の通りである。 .TP \fBIPC_STAT\fP \fIsemid\fP に関連づけられたカーネルデータ構造体の情報を \fIarg.buf\fP で指された \fIsemid_ds\fP 構造体へコピーする。 \fIsemnum\fP 引数は無視される。 呼び出したプロセスはそのセマフォ集合に対する 読み込み許可を持たなければならない。 .TP \fBIPC_SET\fP Write the values of some members of the \fIsemid_ds\fP structure pointed to by \fIarg.buf\fP to the kernel data structure associated with this semaphore set, updating also its \fIsem_ctime\fP member. .IP The following members of the structure are updated: \fIsem_perm.uid\fP, \fIsem_perm.gid\fP, and (the least significant 9 bits of) \fIsem_perm.mode\fP. .IP The effective UID of the calling process must match the owner (\fIsem_perm.uid\fP) or creator (\fIsem_perm.cuid\fP) of the semaphore set, or the caller must be privileged. The argument \fIsemnum\fP is ignored. .TP \fBIPC_RMID\fP セマフォ集合をただちに削除し、その集合上の \fBsemop\fP(2) コールでブロックされている全てのプロセスを目覚めさせる (エラー値が返されて、 \fIerrno\fP に \fBEIDRM\fP が設定される)。 呼び出したプロセスの実効ユーザー ID が そのセマフォ集合の作成者または所有者と一致するか、 呼び出した人が特権を持たなければならない。 \fIsemnum\fP 引数は無視される。 .TP \fBIPC_INFO\fP (Linux 固有) システム全体でのセマフォの制限とパラメーターに関する情報を、 \fIarg.__buf\fP が指す構造体に入れて返す。 この構造体は \fIseminfo\fP 型である。 \fIseminfo\fP は \fB_GNU_SOURCE\fP 機能検査マクロが定義された場合に \fI\fP で以下のように定義される: .IP .in +4n .EX struct seminfo { int semmap; /* セマフォマップの最大エントリー数; カーネル内では未使用 */ int semmni; /* セマフォ集合の最大数 */ int semmns; /* 全セマフォ集合中のセマフォの 最大数 */ int semmnu; /* アンドゥ構造体のシステム全体での 最大数; カーネル内では未使用 */ int semmsl; /* 一つのセマフォ集合の最大セマフォ数 */ int semopm; /* semop(2) に渡す操作の最大数 */ int semume; /* プロセスあたりのアンドゥエントリー の最大数; カーネル内では未使用 */ int semusz; /* 構造体 sem_undo のサイズ */ int semvmx; /* セマフォの最大値 */ int semaem; /* セマフォの調整 (semaphore adjustment; SEM_UNDO) のために記録される最大値 */ }; .EE .in .IP 設定 \fIsemmsl\fP, \fIsemmns\fP, \fIsemopm\fP, \fIsemmni\fP は \fI/proc/sys/kernel/sem\fP 経由で変更可能である。 詳しくは \fBproc\fP(5) を参照。 .TP \fBSEM_INFO\fP (Linux 固有) \fBIPC_INFO\fP のときと同じ情報を格納した \fIseminfo\fP 構造体を返す。 但し、以下のフィールドにはセマフォが消費しているシステム資源に 関する情報が格納される点が異なる。 \fIsemusz\fP フィールドは現在システム上に存在するセマフォ集合の数を返す。 \fIsemaem\fP フィールドはシステム上の全てのセマフォ集合に含まれる セマフォの総数を返す。 .TP \fBSEM_STAT\fP (Linux 固有) \fBIPC_STAT\fP と同じく \fIsemid_ds\fP 構造体を返す。 但し、 \fIsemid\fP 引数は、セマフォ識別子ではなく、システム上の全てのセマフォ集合 に関する情報を管理するカーネルの内部配列へのインデックスである。 .TP \fBSEM_STAT_ANY\fP (Linux 固有, Linux 4.17 以降) Return a \fIseminfo\fP structure containing the same information as for \fBSEM_STAT\fP. However, \fIsem_perm.mode\fP is not checked for read access for \fIsemid\fP meaning that any user can employ this operation (just as any user may read \fI/proc/sysvipc/sem\fP to obtain the same information). .TP \fBGETALL\fP 集合の全てのセマフォの \fBsemval\fP の値 (現在の値) を \fIarg.array\fP に返す。 \fIsemnum\fP 引数は無視される。 呼び出したプロセスはそのセマフォ集合に読み込み許可を持たなければならない。 .TP \fBGETNCNT\fP Return the \fBsemncnt\fP value for the \fIsemnum\fP\-th semaphore of the set (i.e., the number of processes waiting for the semaphore's value to increase). The calling process must have read permission on the semaphore set. .TP \fBGETPID\fP 集合の \fIsemnum\fP 番目のセマフォの \fBsempid\fP の値を返す。この値は、そのセマフォに対して最後に操作を行ったプロセスの PID である (ただし「バグ」を参照)。呼び出したプロセスはそのセマフォ集合に読み込み許可を持たなければならない。 .TP \fBGETVAL\fP 集合の \fIsemnum\fP 番目のセマフォの \fBsemval\fP (セマフォの値) を返す。 呼び出したプロセスはそのセマフォ集合に読み込み許可を持たなければならない。 .TP \fBGETZCNT\fP 集合の \fIsemnum\fP 番目のセマフォの \fBsemzcnt\fP の値 (つまり、そのセマフォの値が 0 になるのを待っているプロセスの数) を返す。呼び出したプロセスはそのセマフォ集合に読み込み許可を持たなければならない。 .TP \fBSETALL\fP 集合の全てのセマフォの \fBsemval\fP 値に \fIarg.array\fP で指定された値を設定する。 その集合に関連する \fIsemid_ds\fP 構造体の \fIsem_ctime\fP メンバーの値も更新する。 全てのプロセスのセマフォの変更についてのアンドゥエントリー (\fBsemop\fP(2) を参照) は消去 (clear) される。 セマフォの値の変更により、他のプロセス内でブロックされている \fBsemop\fP(2) コールの続行が許可されると、それらのプロセスは起こされる (wake up)。 \fIsemnum\fP 引数は無視される。 呼び出したプロセスはそのセマフォ集合に 変更 (書き込み) 許可を持たなければならない。 .TP \fBSETVAL\fP 集合の \fIsemnum\fP 番目のセマフォのセマフォ地 (\fBsemval\fP) に \fIarg.val\fP の値を設定する。その集合に関連する \fIsemid_ds\fP 構造体の \fIsem_ctime\fP メンバーの値も更新する。 全てのプロセスのセマフォの変更についてのアンドゥエントリーは消去される。 セマフォの値の変更により、他のプロセス内でブロックされている \fBsemop\fP(2) コールの続行が許可されると、それらのプロセスは起こされる (wake up)。 呼び出したプロセスはそのセマフォ集合に 変更 (書き込み) 許可を持たなければならない。 .SH 返り値 失敗した場合、 \fBsemctl\fP() は \-1 を返し、 \fIerrno\fP にそのエラーを示す。 .PP そうでなければシステムコールは \fIcmd\fP によって以下の負でない値を返す: .TP \fBGETNCNT\fP \fBsemncnt\fP の値 .TP \fBGETPID\fP \fBsempid\fP の値 .TP \fBGETVAL\fP \fBsemval\fP の値 .TP \fBGETZCNT\fP \fBsemzcnt\fP の値 .TP \fBIPC_INFO\fP 全てのセマフォ集合に関する情報を管理しているカーネルの内部配列の使用中 エントリーのインデックスの最大値 (この情報は、システムの全てのセマフォ集合に関する情報を取得するために操作 \fBSEM_STAT\fP や \fBSEM_STAT_ANY\fP を繰り返し実行する際に使用できる) .TP \fBSEM_INFO\fP \fBIPC_INFO\fP と同じ .TP \fBSEM_STAT\fP \fIsemid\fP で指定されたインデックスを持つセマフォ集合の識別子 .TP \fBSEM_STAT_ANY\fP \fBSEM_STAT\fP と同じ。 .PP \fIcmd\fP の値がそれ以外の場合、成功すると 0 が返される。 .SH エラー 失敗した場合は \fIerrno\fP には以下の値のどれかが設定される: .TP \fBEACCES\fP The argument \fIcmd\fP has one of the values \fBGETALL\fP, \fBGETPID\fP, \fBGETVAL\fP, \fBGETNCNT\fP, \fBGETZCNT\fP, \fBIPC_STAT\fP, \fBSEM_STAT\fP, \fBSEM_STAT_ANY\fP, \fBSETALL\fP, or \fBSETVAL\fP and the calling process does not have the required permissions on the semaphore set and does not have the \fBCAP_IPC_OWNER\fP capability in the user namespace that governs its IPC namespace. .TP \fBEFAULT\fP \fIarg.buf\fP または \fIarg.array\fP で指されているアドレスにアクセスすることができない。 .TP \fBEIDRM\fP セマフォ集合が削除された。 .TP \fBEINVAL\fP \fIcmd\fP または \fIsemid\fP に無効な値が指定された。 もしくは、 \fBSEM_STAT\fP 操作の場合に、 \fIsemid\fP で指定されたインデックス値が現在未使用の配列のスロットを参照いていた。 .TP \fBEPERM\fP \fIcmd\fP 引数に \fBIPC_SET\fP または \fBIPC_RMID\fP が指定され、呼び出したプロセスの実効ユーザー ID がセマフォの (\fIsem_perm.cuid\fP で見つかる) 作成者または (\fIsem_perm.uid\fP で見つかる) 所有者でもなく、 プロセスが \fBCAP_SYS_ADMIN\fP ケーパビリティを持たない。 .TP \fBERANGE\fP \fIcmd\fP 引数に \fBSETALL\fP または \fBSETVAL\fP が指定され、(集合のセマフォのどれかの) \fBsemval\fP に設定される値が 0 より小さいか、実装の制限 \fBSEMVMX\fP よりも大きい。 .SH 準拠 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW. POSIX.1\-2001, POSIX.1\-2008, SVr4. .PP .\" POSIX.1-2001, POSIX.1-2008 POSIX.1 では \fIsemid_ds\fP 構造体の \fIsem_nsems\fP フィールドは \fIunsigned\ short\fP 型を持つと規定されており、 他のほとんどのシステムでこのフィールドは \fIunsigned\ short\fP 型になっている。 Linux 2.4 以前ではそうなっていたが、 Linux 2.4 以降ではこのフィールドは \fIunsigned\ long\fP 型である。 .SH 注意 .\" Like Linux, the FreeBSD man pages still document .\" the inclusion of these header files. Linux や POSIX の全てのバージョンでは、 \fI\fP と \fI\fP のインクルードは必要ない。しかしながら、いくつかの古い実装ではこれらのヘッダーファイルのインクルードが必要であり、 SVID でもこれらのインクルードをするように記載されている。このような古いシステムへの移植性を意図したアプリケーションではこれらのファイルをインクルードする必要があるかもしれない。 .PP \fBIPC_INFO\fP, \fBSEM_STAT\fP, \fBSEM_INFO\fP 操作は \fBipcs\fP(1) プログラムによって割当られた資源について情報を提供するために使用される。 将来的にはこれらは変更されるか、 \fI/proc\fP ファイルシステムインターフェースに移動されるかもしれない。 .PP \fI構造体 semid_ds\fP 内の多くのフィールドは、 Linux 2.2 では \fIshort\fP 型だったが、Linux 2.4 では \fIlong\fP 型になった。 この利点を生かすには、glibc\-2.1.91 以降の環境下で 再コンパイルすれば十分である。 カーネルは新しい形式の呼び出しと古い形式の呼び出しを \fIcmd\fP 内の \fBIPC_64\fP フラグで区別する。 .PP .\" POSIX.1-2001, POSIX.1-2008 初期のバージョンの glibc では、 \fIsemun\fP 共用体は \fI\fP で定義されていたが、 POSIX.1 では呼び出し側がこの共用体を定義する必要がある。 この共用体が定義されて\fIいない\fP glibc のバージョンでは、 マクロ \fB_SEM_SEMUN_UNDEFINED\fP が \fI\fP で定義されている。 .PP 以下は \fBsemctl\fP() コールに影響するセマフォ集合のシステム制限: .TP \fBSEMVMX\fP \fBsemval\fP の最大値 : 実装依存 (32767)。 .PP .\" 移植性を高めるための一番良い方法は、常に 4 個の引数で \fBsemctl\fP() を呼び出すことである。 .SS "The sempid value" POSIX.1 defines \fIsempid\fP as the "process ID of [the] last operation" on a semaphore, and explicitly notes that this value is set by a successful \fBsemop\fP(2) call, with the implication that no other interface affects the \fIsempid\fP value. .PP .\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin While some implementations conform to the behavior specified in POSIX.1, others do not. (The fault here probably lies with POSIX.1 inasmuch as it likely failed to capture the full range of existing implementation behaviors.) Various other implementations also update \fIsempid\fP for the other operations that update the value of a semaphore: the \fBSETVAL\fP and \fBSETALL\fP operations, as well as the semaphore adjustments performed on process termination as a consequence of the use of the \fBSEM_UNDO\fP flag (see \fBsemop\fP(2)). .PP .\" commit a5f4db877177d2a3d7ae62a7bac3a5a27e083d7f Linux also updates \fIsempid\fP for \fBSETVAL\fP operations and semaphore adjustments. However, somewhat inconsistently, up to and including Linux 4.5, the kernel did not update \fIsempid\fP for \fBSETALL\fP operations. This was rectified in Linux 4.6. .SH 例 \fBshmop\fP(2) 参照。 .SH 関連項目 \fBipc\fP(2), \fBsemget\fP(2), \fBsemop\fP(2), \fBcapabilities\fP(7), \fBsem_overview\fP(7), \fBsysvipc\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。