2008-03-09 19:14:36 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2008-03-09 19:14:36 +00:00
|
|
|
* Copyright (c) 2005-2008 Poul-Henning Kamp
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
|
|
|
|
#include "fifolog.h"
|
|
|
|
#include "libfifolog.h"
|
|
|
|
|
|
|
|
const char *
|
2011-02-28 09:11:46 +00:00
|
|
|
fifolog_create(const char *fn, off_t size, ssize_t recsize)
|
2008-03-09 19:14:36 +00:00
|
|
|
{
|
|
|
|
int i, fd;
|
2011-03-01 08:52:10 +00:00
|
|
|
ssize_t u;
|
2016-11-22 22:10:58 +00:00
|
|
|
u_int uu;
|
2008-03-09 19:14:36 +00:00
|
|
|
off_t ms;
|
|
|
|
struct stat st;
|
|
|
|
char *buf;
|
|
|
|
int created;
|
|
|
|
|
|
|
|
fd = open(fn, O_WRONLY | O_TRUNC | O_EXCL | O_CREAT, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
created = 0;
|
|
|
|
fd = open(fn, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return ("Could not open");
|
|
|
|
} else
|
|
|
|
created = 1;
|
|
|
|
|
|
|
|
/* Default sectorsize is 512 */
|
|
|
|
if (recsize == 0)
|
|
|
|
recsize = 512;
|
|
|
|
|
|
|
|
/* See what we got... */
|
|
|
|
i = fstat(fd, &st);
|
|
|
|
assert(i == 0);
|
|
|
|
if (!S_ISBLK(st.st_mode) &&
|
|
|
|
!S_ISCHR(st.st_mode) &&
|
|
|
|
!S_ISREG(st.st_mode)) {
|
|
|
|
assert(!close (fd));
|
|
|
|
return ("Wrong file type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!created && S_ISREG(st.st_mode)) {
|
|
|
|
assert(!close (fd));
|
|
|
|
return ("Wrong file type");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For raw disk with larger sectors: use 1 sector */
|
2016-11-22 22:10:58 +00:00
|
|
|
i = ioctl(fd, DIOCGSECTORSIZE, &uu);
|
|
|
|
u = uu;
|
2008-03-09 19:14:36 +00:00
|
|
|
if (i == 0 && (u > recsize || (recsize % u) != 0))
|
|
|
|
recsize = u;
|
|
|
|
|
|
|
|
/* If no configured size, or too large for disk, use device size */
|
|
|
|
i = ioctl(fd, DIOCGMEDIASIZE, &ms);
|
|
|
|
if (i == 0 && (size == 0 || size > ms))
|
|
|
|
size = ms;
|
|
|
|
|
|
|
|
if (size == 0 && S_ISREG(st.st_mode))
|
|
|
|
size = st.st_size;
|
|
|
|
|
2011-02-25 09:40:17 +00:00
|
|
|
if (size == 0)
|
2008-03-09 19:14:36 +00:00
|
|
|
size = recsize * (off_t)(24*60*60);
|
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode) && ftruncate(fd, size) < 0)
|
|
|
|
return ("Could not ftrunc");
|
|
|
|
|
2016-09-14 16:34:52 +00:00
|
|
|
buf = calloc(1, recsize);
|
2008-03-09 19:14:36 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return ("Could not malloc");
|
|
|
|
|
|
|
|
strcpy(buf, FIFOLOG_FMT_MAGIC); /*lint !e64 */
|
|
|
|
be32enc(buf + FIFOLOG_OFF_BS, recsize);
|
2011-02-28 09:11:46 +00:00
|
|
|
if (recsize != pwrite(fd, buf, recsize, 0)) {
|
2008-03-09 19:14:36 +00:00
|
|
|
i = errno;
|
|
|
|
free(buf);
|
|
|
|
errno = i;
|
|
|
|
return ("Could not write first sector");
|
|
|
|
}
|
|
|
|
memset(buf, 0, recsize);
|
|
|
|
if ((int)recsize != pwrite(fd, buf, recsize, recsize)) {
|
|
|
|
i = errno;
|
|
|
|
free(buf);
|
|
|
|
errno = i;
|
|
|
|
return ("Could not write second sector");
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
assert(0 == close(fd));
|
|
|
|
return (NULL);
|
|
|
|
}
|