4444from lib .core .settings import INFERENCE_EQUALS_CHAR
4545from lib .core .settings import INFERENCE_NOT_EQUALS_CHAR
4646from lib .core .settings import PYVERSION
47+ from lib .core .threads import getCurrentThreadData
48+ from lib .core .threads import runThreads
4749from lib .core .unescaper import unescaper
4850from lib .request .connect import Connect as Request
4951
@@ -301,26 +303,31 @@ def etaProgressUpdate(charTime, index):
301303
302304 # Go multi-threading (--threads > 1)
303305 if conf .threads > 1 and isinstance (length , int ) and length > 1 :
304- value = [ None ] * length
305- index = [ firstChar ] # As list for python nested function scoping
306- idxlock = threading .Lock ()
307- iolock = threading .Lock ()
308- valuelock = threading .Lock ()
309- kb .threadContinue = True
310-
311- def downloadThread ():
312- try :
306+ value = []
307+ threadData = getCurrentThreadData ()
308+
309+ threadData .shared .value = [ None ] * length
310+ threadData .shared .index = [ firstChar ] # As list for python nested function scoping
311+
312+ lockNames = ('iolock' , 'idxlock' , 'valuelock' )
313+ for lock in lockNames :
314+ kb .locks [lock ] = threading .Lock ()
315+
316+ try :
317+ def blindThread ():
318+ threadData = getCurrentThreadData ()
319+
313320 while kb .threadContinue :
314- idxlock .acquire ()
321+ kb . locks . idxlock .acquire ()
315322
316- if index [0 ] >= length :
317- idxlock .release ()
323+ if threadData . shared . index [0 ] >= length :
324+ kb . locks . idxlock .release ()
318325
319326 return
320327
321- index [0 ] += 1
322- curidx = index [0 ]
323- idxlock .release ()
328+ threadData . shared . index [0 ] += 1
329+ curidx = threadData . shared . index [0 ]
330+ kb . locks . idxlock .release ()
324331
325332 if kb .threadContinue :
326333 charStart = time .time ()
@@ -330,14 +337,14 @@ def downloadThread():
330337 else :
331338 break
332339
333- valuelock .acquire ()
334- value [curidx - 1 ] = val
335- currentValue = list (value )
336- valuelock .release ()
340+ kb . locks . valuelock .acquire ()
341+ threadData . shared . value [curidx - 1 ] = val
342+ currentValue = list (threadData . shared . value )
343+ kb . locks . valuelock .release ()
337344
338345 if kb .threadContinue :
339346 if showEta :
340- etaProgressUpdate (time .time () - charStart , index [0 ])
347+ etaProgressUpdate (time .time () - charStart , threadData . shared . index [0 ])
341348 elif conf .verbose >= 1 :
342349 startCharIndex = 0
343350 endCharIndex = 0
@@ -370,14 +377,14 @@ def downloadThread():
370377 status = ' %d/%d (%d%s)' % (count , length , round (100.0 * count / length ), '%' )
371378 output += status if count != length else " " * len (status )
372379
373- iolock .acquire ()
380+ kb . locks . iolock .acquire ()
374381 dataToStdout ("\r [%s] [INFO] retrieved: %s" % (time .strftime ("%X" ), filterControlChars (output )))
375- iolock .release ()
382+ kb . locks . iolock .release ()
376383
377384 if not kb .threadContinue :
378385 if int (threading .currentThread ().getName ()) == numThreads - 1 :
379386 partialValue = unicode ()
380- for v in value :
387+ for v in threadData . shared . value :
381388 if v is None :
382389 break
383390 elif isinstance (v , basestring ):
@@ -386,57 +393,14 @@ def downloadThread():
386393 if len (partialValue ) > 0 :
387394 dataToSessionFile (replaceNewlineTabs (partialValue ))
388395
389- except (sqlmapConnectionException , sqlmapValueException ), errMsg :
390- print
391- kb .threadException = True
392- logger .error ("thread %s: %s" % (threading .currentThread ().getName (), errMsg ))
393-
394- except KeyboardInterrupt :
395- kb .threadException = True
396-
397- print
398- logger .debug ("waiting for threads to finish" )
399-
400- try :
401- while (threading .activeCount () > 1 ):
402- pass
403-
404- except KeyboardInterrupt :
405- raise sqlmapThreadException , "user aborted"
406-
407- except :
408- print
409- kb .threadException = True
410- errMsg = unhandledExceptionMessage ()
411- logger .error ("thread %s: %s" % (threading .currentThread ().getName (), errMsg ))
412- traceback .print_exc ()
413-
414- # Start the threads
415- for numThread in range (numThreads ):
416- thread = threading .Thread (target = downloadThread , name = str (numThread ))
417-
418- if PYVERSION >= "2.6" :
419- thread .daemon = True
420- else :
421- thread .setDaemon (True )
422-
423- thread .start ()
424- threads .append (thread )
425-
426- # And wait for them to all finish
427- try :
428- alive = True
429- while alive :
430- alive = False
431- for thread in threads :
432- if thread .isAlive ():
433- alive = True
434- time .sleep (1 )
396+ runThreads (numThreads , blindThread , startThreadMsg = False )
435397
436398 except KeyboardInterrupt :
437- kb .threadContinue = False
438399 raise
439400
401+ finally :
402+ value = threadData .shared .value
403+
440404 infoMsg = None
441405
442406 # If we have got one single character not correctly fetched it
0 commit comments