From: David House <dhouse@janestreet.com>
To: "Ricardo Catalinas Jiménez" <jimenezrick@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: Unix.getlogin () fails when stdin is redirected
Date: Tue, 20 Mar 2012 18:28:55 +0000 [thread overview]
Message-ID: <4F68CC67.3020207@janestreet.com> (raw)
In-Reply-To: <20120320180759.GE935@viper.local>
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</dev/null' makes Unix.getlogin fail:
>>
>> 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
next prev parent reply other threads:[~2012-03-20 18:29 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-20 17:51 [Caml-list] " Ricardo Catalinas Jiménez
2012-03-20 18:07 ` [Caml-list] " Ricardo Catalinas Jiménez
2012-03-20 18:28 ` David House [this message]
2012-03-20 18:48 ` Till Varoquaux
2012-03-20 19:41 ` Yaron Minsky
2012-03-20 20:12 ` Till Varoquaux
2012-03-20 22:45 ` oliver
2012-03-21 12:26 ` Török Edwin
2012-03-21 2:17 ` [Caml-list] " malc
2012-03-21 11:29 ` Gerd Stolpmann
2012-03-21 17:59 ` malc
2012-03-21 0:51 [Caml-list] " Gerd Stolpmann
2012-03-21 2:10 ` Yaron Minsky
2012-03-21 2:22 ` Yaron Minsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4F68CC67.3020207@janestreet.com \
--to=dhouse@janestreet.com \
--cc=caml-list@inria.fr \
--cc=jimenezrick@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox