Skip to content

Instantly share code, notes, and snippets.

@step-
Created September 18, 2018 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save step-/40a4229a0c23bf1494daffe81d608b5e to your computer and use it in GitHub Desktop.
Save step-/40a4229a0c23bf1494daffe81d608b5e to your computer and use it in GitHub Desktop.
Remove (yad) unreleased shared memory blocks (Linux).
#!/bin/sh
# rm-yad-shmem for yad 0.36.3 and possibly other versions.
# Motivation
#
# Some times, when you kill [-USR1/-USR2] a plugged dialog, or the paned/tabbed
# dialog that hosts the plugged dialog, its shared memory (shmem) may not be
# fully released. This tool aids in releasing shmem blocks interactively.
# Limitation
#
# As far as I know, telling which shmem blocks a defunct process owned isn't
# possible. So this tool can only help identifying unowned blocks, and you are
# left to decide whether those belonged to the killed yad or to something
# totally different. There is nothing really specific to yad in this script.
# See also https://stackoverflow.com/a/5658711
NATTCH=0 # query
ipcs -m |
while read \
key shmid owner perms bytes nattch status
do
if [ $NATTCH = "$nattch" ]; then
printf "key %d (0x%x) shmid %d has %d attached processes\n" $key $key $shmid $nattch
printf "%s is the owner\n" $owner
ipcs -mp |
while read \
shmid2 owner2 cpid lpid
do
if [ $shmid = "$shmid2" ]; then
printf "PID %d created %d and PID %d last attached to it\n" $cpid $shmid2 $lpid
{
ps --cols 9999 -ho pid,cmd $cpid 2>/dev/null
grep $shmid2 /proc/$cpid/maps 2>/dev/null
} || echo "PID $cpid is no longer running"
{
ps --cols 9999 -ho pid,cmd $lpid 2>/dev/null
grep $shmid2 /proc/$lpid/maps 2>/dev/null
} || echo "PID $lpid is no longer running"
printf "Remove shmid %s ? (y/N) " $shmid2
read reply < /dev/tty
case $reply in y*|Y* ) ipcrm shm $shmid2;; esac
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment