#include #include #include #include #include extern int errno; void main(int argc, char *argv[]) { int id ; int i; char c; int uid, gid; int isDelete=0; struct semid_ds mysemds; union semun { int val; struct semid_ds *buf; ushort *array; } arg; /* point to allocated space first */ arg.buf = &mysemds; /* Get args */ while((c=getopt(argc, argv,"hd"))!=(char)(-1)) switch(c) { case 'h': printf("Version: 1.11, Author: Liao Dongyi(liaody@mit.edu)\n" " Check and manipulate shared memory blocks\n" "Usage: %s -hd\n" "-h\tThis message\n" "-d\tDelete every found shared memory if possible\n" ,argv[0]); exit(0); case 'd': isDelete=1; break; case 'c':; } /* Initialize, getuid, etc..*/ printf("User ID=%d, Group ID=%d\n",uid=getuid(), gid=getgid()); /* Try the semctl(IPC_STAT) to look at all the shared memory */ printf("****Looking for semaphore id=0--65535...\n"); for(i=0;i<65536;i++) if(semctl(i,0,IPC_STAT,arg)!=-1) { printf(" semctl successed on id=%d.\n", i); printf(" # of elements : %d\n",arg.buf->sem_nsems); printf(" last change time : %s",ctime(&(arg.buf->sem_ctime))); printf(" Key : %d\n",arg.buf->sem_perm.key); printf(" UGID : %d,%d(%s)\n",arg.buf->sem_perm.uid, arg.buf->sem_perm.gid, (uid==arg.buf->sem_perm.uid && gid==arg.buf->sem_perm.gid)? "own":"not own"); if(isDelete) if(semctl(i,0,IPC_RMID,NULL)==-1) printf(" - Fail to remove\n"); else printf(" - Success to remove\n"); } else if(errno != EINVAL && errno != EIDRM) { printf (" semctl failed on id=%d (%s).\n", i, strerror(errno)) ; if(isDelete) if(semctl(i,0,IPC_RMID,NULL)==-1) printf(" - Fail to remove\n"); else printf(" - Success to remove\n"); } }