Skip to content

Commit 6cd94c1

Browse files
committed
using _SC_NPROCESSORS_CONF instead in case of cpu put offline.
addressing other remarks.
1 parent d1bce65 commit 6cd94c1

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

ext/pcntl/config.m4

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ if test "$PHP_PCNTL" != "no"; then
77
AC_CHECK_FUNCS([fork], [], [AC_MSG_ERROR([pcntl: fork() not supported by this platform])])
88
AC_CHECK_FUNCS([waitpid], [], [AC_MSG_ERROR([pcntl: waitpid() not supported by this platform])])
99
AC_CHECK_FUNCS([sigaction], [], [AC_MSG_ERROR([pcntl: sigaction() not supported by this platform])])
10-
AC_CHECK_FUNCS([
11-
getpriority
12-
setpriority
13-
wait3
14-
wait4
15-
sigwaitinfo
16-
sigtimedwait
17-
unshare
18-
rfork
19-
forkx
20-
pidfd_open
21-
sched_setaffinity])
10+
AC_CHECK_FUNCS([getpriority setpriority wait3 wait4 sigwaitinfo sigtimedwait unshare rfork forkx pidfd_open sched_setaffinity])
2211

2312
AC_CHECK_TYPE([siginfo_t],[PCNTL_CFLAGS="-DHAVE_STRUCT_SIGINFO_T"],,[#include <signal.h>])
2413

ext/pcntl/pcntl.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
15141514
RETURN_FALSE;
15151515
}
15161516

1517-
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_ONLN);
1517+
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
15181518
array_init(return_value);
15191519

15201520
for (zend_ulong i = 0; i < maxcpus; i ++) {
@@ -1544,18 +1544,25 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15441544

15451545
// 0 == getpid in this context, we're just saving a syscall
15461546
pid = pid_is_null ? 0 : pid;
1547-
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_ONLN);
1547+
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
15481548
CPU_ZERO(&mask);
15491549

15501550
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
15511551
ZVAL_DEREF(ncpu);
15521552
zend_long cpu;
15531553
if (Z_TYPE_P(ncpu) != IS_LONG) {
15541554
if (Z_TYPE_P(ncpu) == IS_STRING) {
1555-
cpu = zval_get_long(ncpu);
1555+
char *str = Z_STRVAL_P(ncpu);
1556+
if (EXPECTED(strlen(str) && isdigit(str[0]))) {
1557+
cpu = zval_get_long(ncpu);
1558+
} else {
1559+
zend_value_error("cpu id invalid type (%s)", str);
1560+
RETURN_THROWS();
1561+
}
15561562
} else {
15571563
zend_string *wcpu = zval_get_string_func(ncpu);
15581564
zend_value_error("cpu id invalid type (%s)", ZSTR_VAL(wcpu));
1565+
zend_string_release(wcpu);
15591566
RETURN_THROWS();
15601567
}
15611568
} else {

ext/pcntl/tests/pcntl_cpuaffinity.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ array(0) {
6060
}
6161
bool(true)
6262
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) must not be empty
63+
cpu id invalid type (def)
6364
cpu id must be between 0 and %d (%d)
6465
cpu id must be between 0 and %d (-1024)
6566
pcntl_getcpuaffinity(): Argument #1 ($process_id) invalid process (-1024)

0 commit comments

Comments
 (0)