diff --git a/tools/nut-scanner/nut-scanner.c b/tools/nut-scanner/nut-scanner.c index 2288f5cf1e..05e743c6e5 100644 --- a/tools/nut-scanner/nut-scanner.c +++ b/tools/nut-scanner/nut-scanner.c @@ -1312,7 +1312,9 @@ int main(int argc, char *argv[]) } upsdebugx(1, "Parallel scan support: max_threads=%" PRIuSIZE, max_threads); - sem_init(current_sem, 0, (unsigned int)max_threads); + if (sem_init(current_sem, 0, (unsigned int)max_threads)) { + upsdebug_with_errno(4, "%s: sem_init() failed", __func__); + } # endif #else upsdebugx(1, "Parallel scan support: !HAVE_PTHREAD"); diff --git a/tools/nut-scanner/nutscan-init.c b/tools/nut-scanner/nutscan-init.c index 6b891a61de..170249de2e 100644 --- a/tools/nut-scanner/nutscan-init.c +++ b/tools/nut-scanner/nutscan-init.c @@ -159,7 +159,12 @@ void nutscan_init(void) __func__); max_threads = UINT_MAX - 1; } - sem_init(&semaphore, 0, (unsigned int)max_threads); + + upsdebugx(1, "%s: Parallel scan support: max_threads=%" PRIuSIZE, + __func__, max_threads); + if (sem_init(semaphore, 0, (unsigned int)max_threads)) { + upsdebug_with_errno(4, "%s: sem_init() failed", __func__); + } # endif # ifdef HAVE_PTHREAD_TRYJOIN diff --git a/tools/nut-scanner/scan_nut.c b/tools/nut-scanner/scan_nut.c index eb6ca64838..20d1d55e3a 100644 --- a/tools/nut-scanner/scan_nut.c +++ b/tools/nut-scanner/scan_nut.c @@ -296,7 +296,11 @@ nutscan_device_t * nutscan_scan_nut(const char* startIP, const char* stopIP, con __func__); max_threads_scantype = UINT_MAX - 1; } - sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype); + + upsdebugx(4, "%s: sem_init() for %" PRIuSIZE " threads", __func__, max_threads_scantype); + if (sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype)) { + upsdebug_with_errno(4, "%s: sem_init() failed", __func__); + } } # endif /* HAVE_SEMAPHORE */ @@ -356,8 +360,21 @@ nutscan_device_t * nutscan_scan_nut(const char* startIP, const char* stopIP, con sem_wait(semaphore); pass = TRUE; } else { - pass = ((max_threads_scantype == 0 || sem_trywait(semaphore_scantype) == 0) && - sem_trywait(semaphore) == 0); + /* If successful (the lock was acquired), + * sem_wait() and sem_trywait() will return 0. + * Otherwise, -1 is returned and errno is set, + * and the state of the semaphore is unchanged. + */ + int stwST = sem_trywait(semaphore_scantype), stwS = sem_trywait(semaphore); + pass = ((max_threads_scantype == 0 || stwST == 0) && stwS == 0); + upsdebugx(4, "%s: max_threads_scantype=%" PRIuSIZE + " curr_threads=%" PRIuSIZE + " thread_count=%" PRIuSIZE + " stwST=%d stwS=%d pass=%d", + __func__, max_threads_scantype, + curr_threads, thread_count, + stwST, stwS, pass + ); } # else # ifdef HAVE_PTHREAD_TRYJOIN @@ -494,9 +511,10 @@ nutscan_device_t * nutscan_scan_nut(const char* startIP, const char* stopIP, con # ifdef HAVE_SEMAPHORE /* Wait for all current scans to complete */ if (thread_array != NULL) { - upsdebugx (2, "%s: Running too many scanning threads, " + upsdebugx (2, "%s: Running too many scanning threads (%" + PRIuSIZE "), " "waiting until older ones would finish", - __func__); + __func__, thread_count); for (i = 0; i < thread_count ; i++) { int ret; if (!thread_array[i].active) { diff --git a/tools/nut-scanner/scan_snmp.c b/tools/nut-scanner/scan_snmp.c index 46a5663470..7c415b3e45 100644 --- a/tools/nut-scanner/scan_snmp.c +++ b/tools/nut-scanner/scan_snmp.c @@ -1077,7 +1077,11 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip __func__); max_threads_scantype = UINT_MAX - 1; } - sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype); + + upsdebugx(4, "%s: sem_init() for %" PRIuSIZE " threads", __func__, max_threads_scantype); + if (sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype)) { + upsdebug_with_errno(4, "%s: sem_init() failed", __func__); + } } # endif /* HAVE_SEMAPHORE */ @@ -1131,8 +1135,21 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip sem_wait(semaphore); pass = TRUE; } else { - pass = ((max_threads_scantype == 0 || sem_trywait(semaphore_scantype) == 0) && - sem_trywait(semaphore) == 0); + /* If successful (the lock was acquired), + * sem_wait() and sem_trywait() will return 0. + * Otherwise, -1 is returned and errno is set, + * and the state of the semaphore is unchanged. + */ + int stwST = sem_trywait(semaphore_scantype), stwS = sem_trywait(semaphore); + pass = ((max_threads_scantype == 0 || stwST == 0) && stwS == 0); + upsdebugx(4, "%s: max_threads_scantype=%" PRIuSIZE + " curr_threads=%" PRIuSIZE + " thread_count=%" PRIuSIZE + " stwST=%d stwS=%d pass=%d", + __func__, max_threads_scantype, + curr_threads, thread_count, + stwST, stwS, pass + ); } # else # ifdef HAVE_PTHREAD_TRYJOIN @@ -1252,9 +1269,10 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip # ifdef HAVE_SEMAPHORE /* Wait for all current scans to complete */ if (thread_array != NULL) { - upsdebugx (2, "%s: Running too many scanning threads, " + upsdebugx (2, "%s: Running too many scanning threads (%" + PRIuSIZE "), " "waiting until older ones would finish", - __func__); + __func__, thread_count); for (i = 0; i < thread_count ; i++) { int ret; if (!thread_array[i].active) { diff --git a/tools/nut-scanner/scan_xml_http.c b/tools/nut-scanner/scan_xml_http.c index c7db37c03c..934f66efd8 100644 --- a/tools/nut-scanner/scan_xml_http.c +++ b/tools/nut-scanner/scan_xml_http.c @@ -484,7 +484,11 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char __func__); max_threads_scantype = UINT_MAX - 1; } - sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype); + + upsdebugx(4, "%s: sem_init() for %" PRIuSIZE " threads", __func__, max_threads_scantype); + if (sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype)) { + upsdebug_with_errno(4, "%s: sem_init() failed", __func__); + } } # endif /* HAVE_SEMAPHORE */ @@ -510,11 +514,24 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char * all earlier runners */ if (max_threads_scantype > 0) sem_wait(semaphore_scantype); - sem_wait(semaphore); - pass = TRUE; + sem_wait(semaphore); + pass = TRUE; } else { - pass = ((max_threads_scantype == 0 || sem_trywait(semaphore_scantype) == 0) && - sem_trywait(semaphore) == 0); + /* If successful (the lock was acquired), + * sem_wait() and sem_trywait() will return 0. + * Otherwise, -1 is returned and errno is set, + * and the state of the semaphore is unchanged. + */ + int stwST = sem_trywait(semaphore_scantype), stwS = sem_trywait(semaphore); + pass = ((max_threads_scantype == 0 || stwST == 0) && stwS == 0); + upsdebugx(4, "%s: max_threads_scantype=%" PRIuSIZE + " curr_threads=%" PRIuSIZE + " thread_count=%" PRIuSIZE + " stwST=%d stwS=%d pass=%d", + __func__, max_threads_scantype, + curr_threads, thread_count, + stwST, stwS, pass + ); } # else # ifdef HAVE_PTHREAD_TRYJOIN @@ -643,9 +660,10 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char # ifdef HAVE_SEMAPHORE /* Wait for all current scans to complete */ if (thread_array != NULL) { - upsdebugx (2, "%s: Running too many scanning threads, " + upsdebugx (2, "%s: Running too many scanning threads (%" + PRIuSIZE "), " "waiting until older ones would finish", - __func__); + __func__, thread_count); for (i = 0; i < thread_count ; i++) { int ret; if (!thread_array[i].active) {