Bug 36755 - Avoid fork/exec in chmod intrinsic
Summary: Avoid fork/exec in chmod intrinsic
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.4.0
: P3 minor
Target Milestone: ---
Assignee: Tobias Burnus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-07 20:58 UTC by H.J. Lu
Modified: 2012-01-12 20:29 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-09-28 00:02:17


Attachments
Incomplete chmod parser (1.01 KB, text/plain)
2012-01-12 08:04 UTC, Tobias Burnus
Details
Improved version: POSIXly correct chmod as separate program (2.17 KB, text/plain)
2012-01-12 17:31 UTC, Tobias Burnus
Details

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2008-07-07 20:58:11 UTC
The chmod intrinsic should be implemented with chmod () instead
of fork/exec.
Comment 1 Andrew Pinski 2008-07-07 21:00:59 UTC
Why is really an issue anyways? chmod should not be used too much anyways. 
Comment 2 H.J. Lu 2008-07-09 20:34:57 UTC
The current fork/exec implementation is incorrect. Basically, it
re-implements system (). It is tricky to get it 100% correct. One
can take a look at system () source to see it him/herself.  I think
we should call chmod directly, rather than fix fork/exec.
Comment 3 Andrew Pinski 2008-07-09 20:48:52 UTC
> The current fork/exec implementation is incorrect.

How is it incorrect?  Since file could have spaces in it, using system is not useful at all and even harder to do the correct thing.  Really it does not use system because creating a string to use with system makes it even more incorrect than it is currently.
Comment 4 H.J. Lu 2008-07-09 21:18:05 UTC
(In reply to comment #3)
> > The current fork/exec implementation is incorrect.
> 
> How is it incorrect?  Since file could have spaces in it, using system is not
> useful at all and even harder to do the correct thing.  Really it does not use
> system because creating a string to use with system makes it even more
> incorrect than it is currently.
> 

It should handle signal and check return from wait to match pid.
Comment 6 Tobias Burnus 2011-11-09 22:00:33 UTC
Another reason for using chmod() directly is that some systems cannot fork but still offer the function call.

I think the reason that one does the call instead of calling the library function is that one either needs to restrict the call or that one needs a fancy parser to support numeric values such as 0777 but also characters such as a+x for which the full pattern is [ugoa][+-=][rwxXst].
Comment 7 Tobias Burnus 2012-01-12 08:04:57 UTC
Created attachment 26305 [details]
Incomplete chmod parser

The attached chmod.c implements an incomplete chmod argument parser.
TODO:
- Check for the missing items and what should be supported
- Implement it in libgfortran and document the supported syntax.
Comment 8 Tobias Burnus 2012-01-12 17:31:37 UTC
Created attachment 26307 [details]
Improved version: POSIXly correct chmod as separate program

Attached: A "chmod" program, which takes a string and a file name and should act as a POSIX chmod program would do. Thus, it honors the "umask", allows for fancy combinations such as "g+w-r,a+x,-w,o=u,u+s,+t".

TODO:
- Convert this into libgfortran/intrinsic/chmod.c
- Write a fancy documentation for http://gcc.gnu.org/onlinedocs/gfortran/CHMOD.html which actually describes which modes are supported - and stats that the mode argument is in line with the "chmod utility" of POSIX (IEEE Std 1003.1-2001).
Comment 9 Tobias Burnus 2012-01-12 19:25:31 UTC
(In reply to comment #8)
> It honors the "umask", allows for fancy combinations such as
> "g+w-r,a+x,-w,o=u,u+s,+t".

Seemingly, I misread POSIX: For my program o=u sets "other" to the original permissions. By contrast, GNU chmod sets it to the last permission. For:
  umask 022; mkdir foo; chmod g+w-r,a+x,-w,o=u foo
the difference is whether other is r-x or rwx. In the code: Simply replace "old_mode" by "file_mode".

I have meanwhile also converted the fixed program into a libgfortran patch:
http://gcc.gnu.org/ml/fortran/2012-01/msg00126.html
Comment 10 Tobias Burnus 2012-01-12 20:26:18 UTC
Author: burnus
Date: Thu Jan 12 20:26:10 2012
New Revision: 183137

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183137
Log:
2012-01-12  Tobias Burnus  <burnus@net-b.de>

        PR fortran/36755
        * intrinsic.texi (CHMOD): Extend a bit and remove statement
        that /bin/chmod is called.

2012-01-12  Tobias Burnus  <burnus@net-b.de>

        PR fortran/36755
        * intrinsics/chmod.c (chmod_func): Replace call to /bin/chmod


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/intrinsic.texi
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/intrinsics/chmod.c
Comment 11 Tobias Burnus 2012-01-12 20:29:19 UTC
FIXED on the 4.7 trunk.

Thanks HJ for the report!