Skip to content

All FreeBSD do not implement the function sysconf with arguments #13187

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions ext/posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,11 @@ PHP_FUNCTION(posix_ttyname)
#if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX)
buflen = sysconf(_SC_TTY_NAME_MAX);
if (buflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
Copy link
Member

Choose a reason for hiding this comment

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

what makes you think _SC_PAGESIZE might not be defined ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't recollect the exact reason at the moment but so far from my Goldfish memory I think it was that either Zend or zts redefines the page size with it's real size and checks in this similar fashion. Or it might have been too old when SC_PAGESIZE was not defined in FreeBSD at all. Because at one point there was only _SC_PAGE_SIZE rather then _SC_PAGESIZE

Copy link
Member

Choose a reason for hiding this comment

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

I looked even at freebsd 5.0, seems it was already there.

buflen = sysconf(_SC_PAGESIZE);
#else
RETURN_FALSE;
#endif
}
p = emalloc(buflen);

Expand Down Expand Up @@ -784,7 +788,11 @@ PHP_FUNCTION(posix_getgrnam)
#if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
if (buflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
buflen = sysconf(_SC_PAGESIZE);
#else
RETURN_FALSE;
#endif
Comment on lines 789 to +795
Copy link
Member

Choose a reason for hiding this comment

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

So I have already asked about this and probably not in a clear way. So my main question was don't you just do this:

#if defined(__FreeBSD__)
	buflen = sysconf(_SC_PAGESIZE);
#else
	buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
#endif
	if (buflen < 1) {	
		RETURN_FALSE;

The only reason that I could see for not doing this is that you somehow expect that this is going to be supported on FreeBSD in the future. Is that the case? If so, I'm not sure we should be predicting what might happen (especially if it's more likely not going to happen). But if you really think that it's quite likely, then I'm fine with it.

In such case you should still check the return value of of the second sysconf call even though it is unlikely to fail. So something like this should be done:

	buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
	if (buflen < 1) {
#if defined(__FreeBSD__)
		buflen = sysconf(_SC_PAGESIZE);
		if (buflen < 1) {
			RETURN_FALSE;
		}
#else
		RETURN_FALSE;
#endif

}
buf = emalloc(buflen);
try_again:
Expand Down Expand Up @@ -840,7 +848,11 @@ PHP_FUNCTION(posix_getgrgid)

grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
if (grbuflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
grbuflen = sysconf(_SC_PAGESIZE);
#else
RETURN_FALSE;
#endif
}

grbuf = emalloc(grbuflen);
Expand Down Expand Up @@ -914,7 +926,11 @@ PHP_FUNCTION(posix_getpwnam)
#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
if (buflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
buflen = sysconf(_SC_PAGESIZE);
#else
RETURN_FALSE;
#endif
}
buf = emalloc(buflen);
pw = &pwbuf;
Expand Down Expand Up @@ -969,7 +985,11 @@ PHP_FUNCTION(posix_getpwuid)
#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
if (pwbuflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
pwbuflen = sysconf(_SC_PAGESIZE);
#else
RETURN_FALSE;
#endif
}
pwbuf = emalloc(pwbuflen);

Expand Down
8 changes: 8 additions & 0 deletions ext/standard/filestat.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,11 @@ PHPAPI zend_result php_get_gid_by_name(const char *name, gid_t *gid)
char *grbuf;

if (grbuflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
grbuflen = sysconf(_SC_PAGESIZE);
#else
return FAILURE;
#endif
}

grbuf = emalloc(grbuflen);
Expand Down Expand Up @@ -407,7 +411,11 @@ PHPAPI zend_result php_get_uid_by_name(const char *name, uid_t *uid)
char *pwbuf;

if (pwbuflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
pwbuflen = sysconf(_SC_PAGESIZE);
#else
return FAILURE;
#endif
}

pwbuf = emalloc(pwbuflen);
Expand Down
4 changes: 4 additions & 0 deletions main/fopen_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,11 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
char *pwbuf;

if (pwbuflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
pwbuflen = sysconf(_SC_PAGESIZE);
#else
return FAILURE;
#endif
}

pwbuf = emalloc(pwbuflen);
Expand Down
4 changes: 4 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,11 @@ PHPAPI char *php_get_current_user(void)
char *pwbuf;

if (pwbuflen < 1) {
#if defined(__FreeBSD__) && defined(_SC_PAGESIZE)
pwbuflen = sysconf(_SC_PAGESIZE);
#else
return "";
#endif
}
pwbuf = emalloc(pwbuflen);
if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
Expand Down