Skip to content

Commit 866fbc2

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

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
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: 12 additions & 5 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,26 +1544,33 @@ 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+
zend_ulong tmp;
1556+
if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
1557+
zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
1558+
RETURN_THROWS();
1559+
}
1560+
1561+
cpu = (zend_long)tmp;
15561562
} else {
15571563
zend_string *wcpu = zval_get_string_func(ncpu);
1558-
zend_value_error("cpu id invalid type (%s)", ZSTR_VAL(wcpu));
1564+
zend_argument_value_error(2, "cpu id invalid type (%s)", ZSTR_VAL(wcpu));
1565+
zend_string_release(wcpu);
15591566
RETURN_THROWS();
15601567
}
15611568
} else {
15621569
cpu = Z_LVAL_P(ncpu);
15631570
}
15641571

15651572
if (cpu < 0 || cpu >= maxcpus) {
1566-
zend_value_error("cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu);
1573+
zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu);
15671574
RETURN_THROWS();
15681575
}
15691576

ext/pcntl/tests/pcntl_cpuaffinity.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ array(0) {
6060
}
6161
bool(true)
6262
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) must not be empty
63-
cpu id must be between 0 and %d (%d)
64-
cpu id must be between 0 and %d (-1024)
63+
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id invalid value (def)
64+
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id must be between 0 and %d (%d)
65+
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id must be between 0 and %d (-1024)
6566
pcntl_getcpuaffinity(): Argument #1 ($process_id) invalid process (-1024)
6667

6768
Warning: Array to string conversion in %s on line %d
68-
cpu id invalid type (Array)
69+
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id invalid type (Array)

0 commit comments

Comments
 (0)