mount_setattr.2: Add a reference to mount_namespaces(7) in discussion of propagation...
[man-pages.git] / man3 / ttyname.3
bloba3e8112bddca47b66a2bb930bd05c3afce7d65db
1 .\" Copyright (c) 1995 Jim Van Zandt <jrv@vanzandt.mv.com>
2 .\"
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
8 .\"
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
13 .\"
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 .\" GNU General Public License for more details.
18 .\"
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
22 .\" %%%LICENSE_END
23 .\"
24 .\" Modified 2001-12-13, Martin Schulze <joey@infodrom.org>
25 .\" Added ttyname_r, aeb, 2002-07-20
26 .\"
27 .TH TTYNAME 3 2021-03-22 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 ttyname, ttyname_r \- return name of a terminal
30 .SH SYNOPSIS
31 .nf
32 .B #include <unistd.h>
33 .PP
34 .BI "char *ttyname(int " fd );
35 .BI "int ttyname_r(int " fd ", char *" buf ", size_t " buflen );
36 .fi
37 .SH DESCRIPTION
38 The function
39 .BR ttyname ()
40 returns a pointer to the null-terminated pathname of the terminal device
41 that is open on the file descriptor \fIfd\fP, or NULL on error
42 (for example, if \fIfd\fP is not connected to a terminal).
43 The return value may point to static data, possibly overwritten by the
44 next call.
45 The function
46 .BR ttyname_r ()
47 stores this pathname in the buffer
48 .I buf
49 of length
50 .IR buflen .
51 .SH RETURN VALUE
52 The function
53 .BR ttyname ()
54 returns a pointer to a pathname on success.
55 On error, NULL is returned, and
56 .I errno
57 is set to indicate the error.
58 The function
59 .BR ttyname_r ()
60 returns 0 on success, and an error number upon error.
61 .SH ERRORS
62 .TP
63 .B EBADF
64 Bad file descriptor.
65 .TP
66 .\" glibc commit 15e9a4f378c8607c2ae1aa465436af4321db0e23
67 .B ENODEV
68 .I fd
69 refers to a slave pseudoterminal device
70 but the corresponding pathname could not be found (see NOTES).
71 .TP
72 .B ENOTTY
73 .I fd
74 does not refer to a terminal device.
75 .TP
76 .B ERANGE
77 .RB ( ttyname_r ())
78 .I buflen
79 was too small to allow storing the pathname.
80 .SH ATTRIBUTES
81 For an explanation of the terms used in this section, see
82 .BR attributes (7).
83 .ad l
84 .nh
85 .TS
86 allbox;
87 lbx lb lb
88 l l l.
89 Interface       Attribute       Value
91 .BR ttyname ()
92 T}      Thread safety   MT-Unsafe race:ttyname
94 .BR ttyname_r ()
95 T}      Thread safety   MT-Safe
96 .TE
97 .hy
98 .ad
99 .sp 1
100 .SH CONFORMING TO
101 POSIX.1-2001, POSIX.1-2008, 4.2BSD.
102 .SH NOTES
103 A process that keeps a file descriptor that refers to a
104 .BR pts (4)
105 device open when switching to another mount namespace that uses a different
106 .I /dev/ptmx
107 instance may still accidentally find that a device path of the same name
108 for that file descriptor exists.
109 However, this device path refers to a different device and thus
110 can't be used to access the device that the file descriptor refers to.
111 Calling
112 .BR ttyname ()
114 .BR ttyname_r ()
115 on the file descriptor in the new mount namespace will cause these
116 functions to return NULL and set
117 .I errno
119 .BR ENODEV .
120 .SH SEE ALSO
121 .BR tty (1),
122 .BR fstat (2),
123 .BR ctermid (3),
124 .BR isatty (3),
125 .BR pts (4)