/*********************/ /* semaphore manager */ /*********************/ #include #include #include #include #include extern int errno; int main(int argc, char *argv[]) { int id, i, uid, gid, isDelete=0; char c; struct semid_ds mysemds; union semun 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 semaphores */ 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))); #ifdef _SYS_IPC_BUF_H /* RedHat Linux 5.2: see /usr/include/sys/ipc_buf.h */ printf(" Key : %d\n", arg.buf->sem_perm.__key); #else printf(" Key : %d\n", arg.buf->sem_perm.key); #endif 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"); } return(0); } /* end main() */