Skip to content

[flang] GETLOG runtime and extension implementation: get login username #74628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Dec 21, 2023

Conversation

yiwu0b11
Copy link
Contributor

@yiwu0b11 yiwu0b11 commented Dec 6, 2023

yiwu0b11 and others added 20 commits November 6, 2023 20:51
Get login username, ussage:
CHARACTER(32) :: login
CALL getlog(login)
WRITE(*,*) login
accourding to https://linux.die.net/man/3/getlogin_r,
`_REENTRANT || _POSIX_C_SOURCE >= 199506L` checks the existance of getlogin_r
take copyBufferAndPad() out of anonymous namespace,
so it can be called in other places.
The compiler must have access to the implementations
of templated functions at the points where they're instantiated.
…restore CopyAndPad

Suggested: https://man.archlinux.org/man/getlogin_r.3.en
For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is. This is more flexible precisely because the user can set LOGNAME arbitrarily.

POSIX specification:
LOGNAME
The system shall initialize this variable at the time of login to be the user's login name.

Note that at this point, this implementation might not work on Windows
@yiwu0b11
Copy link
Contributor Author

yiwu0b11 commented Dec 6, 2023

accidently push the wrong branch in previous pr #70917

@yiwu0b11
Copy link
Contributor Author

yiwu0b11 commented Dec 6, 2023

#70917 (comment)
Thanks for the suggestion.
Now it only checks environment variable if there is an error from getlogin_r

@yiwu0b11 yiwu0b11 closed this Dec 6, 2023
@yiwu0b11 yiwu0b11 reopened this Dec 6, 2023
@yiwu0b11
Copy link
Contributor Author

yiwu0b11 commented Dec 7, 2023

getlog library intrinsic will first retrieve user login name using getlog_r on Linux and GetUserName on Windows. If that returns an error, it will then use environment variable to get username.

Copy link
Member

@DavidTruby DavidTruby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yiwu0b11
Copy link
Contributor Author

Hi @jeanPerier Sorry to throw a ping. I have changed the structure and implementation of getlog, it now does the following:
On Linux: use getlogin_r, if fail get from environment variable.
On Windows: get from environment variable to avoid linking to library Advapi32.lib.
Username will be pad with spaces as how gfortran does it.
e.g.

loginName    XXXXX       <--------- if length is longer than name, pad with space
loginNameXXXX            <--------- just right
logiXXXX                 <--------- if shorter still fill with name

Proper test has been added on both Linux and Windows for getlog_r and environment variable.

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Described logic in your comment looks good to me. I have two comments regarding the implementation.

Copy link

github-actions bot commented Dec 13, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@yiwu0b11
Copy link
Contributor Author

Windows CI failure: compiler out of heap space.

@rorth
Copy link
Collaborator

rorth commented Jan 10, 2024

This patch broke the Solaris build:

FAILED: tools/flang/runtime/CMakeFiles/obj.FortranRuntime.dir/extensions.cpp.o 
[...]
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |                        ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   61 |   char str[nameMaxLen];
      |            ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |             ^
1 warning and 1 error generated.

and

FAILED: tools/flang/unittests/Runtime/CMakeFiles/FlangRuntimeTests.dir/CommandTest.cpp.o
[...]
/vol/llvm/src/llvm-project/dist/flang/unittests/Runtime/CommandTest.cpp:530:21: error: use of undeclared identifier 'LOGIN_NAME_MAX' 
  530 |   const int charLen{LOGIN_NAME_MAX + 2};
      |                     ^
/vol/llvm/src/llvm-project/dist/flang/unittests/Runtime/CommandTest.cpp:531:14: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
  531 |   char input[charLen];
      |              ^~~~~~~
/vol/llvm/src/llvm-project/dist/flang/unittests/Runtime/CommandTest.cpp:531:14: note: initializer of 'charLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/unittests/Runtime/CommandTest.cpp:530:13: note: declared here
  530 |   const int charLen{LOGIN_NAME_MAX + 2};
      |             ^

As documented in Solaris limits.h(3HEAD) (and exactly matching XPG7), LOGIN_NAME_MAX not being defined is an allowed configuration:

   Runtime Invariant Values (Possibly Indeterminate)
       A definition of one of the symbolic names  in  the  following  list  is
       omitted  from  <limits.h>  on specific implementations where the corre-
       sponding value is equal to or greater than the stated minimum,  but  is
       unspecified.

       This  indetermination  might  depend  on the amount of available memory
       space on a specific instance of a specific implementation.  The  actual
       value  supported  by  a  specific  instance  will  be  provided  by the
       sysconf() function.
[...]
       LOGIN_NAME_MAX

           Maximum length of a login name.

So the code needs to fall back to sysconf(_SC_LOGIN_NAME_MAX) if LOGIN_NAME_MAX is not defined.

I've used the attached patch to implement this, which allowed the build and ninja check-all to finish. I'm uncertain if support for syscconf() == -1 is really necessary, though.
lnm.txt

@yiwu0b11
Copy link
Contributor Author

yiwu0b11 commented Jan 10, 2024

This patch broke the Solaris build:
I've used the attached patch to implement this, which allowed the build and ninja check-all to finish. I'm uncertain if support for syscconf() == -1 is really necessary, though. lnm.txt

Thanks for reporting and providing a fix for this. I do not have a Solaris instance on hand to test it, so if you don't mind, could you send this out as a pull request?

@rorth
Copy link
Collaborator

rorth commented Jan 11, 2024

Sure: I've just created one slightly changed from the attached patch: [flang] Handle missing LOGIN_NAME_MAX definition in runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:runtime flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants