/* * cc -o checklist checklist.c -lnsl * * note: search for JDS and change the two printf's on systems * that support lock.l_sysid */ #include #include #include #include #include #include #include static char addrbuf[128]; main(argc,argv) int argc; char **argv; { int fdread,fdwrite; struct flock lock; static char *getname(); if (argc != 2) { printf("Usage: checklock \n"); exit(1); } if ((fdread = open(*++argv,O_RDONLY)) < 0) { perror("Unable to open file for read"); exit(2); } fdwrite = open(*argv,O_RDWR); lock.l_whence = 0; lock.l_start = 0L; lock.l_len = 0; lock.l_type = F_RDLCK; if (fcntl(fdread,F_GETLK,(void *)&lock) < 0) { perror("fcn;t failed on read lock"); exit(3); } if (lock.l_type == F_UNLCK) { printf("Read lock is available.\n"); } else { /* JDS printf("Read lock is blocked by %d on %s.\n",lock.l_pid, getname(lock.l_sysid)); */ printf("Read lock is blocked by %d\n",lock.l_pid); } if (fdwrite >= 0) { lock.l_whence = 0; lock.l_start = 0L; lock.l_len = 0; lock.l_type = F_WRLCK; if (fcntl(fdread,F_GETLK,(void *)&lock) < 0) { perror("fcn;t failed on read lock"); exit(4); } if (lock.l_type == F_UNLCK) { printf("Write lock is available.\n"); } else { /* JDS printf("Write lock is blocked by %d on %s.\n",lock.l_pid, getname(lock.l_sysid)); */ printf("Write lock is blocked by %d.\n",lock.l_pid); } } exit(0); } static char *getname(sysid) unsigned long sysid; { struct hostent *node; if (sysid) { if (node = gethostbyaddr((char *)&sysid,sizeof(sysid),AF_INET)) { return(node->h_name); } else { sprintf(addrbuf,"%2.2d.%2.2d.%2.2d.%2.2d", (sysid >> 24), (sysid & 0x00FF0000) >> 16, (sysid & 0x0000FF00) >> 8, (sysid & 0x000000FF)); return(addrbuf); } } else { return("current system"); } }