blob: 1f010c721896a5ebca83af0f72d4b08134d6df1f [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 *
Rob Landley0b22c1c2006-03-14 02:40:51 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen64371702000-03-04 22:23:27 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Erik Andersen64371702000-03-04 22:23:27 +000014#include <sys/types.h>
Erik Andersen64371702000-03-04 22:23:27 +000015#include <fcntl.h>
16#include <sys/ioctl.h>
Eric Andersened3ef502001-01-27 08:24:39 +000017#include <stdlib.h>
Eric Andersen45638df2003-09-15 08:12:53 +000018#include <unistd.h>
Eric Andersen3570a342000-09-25 21:45:58 +000019#include "busybox.h"
Erik Andersen64371702000-03-04 22:23:27 +000020
Bernhard Reutner-Fischer2523da22006-03-18 23:02:45 +000021/* From linux/fs.h */
22#define BLKFLSBUF _IO(0x12,97)
23/* From <linux/fd.h> */
24#define FDFLUSH _IO(2,0x4b)
25
Rob Landleyd221d212006-03-14 20:06:44 +000026int freeramdisk_main(int argc, char **argv)
Erik Andersen64371702000-03-04 22:23:27 +000027{
Eric Andersen77831c12003-03-07 18:09:06 +000028 int result;
Glenn L McGrath8c6887c2003-05-08 13:09:28 +000029 int fd;
Erik Andersen64371702000-03-04 22:23:27 +000030
Rob Landley0b22c1c2006-03-14 02:40:51 +000031 if (argc != 2) bb_show_usage();
Erik Andersen64371702000-03-04 22:23:27 +000032
Glenn L McGrath8c6887c2003-05-08 13:09:28 +000033 fd = bb_xopen(argv[1], O_RDWR);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000034
Rob Landley0b22c1c2006-03-14 02:40:51 +000035 // Act like freeramdisk, fdflush, or both depending on configuration.
Bernhard Reutner-Fischer66753a32006-03-18 15:59:29 +000036 result = ioctl(fd, (ENABLE_FREERAMDISK && bb_applet_name[1]=='r')
Bernhard Reutner-Fischer2523da22006-03-18 23:02:45 +000037 || !ENABLE_FDFLUSH ? BLKFLSBUF : FDFLUSH);
Rob Landley658d2cf2005-09-08 03:11:58 +000038
39 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
40
Rob Landley0b22c1c2006-03-14 02:40:51 +000041 if (result) bb_perror_msg_and_die("%s", argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +000042 return EXIT_SUCCESS;
Erik Andersen64371702000-03-04 22:23:27 +000043}