mksnap_ffs.c revision 111725
1111716Smckusick/*
2111716Smckusick * Copyright (c) 2003 Networks Associates Technology, Inc.
3111716Smckusick * All rights reserved.
4111716Smckusick *
5111716Smckusick * This software was developed for the FreeBSD Project by Marshall
6111716Smckusick * Kirk McKusick and Network Associates Laboratories, the Security
7111716Smckusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8111716Smckusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9111716Smckusick * research program.
10111716Smckusick *
11111716Smckusick * Redistribution and use in source and binary forms, with or without
12111716Smckusick * modification, are permitted provided that the following conditions
13111716Smckusick * are met:
14111716Smckusick * 1. Redistributions of source code must retain the above copyright
15111716Smckusick *    notice, this list of conditions and the following disclaimer.
16111716Smckusick * 2. Redistributions in binary form must reproduce the above copyright
17111716Smckusick *    notice, this list of conditions and the following disclaimer in the
18111716Smckusick *    documentation and/or other materials provided with the distribution.
19111716Smckusick * 3. The names of the authors may not be used to endorse or promote
20111716Smckusick *    products derived from this software without specific prior written
21111716Smckusick *    permission.
22111716Smckusick *
23111716Smckusick * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24111716Smckusick * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25111716Smckusick * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26111716Smckusick * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27111716Smckusick * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28111716Smckusick * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29111716Smckusick * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30111716Smckusick * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31111716Smckusick * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32111716Smckusick * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33111716Smckusick * SUCH DAMAGE.
34111716Smckusick *
35111716Smckusick * $FreeBSD: head/sbin/mksnap_ffs/mksnap_ffs.c 111725 2003-03-02 08:07:57Z mckusick $
36111716Smckusick */
37111716Smckusick
38111716Smckusick#include <sys/param.h>
39111716Smckusick#include <sys/mount.h>
40111716Smckusick#include <sys/stat.h>
41111716Smckusick#include <ufs/ufs/ufsmount.h>
42111716Smckusick#include <err.h>
43111716Smckusick#include <errno.h>
44111725Smckusick#include <fcntl.h>
45111716Smckusick#include <grp.h>
46111716Smckusick#include <stdio.h>
47111716Smckusick#include <stdlib.h>
48111716Smckusick#include <string.h>
49111716Smckusick#include <sysexits.h>
50111716Smckusick#include <unistd.h>
51111716Smckusick
52111716Smckusickvoid
53111716Smckusickusage()
54111716Smckusick{
55111716Smckusick
56111716Smckusick	fprintf(stderr, "usage: mksnap_ffs mountpoint file\n");
57111716Smckusick	exit(EX_USAGE);
58111716Smckusick}
59111716Smckusick
60111716Smckusickint
61111716Smckusickmain(int argc, char **argv)
62111716Smckusick{
63111716Smckusick	const char *dir;
64111716Smckusick	struct ufs_args args;
65111716Smckusick	struct group *grp;
66111725Smckusick	struct stat stbuf;
67111725Smckusick	int fd;
68111716Smckusick
69111716Smckusick	if (argc != 3)
70111716Smckusick		usage();
71111716Smckusick
72111716Smckusick	dir = argv[1];
73111716Smckusick	args.fspec = argv[2];
74111716Smckusick
75111716Smckusick	if ((grp = getgrnam("operator")) == NULL)
76111716Smckusick		errx(1, "Cannot retrieve operator gid");
77111716Smckusick	if (mount("ffs", dir, MNT_UPDATE | MNT_SNAPSHOT, &args) < 0)
78111716Smckusick		err(1, "Cannot create %s", args.fspec);
79111725Smckusick	if ((fd = open(args.fspec, O_RDONLY)) < 0)
80111725Smckusick		err(1, "Cannot open %s", args.fspec);
81111725Smckusick	if (fstat(fd, &stbuf) != 0)
82111725Smckusick		err(1, "Cannot stat %s", args.fspec);
83111725Smckusick	if ((stbuf.st_flags & SF_SNAPSHOT) == 0)
84111725Smckusick		errx(1, "File %s is not a snapshot", args.fspec);
85111725Smckusick	if (fchown(fd, -1, grp->gr_gid) != 0)
86111716Smckusick		err(1, "Cannot chown %s", args.fspec);
87111725Smckusick	if (fchmod(fd, S_IRUSR | S_IRGRP) != 0)
88111716Smckusick		err(1, "Cannot chmod %s", args.fspec);
89111716Smckusick
90111716Smckusick	exit(EXIT_SUCCESS);
91111716Smckusick}
92