Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Rob Landley | 0b22c1c | 2006-03-14 02:40:51 +0000 | [diff] [blame] | 3 | * freeramdisk and fdflush implementations for busybox |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2000 and written by Emanuele Caratti <wiz@iol.it> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * Adjusted a bit by Erik Andersen <andersen@codepoet.org> |
Rob Landley | 0b22c1c | 2006-03-14 02:40:51 +0000 | [diff] [blame] | 7 | * Unified with fdflush by Tito Ragusa <farmatito@tiscali.it> |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config:config FDFLUSH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 12 | //config: bool "fdflush (1.4 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: select PLATFORM_LINUX |
| 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 23 | //config: |
| 24 | //config:config FREERAMDISK |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 25 | //config: bool "freeramdisk (1.4 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 26 | //config: default y |
| 27 | //config: select PLATFORM_LINUX |
| 28 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 35 | |
Denys Vlasenko | ec98e3a | 2017-08-07 23:17:14 +0200 | [diff] [blame^] | 36 | // APPLET_ODDNAME:name main location suid_type help |
| 37 | //applet:IF_FDFLUSH( APPLET_ODDNAME(fdflush, freeramdisk, BB_DIR_BIN, BB_SUID_DROP, fdflush )) |
| 38 | //applet:IF_FREERAMDISK(APPLET_NOEXEC(freeramdisk, freeramdisk, BB_DIR_SBIN, BB_SUID_DROP, freeramdisk)) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 39 | |
| 40 | //kbuild:lib-$(CONFIG_FDFLUSH) += freeramdisk.o |
| 41 | //kbuild:lib-$(CONFIG_FREERAMDISK) += freeramdisk.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 42 | |
| 43 | //usage:#define freeramdisk_trivial_usage |
| 44 | //usage: "DEVICE" |
| 45 | //usage:#define freeramdisk_full_usage "\n\n" |
| 46 | //usage: "Free all memory used by the specified ramdisk" |
| 47 | //usage: |
| 48 | //usage:#define freeramdisk_example_usage |
| 49 | //usage: "$ freeramdisk /dev/ram2\n" |
| 50 | //usage: |
| 51 | //usage:#define fdflush_trivial_usage |
| 52 | //usage: "DEVICE" |
| 53 | //usage:#define fdflush_full_usage "\n\n" |
| 54 | //usage: "Force floppy disk drive to detect disk change" |
| 55 | |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 56 | #include <sys/mount.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 57 | #include "libbb.h" |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 58 | |
Bernhard Reutner-Fischer | 2523da2 | 2006-03-18 23:02:45 +0000 | [diff] [blame] | 59 | /* From <linux/fd.h> */ |
| 60 | #define FDFLUSH _IO(2,0x4b) |
| 61 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 62 | int freeramdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 63 | int freeramdisk_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 64 | { |
Glenn L McGrath | 8c6887c | 2003-05-08 13:09:28 +0000 | [diff] [blame] | 65 | int fd; |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 66 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 67 | fd = xopen(single_argv(argv), O_RDWR); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 68 | |
Rob Landley | 0b22c1c | 2006-03-14 02:40:51 +0000 | [diff] [blame] | 69 | // Act like freeramdisk, fdflush, or both depending on configuration. |
Denys Vlasenko | 74c05f5 | 2017-08-04 17:36:16 +0200 | [diff] [blame] | 70 | ioctl_or_perror_and_die(fd, |
| 71 | ((ENABLE_FREERAMDISK && applet_name[1] == 'r') || !ENABLE_FDFLUSH) |
| 72 | ? BLKFLSBUF |
| 73 | : FDFLUSH, |
| 74 | NULL, "%s", argv[1] |
| 75 | ); |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 76 | |
| 77 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
| 78 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 79 | return EXIT_SUCCESS; |
Erik Andersen | 6437170 | 2000-03-04 22:23:27 +0000 | [diff] [blame] | 80 | } |