/usr/bin/tfQManager.torrentflux is in torrentflux 2.4-5.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | #!/usr/bin/python
# Written by kboy
# v 1.01 Mar 20, 06
import commands, os, re, sys
from os import popen, getpid, remove
from sys import argv, stdout
from time import time, strftime
import thread, threading, time
DEBUG = False
if __debug__: LOGFILE=open(argv[1]+"tfQManager.log","w")
def run(qDirectory,maxSvrThreads,maxUsrThreads,sleepInterval,execPath):
displayParams(qDirectory,maxSvrThreads,maxUsrThreads,sleepInterval,execPath)
#Check to see if already running.
lastPID = "0"
try:
f=open(qDirectory+"tfQManager.pid",'r')
lastPID = f.readline().strip()
f.close()
if __debug__: traceMsg("Last QManager pid" + str(lastPID))
except:
pass
if (int(lastPID) > 0):
if (checkPIDStatus(lastPID) > 0):
if __debug__: traceMsg("Already Running on pid:" + lastPID)
raise KeyboardInterrupt
if __debug__: traceMsg("QManager Starting")
f=open(qDirectory+"tfQManager.pid",'w')
f.write(str(getpid()) + "\n")
f.flush()
f.close()
if __debug__: traceMsg("QManager PID :" + str(getpid()))
# Extract from the execPath the Btphptornado.py script line.
# this will be used during the process Counts to ensure we are
# unique from other running instances.
ePath = execPath.split()
btphp = ePath[-1]
if __debug__: traceMsg("btphp ->"+btphp)
if (1):
try:
while 1:
threadCount = checkThreadCount(btphp)
if __debug__: traceMsg("CurrentThreadCount = " + str( threadCount ))
#
# Start Looping untill we have maxSvrThreads.
# Or no Qinfo Files.
#
while int(threadCount) <= int(maxSvrThreads):
try:
#
# Get the Next File.
# Check to see if we got a file back.
# if not break out of looping we don't have any files.
#
fileList = []
fileList = getFileList(qDirectory)
for currentFile in fileList:
if currentFile == "":
break
# set the name of the current statsFile
statsFile = currentFile.replace('/queue','').strip('.Qinfo')
if __debug__: traceMsg("statsFile = " + statsFile)
#
# get the User name if we didn't get one
# something was wrong with this file.
#
currentUser = getUserName(statsFile)
if currentUser == "":
if __debug__: traceMsg("No User Found : " + currentFile)
# Prep StatsFile
updateStats(statsFile, '0')
removeFile(currentFile)
break
else:
if __debug__: traceMsg("Current User: " + currentUser)
#
# Now check user thread count
#
usrThreadCount = getUserThreadCount(currentUser, btphp)
#
# check UserThreadCount
#
if int(usrThreadCount) < int(maxUsrThreads):
#
# Now check to see if we start a new thread will we be over the max ?
#
threadCount = checkThreadCount(btphp)
if int(threadCount) + 1 <= int(maxSvrThreads):
if int(usrThreadCount) + 1 <= int(maxUsrThreads):
cmdToRun = getCommandToRun(currentFile)
#if __debug__: traceMsg(" Cmd :" + cmdToRun)
if (re.search(currentUser,cmdToRun) == 0):
if __debug__: traceMsg("Incorrect User found in Cmd")
cmdToRun = ''
if (re.search('\|',cmdToRun) > 0):
if __debug__: traceMsg(" Failed pipe ")
cmdToRun = ''
else:
cmdToRun = execPath + cmdToRun
cmdToRun = cmdToRun.replace('TFQUSERNAME', currentUser)
#if __debug__: traceMsg(" Cmd :" + cmdToRun)
if cmdToRun != "":
#PrepStatsFile
updateStats(statsFile, '1')
if __debug__: traceMsg("Fire off command")
try:
garbage = doCommand(cmdToRun)
#
# wait until the torrent process starts
# and creates a pid file.
# once this happens we can remove the Qinfo.
#
for i in range(5):
try:
time.sleep(2)
f=open(statsFile+".pid",'r')
f.close()
break
except:
if i == 5:
if __debug__: traceMsg("pid file not created. Continue.")
continue
# Ok this one started Remove Qinfo File.
if __debug__: traceMsg("Removing : " + currentFile)
removeFile(currentFile)
except:
continue
else:
#
# Something wrong with command file.
#
if __debug__: traceMsg("Unable to obtain valid cmdToRun : " + currentFile)
removeFile(currentFile)
else:
if __debug__: traceMsg("Skipping this file since the User has to many threads")
if __debug__: traceMsg("Skipping : " + currentFile)
else:
if __debug__: traceMsg("Skipping this file since the Server has to many threads")
if __debug__: traceMsg("Skipping : " + currentFile)
break
except:
break
threadCount = checkThreadCount(btphp)
if __debug__: traceMsg("CurrentThreadCount = " + str( threadCount ))
if __debug__: traceMsg("Sleeping...")
time.sleep(float(sleepInterval))
except:
removeFile(qDirectory+"tfQManager.pid")
else:
LOG = True
if __debug__: traceMsg("Only supported client is btphptornado.")
removeFile(qDirectory+"tfQManager.pid")
def checkThreadCount(btphp):
if __debug__: traceMsg("->checkTreadCount")
psLine = []
line = ""
counter = 0
list = doCommand("ps x -o pid,ppid,command -ww | grep '" + btphp + "' | grep -v " + argv[0] + " | grep -v grep")
try:
for c in list:
line += c
if c == '\n':
psLine.append(line.strip())
line = ""
# look for the grep line
for line in psLine:
if (re.search(btphp,line) > 0):
# now see if this is the main process and not a child.
if (re.search(' 1 /',line) > 0):
counter += 1
if __debug__: traceMsg(" -- Counted -- ")
if __debug__: traceMsg(line)
except:
counter = 0
return counter
def checkPIDStatus(pid):
if __debug__: traceMsg("->checkPIDStatus (" + pid + ")")
counter = 0
list = doCommand("ps -p "+pid+" -o pid= -ww")
try:
counter = len(list)
except:
counter = 0
return counter
def getUserThreadCount(userName, btphp):
if __debug__: traceMsg("->getUserThreadCount (" + userName + ")")
psLine = []
line = ""
counter = 0
list = doCommand("ps x -o pid,ppid,command -ww | grep '" + btphp + "' | grep -v " + argv[0] + " | grep -v grep")
try:
for c in list:
line += c
if c == '\n':
psLine.append(line.strip())
line = ""
# look for the grep line
for line in psLine:
if (re.search(btphp,line) > 0):
# now see if this is the main process and not a child.
if (re.search(' 1 /',line) > 0):
# look for the userName
if re.search(userName,line) > 0:
counter += 1
if __debug__: traceMsg(" -- Counted -- ")
if __debug__: traceMsg(line)
except:
counter = 0
if __debug__: traceMsg("->getUserThreadCount is (" + str(counter)+")")
return counter
def removeFile(currentFile):
if __debug__: traceMsg("->removeFile (" + currentFile + ")")
os.remove(currentFile)
return
def doCommand( command ):
if __debug__: traceMsg("->doCommand (" + command + ")")
#
# Fire off a command returning the output
#
return popen(command).read()
def doCommandPID( command ):
if __debug__: traceMsg("->doCommandPID (" + command + ")")
#
# Fire off a command returning the pid
#
#return popen2.Popen4(command).pid
garbage = doCommand(command)
return getPIDofCmd(command)
def getPIDofCmd(command):
if __debug__: traceMsg("->getPIDofCmd (" + command + ")")
cmdPID = ""
psLine = []
endOfPID = False
line = ""
list = doCommand("ps x -ww | grep \'"+command+"\' | grep -v grep")
if DEBUG:
print list
try:
for c in list:
line += c
if c == '\n':
psLine.append(line.strip(' '))
# look for the grep line
for line in psLine:
for e in line:
if e.isdigit() and endOfPID == False:
cmdPID += e
else:
endOfPID = True
break
except:
cmdPID = "0"
if __debug__: traceMsg(cmdPID)
return cmdPID
def getCommandToRun(currentFile):
if __debug__: traceMsg("->getCommandToRun (" + currentFile + ")")
#Open the File and return the Command to Run.
cmdToExec = ""
try:
f=open(currentFile,'r')
cmdToExec = f.readline()
f.close
except:
cmdToExec = ""
return cmdToExec
def getFileList(fDirectory):
if __debug__: traceMsg("->getFileList (" + fDirectory + ")")
# Get the list of Qinfo files.
fileList = []
try:
times = {}
for fName in os.listdir(fDirectory):
if re.search('\.Qinfo',fName) > 0:
p = os.path.join(fDirectory,fName)
times.setdefault(str(os.path.getmtime(p)),[]).append(p)
l = times.keys()
l.sort()
for i in l:
for f in times[i]:
fileList.append(f)
except:
fileList = ""
return fileList
def getUserName(fName):
if __debug__: traceMsg("->getUserName (" + fName + ")")
userName = ""
try:
f=open(fName,'r')
garbage = f.readline()
garbage = f.readline()
garbage = f.readline()
garbage = f.readline()
garbage = f.readline()
userName = f.readline().strip()
f.close()
if __debug__: traceMsg("userName : " + userName)
except:
userName = ""
return userName
def updateStats(fName, status = '1'):
if __debug__: traceMsg("->updateStats (" + fName + ")")
fp=open(fName,'r+')
fp.seek(0)
fp.write(status)
fp.flush()
fp.close
def displayParams(qDirectory,maxSvrThreads,maxUsrThreads,sleepInterval,execPath):
if __debug__:
traceMsg("qDir : " + qDirectory)
traceMsg("maxSvr : " + str(maxSvrThreads))
traceMsg("maxUsr : " + str(maxUsrThreads))
traceMsg("sleepI : " + str(sleepInterval))
traceMsg("execPath : " + str(execPath))
return
def traceMsg(msg):
if DEBUG:
print msg
if __debug__:
LOGFILE.write(msg + "\n")
LOGFILE.flush()
if __name__ == '__main__':
run(argv[1],argv[2],argv[3],argv[4],argv[5])
|