blob: 309169d2533766ee4df0feceae1d98ea4adf3fc9 [file] [log] [blame]
Erik Andersen64371702000-03-04 22:23:27 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landley0b22c1c2006-03-14 02:40:51 +00003 * freeramdisk and fdflush implementations for busybox
Erik Andersen64371702000-03-04 22:23:27 +00004 *
5 * Copyright (C) 2000 and written by Emanuele Caratti <wiz@iol.it>
Eric Andersencb81e642003-07-14 21:21:08 +00006 * Adjusted a bit by Erik Andersen <andersen@codepoet.org>
Rob Landley0b22c1c2006-03-14 02:40:51 +00007 * Unified with fdflush by Tito Ragusa <farmatito@tiscali.it>
Erik Andersen64371702000-03-04 22:23:27 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Erik Andersen64371702000-03-04 22:23:27 +000010 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config:config FDFLUSH
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "fdflush (1.3 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010014//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//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.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010022//config:
23//config:config FREERAMDISK
Denys Vlasenkob097a842018-12-28 03:20:17 +010024//config: bool "freeramdisk (1.3 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010025//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010026//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//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.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010033
Denys Vlasenkoec98e3a2017-08-07 23:17:14 +020034// APPLET_ODDNAME:name main location suid_type help
35//applet:IF_FDFLUSH( APPLET_ODDNAME(fdflush, freeramdisk, BB_DIR_BIN, BB_SUID_DROP, fdflush ))
36//applet:IF_FREERAMDISK(APPLET_NOEXEC(freeramdisk, freeramdisk, BB_DIR_SBIN, BB_SUID_DROP, freeramdisk))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010037
38//kbuild:lib-$(CONFIG_FDFLUSH) += freeramdisk.o
39//kbuild:lib-$(CONFIG_FREERAMDISK) += freeramdisk.o
Pere Orga5bc8c002011-04-11 03:29:49 +020040
41//usage:#define freeramdisk_trivial_usage
42//usage: "DEVICE"
43//usage:#define freeramdisk_full_usage "\n\n"
44//usage: "Free all memory used by the specified ramdisk"
45//usage:
46//usage:#define freeramdisk_example_usage
47//usage: "$ freeramdisk /dev/ram2\n"
48//usage:
49//usage:#define fdflush_trivial_usage
50//usage: "DEVICE"
51//usage:#define fdflush_full_usage "\n\n"
52//usage: "Force floppy disk drive to detect disk change"
53
Denys Vlasenkoda49f582009-07-08 02:58:38 +020054#include <sys/mount.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000055#include "libbb.h"
Erik Andersen64371702000-03-04 22:23:27 +000056
Bernhard Reutner-Fischer2523da22006-03-18 23:02:45 +000057/* From <linux/fd.h> */
58#define FDFLUSH _IO(2,0x4b)
59
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int freeramdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010061int freeramdisk_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen64371702000-03-04 22:23:27 +000062{
Glenn L McGrath8c6887c2003-05-08 13:09:28 +000063 int fd;
Erik Andersen64371702000-03-04 22:23:27 +000064
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010065 fd = xopen(single_argv(argv), O_RDWR);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000066
Rob Landley0b22c1c2006-03-14 02:40:51 +000067 // Act like freeramdisk, fdflush, or both depending on configuration.
Denys Vlasenko74c05f52017-08-04 17:36:16 +020068 ioctl_or_perror_and_die(fd,
69 ((ENABLE_FREERAMDISK && applet_name[1] == 'r') || !ENABLE_FDFLUSH)
70 ? BLKFLSBUF
71 : FDFLUSH,
72 NULL, "%s", argv[1]
73 );
Rob Landley658d2cf2005-09-08 03:11:58 +000074
75 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
76
Matt Kraai3e856ce2000-12-01 02:55:13 +000077 return EXIT_SUCCESS;
Erik Andersen64371702000-03-04 22:23:27 +000078}