mksnap_ffs.c revision 121706
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 121706 2003-10-29 21:21:09Z trhodes $ 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 52121706Strhodesvoid usage(void); 53121706Strhodes 54111716Smckusickvoid 55111716Smckusickusage() 56111716Smckusick{ 57111716Smckusick 58121706Strhodes fprintf(stderr, "usage: mksnap_ffs mountpoint snapshot_name\n"); 59111716Smckusick exit(EX_USAGE); 60111716Smckusick} 61111716Smckusick 62111716Smckusickint 63111716Smckusickmain(int argc, char **argv) 64111716Smckusick{ 65111716Smckusick const char *dir; 66111716Smckusick struct ufs_args args; 67111716Smckusick struct group *grp; 68111725Smckusick struct stat stbuf; 69111725Smckusick int fd; 70111716Smckusick 71111716Smckusick if (argc != 3) 72111716Smckusick usage(); 73111716Smckusick 74111716Smckusick dir = argv[1]; 75111716Smckusick args.fspec = argv[2]; 76111716Smckusick 77111716Smckusick if ((grp = getgrnam("operator")) == NULL) 78111716Smckusick errx(1, "Cannot retrieve operator gid"); 79111716Smckusick if (mount("ffs", dir, MNT_UPDATE | MNT_SNAPSHOT, &args) < 0) 80111716Smckusick err(1, "Cannot create %s", args.fspec); 81111725Smckusick if ((fd = open(args.fspec, O_RDONLY)) < 0) 82111725Smckusick err(1, "Cannot open %s", args.fspec); 83111725Smckusick if (fstat(fd, &stbuf) != 0) 84111725Smckusick err(1, "Cannot stat %s", args.fspec); 85111725Smckusick if ((stbuf.st_flags & SF_SNAPSHOT) == 0) 86111725Smckusick errx(1, "File %s is not a snapshot", args.fspec); 87111725Smckusick if (fchown(fd, -1, grp->gr_gid) != 0) 88111716Smckusick err(1, "Cannot chown %s", args.fspec); 89111725Smckusick if (fchmod(fd, S_IRUSR | S_IRGRP) != 0) 90111716Smckusick err(1, "Cannot chmod %s", args.fspec); 91111716Smckusick 92111716Smckusick exit(EXIT_SUCCESS); 93111716Smckusick} 94