]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/fsfreeze.c
sys-utils: verify writing to streams was successful
[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 "blkdev.h"
25 #include "nls.h"
26 #include "closestream.h"
27 #include "c.h"
28
29 static int freeze_f(int fd)
30 {
31 return ioctl(fd, FIFREEZE, 0);
32 }
33
34 static int unfreeze_f(int fd)
35 {
36 return ioctl(fd, FITHAW, 0);
37 }
38
39 static void __attribute__((__noreturn__)) usage(FILE *out)
40 {
41 fputs(_("\nUsage:\n"), out);
42 fprintf(out,
43 _(" %s [options] <mount point>\n"), program_invocation_short_name);
44
45 fputs(_("\nOptions:\n"), out);
46 fputs(_(" -h, --help this help\n"
47 " -f, --freeze freeze the filesystem\n"
48 " -u, --unfreeze unfreeze the filesystem\n"), out);
49
50 fputs(_("\nFor more information see fsfreeze(8).\n"), out);
51
52 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
53 }
54
55 int main(int argc, char **argv)
56 {
57 int fd = -1, c;
58 int freeze = -1, rc = EXIT_FAILURE;
59 char *path;
60 struct stat sb;
61
62 static const struct option longopts[] = {
63 { "help", 0, 0, 'h' },
64 { "freeze", 0, 0, 'f' },
65 { "unfreeze", 0, 0, 'u' },
66 { NULL, 0, 0, 0 }
67 };
68
69 setlocale(LC_ALL, "");
70 bindtextdomain(PACKAGE, LOCALEDIR);
71 textdomain(PACKAGE);
72 atexit(close_stdout);
73
74 while ((c = getopt_long(argc, argv, "hfu", longopts, NULL)) != -1) {
75 switch(c) {
76 case 'h':
77 usage(stdout);
78 break;
79 case 'f':
80 freeze = TRUE;
81 break;
82 case 'u':
83 freeze = FALSE;
84 break;
85 default:
86 usage(stderr);
87 break;
88 }
89 }
90
91 if (freeze == -1)
92 errx(EXIT_FAILURE, _("no action specified"));
93 if (optind == argc)
94 errx(EXIT_FAILURE, _("no filename specified"));
95 path = argv[optind++];
96
97 if (optind != argc) {
98 warnx(_("unexpected number of arguments"));
99 usage(stderr);
100 }
101
102 fd = open(path, O_RDONLY);
103 if (fd < 0)
104 err(EXIT_FAILURE, _("%s: open failed"), path);
105
106 if (fstat(fd, &sb) == -1) {
107 warn(_("%s: fstat failed"), path);
108 goto done;
109 }
110
111 if (!S_ISDIR(sb.st_mode)) {
112 warnx(_("%s: is not a directory"), path);
113 goto done;
114 }
115
116 if (freeze) {
117 if (freeze_f(fd)) {
118 warn(_("%s: freeze failed"), path);
119 goto done;
120 }
121 } else {
122 if (unfreeze_f(fd)) {
123 warn(_("%s: unfreeze failed"), path);
124 goto done;
125 }
126 }
127
128 rc = EXIT_SUCCESS;
129 done:
130 if (fd >= 0)
131 close(fd);
132 return rc;
133 }
134