/*************************/ /* shared memory manager */ /*************************/ #include #include #include #include #include extern int errno; int main(int argc, char *argv[]) { int id ; int i; struct shmid_ds buf; char c; int uid, gid; int isDelete=0; /* 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 shmctl(IPC_STAT) to look at all the shared memory */ printf("Looking for shared-memory id=0--65535...\n"); for(i=0; i<65536; i++) if (shmctl(i,IPC_STAT,&buf)!=-1) { printf(" shmctl successed on id=%d.\n", i); printf(" Size : %d\n",buf.shm_segsz); printf(" Time : %s",ctime(&buf.shm_atime)); #ifdef _SYS_IPC_BUF_H /* RedHat Linux 5.2: see /usr/include/sys/ipc_buf.h */ printf(" Key : %d\n", buf.shm_perm.__key); #else printf(" Key : %d\n", buf.shm_perm.key); #endif printf(" UGID : %d,%d(%s)\n",buf.shm_perm.uid,buf.shm_perm.gid, (uid==buf.shm_perm.uid && gid==buf.shm_perm.gid)? "own":"not own"); if (isDelete) if (shmctl(i,IPC_RMID,NULL)==-1) printf(" - Fail to remove\n"); else printf(" - Success to remove\n"); } else if (errno != EINVAL && errno != EIDRM) { printf (" shmctl failed on id=%d (%s).\n", i, strerror(errno)) ; if (isDelete) if (shmctl(i,IPC_RMID,NULL)==-1) printf(" - Fail to remove\n"); else printf(" - Success to remove\n"); } return(1); } /* end main() */