Skip to content
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

Rewrite get_process_username() in C for Windows #114

Closed
giampaolo opened this issue May 23, 2014 · 7 comments
Closed

Rewrite get_process_username() in C for Windows #114

giampaolo opened this issue May 23, 2014 · 7 comments

Comments

@giampaolo
Copy link
Owner

From g.rodola on October 10, 2010 22:53:04

Here's an extract of the original wj32 patch which implements process 
username retrieval in C, replacing the old implementation using WMI.
This would address the following issues:

- pywin32 dependancy no longer necessary
- removal of psutil/wmi.py script from the code base
- an order of magnitude faster (WMI was deadly slow)

The patch as-is apparently works:

>>> import psutil, os
>>> psutil.Process(os.getpid()).username
'win-7\\giampaolo'
>>>

...but when attempt to retrieve the username of all processes it 
crashes after a while:

>>> import psutil, os
>>> for p in psutil.process_iter():
...     print p.pid, p.name, p.username
...
0 System Idle Process NT AUTHORITY\SYSTEM
4 System NT AUTHORITY\SYSTEM
220 smss.exe NT AUTHORITY\SYSTEM
316 csrss.exe NT AUTHORITY\SYSTEM
364 wininit.exe NT AUTHORITY\SYSTEM
388 csrss.exe NT AUTHORITY\SYSTEM
412 services.exe NT AUTHORITY\SYSTEM
432 lsass.exe NT AUTHORITY\SYSTEM
440 lsm.exe NT AUTHORITY\SYSTEM
560 winlogon.exe NT AUTHORITY\SYSTEM
588 svchost.exe NT AUTHORITY\SYSTEM
648 nvvsvc.exe NT AUTHORITY\SYSTEM
692 svchost.exe NT AUTHORITY\NETWORK SERVICE
804 svchost.exe NT AUTHORITY\LOCAL SERVICE
836 svchost.exe NT AUTHORITY\SYSTEM
876 svchost.exe NT AUTHORITY\SYSTEM
936 audiodg.exe

No error message of any kind is returned. A window is just prompted 
informing that "python has stopped working".
Tried on Windows 7 64 bit, python 2.7 32 bit.

Attachment: username.patch original-wj32.patch

Original issue: http://code.google.com/p/psutil/issues/detail?id=114

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From g.rodola on October 10, 2010 15:20:53

I've tried to debug this a little and it seems the problem occurs 
when building the username string:

    /* Build the full username string. */
    fullName = malloc(( + 1 + nameSize + 1) * sizeof(TCHAR));
    memcpy(fullName, domainName, domainNameSize);
    fullName[domainNameSize] = '\\';
    memcpy(&fullName[domainNameSize + 1], name, nameSize);
    fullName[domainNameSize + 1 + nameSize] = '\0';

This change seems to fix the issue:

- fullName = malloc(( + 1 + nameSize + 1) * sizeof(TCHAR));
+ fullName = malloc(( + 1 + nameSize + 1) * sizeof(PSTR));

@giampaolo
Copy link
Owner Author

From g.rodola on October 10, 2010 16:08:37

Committed in r669 .
I've also added a credits file to mention wj32 cotribution: 
http://psutil.googlecode.com/svn-history/r669/trunk/CREDITS I'm not 
sure if he's gonna read this but me and Jay really appreciated the 
work he has done. Thanks a lot.

Status: Fixed
Labels: -Progress-0in4 Milestone-0.2.0

@giampaolo
Copy link
Owner Author

From wj32...@gmail.com on October 10, 2010 22:40:44

Sorry, the patch I submitted has a memory leak. In the second "if 
(!GetTokenInformation(..." there needs to be a "free(user);".

Also, the crash occurs because space isn't reserved for the domain 
name. Somehow I left it out - that line needs to be:

fullName = malloc((domainNameSize + 1 + nameSize + 1) * sizeof(TCHAR));

@giampaolo
Copy link
Owner Author

From g.rodola on October 11, 2010 00:53:26

wj32, would you be interested in having commit access and/or become a project member?
Me and Jay are more focused on UNIX development and a Windows expert 
would be great to have.

@giampaolo
Copy link
Owner Author

From wj32...@gmail.com on October 11, 2010 01:00:43

No, I have my own project (Process Hacker) to run ;) Anyway, I don't 
know enough Python to be of much use to you guys. I would however be 
glad to help out with anything that needs adding/fixing.

@giampaolo
Copy link
Owner Author

From g.rodola on October 11, 2010 03:17:47

Ok, I'm going to CC you in new tickets involving Windows development then.
Thanks again for your help.

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:55:01

Updated csets after the SVN -> Mercurial migration: r669 == revision c6c27fae9599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant