blob: 6e2ff0a543597a8f29edfe51eb325b1f61bd9fab [file] [log] [blame]
Denys Vlasenkob9512fa2017-04-11 11:53:05 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
6 */
Denys Vlasenkob9512fa2017-04-11 11:53:05 +02007//config:config FSFREEZE
Denys Vlasenkob097a842018-12-28 03:20:17 +01008//config: bool "fsfreeze (3.5 kb)"
Denys Vlasenkob9512fa2017-04-11 11:53:05 +02009//config: default y
Denys Vlasenkob9512fa2017-04-11 11:53:05 +020010//config: select LONG_OPTS
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: Halt new accesses and flush writes on a mounted filesystem.
Denys Vlasenkob9512fa2017-04-11 11:53:05 +020013
Denys Vlasenko9f598492017-08-05 01:29:12 +020014//applet:IF_FSFREEZE(APPLET_NOEXEC(fsfreeze, fsfreeze, BB_DIR_USR_SBIN, BB_SUID_DROP, fsfreeze))
Denys Vlasenkob9512fa2017-04-11 11:53:05 +020015
16//kbuild:lib-$(CONFIG_FSFREEZE) += fsfreeze.o
17
18//usage:#define fsfreeze_trivial_usage
19//usage: "--[un]freeze MOUNTPOINT"
20//usage:#define fsfreeze_full_usage "\n\n"
21//usage: "Flush and halt writes to MOUNTPOINT"
22
23#include "libbb.h"
24#include <linux/fs.h>
25
26#ifndef FIFREEZE
27# define FIFREEZE _IOWR('X', 119, int)
28# define FITHAW _IOWR('X', 120, int)
29#endif
30
31int fsfreeze_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32int fsfreeze_main(int argc UNUSED_PARAM, char **argv)
33{
34 unsigned opts;
35 int fd;
36
Denys Vlasenkob9512fa2017-04-11 11:53:05 +020037 /* exactly one non-option arg: the mountpoint */
38 /* one of opts is required */
39 /* opts are mutually exclusive */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020040 opts = getopt32long(argv, "^"
41 "" /* no opts */
42 "\0" "=1:""\xff:\xfe:""\xff--\xfe:\xfe--\xff",
Denys Vlasenko036585a2017-08-08 16:38:18 +020043 "freeze\0" No_argument "\xff"
44 "unfreeze\0" No_argument "\xfe"
45 );
Denys Vlasenkob9512fa2017-04-11 11:53:05 +020046
47 fd = xopen(argv[optind], O_RDONLY);
48 /* Works with NULL arg on linux-4.8.0 */
49 xioctl(fd, (opts & 1) ? FIFREEZE : FITHAW, NULL);
50
51 return EXIT_SUCCESS;
52}