Recent

Author Topic: (SOLVED) Need to get username and computername for logging, win and unix  (Read 1054 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 257
I am porting an old application from Delphi to Lazarus to make it dual platform (Windows and Linux).
In this application there is a logging class (also used elsewhere) which among other things writes a summary at the top of each new log file.
In this summary is the currently logged on user and the name of the computer.

This code worked on Windows but it does not give me the computer name in Linux (Raspbian Buster):

Code: Pascal  [Select][+][-]
  1. const
  2.   {$IFDEF UNIX}
  3.     USERNAME = 'USER';
  4.     COMPUTER = 'HOSTNAME';
  5.   {$ELSE}
  6.     USERNAME = 'USERNAME';
  7.     COMPUTER = 'COMPUTERNAME';
  8.   {$ENDIF}
  9. ...
  10. begin
  11.   ...
  12.     slHeader.Add('Computer name:   ' + GetEnvironmentVariable(COMPUTER));
  13.     slHeader.Add('Current user:    ' + GetEnvironmentVariable(USERNAME));

The current user is properly extracted but not the computer name on Linux.
Both are present on Windows.

What can I do in order to get the computer name on Raspbian Linux?

« Last Edit: July 11, 2020, 10:58:36 pm by BosseB »
--
Bo Berglund
Sweden

winni

  • Hero Member
  • *****
  • Posts: 1774
Re: Need to get username and computername for logging, win and unix
« Reply #1 on: July 11, 2020, 05:56:02 pm »
Hi!

For Linux  start a TProcess with

Code: Pascal  [Select][+][-]
  1. MyProcess.Executable := 'hostname';

Winni

rvk

  • Hero Member
  • *****
  • Posts: 4281
Re: Need to get username and computername for logging, win and unix
« Reply #2 on: July 11, 2020, 06:06:34 pm »
What can I do in order to get the computer name on Raspbian Linux?
Strange.
What does echo $HOSTNAME give you in a console?
For me it gives the hostname on a RPI.

BosseB

  • Sr. Member
  • ****
  • Posts: 257
Re: Need to get username and computername for logging, win and unix
« Reply #3 on: July 11, 2020, 06:46:06 pm »
What can I do in order to get the computer name on Raspbian Linux?
Strange.
What does echo $HOSTNAME give you in a console?
For me it gives the hostname on a RPI.
How very strange....
I had done this to check why this happens:
Code: Text  [Select][+][-]
  1. $ env | grep HOSTNAME
  2. $
As you can see it returns empty.
But:
Code: Text  [Select][+][-]
  1. $ echo $HOSTNAME
  2. rpi4-gui
  3. $

So executing the echo brings out the hostname, but listing all the environment variables does not.
I started by listing them all when I found that the log was missing the hostname and env does not return the HOSTNAME entry...
Since the username is extracted using the same call I checked the definition of GetEnvironmentVariable found in osutil.inc below fpc rtl:

Code: Pascal  [Select][+][-]
  1. Function GetEnvironmentVariable(Const EnvVar : UnicodeString) : UnicodeString;
  2. begin
  3.   result:=UnicodeString(GetEnvironmentVariable(AnsiString(EnvVar)));
  4. end;

and:

Code: Pascal  [Select][+][-]
  1. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  2. begin
  3.   { no need to adjust the code page of EnvVar to DefaultSystemCodePage, as only
  4.     ASCII identifiers are supported }
  5.   Result:=BaseUnix.FPGetenv(PChar(pointer(EnvVar)));
  6. end;
  7.  

There must be a difference in how echo grabs the variable HOSTNAME compared to USER, which is listed among the env vars.
So the question remains, how can I fix this?
--
Bo Berglund
Sweden

winni

  • Hero Member
  • *****
  • Posts: 1774
Re: Need to get username and computername for logging, win and unix
« Reply #4 on: July 11, 2020, 07:11:35 pm »
Hi!

If the HOSTNAME is not in your enviromnment this is not correct .

Set your HOSTNAME in the environment

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

Afterwards all works as it should

Winni

rvk

  • Hero Member
  • *****
  • Posts: 4281
Re: Need to get username and computername for logging, win and unix
« Reply #5 on: July 11, 2020, 07:11:51 pm »
It seems like HOSTNAME is a shell variable and not an environment variable.

https://stackoverflow.com/a/6353115/1037511
https://stackoverflow.com/a/56654639/1037511

You could do export HOSTNAME but otherwise you should follow the instructions of winni.

Warfley

  • Sr. Member
  • ****
  • Posts: 302
Re: Need to get username and computername for logging, win and unix
« Reply #6 on: July 11, 2020, 07:27:52 pm »
Pretty simple, don't rely on environment variables, because these are completely dependent on the parent process and how the current process was started.

For Linux the correct way to get the hostname is to read the pseudo file located at /proc/sys/kernel/hostname
Example using cat:
Code: Pascal  [Select][+][-]
  1. cat /proc/sys/kernel/hostname

bylaardt

  • Sr. Member
  • ****
  • Posts: 308
Re: Need to get username and computername for logging, win and unix
« Reply #7 on: July 11, 2020, 07:29:26 pm »
other wy is read /etc/hostname
in console:
Code: Bash  [Select][+][-]
  1. cat /etc/hostname

winni

  • Hero Member
  • *****
  • Posts: 1774
Re: Need to get username and computername for logging, win and unix
« Reply #8 on: July 11, 2020, 07:33:04 pm »
Hi!

Nobody knows everything:

The correct way to get the hostname in Linux is

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

A good Linux has the hostname inside the environment.

It seems to be (again) an absurdity of Debian that it is not contained.

Suse and a lot of others do.

Winni



Warfley

  • Sr. Member
  • ****
  • Posts: 302
Re: Need to get username and computername for logging, win and unix
« Reply #9 on: July 11, 2020, 07:56:49 pm »
Nobody knows everything:

The correct way to get the hostname in Linux is

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

No it's not hostname is an executable that is part of the gnu inet-utils (or BSD inet-tools), this must be installed on your distro. Sure most linux distros ship it automatically, but there is no guarantee that it will be shipped. Case in point: Archlinux.

There are two correct ways first one is to use the kernel provided pseudo files like /proc/sys/kernel/hostname, which works fine under linux but might not under BSD or other Unix systems like MacOS (cant test it right now).
The other way is to use the function gethostname defined in the posix standard. This is guaranteed to work on any posix compatible system, including but not limited to BSD, Linux and MacOS

Besides that hostname might not be available, starting a process takes orders of magnitude longer than a normal system call or reading a pseudo file.
« Last Edit: July 11, 2020, 07:58:36 pm by Warfley »

winni

  • Hero Member
  • *****
  • Posts: 1774
Re: Need to get username and computername for logging, win and unix
« Reply #10 on: July 11, 2020, 07:58:34 pm »
Again and again:

The blind man talks about the color.

Winni

Warfley

  • Sr. Member
  • ****
  • Posts: 302
Re: Need to get username and computername for logging, win and unix
« Reply #11 on: July 11, 2020, 08:01:18 pm »
Again and again:

The blind man talks about the color.

Winni
Why? because I'm telling you that the hostname executable is not a reliable way to get the hostname?

Case in point:
Code: Bash  [Select][+][-]
  1. ➜  ~ docker run -it archlinux    
  2. [root@da845c2d18c9 /]# hostname
  3. bash: hostname: command not found
  4. [root@da845c2d18c9 /]# cat /proc/sys/kernel/hostname
  5. da845c2d18c9
  6.  
« Last Edit: July 11, 2020, 08:03:51 pm by Warfley »

rvk

  • Hero Member
  • *****
  • Posts: 4281
Re: Need to get username and computername for logging, win and unix
« Reply #12 on: July 11, 2020, 08:25:07 pm »
So... Use the GetHostName function from the linux unit.

http://porthos.ist.utl.pt/docs/fpc/units/node288.html

Warfley

  • Sr. Member
  • ****
  • Posts: 302
Re: Need to get username and computername for logging, win and unix
« Reply #13 on: July 11, 2020, 08:36:37 pm »
This function is also in the Unix unit, anyone knows if theres a difference?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8720
  • FPC developer.
Re: Need to get username and computername for logging, win and unix
« Reply #14 on: July 11, 2020, 10:04:58 pm »
Afaik fpuname() based detections (like gethostname) only echo static local name.

If the hostname is overriden by some daemon (yellow pages or whatever), then it doesn't reflect that.

Note that is just what I remember from what I read over the years, I don't have real experience with such setups.

 

TinyPortal © 2005-2018