3
0

freeramdisk.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * freeramdisk and fdflush implementations for busybox
  4. *
  5. * Copyright (C) 2000 and written by Emanuele Caratti <wiz@iol.it>
  6. * Adjusted a bit by Erik Andersen <andersen@codepoet.org>
  7. * Unified with fdflush by Tito Ragusa <farmatito@tiscali.it>
  8. *
  9. * Licensed under GPLv2, see file LICENSE in this source tree.
  10. */
  11. //config:config FDFLUSH
  12. //config: bool "fdflush (1.6 kb)"
  13. //config: default y
  14. //config: help
  15. //config: fdflush is only needed when changing media on slightly-broken
  16. //config: removable media drives. It is used to make Linux believe that a
  17. //config: hardware disk-change switch has been actuated, which causes Linux to
  18. //config: forget anything it has cached from the previous media. If you have
  19. //config: such a slightly-broken drive, you will need to run fdflush every time
  20. //config: you change a disk. Most people have working hardware and can safely
  21. //config: leave this disabled.
  22. //config:
  23. //config:config FREERAMDISK
  24. //config: bool "freeramdisk (1.6 kb)"
  25. //config: default y
  26. //config: help
  27. //config: Linux allows you to create ramdisks. This utility allows you to
  28. //config: delete them and completely free all memory that was used for the
  29. //config: ramdisk. For example, if you boot Linux into a ramdisk and later
  30. //config: pivot_root, you may want to free the memory that is allocated to the
  31. //config: ramdisk. If you have no use for freeing memory from a ramdisk, leave
  32. //config: this disabled.
  33. // APPLET_ODDNAME:name main location suid_type help
  34. //applet:IF_FDFLUSH( APPLET_ODDNAME(fdflush, freeramdisk, BB_DIR_BIN, BB_SUID_DROP, fdflush ))
  35. //applet:IF_FREERAMDISK(APPLET_NOEXEC(freeramdisk, freeramdisk, BB_DIR_SBIN, BB_SUID_DROP, freeramdisk))
  36. //kbuild:lib-$(CONFIG_FDFLUSH) += freeramdisk.o
  37. //kbuild:lib-$(CONFIG_FREERAMDISK) += freeramdisk.o
  38. //usage:#define freeramdisk_trivial_usage
  39. //usage: "DEVICE"
  40. //usage:#define freeramdisk_full_usage "\n\n"
  41. //usage: "Free all memory used by the specified ramdisk"
  42. //usage:
  43. //usage:#define freeramdisk_example_usage
  44. //usage: "$ freeramdisk /dev/ram2\n"
  45. //usage:
  46. //usage:#define fdflush_trivial_usage
  47. //usage: "DEVICE"
  48. //usage:#define fdflush_full_usage "\n\n"
  49. //usage: "Force floppy disk drive to detect disk change"
  50. #include <sys/mount.h>
  51. #include "libbb.h"
  52. /* From <linux/fd.h> */
  53. #define FDFLUSH _IO(2,0x4b)
  54. int freeramdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  55. int freeramdisk_main(int argc UNUSED_PARAM, char **argv)
  56. {
  57. int fd;
  58. fd = xopen(single_argv(argv), O_RDWR);
  59. // Act like freeramdisk, fdflush, or both depending on configuration.
  60. ioctl_or_perror_and_die(fd,
  61. ((ENABLE_FREERAMDISK && applet_name[1] == 'r') || !ENABLE_FDFLUSH)
  62. ? BLKFLSBUF
  63. : FDFLUSH,
  64. NULL, "%s", argv[1]
  65. );
  66. if (ENABLE_FEATURE_CLEAN_UP) close(fd);
  67. return EXIT_SUCCESS;
  68. }