1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" And Copyright (C) 2011 Guillem Jover <guillem@hadrons.org>
3 .\" And Copyright (C) 2006, 2014 Michael Kerrisk
4 .\" All rights reserved.
6 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
38 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
39 .\" Modified Tue Jul 9 23:55:17 1996 by aeb
40 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
41 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
42 .\" Added text on dynamically allocating buffer + example program
44 .\"*******************************************************************
46 .\" This file was generated with po4a. Translate the source file.
48 .\"*******************************************************************
50 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
51 .\" all rights reserved.
52 .\" Translated 1997-12-12, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
53 .\" Updated 2005-02-10, Yuichi SATO <ysato444@yahoo.co.jp>
54 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
55 .\" Updated 2008-08-06, Akihiro MOTOKI, LDP v3.05
56 .\" Updated 2012-05-08, Akihiro MOTOKI <amotoki@gmail.com>
58 .TH READLINK 2 2014\-10\-15 Linux "Linux Programmer's Manual"
60 readlink, readlinkat \- シンボリックリンクの値を読む
63 \fB#include <unistd.h>\fP
65 \fBssize_t readlink(const char *\fP\fIpathname\fP\fB, char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsiz\fP\fB);\fP
67 \fB#include <fcntl.h> \fP/* AT_* 定数の定義 */
68 \fB#include <unistd.h>\fP
70 \fBssize_t readlinkat(int \fP\fIdirfd\fP\fB, const char *\fP\fIpathname\fP\fB,\fP
71 \fB char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsiz\fP\fB);\fP
75 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
81 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
90 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
98 \fBreadlink\fP() は \fIpathname\fP で与えられたシンボリックリンクの内容を \fIbuf\fP バッファーへ格納する、 \fIbuf\fP
99 のサイズは \fIbufsiz\fP である。 \fBreadlink\fP() はヌルバイトを \fIbuf\fP に追加しない。
100 その内容全てを格納するのにバッファーが小さ過ぎる場合は、 (\fIbufsiz\fP バイトの長さに) 内容を切り詰める。
102 \fBreadlinkat\fP() システムコールは \fBreadlink\fP() と全く同様に動作するが、以下で説明する点が異なる。
104 \fIpathname\fP で指定されたパス名が相対パスの場合、このパス名はファイルディスクリプター \fIdirfd\fP
105 が参照するディレクトリに対する相対パスと解釈される (\fBreadlink\fP()
106 に相対パス名を渡した場合のように、呼び出したプロセスのカレントワーキングディレクトリに対する相対パスではない)。
108 \fIpathname\fP で指定されたパス名が相対パスで、 \fIdirfd\fP が特別な値 \fBAT_FDCWD\fP の場合、 (\fBreadlink\fP()
109 と同様に) \fIpathname\fP は呼び出したプロセスのカレントワーキングディレクトリに対する相対パスと解釈される。
111 \fIpathname\fP で指定されたパス名が絶対パスの場合、 \fIdirfd\fP は無視される。
113 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
114 Since Linux 2.6.39, \fIpathname\fP can be an empty string, in which case the
115 call operates on the symbolic link referred to by \fIdirfd\fP (which should
116 have have been obtained using \fBopen\fP(2) with the \fBO_PATH\fP and
117 \fBO_NOFOLLOW\fP flags).
119 \fBreadlinkat\fP() の必要性についての説明については \fBopenat\fP(2) を参照。
121 成功すると、これらのシステムコールは \fIbuf\fP に格納されたバイト数を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP
126 パスのディレクトリ部分に検索許可が与えられていない (\fBpath_resolution\fP(7) も参照すること)。
129 \fIbuf\fP がプロセスに割り当てられたアドレス空間の外を指している。
132 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
133 .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed,
134 .\" and this error can also occur if bufsiz < 0.
135 .\" See: http://thread.gmane.org/gmane.linux.man/380
136 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
140 指定したファイルがシンボリックリンクでない。
143 ファイルシステムの読み込み中に I/O エラーが起こった。
158 パスのディレクトリ部分がディレクトリでない。
160 \fBreadlinkat\fP() では以下のエラーも発生する。
163 \fIdirfd\fP が有効なファイルディスクリプタではない。
166 \fIpathname\fP が相対パスで、 \fIdirfd\fP がディレクトリ以外のファイルを参照しているファイルディスクリプタである。
168 \fBreadlinkat\fP() はカーネル 2.6.16 で Linux に追加された。 ライブラリによるサポートはバージョン 2.4 で glibc
171 4.4BSD (\fBreadlink\fP() は 4.2BSD で初めて登場した), POSIX.1\-2001, POSIX.1\-2008.
173 \fBreadlinkat\fP(): POSIX.1\-2008.
175 バージョン 2.4 以前の glibc (バージョン 2.4 を含む) では、 \fBreadlink\fP() の返り値の型は \fIint\fP
176 で宣言されていた。現在では、返り値の型は \fIssize_t\fP である (返り値 \fIssize_t\fP は POSIX.1\-2001 で (新たに)
179 静的な大きさのバッファを使うと、 シンボリックリンクの内容を格納するのに十分な領域がない場合がある。 バッファに必要なサイズは、
180 そのシンボリックリンクに対して \fBlstat\fP(2) の呼び出しで返される \fIstat.st_size\fP の値から取得できる。 ただし、
181 \fBreadlink\fP() や \fBreadlinkat\fP() が書き込んだバイト数をチェックして、
182 シンボリックリンクのサイズが二つの呼び出しの間で増えていないことを確認すべきである。 \fBreadlink\fP() や \fBreadlinkat\fP()
183 用のバッファを動的に割り当てる方法でも、 バッファサイズとして \fIPATH_MAX\fP を使用する場合に共通する移植性の問題を解決することができる。
184 なぜなら、POSIX では、 システムがそのような上限値を定義していない場合には、 \fIPATH_MAX\fP
185 が定義されることが保証されていないからである。
187 On older kernels where \fBreadlinkat\fP() is unavailable, the glibc wrapper
188 function falls back to the use of \fBreadlink\fP(). When \fIpathname\fP is a
189 relative pathname, glibc constructs a pathname based on the symbolic link in
190 \fI/proc/self/fd\fP that corresponds to the \fIdirfd\fP argument.
192 以下のプログラムは、 \fBreadlink\fP() が必要とするバッファを、
193 \fBlstat\fP() が提供する情報に基づいて動的に割り当てる。
194 また、両方の呼び出し間で競合条件がないことを保証している。
197 #include <sys/types.h>
198 #include <sys/stat.h>
204 main(int argc, char *argv[])
211 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
215 if (lstat(argv[1], &sb) == \-1) {
220 linkname = malloc(sb.st_size + 1);
221 if (linkname == NULL) {
222 fprintf(stderr, "insufficient memory\en");
226 r = readlink(argv[1], linkname, sb.st_size + 1);
233 if (r > sb.st_size) {
234 fprintf(stderr, "symlink increased in size "
235 "between lstat() and readlink()\en");
239 linkname[r] = \(aq\e0\(aq;
241 printf("\(aq%s\(aq points to \(aq%s\(aq\en", argv[1], linkname);
249 \fBreadlink\fP(1), \fBlstat\fP(2), \fBstat\fP(2), \fBsymlink\fP(2), \fBrealpath\fP(3),
250 \fBpath_resolution\fP(7), \fBsymlink\fP(7)
252 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.76 の一部
253 である。プロジェクトの説明とバグ報告に関する情報は
254 http://www.kernel.org/doc/man\-pages/ に書かれている。