Man page explains that ptsname(fd) should return a (char*) to a pseudo-terminal. On my system, it returns a something which when used as a (char*) yields a Seg Fault. Notice, however, that ptsname_r(fd, (char*)buffer, man_len) works as advertised. The bug arises in x11-apps/xconsole, which depends on ptsname. Here is the obligatory emerge --info: ===================================== fmccor@liasis LOGO [5]% emerge --info Portage 2.1_pre6-r5 (default-linux/amd64/2006.0, gcc-3.4.4, glibc-2.3.5-r2, 2.6.15-gentoo-r7-ail-simulation-sensors x86_64) ================================================================= System uname: 2.6.15-gentoo-r7-ail-simulation-sensors x86_64 Dual Core AMD Opteron(tm) Processor 165 Gentoo Base System version 1.6.14 dev-lang/python: 2.4.2 sys-apps/sandbox: 1.2.17 sys-devel/autoconf: 2.13, 2.59-r7 sys-devel/automake: 1.4_p6, 1.5, 1.6.3, 1.7.9-r1, 1.8.5-r3, 1.9.6-r1 sys-devel/binutils: 2.16.1 sys-devel/libtool: 1.5.22 virtual/os-headers: 2.6.11-r2 ACCEPT_KEYWORDS="amd64" AUTOCLEAN="yes" CBUILD="x86_64-pc-linux-gnu" CFLAGS="-O2 -pipe -march=k8" CHOST="x86_64-pc-linux-gnu" CONFIG_PROTECT="/etc /usr/kde/2/share/config /usr/kde/3.4/env /usr/kde/3.4/share/config /usr/kde/3.4/shutdown /usr/kde/3/share/config /usr/lib64/mozilla/defaults/pref /usr/share/X11/xkb /usr/share/config /var/qmail/control" CONFIG_PROTECT_MASK="/etc/gconf /etc/terminfo /etc/texmf/web2c /etc/env.d" CXXFLAGS="-O2 -pipe -march=k8" DISTDIR="/usr/portage/distfiles" FEATURES="autoconfig ccache cvs distlocks metadata-transfer sandbox sfperms strict" GENTOO_MIRRORS="http://mirror.datapipe.net/gentoo ftp://distro.ibiblio.org/pub/linux/distributions/gentoo/ http://gentoo.chem.wisc.edu/gentoo/ http://mirror.phy.olemiss.edu/mirror/gentoo" LC_ALL="en_US.utf8" MAKEOPTS="-j3" PKGDIR="/usr/portage/packages" PORTAGE_TMPDIR="/var/tmp" PORTDIR="/usr/portage" PORTDIR_OVERLAY="/usr/local/portage" SYNC="rsync://rsync.gentoo.org/gentoo-portage" USE="amd64 X Xaw3d alsa apache2 avi berkdb bitmap-fonts bzip2 cairo cli crypt cscope ctype cups dba dri eds emboss encode expat fastbuild foomaticdb force-cgi-redirect fortran ftp gd gif glx gpm graphviz gstreamer gtk gtk2 imlib jpeg kerberos ldap lzw lzw-tiff memlimit mp3 mpeg mysql ncurses nls nptl opengl pam pcre pdf pdflib perl png posix python qt quicktime readline ruby ruby18 sdl session simplexml soap sockets spell spl sqlite ssl stroke tcltk tcpd tetex tiff tokenizer truetype truetype-fonts type1-fonts unicode usb xml xpm xsl xv zlib elibc_glibc input_devices_evdev input_devices_keyboard input_devices_mouse kernel_linux userland_GNU video_cards_dummy video_cards_fbdev video_cards_nv video_cards_nvidia" Unset: ASFLAGS, CTARGET, EMERGE_DEFAULT_OPTS, LANG, LDFLAGS, LINGUAS ======================================================
Created attachment 82708 [details] Tiny example program extracted from xconsole Compile with my normal CFLAGS, it uses ptsname_r, and works fine. Add -DSEG_FAULT, it compiles as used in xconsole, and SegFaults: /dev/ptmx is open with descr = 3 fd 3 gets pseudo-terminal pointer aade5990 Segmentation fault For what it's worth, this gets recorded in dmesg, too, thus: bug[7991]: segfault at ffffffffaade5990 rip 00002aaaaac2ed00 rsp 00007fffffe167d8 error 4 (where I used gcc -o bug -DSEG_FAULT -O2 -march=k8 -pipe ptsname-bug.c)
Created attachment 82709 [details] Tiny example program extracted from xconsole Compile with my normal CFLAGS, it uses ptsname_r, and works fine. Add -DSEG_FAULT, it compiles as used in xconsole, and SegFaults: /dev/ptmx is open with descr = 3 fd 3 gets pseudo-terminal pointer aade5990 Segmentation fault For what it's worth, this gets recorded in dmesg, too, thus: bug[7991]: segfault at ffffffffaade5990 rip 00002aaaaac2ed00 rsp 00007fffffe167d8 error 4 (where I used gcc -o bug -DSEG_FAULT -O2 -march=k8 -pipe ptsname-bug.c)
dont ignore warnings, they exist for a reason :P build with -Wall and you should see why your example code is wrong ...
(In reply to comment #3) > dont ignore warnings, they exist for a reason :P > > build with -Wall and you should see why your example code is wrong ... > I see the warning. But it's not my code, it's from xconsole, and it conforms to the documentation. I normally presume documentation to be correct, and so the warning indicative of a bug.
And for what it's worth, on sparc I get the same warning, but output (in toto) is: ====================================== gcc -o bug -O2 -mcpu=ultrasparc3 -DSEG_FAULT pts*c ptsname-bug.c: In function `main': ptsname-bug.c:26: warning: assignment makes pointer from integer without a cast fmccor@polylepis Packages [12]% ./bug /dev/ptmx is open with descr = 3 fd 3 gets pseudo-terminal pointer 70195668 Pseudo-terminal is /dev/pts/13 ==================================== Which is what man page says should happen.
Curious, because in glibc (sysdeps/unix/sysv/linux/ptsname.c): char * ptsname (int fd) { return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer; } where ptrname_r is a weak alias for __ptsname_r and buffer is declared 'static char buffer[29];' - so really there should be no difference. > fd 3 gets pseudo-terminal pointer aade5990 > segfault at ffffffffaade5990 suggests a sign problem, but I think that's in your fprintf, which should be: fprintf(stderr, "fd %d gets pseudo-terminal pointer %p\n", pty, ptc); What happens if you add a cast to '(char *)' in front of the call to ptsname - shuts GCC up, so perhaps it has an effect.
it works on sparc because sparc is a 32bit host amd64 is a 64bit host implicit prototypes have an int as a return value ... on amd64, an int is a 32bit quantity ... thus your 64bit pointer is truncated to 32bits and everything crashes the bug is in xconsole, not glibc ... and i'm pretty sure xconsole does *not* conform to the documentation as the man page says you need to define at least _XOPEN_SOURCE before including the header file ... or you could be lazy and just define _GNU_SOURCE