xref: /freebsd/usr.sbin/fifolog/lib/fifolog_create.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1662cb04cSPoul-Henning Kamp /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4662cb04cSPoul-Henning Kamp  * Copyright (c) 2005-2008 Poul-Henning Kamp
5662cb04cSPoul-Henning Kamp  * All rights reserved.
6662cb04cSPoul-Henning Kamp  *
7662cb04cSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
8662cb04cSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
9662cb04cSPoul-Henning Kamp  * are met:
10662cb04cSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
11662cb04cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
12662cb04cSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
13662cb04cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
14662cb04cSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
15662cb04cSPoul-Henning Kamp  *
16662cb04cSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17662cb04cSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18662cb04cSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19662cb04cSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20662cb04cSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21662cb04cSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22662cb04cSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23662cb04cSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24662cb04cSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25662cb04cSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26662cb04cSPoul-Henning Kamp  * SUCH DAMAGE.
27662cb04cSPoul-Henning Kamp  */
28662cb04cSPoul-Henning Kamp 
29662cb04cSPoul-Henning Kamp #include <assert.h>
30662cb04cSPoul-Henning Kamp #include <errno.h>
31662cb04cSPoul-Henning Kamp #include <stdio.h>
32662cb04cSPoul-Henning Kamp #include <string.h>
33662cb04cSPoul-Henning Kamp #include <unistd.h>
34662cb04cSPoul-Henning Kamp #include <fcntl.h>
35662cb04cSPoul-Henning Kamp #include <stdlib.h>
36662cb04cSPoul-Henning Kamp #include <sys/endian.h>
37662cb04cSPoul-Henning Kamp #include <sys/stat.h>
38662cb04cSPoul-Henning Kamp #include <sys/disk.h>
39662cb04cSPoul-Henning Kamp 
40662cb04cSPoul-Henning Kamp #include "fifolog.h"
41662cb04cSPoul-Henning Kamp #include "libfifolog.h"
42662cb04cSPoul-Henning Kamp 
43662cb04cSPoul-Henning Kamp const char *
fifolog_create(const char * fn,off_t size,ssize_t recsize)446e482ac5SPoul-Henning Kamp fifolog_create(const char *fn, off_t size, ssize_t recsize)
45662cb04cSPoul-Henning Kamp {
46662cb04cSPoul-Henning Kamp 	int i, fd;
47818bc415SPoul-Henning Kamp 	ssize_t u;
48e10312caSPoul-Henning Kamp 	u_int uu;
49662cb04cSPoul-Henning Kamp 	off_t ms;
50662cb04cSPoul-Henning Kamp 	struct stat st;
51662cb04cSPoul-Henning Kamp 	char *buf;
52662cb04cSPoul-Henning Kamp 	int created;
53662cb04cSPoul-Henning Kamp 
54662cb04cSPoul-Henning Kamp 	fd = open(fn, O_WRONLY | O_TRUNC | O_EXCL | O_CREAT, 0644);
55662cb04cSPoul-Henning Kamp 	if (fd < 0) {
56662cb04cSPoul-Henning Kamp 		created = 0;
57662cb04cSPoul-Henning Kamp 		fd = open(fn, O_WRONLY);
58662cb04cSPoul-Henning Kamp 		if (fd < 0)
59662cb04cSPoul-Henning Kamp 			return ("Could not open");
60662cb04cSPoul-Henning Kamp 	} else
61662cb04cSPoul-Henning Kamp 		created = 1;
62662cb04cSPoul-Henning Kamp 
63662cb04cSPoul-Henning Kamp 	/* Default sectorsize is 512 */
64662cb04cSPoul-Henning Kamp 	if (recsize == 0)
65662cb04cSPoul-Henning Kamp 		recsize = 512;
66662cb04cSPoul-Henning Kamp 
67662cb04cSPoul-Henning Kamp 	/* See what we got... */
68662cb04cSPoul-Henning Kamp 	i = fstat(fd, &st);
69662cb04cSPoul-Henning Kamp 	assert(i == 0);
70662cb04cSPoul-Henning Kamp 	if (!S_ISBLK(st.st_mode) &&
71662cb04cSPoul-Henning Kamp 	    !S_ISCHR(st.st_mode) &&
72662cb04cSPoul-Henning Kamp 	    !S_ISREG(st.st_mode)) {
73662cb04cSPoul-Henning Kamp 		assert(!close (fd));
74662cb04cSPoul-Henning Kamp 		return ("Wrong file type");
75662cb04cSPoul-Henning Kamp 	}
76662cb04cSPoul-Henning Kamp 
77662cb04cSPoul-Henning Kamp 	if(!created && S_ISREG(st.st_mode)) {
78662cb04cSPoul-Henning Kamp 		assert(!close (fd));
79662cb04cSPoul-Henning Kamp 		return ("Wrong file type");
80662cb04cSPoul-Henning Kamp 	}
81662cb04cSPoul-Henning Kamp 
82662cb04cSPoul-Henning Kamp 	/* For raw disk with larger sectors: use 1 sector */
83e10312caSPoul-Henning Kamp 	i = ioctl(fd, DIOCGSECTORSIZE, &uu);
84e10312caSPoul-Henning Kamp 	u = uu;
85662cb04cSPoul-Henning Kamp 	if (i == 0 && (u > recsize || (recsize % u) != 0))
86662cb04cSPoul-Henning Kamp 		recsize = u;
87662cb04cSPoul-Henning Kamp 
88662cb04cSPoul-Henning Kamp 	/* If no configured size, or too large for disk, use device size */
89662cb04cSPoul-Henning Kamp 	i = ioctl(fd, DIOCGMEDIASIZE, &ms);
90662cb04cSPoul-Henning Kamp 	if (i == 0 && (size == 0 || size > ms))
91662cb04cSPoul-Henning Kamp 		size = ms;
92662cb04cSPoul-Henning Kamp 
93662cb04cSPoul-Henning Kamp 	if (size == 0 && S_ISREG(st.st_mode))
94662cb04cSPoul-Henning Kamp 		size = st.st_size;
95662cb04cSPoul-Henning Kamp 
96662cb04cSPoul-Henning Kamp 	if (size == 0)
97662cb04cSPoul-Henning Kamp 		size = recsize * (off_t)(24*60*60);
98662cb04cSPoul-Henning Kamp 
99662cb04cSPoul-Henning Kamp 	if (S_ISREG(st.st_mode) && ftruncate(fd, size) < 0)
100662cb04cSPoul-Henning Kamp 		return ("Could not ftrunc");
101662cb04cSPoul-Henning Kamp 
10260e600c8SPedro F. Giffuni 	buf = calloc(1, recsize);
103662cb04cSPoul-Henning Kamp 	if (buf == NULL)
104662cb04cSPoul-Henning Kamp 		return ("Could not malloc");
105662cb04cSPoul-Henning Kamp 
106662cb04cSPoul-Henning Kamp 	strcpy(buf, FIFOLOG_FMT_MAGIC);		/*lint !e64 */
107662cb04cSPoul-Henning Kamp 	be32enc(buf + FIFOLOG_OFF_BS, recsize);
1086e482ac5SPoul-Henning Kamp 	if (recsize != pwrite(fd, buf, recsize, 0)) {
109662cb04cSPoul-Henning Kamp 		i = errno;
110662cb04cSPoul-Henning Kamp 		free(buf);
111662cb04cSPoul-Henning Kamp 		errno = i;
112662cb04cSPoul-Henning Kamp 		return ("Could not write first sector");
113662cb04cSPoul-Henning Kamp 	}
114662cb04cSPoul-Henning Kamp 	memset(buf, 0, recsize);
115662cb04cSPoul-Henning Kamp 	if ((int)recsize != pwrite(fd, buf, recsize, recsize)) {
116662cb04cSPoul-Henning Kamp 		i = errno;
117662cb04cSPoul-Henning Kamp 		free(buf);
118662cb04cSPoul-Henning Kamp 		errno = i;
119662cb04cSPoul-Henning Kamp 		return ("Could not write second sector");
120662cb04cSPoul-Henning Kamp 	}
121662cb04cSPoul-Henning Kamp 	free(buf);
122662cb04cSPoul-Henning Kamp 	assert(0 == close(fd));
123662cb04cSPoul-Henning Kamp 	return (NULL);
124662cb04cSPoul-Henning Kamp }
125