You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-core/core/server/gen_pid.py

165 lines
5.1 KiB

#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, pickle, threading
import random, datetime
import soaplib
from soaplib.serializers.primitive import String, Integer
from soaplib.serializers.clazz import Array
from cert_cmd import find_cert_id
# process management
class CoreWsdl () :
# delete process id from list process
def del_pid(self, pid):
try:
rst = []
pid_str = str(pid)
# open the file list of process
with open(self.pids_file) as fd:
t = fd.read()
for line in t.splitlines():
# Leave all but removed
if line != pid_str:
rst.append(line)
# write all in file
fd = open(self.pids_file, 'w')
fd.write('\n'.join(rst))
fd.write('\n') # with join we lose the last newline char
fd.close()
self.glob_process_dict.pop(pid)
self.glob_progress_dict.pop(pid)
self.glob_table_dict.pop(pid)
self.glob_frame_list.pop(pid)
return 0
except:
return 1
# find process id in file processes, 1 - yes, 0 - none
def find_pid_in_file(self, find_pid):
temp_line = ''
# create, if file not exists
if not os.path.exists(self.pids_file):
temp = open(self.pids_file, 'w')
temp.close()
with open(self.pids_file) as fd:
t = fd.read()
# for each line
for line in t.splitlines():
try:
temp_line = int(line)
except:
pass
# if process id found
if temp_line == find_pid:
return 1
fd.close()
return 0
# add process id in file
def add_pid_in_file(self, pid):
pid_t = str(pid)
fd = open(self.pids_file, 'a')
fd.write(pid_t)
fd.write('\n')
fd.close()
return 0
# issue new pid for created process
def gen_pid(self):
# flag = 1 - exists, 0 - missing in PID_FILE
flag = 1
# while exists generation new process id
while flag == 1:
new_pid = random.randint(1,self.max_pid)
# find in file
flag = self.find_pid_in_file(new_pid)
# add new process id in list
self.add_pid_in_file(new_pid)
return new_pid
def check_sid_cert (self, sid):
curThread = threading.currentThread()
certificate = curThread.client_cert
cert_id = find_cert_id(certificate, self.data_path, self.certbase)
# if certificate not found in database
if cert_id == 0:
return -1
# check, This certificate is launched session
# Data taken from sid.db
flag = 0
# create, if file not exists
if not os.path.exists(self.sids_file):
temp = open(self.sids_file, 'w')
temp.close()
fd = open(self.sids_file, 'r')
while 1:
try:
# read all on one record
list_sid = pickle.load(fd)
except:
break
# when session id equal readable...
if int(sid) == list_sid[0]:
# ... and certificate id equal launched this session...
if int(cert_id) == list_sid[1]:
# ... set flag
flag = 1
fd.close()
# if validation fails
return flag
def serv_pid_kill (self, pid, sid, certbase):
""" Set flag to complete the process """
check_sid = self.check_sid_cert(sid)
if not check_sid:
return -2
# write complete flag (pid_list[6] = 1) in process file
if not os.path.exists(self.pids):
os.system('mkdir %s' %self.pids)
pid_file_kill = self.pids + "/%d.pid" %pid
#try:
# set complete flag
#self.glob_process_dict[pid]['flag'] = 1
meth = self.process_pid[pid]
if meth.is_alive():
try:
os.kill(meth.pid, 2)
except OSError, e:
print 'No such process %d' %meth.pid, e
return 0
#except:
#return 1
## check exhibited a complete flag
#def child_kill(self, pid):
#try:
## if set, return 1
#if self.glob_process_dict[pid]['flag'] == 1:
#return 1
#return 0
#except:
#return 0