3
0

freeramdisk.c 2.9 KB

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