Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/api/python/hidra/utils/utils_netgroup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

from ctypes import *
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use '*' imports in libraries. This has multiple disadvantages.


def get_hostname(netgroup):

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a doctring that describes what the function does, what are the inputs and outputs and any special or surprising behavior one needs to know to use it. Best would be to also add an example.

libc = CDLL("libc.so.6")
if (libc != None):
pass
else:
libc = CDLL("libc.so.7")
Comment on lines +6 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a better way that going through a fixed list of library names? Why are "libc.so.5" or libc.so.8 not included in that list?

Also it is probably better to not do the lookup at every function call but only once at module load time.



hosts = None
if libc.setnetgrent(netgroup):
Copy link
Contributor

Choose a reason for hiding this comment

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

One needs to check that netgroup is of the correct type, otherwise this might cause a segfault.

# returns 1 if successful and 0 otherwise
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment should be above or next to the line it refers to.

host = c_char_p()
user = c_char_p()
domain = c_char_p()

hosts = []
while libc.getnetgrent(byref(host), byref(user), byref(domain)):
if host:
# check is host is not a NULL pointer
hosts.append(host.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always safe? What about interleaving calls from multiple threads?
Should this return str instead of bytes?


return(hosts)
Copy link
Contributor

Choose a reason for hiding this comment

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

The difference between None and [] might be too subtle. Maybe throw an error instead of returning None?



hosts = get_hostname(b"a3p62-hosts")
print(hosts)
print("done")