-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[Linux] psutil.users() always produces "host=localhost", not expected IP #2470
Comments
I think I'm misinterpreting what your saying here but here goes. Are you saying that you expect >>> socket.gethostbyname("localhost")
'127.0.0.1' |
Thanks @giampaolo for your response. Yes, I'm afraid we're talking about different things here. My issue is not with the name resolution, nor regarding localhost/127.0.0.1. The described problem here is that with I think, the intention of the referenced code line (for Linux X Window System check) is: But, IMHO, for this to work as intended you would need (because of the lexicographical comparison done by
A small code example: // g++ ut_strcmp.cc -o ut_strcmp
// ./ut_strcmp :0
// ./ut_strcmp foobar
#include <iostream>
#include <cstring>
int main(int argn, char** args) {
if (argn < 2) {
std::cerr << "... <arg>" << std::endl;
return 1;
}
const char* host = args[1];
std::cout << "strcmp(host, :0): " << strcmp(host, ":0") << std::endl;
std::cout << "strcmp(host, :0.0): " << strcmp(host, ":0:0") << std::endl;
std::cout << "strcmp(..) || strcmp(...): " << (strcmp(host, ":0") || strcmp(host, ":0:0")) << std::endl;
// incorrect
if (strcmp(host, ":0") || strcmp(host, ":0.0"))
std::cout << "(with no check for == 0, host this is always) " << "localhost" << std::endl;
else
// never happens
std::cout << "this never happens :(" << host << std::endl;
// correct
if (strcmp(host, ":0") == 0 || strcmp(host, ":0.0") == 0)
// host is a standard X Window identifier
std::cout << "(this is correct) " << "localhost" << std::endl;
else
std::cout << host << std::endl;
return 0;
} $ g++ ut_strcmp.cc -o ut_strcmp && ./ut_strcmp :0
strcmp(host, :0): 0
strcmp(host, :0.0): -58
strcmp(..) || strcmp(...): 1
with no check for == 0, host this is always: localhost
host: localhost
$ g++ ut_strcmp.cc -o ut_strcmp && ./ut_strcmp 192.168.56.1
strcmp(host, :0): 44
strcmp(host, :0.0): 44
strcmp(..) || strcmp(...): 1
with no check for == 0, host this is always: localhost
host: 192.168.56.1 |
Oh I get it now. And now that I re-read your first message it was clear already, but my brain malfunctioned. :) |
@giampaolo thanks a lot! I just wanted to start the PR... ;-) However, I can confirm that it now works (again) as expected. I tested it with a VM with 1 login on console (tty7) and 1 remote SSH login (pts/1). Now, fixed, correct (687695c):
=> For comparison, before, incorrect, with bug (3d12e67):
|
Summary
Description
For
python3 -c "import psutil; print(psutil.users())"
it gives always "host=localhost":suser(name='user', terminal='pts/1', host='localhost', started=..., pid=13535)
But, the expected is an IP (as long as getutent() doesn't produce ":0"/":0.0"), e.g.:
suser(name='user', terminal='pts/1', host='192.168.56.1', started=..., pid=13535)
With psutil version 5.9.0 I get the correct
host='192.168.56.1'
.With 6.1.0 it's always "localhost".
Is it possible that the
strcmp
could be the problem?IS:
psutil/psutil/arch/linux/users.c
Line 36 in fb68f9f
if (strcmp(ut->ut_host, ":0") || strcmp(ut->ut_host, ":0.0"))
SHOULD:
if (strcmp(ut->ut_host, ":0") == 0 || strcmp(ut->ut_host, ":0.0") == 0)
I would expect the IF to check whether host is ":0" or ":0.0".
The text was updated successfully, but these errors were encountered: