From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr 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 q2KKChrr024678 for ; Tue, 20 Mar 2012 21:12:43 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuwBAPHjaE/RVdW2kGdsb2JhbAA5CoVKn2GROggiAQEBAQkJDQcUBCOCCQEBAQQSAg8EGQEtCwEDDAEFAwILDQICCQkBEwICIQESAQUBChIZEhCHaAuhAAqLRYM/hTmBTQEFC4EkiD1sgwMBgg+BFgSVX4sugxo9gVeCToE6 X-IronPort-AV: E=Sophos;i="4.73,619,1325458800"; d="scan'208";a="150422202" Received: from mail-yx0-f182.google.com ([209.85.213.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Mar 2012 21:12:38 +0100 Received: by yenl9 with SMTP id l9so666566yen.27 for ; Tue, 20 Mar 2012 13:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=9xEm3UhWbkxRFI4ozKk94lL/6eww7nJAEcLMvfGg0P8=; b=v8af/lIMY2yeIWoC9Z0YF1tKVrAZyrWUoXwCjkQuj6HQpweTMiOoX2mo9yOlfZtQc8 BCTJbk1FFeo6xbf9v9AF//uIsBQYKs03/RMCXGND2USUJEYMMzWdJMEgqC5W+y1spaFw NPDtKJ5kSzSzTk92sbibWuU/5jOreMxN3F8rvAU3SsFiABR/DebarmhNrD5+eoy/U/V4 DS7w4OeFJ63yWah6N1KURquA1h7NL1yf2YUrS9JAIbC959iNu3VzEnYSVu2cgpERtskC +5ge+mrz6hXmPA8LjdomcM6yoaxWkGxq+KBuOeksJc/5fNGmoKKiSUTzS/V8ljT2Od7x f++w== MIME-Version: 1.0 Received: by 10.182.134.97 with SMTP id pj1mr1760200obb.2.1332274356830; Tue, 20 Mar 2012 13:12:36 -0700 (PDT) Sender: till.varoquaux@gmail.com Received: by 10.182.96.228 with HTTP; Tue, 20 Mar 2012 13:12:36 -0700 (PDT) In-Reply-To: References: <20120320175113.GC935@viper.local> <20120320180759.GE935@viper.local> <4F68CC67.3020207@janestreet.com> Date: Tue, 20 Mar 2012 16:12:36 -0400 X-Google-Sender-Auth: eC9gaZA3B5C7cyc82mS2Ob2dXUI Message-ID: From: Till Varoquaux To: yminsky@gmail.com Cc: David House , =?UTF-8?Q?Ricardo_Catalinas_Jim=C3=A9nez?= , caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q2KKChrr024678 X-Validation-by: till@pps.jussieu.fr Subject: Re: [Caml-list] Re: Unix.getlogin () fails when stdin is redirected As far as I can tell getpwuid(geteuid()) is what the (deprecated) cuserid function does. The linux man page explains the difference between the two functions like this: _ These functions let your program identify positively the user who is running (cuserid()) or the user who logged in this session (getlogin()). (These can differ when set-user-ID programs are involved.) I expect there to be more points were the behaviours of those two functions diverge (e.g.: sudo, deamons etc...). In general I am very skeptic any time I see a one liner that is sold as a working alternative to a glibc function. The glibc has issues and pitfalls but they tend to be very well documented. "Better the devil you know"... Till On Tue, Mar 20, 2012 at 3:41 PM, Yaron Minsky wrote: > 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 = 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 >> > >> > >> > >> > >> > -- >> > 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 >> >