.\" Copyright (c) 1983, 1991 The Regents of the University of California. .\" And Copyright (C) 2011 Guillem Jover .\" And Copyright (C) 2006, 2014 Michael Kerrisk .\" All rights reserved. .\" .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB) .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" %%%LICENSE_END .\" .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91 .\" .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Tue Jul 9 23:55:17 1996 by aeb .\" Modified Fri Jan 24 00:26:00 1997 by aeb .\" 2011-09-20, Guillem Jover : .\" Added text on dynamically allocating buffer + example program .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated 1997-12-12, HANATAKA Shinya .\" Updated 2005-02-10, Yuichi SATO .\" Updated 2006-07-19, Akihiro MOTOKI , LDP v2.36 .\" Updated 2008-08-06, Akihiro MOTOKI, LDP v3.05 .\" Updated 2012-05-08, Akihiro MOTOKI .\" .TH READLINK 2 2020\-06\-09 Linux "Linux Programmer's Manual" .SH 名前 readlink, readlinkat \- シンボリックリンクの値を読む .SH 書式 .nf \fB#include \fP .PP \fBssize_t readlink(const char *\fP\fIpathname\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsiz\fP\fB);\fP \fB#include \fP/* AT_* 定数の定義 */ \fB#include \fP .PP \fBssize_t readlinkat(int \fP\fIdirfd\fP\fB, const char *\fP\fIpathname\fP\fB,\fP \fB char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsiz\fP\fB);\fP .PP .fi .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP .ad l \fBreadlink\fP(): .RS 4 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED _XOPEN_SOURCE\ >=\ 500 || _POSIX_C_SOURCE\ >=\ 200112L || /* glibc 2.19 以前: */ _BSD_SOURCE .RE .PP \fBreadlinkat\fP(): .PD 0 .ad l .RS 4 .TP 4 glibc 2.10 以降: _POSIX_C_SOURCE\ >=\ 200809L .TP glibc 2.10 より前: _ATFILE_SOURCE .RE .ad b .PD .SH 説明 \fBreadlink\fP() は \fIpathname\fP で与えられたシンボリックリンクの内容を \fIbuf\fP バッファーへ格納する、 \fIbuf\fP のサイズは \fIbufsiz\fP である。 \fBreadlink\fP() はヌルバイトを \fIbuf\fP に追加しない。 その内容全てを格納するのにバッファーが小さ過ぎる場合は、 (\fIbufsiz\fP バイトの長さに) 内容を (黙って) 切り詰める。 .SS readlinkat() \fBreadlinkat\fP() システムコールは \fBreadlink\fP() と全く同様に動作するが、以下で説明する点が異なる。 .PP \fIpathname\fP で指定されたパス名が相対パスの場合、このパス名はファイルディスクリプター \fIdirfd\fP が参照するディレクトリに対する相対パスと解釈される (\fBreadlink\fP() に相対パス名を渡した場合のように、呼び出したプロセスのカレントワーキングディレクトリに対する相対パスではない)。 .PP \fIpathname\fP で指定されたパス名が相対パスで、 \fIdirfd\fP が特別な値 \fBAT_FDCWD\fP の場合、 (\fBreadlink\fP() と同様に) \fIpathname\fP は呼び出したプロセスのカレントワーキングディレクトリに対する相対パスと解釈される。 .PP \fIpathname\fP で指定されたパス名が絶対パスの場合、 \fIdirfd\fP は無視される。 .PP .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d Linux 2.6.39 以降では、 \fIpathname\fP に空文字列を指定できる。 その場合、呼び出しは \fIdirfd\fP が参照するシンボリックリンクに対して行われる (\fIdirfd\fP はフラグ \fBO_PATH\fP と \fBO_NOFOLLOW\fP を指定した \fBopen\fP(2) を使って取得すべきである)。 .PP \fBreadlinkat\fP() の必要性についての説明については \fBopenat\fP(2) を参照。 .SH 返り値 On success, these calls return the number of bytes placed in \fIbuf\fP. (If the returned value equals \fIbufsiz\fP, then truncation may have occurred.) On error, \-1 is returned and \fIerrno\fP is set to indicate the error. .SH エラー .TP \fBEACCES\fP パスのディレクトリ部分に検索許可が与えられていない (\fBpath_resolution\fP(7) も参照すること)。 .TP \fBEFAULT\fP \fIbuf\fP がプロセスに割り当てられたアドレス空間の外を指している。 .TP \fBEINVAL\fP .\" At the glibc level, bufsiz is unsigned, so this error can only occur .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed, .\" and this error can also occur if bufsiz < 0. .\" See: http://thread.gmane.org/gmane.linux.man/380 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall? \fIbufsiz\fP が正でない。 .TP \fBEINVAL\fP The named file (i.e., the final filename component of \fIpathname\fP) is not a symbolic link. .TP \fBEIO\fP ファイルシステムの読み込み中に I/O エラーが起こった。 .TP \fBELOOP\fP パス名にシンボリックリンクが多すぎる。 .TP \fBENAMETOOLONG\fP パス名かパス名の一部分が長過ぎる。 .TP \fBENOENT\fP その名前のファイルが存在しない。 .TP \fBENOMEM\fP 十分なカーネルメモリーがない。 .TP \fBENOTDIR\fP パスのディレクトリ部分がディレクトリでない。 .PP \fBreadlinkat\fP() では以下のエラーも発生する。 .TP \fBEBADF\fP \fIdirfd\fP が有効なファイルディスクリプターではない。 .TP \fBENOTDIR\fP \fIpathname\fP が相対パスで、 \fIdirfd\fP がディレクトリ以外のファイルを参照しているファイルディスクリプターである。 .SH バージョン \fBreadlinkat\fP() はカーネル 2.6.16 で Linux に追加された。 ライブラリによるサポートはバージョン 2.4 で glibc に追加された。 .SH 準拠 4.4BSD (\fBreadlink\fP() は 4.2BSD で初めて登場した), POSIX.1\-2001, POSIX.1\-2008. .PP \fBreadlinkat\fP(): POSIX.1\-2008. .SH 注意 バージョン 2.4 以前の glibc (バージョン 2.4 を含む) では、 \fBreadlink\fP() の返り値の型は \fIint\fP で宣言されていた。現在では、返り値の型は \fIssize_t\fP である (返り値 \fIssize_t\fP は POSIX.1\-2001 で (新たに) 必須となった)。 .PP 静的な大きさのバッファーを使うと、 シンボリックリンクの内容を格納するのに十分な領域がない場合がある。 バッファーに必要なサイズは、 そのシンボリックリンクに対して \fBlstat\fP(2) の呼び出しで返される \fIstat.st_size\fP の値から取得できる。 ただし、 \fBreadlink\fP() や \fBreadlinkat\fP() が書き込んだバイト数をチェックして、 シンボリックリンクのサイズが二つの呼び出しの間で増えていないことを確認すべきである。 \fBreadlink\fP() や \fBreadlinkat\fP() 用のバッファーを動的に割り当てる方法でも、 バッファーサイズとして \fBPATH_MAX\fP を使用する場合に共通する移植性の問題を解決することができる。 なぜなら、POSIX では、 システムがそのような上限値を定義していない場合には、 \fBPATH_MAX\fP が定義されることが保証されていないからである。 .SS "glibc での注意" \fBreadlinkat\fP() が利用できない古いカーネルでは、 glibc ラッパー関数は \fBreadlink\fP() を使用するモードにフォールバックする。 \fIpathname\fP が相対パスの場合、 glibc は \fIdirfd\fP 引数に対応する \fI/proc/self/fd\fP のシンボリックリンクに基づいてパス名を構成する。 .SH 例 The following program allocates the buffer needed by \fBreadlink\fP() dynamically from the information provided by \fBlstat\fP(2), falling back to a buffer of size \fBPATH_MAX\fP in cases where \fBlstat\fP(2) reports a size of zero. .PP .EX #include #include #include #include #include #include int main(int argc, char *argv[]) { struct stat sb; char *buf; ssize_t nbytes, bufsiz; if (argc != 2) { fprintf(stderr, "Usage: %s \en", argv[0]); exit(EXIT_FAILURE); } if (lstat(argv[1], &sb) == \-1) { perror("lstat"); exit(EXIT_FAILURE); } /* Add one to the link size, so that we can determine whether the buffer returned by readlink() was truncated. */ bufsiz = sb.st_size + 1; /* Some magic symlinks under (for example) /proc and /sys report \(aqst_size\(aq as zero. In that case, take PATH_MAX as a "good enough" estimate. */ if (sb.st_size == 0) bufsiz = PATH_MAX; buf = malloc(bufsiz); if (buf == NULL) { perror("malloc"); exit(EXIT_FAILURE); } nbytes = readlink(argv[1], buf, bufsiz); if (nbytes == \-1) { perror("readlink"); exit(EXIT_FAILURE); } printf("\(aq%s\(aq points to \(aq%.*s\(aq\en", argv[1], (int) nbytes, buf); /* If the return value was equal to the buffer size, then the the link target was larger than expected (perhaps because the target was changed between the call to lstat() and the call to readlink()). Warn the user that the returned target may have been truncated. */ if (nbytes == bufsiz) printf("(Returned buffer may have been truncated)\en"); free(buf); exit(EXIT_SUCCESS); } .EE .SH 関連項目 \fBreadlink\fP(1), \fBlstat\fP(2), \fBstat\fP(2), \fBsymlink\fP(2), \fBrealpath\fP(3), \fBpath_resolution\fP(7), \fBsymlink\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。