#!/usr/bin/python
#
# $Id: cleanipcs,v 1.4 2005/10/12 18:09:49 mjk Exp $
#
# @Copyright@
# 
# 				Rocks
# 		         www.rocksclusters.org
# 		           version 4.1 (fuji)
# 
# Copyright (c) 2005 The Regents of the University of California. All
# rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 
# 3. All advertising materials mentioning features or use of this
# software must display the following acknowledgement: 
# 
# 	"This product includes software developed by the Rocks 
# 	Cluster Group at the San Diego Supercomputer Center and
# 	its contributors."
# 
# 4. Neither the name or logo of this software nor the names of its
# authors may be used to endorse or promote products derived from this
# software without specific prior written permission.  The name of the
# software includes the following terms, and any derivatives thereof:
# "Rocks", "Rocks Clusters", and "Avalanche Installer".
# 
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# @Copyright@
#
# $Log: cleanipcs,v $
# Revision 1.4  2005/10/12 18:09:49  mjk
# final copyright for 4.1
#
# Revision 1.3  2005/09/16 01:03:26  mjk
# updated copyright
#
# Revision 1.2  2005/05/24 21:22:50  mjk
# update copyright, release is not any closer
#
# Revision 1.1  2005/03/12 00:05:48  bruno
# new files
#
#

import os
import os.path
import sys
import popen2
import string

active_list = []

def active_processes():
	cmd = 'ps --no-headers -A'

	r, w = popen2.popen2(cmd)

	for line in r.readlines():
		l = string.split(line)

		if len(l) > 0:
			active_list.append(l[0])

	return

def sem_array_in_use(semid):
	cmd = 'ipcs -s -i %s' % (semid)

	r, w = popen2.popen2(cmd)

	start = 0
	for line in r.readlines():
		l = string.split(line)

		if len(l) == 0:
			continue

		if l[0] == 'semnum':
			start = 1
			continue

		if start == 0:
			continue

		if len(l) == 5:
			if l[4] in active_list:
				return 1

	return 0


def clean_semaphore_arrays():
	#
	# cleanup semaphore arrays
	#
	cmd = 'ipcs -s'

	r, w = popen2.popen2(cmd)

	start = 0
	for line in r.readlines():
		l = string.split(line)

		if len(l) == 0:
			continue

		if l[0] == 'key':
			start = 1
			continue

		if start == 0:
			continue

		if not sem_array_in_use(l[1]):
			cmd = 'ipcrm -s %s' % (l[1])
			os.system(cmd)

	return


def clean_shared_memory():
	#
	# cleanup shared memory segments
	#
	cmd = 'ipcs -m'

	r, w = popen2.popen2(cmd)

	start = 0
	for line in r.readlines():
		l = string.split(line)

		if len(l) == 0:
			continue

		if l[0] == 'key':
			start = 1
			continue

		if start == 0:
			continue

		if len(l) > 5 and l[5] == '0':
			cmd = 'ipcrm -m %s' % (l[1])
			os.system(cmd)

	return
	

active_processes()
clean_semaphore_arrays()
clean_shared_memory()

