@@ -1487,14 +1487,13 @@ 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
1494
1493
Z_PARAM_LONG_OR_NULL (pid , pid_is_null )
1495
1494
ZEND_PARSE_PARAMETERS_END ();
1496
1495
1497
- // 0 == getpid in this context, we re just saving a syscall
1496
+ // 0 == getpid in this context, we' re just saving a syscall
1498
1497
pid = pid_is_null ? 0 : pid ;
1499
1498
1500
1499
CPU_ZERO (& mask );
@@ -1512,13 +1511,13 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
1512
1511
php_error_docref (NULL , E_WARNING , "Error %d" , errno );
1513
1512
}
1514
1513
1515
- RETURN_EMPTY_ARRAY () ;
1514
+ RETURN_FALSE ;
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
}
@@ -1530,36 +1529,48 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
1530
1529
zend_long pid ;
1531
1530
bool pid_is_null = 1 ;
1532
1531
cpu_set_t mask ;
1533
- zval * hmask = NULL , * cpu ;
1534
- zend_ulong maxcpus ;
1532
+ zval * hmask = NULL , * ncpu ;
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
- // 0 == getpid in this context, we re just saving a syscall
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
- 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 )) {
1555
- CPU_SET (Z_LVAL_P (cpu ), & mask );
1550
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (hmask ), ncpu ) {
1551
+ ZVAL_DEREF (ncpu );
1552
+ zend_long cpu ;
1553
+ if (Z_TYPE_P (ncpu ) != IS_LONG ) {
1554
+ if (Z_TYPE_P (ncpu ) == IS_STRING ) {
1555
+ cpu = zval_get_long (ncpu );
1556
+ } else {
1557
+ zend_string * wcpu = zval_get_string_func (ncpu );
1558
+ zend_value_error ("cpu id invalid type (%s)" , ZSTR_VAL (wcpu ));
1559
+ RETURN_THROWS ();
1560
+ }
1561
+ } else {
1562
+ cpu = Z_LVAL_P (ncpu );
1556
1563
}
1557
- } ZEND_HASH_FOREACH_END ();
1558
1564
1559
- if (!CPU_COUNT (& mask )) {
1560
- zend_argument_value_error (2 , "invalid cpu affinity mapping" );
1561
- RETURN_THROWS ();
1562
- }
1565
+ if (cpu < 0 || cpu >= maxcpus ) {
1566
+ zend_value_error ("cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")" , maxcpus , cpu );
1567
+ RETURN_THROWS ();
1568
+ }
1569
+
1570
+ if (!CPU_ISSET (cpu , & mask )) {
1571
+ CPU_SET (cpu , & mask );
1572
+ }
1573
+ } ZEND_HASH_FOREACH_END ();
1563
1574
1564
1575
if (sched_setaffinity (pid , sizeof (mask ), & mask ) != 0 ) {
1565
1576
PCNTL_G (last_error ) = errno ;
@@ -1570,6 +1581,8 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
1570
1581
case EPERM :
1571
1582
php_error_docref (NULL , E_WARNING , "Calling process not having the proper privileges" );
1572
1583
break ;
1584
+ default :
1585
+ php_error_docref (NULL , E_WARNING , "Error %d" , errno );
1573
1586
}
1574
1587
RETURN_FALSE ;
1575
1588
} else {
0 commit comments