nix icon indicating copy to clipboard operation
nix copied to clipboard

expose getpwent and getgrent

Open SteveLauC opened this issue 3 years ago • 5 comments
trafficstars

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

    1. On success: return 0, result will be non-NULL
    2. On error: return the error number, result will be NULL
    3. End: return 0, result will be NULL.
  • dragonfly: getpwent_r man page

    1. On success: return 0 and result will be non-NULL
    2. On error: return the error number, result will be NULL
    3. End: return 0, result will be NULL

The return values on freebsd and dragonfly are the same.

  • netbsd: getpwent_r man page

    1. On success: return 0, result will be non-NULL,
    2. On error: return a non-zero value (errno is set), result will be NULL
    3. End: return 0, result will be NULL
  • Linux: getpwent_r man page

    1. On success: return 0, result will be non-NULL
    2. On error: return the error number (not ENOENT), result will be NULL
    3. End: return ENOENT and result will be NULL.

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.

SteveLauC avatar Sep 16 '22 08:09 SteveLauC

Haven't added the changes to CHANGELOG.md.

Will add it, maybe after #1817 is merged, or there will be a merge conflict.

SteveLauC avatar Sep 16 '22 08:09 SteveLauC

The CI for fuchsia failed because setgrent/getgrent/endgrent are currently not exposed in libc.

PR filed.

SteveLauC avatar Sep 16 '22 09:09 SteveLauC

This setpwent business sucks. Very un-Rusty. I wonder if it would better just to use the raceless fgetpwent_r function 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?

SteveLauC avatar Sep 18 '22 01:09 SteveLauC