xref: /openbsd/usr.sbin/config/mkswap.c (revision 8529ddd3)
1 /*	$OpenBSD: mkswap.c,v 1.15 2015/01/16 06:40:16 deraadt Exp $	*/
2 /*	$NetBSD: mkswap.c,v 1.5 1996/08/31 20:58:27 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * All advertising materials mentioning features or use of this software
13  * must display the following acknowledgement:
14  *	This product includes software developed by the University of
15  *	California, Lawrence Berkeley Laboratories.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)mkswap.c	8.1 (Berkeley) 6/6/93
42  */
43 
44 #include <sys/param.h>	/* NODEV */
45 
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "config.h"
52 #include "sem.h"
53 
54 static int mkoneswap(struct config *);
55 
56 /*
57  * Make the various swap*.c files.  Nothing to do for generic swap.
58  */
59 int
60 mkswap(void)
61 {
62 	struct config *cf;
63 
64 	for (cf = allcf; cf != NULL; cf = cf->cf_next)
65 		if (cf->cf_root != NULL && mkoneswap(cf))
66 			return (1);
67 	return (0);
68 }
69 
70 static char *
71 mkdevstr(dev_t d)
72 {
73 	static char buf[32];
74 
75 	if (d == NODEV)
76 		(void)snprintf(buf, sizeof buf, "NODEV");
77 	else
78 		(void)snprintf(buf, sizeof buf, "makedev(%d, %d)",
79 		    major(d), minor(d));
80 	return buf;
81 }
82 
83 static int
84 mkoneswap(struct config *cf)
85 {
86 	char fname[200], *mountroot;
87 	struct nvlist *nv;
88 	FILE *fp;
89 
90 	(void)snprintf(fname, sizeof fname, "swap%s.c", cf->cf_name);
91 	if ((fp = fopen(fname, "w")) == NULL) {
92 		(void)fprintf(stderr, "config: cannot write %s: %s\n",
93 		    fname, strerror(errno));
94 		return (1);
95 	}
96 	if (fputs("\
97 #include <sys/param.h>\n\
98 #include <sys/conf.h>\n\
99 #include <sys/systm.h>\n\n", fp) < 0)
100 		goto wrerror;
101 	nv = cf->cf_root;
102 	if (fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n",
103 	    mkdevstr(nv->nv_int), nv->nv_str) < 0)
104 		goto wrerror;
105 	nv = cf->cf_dump;
106 	if (fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n",
107 	    mkdevstr(nv->nv_int), nv->nv_str) < 0)
108 		goto wrerror;
109 	if (fputs("\nstruct\tswdevt swdevt[] = {\n", fp) < 0)
110 		goto wrerror;
111 	for (nv = cf->cf_swap; nv != NULL; nv = nv->nv_next)
112 		if (fprintf(fp, "\t{ %s,\t0 },\t/* %s */\n",
113 		    mkdevstr(nv->nv_int), nv->nv_str) < 0)
114 			goto wrerror;
115 	if (fputs("\t{ NODEV, 0 }\n};\n\n", fp) < 0)
116 		goto wrerror;
117 	mountroot =
118 	    cf->cf_root->nv_str == s_nfs ? "nfs_mountroot" : "dk_mountroot";
119 	if (fprintf(fp, "int (*mountroot)(void) = %s;\n", mountroot) < 0)
120 		goto wrerror;
121 
122 	if (fclose(fp)) {
123 		fp = NULL;
124 		goto wrerror;
125 	}
126 	return (0);
127 wrerror:
128 	(void)fprintf(stderr, "config: error writing %s: %s\n",
129 	    fname, strerror(errno));
130 	if (fp != NULL)
131 		(void)fclose(fp);
132 	/* (void)unlink(fname); */
133 	return (1);
134 }
135