@@ -1487,7 +1487,6 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
1487
1487
zend_long pid ;
1488
1488
bool pid_is_null = 1 ;
1489
1489
cpu_set_t mask ;
1490
- zend_ulong i , maxcpus ;
1491
1490
1492
1491
ZEND_PARSE_PARAMETERS_START (0 , 1 )
1493
1492
Z_PARAM_OPTIONAL
@@ -1515,10 +1514,10 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
1515
1514
RETURN_EMPTY_ARRAY ();
1516
1515
}
1517
1516
1518
- maxcpus = (zend_ulong )sysconf (_SC_NPROCESSORS_ONLN );
1517
+ zend_ulong maxcpus = (zend_ulong )sysconf (_SC_NPROCESSORS_ONLN );
1519
1518
array_init (return_value );
1520
1519
1521
- for (i = 0 ; i < maxcpus ; i ++ ) {
1520
+ for (zend_ulong i = 0 ; i < maxcpus ; i ++ ) {
1522
1521
if (CPU_ISSET (i , & mask )) {
1523
1522
add_next_index_long (return_value , i );
1524
1523
}
@@ -1531,36 +1530,46 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
1531
1530
bool pid_is_null = 1 ;
1532
1531
cpu_set_t mask ;
1533
1532
zval * hmask = NULL , * cpu ;
1534
- zend_ulong maxcpus ;
1535
1533
1536
1534
ZEND_PARSE_PARAMETERS_START (0 , 2 )
1537
1535
Z_PARAM_OPTIONAL
1538
1536
Z_PARAM_LONG_OR_NULL (pid , pid_is_null )
1539
1537
Z_PARAM_ARRAY (hmask )
1540
1538
ZEND_PARSE_PARAMETERS_END ();
1541
1539
1542
- if (!hmask || Z_ARRVAL_P (hmask )-> nNumOfElements == 0 ) {
1540
+ if (!hmask || zend_hash_num_elements ( Z_ARRVAL_P (hmask )) == 0 ) {
1543
1541
zend_argument_value_error (2 , "must not be empty" );
1544
1542
RETURN_THROWS ();
1545
1543
}
1546
1544
1547
1545
// 0 == getpid in this context, we re just saving a syscall
1548
1546
pid = pid_is_null ? 0 : pid ;
1549
- maxcpus = sysconf (_SC_NPROCESSORS_ONLN );
1547
+ zend_ulong maxcpus = ( zend_ulong ) sysconf (_SC_NPROCESSORS_ONLN );
1550
1548
CPU_ZERO (& mask );
1551
1549
1552
1550
ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (hmask ), cpu ) {
1553
- if (Z_TYPE_P (cpu ) == IS_LONG && Z_LVAL_P (cpu ) >= 0 &&
1554
- Z_LVAL_P (cpu ) < maxcpus && !CPU_ISSET (Z_LVAL_P (cpu ), & mask )) {
1551
+ if (Z_TYPE_P (cpu ) != IS_LONG ) {
1552
+ if (Z_TYPE_P (cpu ) == IS_STRING ) {
1553
+ convert_to_long (cpu );
1554
+ } else {
1555
+ convert_to_string (cpu );
1556
+ zend_value_error ("cpu id invalid type (%s)" , Z_STRVAL_P (cpu ));
1557
+ RETURN_THROWS ();
1558
+ }
1559
+ }
1560
+
1561
+ ZVAL_DEREF (cpu );
1562
+
1563
+ if (Z_LVAL_P (cpu ) < 0 || Z_LVAL_P (cpu ) >= maxcpus ) {
1564
+ zend_value_error ("cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")" , maxcpus , Z_LVAL_P (cpu ));
1565
+ RETURN_THROWS ();
1566
+ }
1567
+
1568
+ if (!CPU_ISSET (Z_LVAL_P (cpu ), & mask )) {
1555
1569
CPU_SET (Z_LVAL_P (cpu ), & mask );
1556
1570
}
1557
1571
} ZEND_HASH_FOREACH_END ();
1558
1572
1559
- if (!CPU_COUNT (& mask )) {
1560
- zend_argument_value_error (2 , "invalid cpu affinity mapping" );
1561
- RETURN_THROWS ();
1562
- }
1563
-
1564
1573
if (sched_setaffinity (pid , sizeof (mask ), & mask ) != 0 ) {
1565
1574
PCNTL_G (last_error ) = errno ;
1566
1575
switch (errno ) {
@@ -1570,6 +1579,8 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
1570
1579
case EPERM :
1571
1580
php_error_docref (NULL , E_WARNING , "Calling process not having the proper privileges" );
1572
1581
break ;
1582
+ default :
1583
+ php_error_docref (NULL , E_WARNING , "Error %d" , errno );
1573
1584
}
1574
1585
RETURN_FALSE ;
1575
1586
} else {
0 commit comments