From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: gaetan.leurent@ens.fr (=?iso-8859-1?Q?Ga=EBtan?= LEURENT) Newsgroups: gmane.emacs.devel Subject: Re: Race-condition ? Date: Sun, 26 Jun 2005 00:25:56 +0200 Message-ID: References: <85ll4zwjsx.fsf@lola.goethe.zz> Reply-To: emacs-devel@gnu.org NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1119738175 7477 80.91.229.2 (25 Jun 2005 22:22:55 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 25 Jun 2005 22:22:55 +0000 (UTC) Cc: Eli Zaretskii , emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Jun 26 00:22:52 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1DmJ2a-0003JR-OF for ged-emacs-devel@m.gmane.org; Sun, 26 Jun 2005 00:22:21 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DmJ8s-0002EP-QY for ged-emacs-devel@m.gmane.org; Sat, 25 Jun 2005 18:28:50 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1DmJ85-0002A8-Oe for emacs-devel@gnu.org; Sat, 25 Jun 2005 18:28:02 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1DmJ83-00028m-99 for emacs-devel@gnu.org; Sat, 25 Jun 2005 18:28:00 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DmJ82-00027m-Cf for emacs-devel@gnu.org; Sat, 25 Jun 2005 18:27:58 -0400 Original-Received: from [129.199.96.40] (helo=nef2.ens.fr) by monty-python.gnu.org with esmtp (Exim 4.34) id 1DmJ9w-0007Xj-68; Sat, 25 Jun 2005 18:29:56 -0400 Original-Received: from clipper.ens.fr (clipper-gw.ens.fr [129.199.1.22]) by nef2.ens.fr (8.13.2/1.01.28121999) with ESMTP id j5PMPuQU026248 ; Sun, 26 Jun 2005 00:25:56 +0200 (CEST) X-Envelope-To: dak@gnu.org Original-Received: from (leurent@localhost) by clipper.ens.fr (8.13.1/jb-1.1) X-Authentication-Warning: clipper.ens.fr: leurent set sender to gaetan.leurent@ens.fr using -f Original-To: David Kastrup X-Start-Date: Sat, 25 Jun 2005 23:37:25 +0200 X-Spook: Europol Arnett corporate security bootleg unclassified Defcon bomb AK-47 brigand Cohiba Nazi 64 Vauxhall Cross data haven AIMSX SSL In-Reply-To: <85ll4zwjsx.fsf@lola.goethe.zz> (David Kastrup's message of "Fri, 24 Jun 2005 23:01:50 +0200") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (usg-unix-v) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-1.5.10 (nef2.ens.fr [129.199.96.32]); Sun, 26 Jun 2005 00:25:56 +0200 (CEST) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:39521 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:39521 David Kastrup wrote on 24 Jun 2005 23:01:50 +0200: > I fail to see the advantage of using chown, or using fopen and > fchown. In both cases the file name can be changed to refer to > something else before the operation starts. > > The only situation where fchown offers any advantage is where you > _already_ have a file open, like when you write the file after fopen, > and then change its permissions. Yes, that is true. But we are exactly in the situation were fchown is useful. > That is, the owner change must be accomplished in something like > write-region, or it is pointless. As an isolated operation, > fopen/fchown offers no advantage whatsoever. Yes, fopen/fchown would be the same as calling chown. But here we have just created a new file, and we want to change it's owner, so this should be done with fchown. The code is: | Fcopy_file (file, newname, | /* We have already prompted if it was an integer, | so don't have copy-file prompt again. */ | NILP (ok_if_already_exists) ? Qnil : Qt, | Qt, Qnil); | | /* Preserve owner and group, if possible (if we are root). */ | if (stat (SDATA (encoded_file), &data) >=3D 0) | chown (SDATA (encoded_file), data.st_uid, data.st_gid); | | Fdelete_file (file); So, we first open a file and write something in it inside Fcopy_file, then we call chown on the same path. But the file that is at this path could be something else that what the we created in Fcopy_file. We should keep that file open, and call fchown on its filedescriptor, instead of calling chown on it's path. This will need some change in Fcopy_file, to be able to do something with the filedescriptor before the file is closed; the most easy way seems to make a subroutine that does the job of Fcopy_file and returns the fd before closing it, and use this subroutine both in Fcopy_file and here in Frename_file. We also have the same kind of thing inside Fcopy_file (and this one is also in stable emacs) where we call chmod on the file path just after closing the file. [I have replaced some part of the code with //... to make it shorter] | #ifdef VMS | // ... | #else | #ifdef MSDOS | // ... | #else /* not MSDOS */ | ofd =3D emacs_open (SDATA (encoded_newname), | O_WRONLY | O_TRUNC | O_CREAT | | (EQ (mustbenew, Qexcl) ? O_EXCL : 0), | 0666); | #endif /* not MSDOS */ | #endif /* VMS */ | if (ofd < 0) | report_file_error ("Opening output file", Fcons (newname, Qnil)); |=20=20 | record_unwind_protect (close_file_unwind, make_number (ofd)); |=20=20 | immediate_quit =3D 1; | QUIT; | while ((n =3D emacs_read (ifd, buf, sizeof buf)) > 0) | if (emacs_write (ofd, buf, n) !=3D n) | report_file_error ("I/O error", Fcons (newname, Qnil)); | immediate_quit =3D 0; |=20=20 | /* Closing the output clobbers the file times on some systems. */ | if (emacs_close (ofd) < 0) | report_file_error ("I/O error", Fcons (newname, Qnil)); |=20=20 | if (input_file_statable_p) | { | if (!NILP (keep_time)) | { | // ... | } | #ifndef MSDOS | chmod (SDATA (encoded_newname), st.st_mode & 07777); | #else /* MSDOS */ | // ... | #endif /* MSDOS */ | } Here the change is very easy: we should just do fchmod(ofd,...) before emacs_close(ofd). --=20 Ga=EBtan LEURENT