Skip to content

Commit 1ca1c41

Browse files
committed
Auto merge of #2914 - SteveLauC:Wrong-getpwent_r-definition-on-solarish-os, r=JohnTitor
fix wrong definitions of getpwent_r and getgrent_r on solarish os Closes #2908 * [man page for `getpwent_r`](https://illumos.org/man/3C/getpwnam) * [man page for `getgrent_r`](https://illumos.org/man/3C/getgrnam) You may find the definitions for `getpwnam_r/getpwuid_r/getgrnam_r/getgruid_r` exposed by `libc` are also wrong: ```c struct passwd *getpwnam_r(const char *name, struct passwd *pwd, char *buffer, int buflen); ``` ```rust pub fn getpwnam_r( name: *const ::c_char, pwd: *mut passwd, buf: *mut ::c_cha buflen: ::size_t, result: *mut *mut passwd, ) -> ::c_int; ``` But actually they are **correct** as there are the POSIX-conforming definitions (see `Standard conforming` section of above man pages): ``` Standard conforming cc [ flag...] file... -D_POSIX_PTHREAD_SEMANTICS [ library... ] int getpwnam_r(const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result); int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result); ``` `getpwent_r/getgrent_r` don't get lucky, they do not have the POSIX-conforming alternatives. To double check this, I searched its [source code](https://github.com/illumos/illumos-gate/blob/master/usr/src/lib/libc/port/gen/getpwnam_r.c): ```shell $ rg "__posix_getpwnam_r" port/mapfile-vers 1582: __posix_getpwnam_r; port/gen/getpwnam_r.c 152:__posix_getpwnam_r(const char *name, struct passwd *pwd, char *buffer, $ rg "__posix_getpwent_r" $ ```
2 parents 875f3a6 + 8a914ca commit 1ca1c41

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/unix/solarish/compat.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Common functions that are unfortunately missing on illumos and
22
// Solaris, but often needed by other crates.
33

4+
use core::cmp::min;
45
use unix::solarish::*;
56

67
const PTEM: &[u8] = b"ptem\0";
@@ -169,3 +170,51 @@ pub unsafe fn forkpty(
169170

170171
0
171172
}
173+
174+
pub unsafe fn getpwent_r(
175+
pwd: *mut passwd,
176+
buf: *mut ::c_char,
177+
buflen: ::size_t,
178+
result: *mut *mut passwd,
179+
) -> ::c_int {
180+
let old_errno = *::___errno();
181+
*::___errno() = 0;
182+
*result = native_getpwent_r(
183+
pwd,
184+
buf,
185+
min(buflen, ::c_int::max_value() as ::size_t) as ::c_int,
186+
);
187+
188+
let ret = if (*result).is_null() {
189+
*::___errno()
190+
} else {
191+
0
192+
};
193+
*::___errno() = old_errno;
194+
195+
ret
196+
}
197+
198+
pub unsafe fn getgrent_r(
199+
grp: *mut ::group,
200+
buf: *mut ::c_char,
201+
buflen: ::size_t,
202+
result: *mut *mut ::group,
203+
) -> ::c_int {
204+
let old_errno = *::___errno();
205+
*::___errno() = 0;
206+
*result = native_getgrent_r(
207+
grp,
208+
buf,
209+
min(buflen, ::c_int::max_value() as ::size_t) as ::c_int,
210+
);
211+
212+
let ret = if (*result).is_null() {
213+
*::___errno()
214+
} else {
215+
0
216+
};
217+
*::___errno() = old_errno;
218+
219+
ret
220+
}

src/unix/solarish/mod.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,24 +3016,14 @@ extern "C" {
30163016
) -> ::c_int;
30173017
#[cfg_attr(
30183018
any(target_os = "solaris", target_os = "illumos"),
3019-
link_name = "__posix_getpwent_r"
3019+
link_name = "getpwent_r"
30203020
)]
3021-
pub fn getpwent_r(
3022-
pwd: *mut passwd,
3023-
buf: *mut ::c_char,
3024-
buflen: ::size_t,
3025-
result: *mut *mut passwd,
3026-
) -> ::c_int;
3021+
fn native_getpwent_r(pwd: *mut passwd, buf: *mut ::c_char, buflen: ::c_int) -> *mut passwd;
30273022
#[cfg_attr(
30283023
any(target_os = "solaris", target_os = "illumos"),
3029-
link_name = "__posix_getgrent_r"
3024+
link_name = "getgrent_r"
30303025
)]
3031-
pub fn getgrent_r(
3032-
grp: *mut ::group,
3033-
buf: *mut ::c_char,
3034-
buflen: ::size_t,
3035-
result: *mut *mut ::group,
3036-
) -> ::c_int;
3026+
fn native_getgrent_r(grp: *mut ::group, buf: *mut ::c_char, buflen: ::c_int) -> *mut ::group;
30373027
#[cfg_attr(
30383028
any(target_os = "solaris", target_os = "illumos"),
30393029
link_name = "__posix_sigwait"

0 commit comments

Comments
 (0)