nix
nix copied to clipboard
expose getpwent and getgrent
This PR is for issue #1811.
On OSes where getpwent_r/getgrent_r is available, it is used. On other OSes, use getpwent/getgrent. However, illumos and solaris, they do have getpwent_r/getgrent_r, but the definitions exposed by libc are wrong, so I currently use getpwent/getgrent on them.
The implementations of AllUsers::next and AllGroups::next on platforms where getpwent_r/getgrent_r is available are kind of ugly, this is because the return values of these functions are so different:
-
freebsd: getpwent_r man page
- On success: return 0,
resultwill be non-NULL - On error: return the error number,
resultwill be NULL - End: return 0,
resultwill be NULL.
- On success: return 0,
-
dragonfly: getpwent_r man page
- On success: return 0 and
resultwill be non-NULL - On error: return the error number,
resultwill be NULL - End: return 0,
resultwill be NULL
- On success: return 0 and
The return values on freebsd and dragonfly are the same.
-
netbsd: getpwent_r man page
- On success: return 0,
resultwill be non-NULL, - On error: return a non-zero value (errno is set),
resultwill be NULL - End: return 0,
resultwill be NULL
- On success: return 0,
-
Linux: getpwent_r man page
- On success: return 0,
resultwill be non-NULL - On error: return the error number (not ENOENT),
resultwill be NULL - End: return ENOENT and
resultwill be NULL.
- On success: return 0,
Another thing I am not sure about is the return value of AllUsers::next()/AllGroups::next(). In my implementation, it is:
fn next(&mut self) -> Option<Result<User/Group, Errno>>
The inner Result is used to expose the errno value, the drawback of such a design is that the user has to stop iterating when an error occurs.
Haven't added the changes to CHANGELOG.md.
Will add it, maybe after #1817 is merged, or there will be a merge conflict.
The CI for fuchsia failed because setgrent/getgrent/endgrent are currently not exposed in libc.
PR filed.
This
setpwentbusiness sucks. Very un-Rusty. I wonder if it would better just to use the racelessfgetpwent_rfunction instead. I think that's only available on GNU/Linux, but at least it's thread-safe.
Yes, this API sucks. What are we going to do about this pr? Change the implementation for GNU/Linux to fgetpwent_r, what about the other platforms?