Recent

Author Topic: [SOLVED] Linux, GetUsername( I can't get it!  (Read 2007 times)

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Linux, GetUsername( I can't get it!
« Reply #15 on: July 13, 2020, 02:05:16 pm »
Hi!

For Debian and all it's children:

They break the conventions that the hostname is in the environment.

So we have to nurse that baby.

Code: Bash  [Select][+][-]
  1. env HOSTNAME=MySweetRaspi

Now you can proceed like in every other Linux.

Winni

rvk

  • Hero Member
  • *****
  • Posts: 4329
Re: Linux, GetUsername( I can't get it!
« Reply #16 on: July 13, 2020, 02:10:09 pm »
Code: Bash  [Select][+][-]
  1. env HOSTNAME=MySweetRaspi
Now you can proceed like in every other Linux.
And that works with any user?

BTW, it doesn't work for me on a RPI.
You should probably need export to make it stick.
And if you want it to survive a reboot you'll even need to do more (set it in .profile).
And for what profile?
How do you set this systemwide???

Code: [Select]
root@sonarr:~# env HOSTNAME=MySweetRaspi
SHELL=/bin/bash
SUDO_GID=1000
SUDO_COMMAND=/bin/bash
SUDO_USER=pi
NO_AT_BRIDGE=1
PWD=/root
LOGNAME=root
HOME=/root
LANG=en_GB.UTF-8
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
TERM=xterm
USER=root
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SUDO_UID=1000
MAIL=/var/mail/root
TEXTDOMAIN=Linux-PAM
_=/usr/bin/env
HOSTNAME=MySweetRaspi

root@sonarr:~# env | grep HOST
root@sonarr:~#

It's generally better to just use gethostname().

BTW. HOSTNAME is not a posix-compliance. So other UNIX system might also not have it. But some Linux systems do.
https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
« Last Edit: July 13, 2020, 02:17:41 pm by rvk »

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Linux, GetUsername( I can't get it!
« Reply #17 on: July 13, 2020, 02:18:29 pm »
Hi!

No - if you make changes in the environment then you should be of course

Code: Bash  [Select][+][-]
  1. root

Winni

rvk

  • Hero Member
  • *****
  • Posts: 4329
Re: Linux, GetUsername( I can't get it!
« Reply #18 on: July 13, 2020, 02:24:09 pm »
No - if you make changes in the environment then you should be of course
Code: Bash  [Select][+][-]
  1. root
Which is why it's better to use gethostname().

Manjaro is based on Arch Linux. So that's the second we've found.
I'm sure there are more where HOSTNAME isn't defined as environment variable.

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Linux, GetUsername( I can't get it!
« Reply #19 on: July 13, 2020, 02:26:59 pm »

Which is why it's better to use gethostname().

/quote]

I think it is better to have somehthing like a default Linux.

But as you like .....

Winni

Warfley

  • Sr. Member
  • ****
  • Posts: 314
Re: Linux, GetUsername( I can't get it!
« Reply #20 on: July 13, 2020, 02:31:51 pm »
Hi!

No - if you make changes in the environment then you should be of course

Code: Bash  [Select][+][-]
  1. root

Winni
So this is actually quite a problem, because not everyone has root access. One could go into his user profile and set there HOSTNAME=$(cat /proc/sys/kernel/hostname), but again thats just a hack.

For linux it is pretty simple, if something is not part of the kernel or the posix standard, you should probably not rely on it. Everything else is distro dependend.

The other option is to document it as a dependency and for example say: This program is only guaranteed to work on OpenSuse. Which of course means that most linux users won't be able to use it (fact is debian derivates are the most common distros) or "This program needs the HOSTNAME variable to be set" but this might alienate not so tech savy users.

rvk

  • Hero Member
  • *****
  • Posts: 4329
Re: Linux, GetUsername( I can't get it!
« Reply #21 on: July 13, 2020, 02:32:05 pm »
I think it is better to have somehthing like a default Linux.
But as you like .....
I would like that too.
But it's simply not the case.
And on UNIX systems (are they still used?) it's also not usually a env-variable.

Requiring users to set the HOSTNAME is a big ask.
If at all, it should be done by Lazarus itself automatically.
But if the requirement is root, the Lazarus IDE might not always run as root.

So... that's why IMHO the gethostname() is just the better choice at the moment.

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Linux, GetUsername( I can't get it!
« Reply #22 on: July 13, 2020, 02:38:14 pm »
Hi!

It is old habit since UNIX days that the hostname is initialized in a system startup script.

The reasons why Debian and Arch dont do that is a miracle.

Suse Linux and a lot of others posess the hostname inside the environment.

Winni

Warfley

  • Sr. Member
  • ****
  • Posts: 314
Re: Linux, GetUsername( I can't get it!
« Reply #23 on: July 13, 2020, 02:41:06 pm »
I think it is better to have somehthing like a default Linux.

But as you like .....

Winni

There is not such a thing, because Linux is only a kernel, you could probably replace the NT kernel with the Linux kernel in Windows and to the end user it would not be noticable (maybe in performance but who cares). And effectively make windows a Linux distro.
Every Linux distro is it's own OS, and assuming that an environment variable is set between Linux distros is like assuming the environment variables on Windows have the same name.

But there is the POSIX standard. While many Linux distros don't have their compliance certified (because that costs money), nearly all Linux distros are fully compatible with the POSIX standard.

So if you want to have something relyable, check out what is part of the POSIX standard and what not (it's mentioned in the man pages if this is part of any standard). Because this is something you can assume of nearly all linux distros (even though my windows example from above would not be POSIX compatible).
Bonus points: BSD and MacOS and other UNIX systems are also POSIX complient, meaning if you restrict yourself to the POSIX APIs, your program will automatically be cross plattform compatible.

Then there are APIs provided by the Linux Kernel, these are also available under every Linux system, but might change with kernel versions. This includes all the pseudo files located at /proc, which you can use for example to get the hardware spec, process list and so on

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3865
  • I like bugs.
Re: Linux, GetUsername( I can't get it!
« Reply #24 on: July 13, 2020, 02:42:21 pm »
That was discussed in a recent topic.
https://forum.lazarus.freepascal.org/index.php/topic,50533.0.html
Right! I saw that thread but didn't read it carefully.
IDEProcs already depends on Unix and BaseUnix, used heavily in function BackupFileForWrite. I will add more Unix specific code. This works for me :

Code: Pascal  [Select][+][-]
  1. function GetCurrentChangeLog: string;
  2. begin
  3.   Result:='<'+GetCurrentUserName+'@'+
  4.   {$IF defined(MSWindows) or defined(HASAMIGA)}
  5.     GetEnvironmentVariableUTF8('COMPUTERNAME')
  6.   {$ELSE}
  7.     GetHostname
  8.   {$ENDIF}
  9.     + '>';
  10. end;

Question about Amiga: does it need special treatment? Does it have 'COMPUTERNAME'?
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Linux, GetUsername( I can't get it!
« Reply #25 on: July 13, 2020, 03:27:17 pm »
@Warfley

Code: Pascal  [Select][+][-]
  1. uses BGRAbitmap, BGRAdefaultBitmap, BGRAbitmapTypes;
  2. ....
  3.  
  4. procedure Foam (dest: TBGRABitmap);
  5. var i: integer;
  6.     radius, x,y : single;
  7.     col, border: TBGRAPixel;
  8. begin
  9.   col := BGRA (255, 255,200,128);
  10.   border := BGRA(160,160,160,160);
  11.   dest.FillRect(0,0,dest.width,dest.height,BGRAPixelTransparent,dmset);
  12.    for i := 0 to 5000 do
  13.      begin
  14.       x := random (dest.width;
  15.       y := random (dest.height);
  16.       radius := random (4)+ random;
  17.       dest.EllipseAntialias(x,y,radius,radius,border,0.75,col);
  18.       end;
  19. end;
             

Winni

QEnnay

  • Jr. Member
  • **
  • Posts: 56
Re: Linux, GetUsername( I can't get it!
« Reply #26 on: July 13, 2020, 04:17:19 pm »
Whare did you find that?

Code: Pascal  [Select][+][-]
  1. users.pas
  2.  
  3. unit users;
  4.  
  5. Interface
  6. {$mode delphi}
  7. uses UnixType,BaseUnix,pwd,grp, {$ifdef Linux} shadow,{$endif}SysUtils,Classes;
  8.  
  9. Type
  10.   EUserLookupError = Class(Exception);
  11.   EGroupLookupError = Class(Exception);
  12.   EShadowLookupError = Class(Exception);
  13.   TPasswordRecord = Tpasswd;
  14.   PPasswordRecord = ^TPasswordRecord;
  15.  
  16. { User functions }
  17. Function  getpwnam(Const UserName: String) : PPasswordRecord;
  18. Procedure GetUserData(Const UserName : String; Var Data : TPasswordRecord); overload;
  19. Procedure GetUserData(Uid : TUID; Var Data : TPasswordRecord); overload;
  20. function  GetUserName(UID : TUID) : String;
  21. function  GetUserId(Const UserName : String) : TUID;
  22. function  GetUserGid(Const UserName : String) : TGID;
  23. function  GetUserDir(Const UserName : String): String;
  24. function  GetUserDescription(Const UserName : String): String;
  25. Procedure GetUserList(List : Tstrings);overload;
  26. Procedure GetUserList(List : TStrings; WithIDs : Boolean);overload;
  27. [code]
Laz 2.0.8
Linux-Mint 20.04.0 + Cinnamon, Kernel 5.8 (NOT stock Ubuntu/Mint 5.4 Kernel)
Lenovo Flex 5 Ryzen 5 4500, 512GB-NVMe, 16GB memory

rvk

  • Hero Member
  • *****
  • Posts: 4329
Re: Linux, GetUsername( I can't get it!
« Reply #27 on: July 13, 2020, 04:22:23 pm »
Whare did you find that?

Code: Pascal  [Select][+][-]
  1. users.pas
  2.  
  3. unit users;
  4.  
  5. Interface
  6. {$mode delphi}
  7. uses UnixType,BaseUnix,pwd,grp, {$ifdef Linux} shadow,{$endif}SysUtils,Classes;
  8.  
  9. Type
  10.   EUserLookupError = Class(Exception);
  11.   EGroupLookupError = Class(Exception);
  12.   EShadowLookupError = Class(Exception);
  13.   TPasswordRecord = Tpasswd;
  14.   PPasswordRecord = ^TPasswordRecord;
  15.  
  16. { User functions }
  17. Function  getpwnam(Const UserName: String) : PPasswordRecord;
  18. Procedure GetUserData(Const UserName : String; Var Data : TPasswordRecord); overload;
  19. Procedure GetUserData(Uid : TUID; Var Data : TPasswordRecord); overload;
  20. function  GetUserName(UID : TUID) : String;
  21. function  GetUserId(Const UserName : String) : TUID;
  22. function  GetUserGid(Const UserName : String) : TGID;
  23. function  GetUserDir(Const UserName : String): String;
  24. function  GetUserDescription(Const UserName : String): String;
  25. Procedure GetUserList(List : Tstrings);overload;
  26. Procedure GetUserList(List : TStrings; WithIDs : Boolean);overload;
  27. [code]
I don't see
  GetUserName( UserName : String) : String;
there like you stated.
I do see the UID parameter I mentioned.

QEnnay

  • Jr. Member
  • **
  • Posts: 56
Re: Linux, GetUsername( I can't get it!
« Reply #28 on: July 13, 2020, 04:37:06 pm »
Code: Pascal  [Select][+][-]
  1.   Writeln(GetUserName(fpgetuid));
  2.  

Thanks, that works perfectly for me. I got it from "users.pas" after a search here. I could not find anything else that alluded to getting the User-Name with out first knowing it. Not even the Wiki.

With the discussion that has followed my first post, I am not sure if I should mark it SOLVED or not. :)
Laz 2.0.8
Linux-Mint 20.04.0 + Cinnamon, Kernel 5.8 (NOT stock Ubuntu/Mint 5.4 Kernel)
Lenovo Flex 5 Ryzen 5 4500, 512GB-NVMe, 16GB memory

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3865
  • I like bugs.
Re: [SOLVED] Linux, GetUsername( I can't get it!
« Reply #29 on: July 13, 2020, 04:55:36 pm »
I applied the function GetCurrentChangeLog change in r63554. I guess the GetHostName stuff was little out of topic in this GetUserName thread.
If somebody knows about Amiga HostName, please tell me (PM, mail).
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

 

TinyPortal © 2005-2018