• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# shm_clear: clean out unattached (NATTACH=0) shm segments.
4# See ipcs(1) and ipcrm(1).  Tested on Linux and Solaris.
5#
6# Usage:
7#    shm_clear      list and prompt for removal of your unattached shm segments.
8#    shm_clear -y   assume "yes" to all the removal prompts.
9#    shm_clear -l   only list (all of) your shm segments and exit.
10#
11
12#set -xv
13if echo "$1" | grep '^-h' > /dev/null; then
14	# -h or -help
15	tail +3 $0 | head -9
16	exit
17fi
18
19if [ "X$USER" = "X" ]; then
20	USER=$LOGNAME
21fi
22l_arg="shmid.*owner|CREATOR|$USER"
23
24# set up OS dependent cmdline opts, etc.
25if [ `uname` = "Linux" ]; then
26	m_arg="-m"
27	r_arg="shm"
28	g_arg="^0x"
29	s_cmd="ipcs $m_arg -i %ID"
30	awkcut='{print $2, $6}'
31elif [ `uname` = "SunOS" ]; then
32	m_arg="-ma"
33	r_arg="-m"
34	g_arg="^m"
35	s_cmd="ipcs $m_arg | egrep '  %ID  |CREATOR' | grep -v IPC.status"
36	awkcut='{print $2, $9}'
37else
38	echo unsupported OS: `uname`
39	exit 1
40fi
41
42list() {
43	if [ "X$1" = "X-L" ]; then
44		l_arg="$l_arg|."
45		echo "All shm segments for all:"
46	else
47		echo "All shm segments for $USER:"
48	fi
49	ipcs $m_arg | egrep "$l_arg"
50	echo
51}
52
53show() {
54	cmd=`echo "$s_cmd" | sed -e "s/%ID/$1/g"`
55	eval $cmd
56}
57
58remove() {
59	echo ipcrm $r_arg $1
60	     ipcrm $r_arg $1
61}
62
63if [ "X$1" = "X-l" -o "X$1" = "X-L" ]; then
64	# list only.  both attached and unattached listed.
65	list $1
66	exit 0
67fi
68
69if [ "X$1" = "X-y" ]; then
70	shift
71	yes=1	# assume "yes" to all delete questions.
72else
73	yes=""
74fi
75
76list
77
78ids=`ipcs $m_arg | grep "$g_arg" | grep $USER | awk "$awkcut" | grep ' 0$' | awk '{print $1}'`
79if [ "X$ids" = "X" ]; then
80	echo "No unattached shmids for $USER."
81fi
82
83for id in $ids
84do
85	if [ $yes ]; then
86		:
87	else
88		echo "-------------------------------------"
89		show $id
90		printf "\nDelete? [y]/n "
91		read x
92		if echo "$x" | grep -i n > /dev/null; then
93			continue
94		fi
95	fi
96	remove $id
97done
98