From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q2KJfvi4023888 for ; Tue, 20 Mar 2012 20:41:57 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgACAKvcaE9KfVI0kGdsb2JhbABDpSuITgGIawgiAQEBAQkJDQcUBCOCCQEBAQMBCwcCEwQVARsSCwEDAQsGBQsNDQkBFyIBDAEEAQUBAwEGEgYTEhCHYwULoQIKjBMKgmeFNj+BDgEFC41QAYMlBJVfhmeHYT1Cg2OBOg X-IronPort-AV: E=Sophos;i="4.73,619,1325458800"; d="scan'208";a="137001906" Received: from mail-wg0-f52.google.com ([74.125.82.52]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Mar 2012 20:41:52 +0100 Received: by wgbgn7 with SMTP id gn7so269113wgb.9 for ; Tue, 20 Mar 2012 12:41:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=a/XoRS9GS3L/wiiYo89GcnApi00SBD7s05aml3i2aho=; b=UittqeZSQneJO7GZSdo8W5VBOTRsS2A5WWGGqW5pA3Mk9PP55IQzuNTF1+r8plMHlZ XgItrfw0bPzmQwGZ7cV45oKm4ATJGYPn4/yFD3pJg2dlk8+n6wE2zZCb/l5B6sI8DGgz Dw1yP5/TiWGeSIYFhO6FhwQJudVJeQzC3VxuE8liTDLd7MFls1h+9HghpAiiycBBGGNn iiqS/zs5VDgB09bLS3ka+kDxQm6xuecO6XydFnT/iAkd7iYGgztyzNEC0D7HThrs7dK5 ivcAKKm8ucSw0Fs5jlnUf1GNt0QfsmwMcruYQZyk0TI8vzN3xfJkoNPNJtDI7ebPq8vc umdQ== MIME-Version: 1.0 Received: by 10.180.95.34 with SMTP id dh2mr2547409wib.15.1332272511896; Tue, 20 Mar 2012 12:41:51 -0700 (PDT) Received: by 10.216.82.200 with HTTP; Tue, 20 Mar 2012 12:41:51 -0700 (PDT) Reply-To: yminsky@gmail.com In-Reply-To: References: <20120320175113.GC935@viper.local> <20120320180759.GE935@viper.local> <4F68CC67.3020207@janestreet.com> Date: Tue, 20 Mar 2012 15:41:51 -0400 Message-ID: From: Yaron Minsky To: Till Varoquaux Cc: David House , =?ISO-8859-1?Q?Ricardo_Catalinas_Jim=E9nez?= , caml-list@inria.fr Content-Type: multipart/alternative; boundary=f46d04448035d0cd8604bbb1dd5a Subject: Re: [Caml-list] Re: Unix.getlogin () fails when stdin is redirected --f46d04448035d0cd8604bbb1dd5a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Is there a concrete difference in behavior you're concerned about? y On Tue, Mar 20, 2012 at 2:48 PM, Till Varoquaux wrote: > getpwuid(getuid()) is not a synonym for get_login (refer to the > discussion in the POSIX standard[^1]). You should not shadow posix > functions by functions with different semantics in the Unix modules; > providing your own abstraction over the OS is a commendable goal but > you should do so without silently bypassing core functions. > > Till > [1]:http://pubs.opengroup.org/onlinepubs/007904975/functions/getlogin.html > > > On Tue, Mar 20, 2012 at 2:28 PM, David House > wrote: > > 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 =3D Unix.getlogin > > let getlogin () =3D (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=E9nez wrote: > >> > >> On Tue, Mar 20, 2012 at 06:51:13PM +0100, Ricardo Catalinas Jim=E9nez > wrote: > >>> > >>> I found out the next issue in this simple code: > >>> > >>> let () =3D > >>> 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) =3D 3 > >>> fstat(3, {st_mode=3DS_IFREG|0644, st_size=3D509, ...}) =3D 0 > >>> mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, > -1, > >>> 0) =3D 0x7fb125554000 > >>> read(3, "root:x:0:0:root:/root:/bin/bash\n"..., 4096) =3D 509 > >>> read(3, "", 4096) =3D 0 > >>> close(3) =3D 0 > >>> munmap(0x7fb125554000, 4096) =3D 0 > >>> -> ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or > >>> TCGETS, 0x7fff12682c98) =3D -1 ENOTTY (Inappropriate ioctl for device) > >>> write(2, "Fatal error: exception Unix.Unix"..., 59) =3D 59 > >>> exit_group(2) =3D ? > >> > >> > >> > >> 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 > > > > > > > > > > -- > > Caml-list mailing list. Subscription management and archives: > > https://sympa-roc.inria.fr/wws/info/caml-list > > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > > Bug reports: http://caml.inria.fr/bin/caml-bugs > > > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --f46d04448035d0cd8604bbb1dd5a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Is there a concrete difference in behavior you're concerned about?
=
y

On Tue, Mar 20, 2012 at = 2:48 PM, Till Varoquaux <till@pps.jussieu.fr> wrote:
getpwuid(getuid()) is not a synonym for get_= login (refer to the
discussion in the POSIX standard[^1]). You should not shadow posix
functions by functions with different semantics in the Unix modules;
providing your own abstraction over the OS is a commendable goal but
you should do so without silently bypassing core functions.

Till
[1]:http://pubs.opengroup.org/onlinepubs/00790497= 5/functions/getlogin.html


On Tue, Mar 20, 2012 at 2:28 PM, David House <dhouse@janestreet.com> wrote:
> 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 unreliabl= e,
> =A0see the BUGS section of getlogin(3) *)
> let getlogin_orig =3D Unix.getlogin
> let getlogin () =3D (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=E9nez wrote:=
>>
>> On Tue, Mar 20, 2012 at 06:51:13PM +0100, Ricardo Catalinas Jim=E9= nez wrote:
>>>
>>> I found out the next issue in this simple code:
>>>
>>> let () =3D
>>> =A0 =A0 print_endline "Hello";
>>> =A0 =A0 print_endline (Unix.getlogin ())
>>>
>>> Running in the normal case, with `./a.out' gives:
>>>
>>> Hello
>>> ricardo
>>>
>>> But running like `./a.out</dev/null' makes Unix.getlogi= n fail:
>>>
>>> Hello
>>> Fatal error: exception Unix.Unix_error(20, "getlogin"= ;, "")
>>>
>>> A simple strace reveals the problem:
>>>
>>> =A0 =A0open("/etc/passwd", O_RDONLY|O_CLOEXEC) =3D 3=
>>> =A0 =A0fstat(3, {st_mode=3DS_IFREG|0644, st_size=3D509, ...}) = =3D 0
>>> =A0 =A0mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_= ANONYMOUS, -1,
>>> 0) =3D 0x7fb125554000
>>> =A0 =A0read(3, "root:x:0:0:root:/root:/bin/bash\n"..= ., 4096) =3D 509
>>> =A0 =A0read(3, "", 4096) =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =3D 0
>>> =A0 =A0close(3) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0=3D 0
>>> =A0 =A0munmap(0x7fb125554000, 4096) =A0 =A0 =A0 =A0 =A0 =A0=3D= 0
>>> -> =A0ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEX= T_DEVICE or
>>> TCGETS, 0x7fff12682c98) =3D -1 ENOTTY (Inappropriate ioctl for= device)
>>> =A0 =A0write(2, "Fatal error: exception Unix.Unix"..= ., 59) =3D 59
>>> =A0 =A0exit_group(2) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =3D ?
>>
>>
>>
>> Someone knew the answer, man 3 getlogin reads:
>>
>> =A0 =A0 =A0 =A0Note that glibc does not follow the POSIX specifica= tion and uses
>> =A0 =A0 =A0 =A0stdin instead of /dev/tty. =A0A bug. =A0(Other rece= nt systems, like
>> =A0 =A0 =A0 =A0SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8 all retur= n the login
>> =A0 =A0 =A0 =A0name also when stdin is redirected.)
>>
>>
>> Regards
>
>
>
>
> --
> Caml-list mailing list. =A0Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports:
http://caml.inria.fr/bin/caml-bugs
>


--
Caml-list mailing list. =A0Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


--f46d04448035d0cd8604bbb1dd5a--