]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/fsfreeze.c
401ab5c980c3f9078fdb56b8fff78db7381d7665
[thirdparty/util-linux.git] / sys-utils / fsfreeze.c
1 /*
2 * fsfreeze.c -- Filesystem freeze/unfreeze IO for Linux
3 *
4 * Copyright (C) 2010 Hajime Taira <htaira@redhat.com>
5 * Masatake Yamato <yamato@redhat.com>
6 *
7 * This program is free software. You can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation: either version 1 or
10 * (at your option) any later version.
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <linux/fs.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <getopt.h>
23
24 #include "c.h"
25 #include "blkdev.h"
26 #include "nls.h"
27 #include "closestream.h"
28 #include "optutils.h"
29
30 enum fs_operation {
31 NOOP,
32 FREEZE,
33 UNFREEZE
34 };
35
36 static void __attribute__((__noreturn__)) usage(void)
37 {
38 FILE *out = stdout;
39 fputs(USAGE_HEADER, out);
40 fprintf(out,
41 _(" %s [options] <mountpoint>\n"), program_invocation_short_name);
42
43 fputs(USAGE_SEPARATOR, out);
44 fputs(_("Suspend access to a filesystem.\n"), out);
45
46 fputs(USAGE_OPTIONS, out);
47 fputs(_(" -f, --freeze freeze the filesystem\n"), out);
48 fputs(_(" -u, --unfreeze unfreeze the filesystem\n"), out);
49 fputs(USAGE_SEPARATOR, out);
50 printf(USAGE_HELP_OPTIONS(19));
51 printf(USAGE_MAN_TAIL("fsfreeze(8)"));
52
53 exit(EXIT_SUCCESS);
54 }
55
56 int main(int argc, char **argv)
57 {
58 int fd = -1, c;
59 int action = NOOP, rc = EXIT_FAILURE;
60 char *path;
61 struct stat sb;
62
63 static const struct option longopts[] = {
64 { "help", no_argument, NULL, 'h' },
65 { "freeze", no_argument, NULL, 'f' },
66 { "unfreeze", no_argument, NULL, 'u' },
67 { "version", no_argument, NULL, 'V' },
68 { NULL, 0, NULL, 0 }
69 };
70
71 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
72 { 'f','u' }, /* freeze, unfreeze */
73 { 0 }
74 };
75 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
76
77 setlocale(LC_ALL, "");
78 bindtextdomain(PACKAGE, LOCALEDIR);
79 textdomain(PACKAGE);
80 atexit(close_stdout);
81
82 while ((c = getopt_long(argc, argv, "hfuV", longopts, NULL)) != -1) {
83
84 err_exclusive_options(c, longopts, excl, excl_st);
85
86 switch(c) {
87 case 'h':
88 usage();
89 break;
90 case 'f':
91 action = FREEZE;
92 break;
93 case 'u':
94 action = UNFREEZE;
95 break;
96 case 'V':
97 printf(UTIL_LINUX_VERSION);
98 exit(EXIT_SUCCESS);
99 default:
100 errtryhelp(EXIT_FAILURE);
101 }
102 }
103
104 if (action == NOOP)
105 errx(EXIT_FAILURE, _("neither --freeze or --unfreeze specified"));
106 if (optind == argc)
107 errx(EXIT_FAILURE, _("no filename specified"));
108 path = argv[optind++];
109
110 if (optind != argc) {
111 warnx(_("unexpected number of arguments"));
112 errtryhelp(EXIT_FAILURE);
113 }
114
115 fd = open(path, O_RDONLY);
116 if (fd < 0)
117 err(EXIT_FAILURE, _("cannot open %s"), path);
118
119 if (fstat(fd, &sb) == -1) {
120 warn(_("stat of %s failed"), path);
121 goto done;
122 }
123
124 if (!S_ISDIR(sb.st_mode)) {
125 warnx(_("%s: is not a directory"), path);
126 goto done;
127 }
128
129 switch (action) {
130 case FREEZE:
131 if (ioctl(fd, FIFREEZE, 0)) {
132 warn(_("%s: freeze failed"), path);
133 goto done;
134 }
135 break;
136 case UNFREEZE:
137 if (ioctl(fd, FITHAW, 0)) {
138 warn(_("%s: unfreeze failed"), path);
139 goto done;
140 }
141 break;
142 default:
143 abort();
144 }
145
146 rc = EXIT_SUCCESS;
147 done:
148 if (fd >= 0)
149 close(fd);
150 return rc;
151 }
152