ftok in bash

Sometimes sqlrelay does not remove shared memory segments after exiting. This work could be done using ipcs(1) and ipcrm(1) commands. The problem is that we do not actually know exactly what ipc facility to remove. Sqlrelay uses a SysV IPC shared memory resources identified by “keys”. To get a key sqlrelay uses ftok(3) function with a file in “sqlrelay/tmp/ipc” directory and “1” as project id.

So, we simply need to call standard function and remove all the litter? Not so fast 🙂 We only have bash to complete the mission. So, here is ftok implemented in bash:



dev=`stat --format=%d $ipc_file`
ino=`stat --format=%i $ipc_file`
printf "%xn" $((($ino&0xffff)|(($dev&0xff)<<16)|(($proj_id&0xff)<<24)))

Let us test:

srr@mars:~$ ipcs -m | grep sqlrelay
0x010b000f 17334272 sqlrelay 640 1336 3
0x010b0064 17367041 sqlrelay 640 1336 3
0x010b006f 17399810 sqlrelay 640 1336 3
0x010b007d 17432579 sqlrelay 640 1336 3
srr@mars:~$ for i in `find /var/cache/sqlrelay/tmp/ipc/ -type f | grep -v "-"` ; do echo `./ftok $i 1` $i; done
10b000f /var/cache/sqlrelay/tmp/ipc/onyma_develop
10b007d /var/cache/sqlrelay/tmp/ipc/onyma_oper
10b0064 /var/cache/sqlrelay/tmp/ipc/onyma_admin
10b006f /var/cache/sqlrelay/tmp/ipc/onyma_login


Leave a Reply

Your email address will not be published. Required fields are marked *