#ident "$Id$" /* ----------------------------------------------------------------------- * * * Copyright 2001 H. Peter Anvin - All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139, * USA; either version 2 of the License, or (at your option) any later * version; incorporated herein by reference. * * ----------------------------------------------------------------------- */ /* * killramdisk.c * * This utility opens a ramdisk device (/dev/ram*) and attempts * to free its contents. */ #include #include #include #include #include #include #include #ifndef MAJOR # define MAJOR(X) ((X) >> 8) #endif const char *program; void fail(const char *rdname) { const char *err = strerror(errno); fprintf(stderr, "%s: %s: %s\n", program, rdname, err); exit(1); } int main(int argc, char *argv[]) { int i, rd; const char *rdname; struct stat st; program = argv[0]; for ( i = 1 ; i < argc ; i++ ) { rdname = argv[i]; rd = open(rdname, O_RDWR); if ( rd < 0 ) fail(rdname); if ( fstat(rd, &st) < 0 ) fail(rdname); /* There should be a cleaner way to test for this! */ if ( !S_ISBLK(st.st_mode) || MAJOR(st.st_rdev) != 1 ) { fprintf(stderr, "%s: %s: Not a ramdisk device\n", program, rdname); exit(1); } if ( ioctl(rd, BLKFLSBUF, 0) < 0 ) { fail(rdname); } close(rd); } return 0; }