~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to sys-utils/fsfreeze.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2011-05-11 08:38:31 UTC
  • mfrom: (1.3.10 upstream)
  • mto: (1.6.3 upstream) (4.5.5 sid)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: james.westby@ubuntu.com-20110511083831-tty7wnezw55fmrn4
ImportĀ upstreamĀ versionĀ 2.19.1

Show diffs side-by-side

added added

removed removed

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