master xplshn/aruu / cmd / linux / fsfreeze.c
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <sys/ioctl.h>
 4#include <sys/stat.h>
 5#include <sys/types.h>
 6
 7#include <fcntl.h>
 8#include <stdio.h>
 9#include <stdlib.h>
10#include <unistd.h>
11
12#include "util.h"
13
14#define FIFREEZE _IOWR('X', 119, int) /* Freeze */
15#define FITHAW   _IOWR('X', 120, int) /* Thaw */
16
17static void
18usage(void)
19{
20  eprintf("usage: %s (-f | -u) mountpoint\n", argv0);
21}
22
23// ?man fsfreeze: suspend access to a filesystem
24// ?man arguments: (-f | -u) mountpoint
25// ?man freeze or unfreeze a filesystem to allow safe backups
26int
27main(int argc, char *argv[])
28{
29  int  fflag = 0;
30  int  uflag = 0;
31  long p     = 1;
32  int  fd;
33
34  ARGBEGIN
35  {
36    // ?man -f: force the operation
37    case 'f':
38      fflag = 1;
39      break;
40    // ?man -u: unbuffered output
41    case 'u':
42      uflag = 1;
43      break;
44    default:
45      usage();
46  }
47  ARGEND;
48
49  if (argc != 1)
50    usage();
51
52  if ((fflag ^ uflag) == 0)
53    usage();
54
55  fd = open(argv[0], O_RDONLY);
56  if (fd < 0)
57    eprintf("open: %s:", argv[0]);
58  if (ioctl(fd, fflag == 1 ? FIFREEZE : FITHAW, &p) < 0)
59    eprintf("%s %s:", fflag == 1 ? "FIFREEZE" : "FITHAW", argv[0]);
60  close(fd);
61  return 0;
62}