1176998Sphk/*- 2176998Sphk * Copyright (c) 2005-2008 Poul-Henning Kamp 3176998Sphk * All rights reserved. 4176998Sphk * 5176998Sphk * Redistribution and use in source and binary forms, with or without 6176998Sphk * modification, are permitted provided that the following conditions 7176998Sphk * are met: 8176998Sphk * 1. Redistributions of source code must retain the above copyright 9176998Sphk * notice, this list of conditions and the following disclaimer. 10176998Sphk * 2. Redistributions in binary form must reproduce the above copyright 11176998Sphk * notice, this list of conditions and the following disclaimer in the 12176998Sphk * documentation and/or other materials provided with the distribution. 13176998Sphk * 14176998Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15176998Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16176998Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17176998Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18176998Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19176998Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20176998Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21176998Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22176998Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23176998Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24176998Sphk * SUCH DAMAGE. 25176998Sphk * 26176998Sphk * $FreeBSD: stable/10/usr.sbin/fifolog/lib/fifolog_create.c 306910 2016-10-09 19:58:27Z pfg $ 27176998Sphk */ 28176998Sphk 29176998Sphk#include <assert.h> 30176998Sphk#include <errno.h> 31176998Sphk#include <stdio.h> 32176998Sphk#include <string.h> 33176998Sphk#include <unistd.h> 34176998Sphk#include <fcntl.h> 35176998Sphk#include <stdlib.h> 36176998Sphk#include <sys/endian.h> 37176998Sphk#include <sys/stat.h> 38176998Sphk#include <sys/disk.h> 39176998Sphk 40176998Sphk#include "fifolog.h" 41176998Sphk#include "libfifolog.h" 42176998Sphk 43176998Sphkconst char * 44219095Sphkfifolog_create(const char *fn, off_t size, ssize_t recsize) 45176998Sphk{ 46176998Sphk int i, fd; 47219123Sphk ssize_t u; 48176998Sphk off_t ms; 49176998Sphk struct stat st; 50176998Sphk char *buf; 51176998Sphk int created; 52176998Sphk 53176998Sphk fd = open(fn, O_WRONLY | O_TRUNC | O_EXCL | O_CREAT, 0644); 54176998Sphk if (fd < 0) { 55176998Sphk created = 0; 56176998Sphk fd = open(fn, O_WRONLY); 57176998Sphk if (fd < 0) 58176998Sphk return ("Could not open"); 59176998Sphk } else 60176998Sphk created = 1; 61176998Sphk 62176998Sphk /* Default sectorsize is 512 */ 63176998Sphk if (recsize == 0) 64176998Sphk recsize = 512; 65176998Sphk 66176998Sphk /* See what we got... */ 67176998Sphk i = fstat(fd, &st); 68176998Sphk assert(i == 0); 69176998Sphk if (!S_ISBLK(st.st_mode) && 70176998Sphk !S_ISCHR(st.st_mode) && 71176998Sphk !S_ISREG(st.st_mode)) { 72176998Sphk assert(!close (fd)); 73176998Sphk return ("Wrong file type"); 74176998Sphk } 75176998Sphk 76176998Sphk if(!created && S_ISREG(st.st_mode)) { 77176998Sphk assert(!close (fd)); 78176998Sphk return ("Wrong file type"); 79176998Sphk } 80176998Sphk 81176998Sphk /* For raw disk with larger sectors: use 1 sector */ 82176998Sphk i = ioctl(fd, DIOCGSECTORSIZE, &u); 83176998Sphk if (i == 0 && (u > recsize || (recsize % u) != 0)) 84176998Sphk recsize = u; 85176998Sphk 86176998Sphk /* If no configured size, or too large for disk, use device size */ 87176998Sphk i = ioctl(fd, DIOCGMEDIASIZE, &ms); 88176998Sphk if (i == 0 && (size == 0 || size > ms)) 89176998Sphk size = ms; 90176998Sphk 91176998Sphk if (size == 0 && S_ISREG(st.st_mode)) 92176998Sphk size = st.st_size; 93176998Sphk 94219027Sphk if (size == 0) 95176998Sphk size = recsize * (off_t)(24*60*60); 96176998Sphk 97176998Sphk if (S_ISREG(st.st_mode) && ftruncate(fd, size) < 0) 98176998Sphk return ("Could not ftrunc"); 99176998Sphk 100306910Spfg buf = calloc(1, recsize); 101176998Sphk if (buf == NULL) 102176998Sphk return ("Could not malloc"); 103176998Sphk 104176998Sphk strcpy(buf, FIFOLOG_FMT_MAGIC); /*lint !e64 */ 105176998Sphk be32enc(buf + FIFOLOG_OFF_BS, recsize); 106219095Sphk if (recsize != pwrite(fd, buf, recsize, 0)) { 107176998Sphk i = errno; 108176998Sphk free(buf); 109176998Sphk errno = i; 110176998Sphk return ("Could not write first sector"); 111176998Sphk } 112176998Sphk memset(buf, 0, recsize); 113176998Sphk if ((int)recsize != pwrite(fd, buf, recsize, recsize)) { 114176998Sphk i = errno; 115176998Sphk free(buf); 116176998Sphk errno = i; 117176998Sphk return ("Could not write second sector"); 118176998Sphk } 119176998Sphk free(buf); 120176998Sphk assert(0 == close(fd)); 121176998Sphk return (NULL); 122176998Sphk} 123