From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q2KIT3tt021593 for ; Tue, 20 Mar 2012 19:29:04 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvoBAFjLaE8machwl2dsb2JhbABEhUGxRQEBAQEBBhgHO4IJAQEFIwQLAQVAARALGAICBQ0BCAsCAgkDAgECAUUGDQEHAQGICQilIZIhBIEvjCwBgg+BFgSobYFW X-IronPort-AV: E=Sophos;i="4.73,619,1325458800"; d="scan'208";a="150410128" Received: from mx1.janestreet.com (HELO nyc-dmz-mxout1.janestreet.com) ([38.105.200.112]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 20 Mar 2012 19:28:59 +0100 Received: from nyc-qsv-mail1.delacy.com ([172.25.22.57]) by nyc-dmz-mxout1.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1SA3nk-0000J8-TJ; Tue, 20 Mar 2012 14:28:56 -0400 Received: from ldn-qws-011.delacy.com ([172.23.133.111]) by nyc-qsv-mail1.delacy.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.76) (envelope-from ) id 1SA3nk-0004pv-N6; Tue, 20 Mar 2012 14:28:56 -0400 Message-ID: <4F68CC67.3020207@janestreet.com> Date: Tue, 20 Mar 2012 18:28:55 +0000 From: David House User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: =?UTF-8?B?UmljYXJkbyBDYXRhbGluYXMgSmltw6luZXo=?= CC: caml-list@inria.fr References: <20120320175113.GC935@viper.local> <20120320180759.GE935@viper.local> In-Reply-To: <20120320180759.GE935@viper.local> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] Re: Unix.getlogin () fails when stdin is redirected Note that Jane Street's core library [1] does not use getlogin(3) in its replacement Unix module, for exactly this reason: (* The standard getlogin function goes through utmp which is unreliable, see the BUGS section of getlogin(3) *) let getlogin_orig = Unix.getlogin let getlogin () = (Unix.getpwuid (getuid ())).Unix.pw_name [1]: https://bitbucket.org/yminsky/ocaml-core/wiki/Home I just tested your specific example, and it worked fine. On Tue 20 Mar 2012 06:07:59 PM GMT, Ricardo Catalinas Jiménez wrote: > On Tue, Mar 20, 2012 at 06:51:13PM +0100, Ricardo Catalinas Jiménez wrote: >> I found out the next issue in this simple code: >> >> let () = >> print_endline "Hello"; >> print_endline (Unix.getlogin ()) >> >> Running in the normal case, with `./a.out' gives: >> >> Hello >> ricardo >> >> But running like `./a.out> >> Hello >> Fatal error: exception Unix.Unix_error(20, "getlogin", "") >> >> A simple strace reveals the problem: >> >> open("/etc/passwd", O_RDONLY|O_CLOEXEC) = 3 >> fstat(3, {st_mode=S_IFREG|0644, st_size=509, ...}) = 0 >> mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb125554000 >> read(3, "root:x:0:0:root:/root:/bin/bash\n"..., 4096) = 509 >> read(3, "", 4096) = 0 >> close(3) = 0 >> munmap(0x7fb125554000, 4096) = 0 >> -> ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff12682c98) = -1 ENOTTY (Inappropriate ioctl for device) >> write(2, "Fatal error: exception Unix.Unix"..., 59) = 59 >> exit_group(2) = ? > > > Someone knew the answer, man 3 getlogin reads: > > Note that glibc does not follow the POSIX specification and uses > stdin instead of /dev/tty. A bug. (Other recent systems, like > SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8 all return the login > name also when stdin is redirected.) > > > Regards