xref: /freebsd/sbin/mksnap_ffs/mksnap_ffs.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
1 /*
2  * Copyright (c) 2003 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <ufs/ufs/ufsmount.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <grp.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sysexits.h>
50 #include <unistd.h>
51 
52 void
53 usage()
54 {
55 
56 	fprintf(stderr, "usage: mksnap_ffs mountpoint file\n");
57 	exit(EX_USAGE);
58 }
59 
60 int
61 main(int argc, char **argv)
62 {
63 	const char *dir;
64 	struct ufs_args args;
65 	struct group *grp;
66 	struct stat stbuf;
67 	int fd;
68 
69 	if (argc != 3)
70 		usage();
71 
72 	dir = argv[1];
73 	args.fspec = argv[2];
74 
75 	if ((grp = getgrnam("operator")) == NULL)
76 		errx(1, "Cannot retrieve operator gid");
77 	if (mount("ffs", dir, MNT_UPDATE | MNT_SNAPSHOT, &args) < 0)
78 		err(1, "Cannot create %s", args.fspec);
79 	if ((fd = open(args.fspec, O_RDONLY)) < 0)
80 		err(1, "Cannot open %s", args.fspec);
81 	if (fstat(fd, &stbuf) != 0)
82 		err(1, "Cannot stat %s", args.fspec);
83 	if ((stbuf.st_flags & SF_SNAPSHOT) == 0)
84 		errx(1, "File %s is not a snapshot", args.fspec);
85 	if (fchown(fd, -1, grp->gr_gid) != 0)
86 		err(1, "Cannot chown %s", args.fspec);
87 	if (fchmod(fd, S_IRUSR | S_IRGRP) != 0)
88 		err(1, "Cannot chmod %s", args.fspec);
89 
90 	exit(EXIT_SUCCESS);
91 }
92