1 /* $OpenBSD: mkswap.c,v 1.12 2003/06/28 04:55:07 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>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include "config.h"
50 #include "sem.h"
51
52 static int mkoneswap(struct config *);
53
54 /*
55 * Make the various swap*.c files. Nothing to do for generic swap.
56 */
57 int
mkswap(void)58 mkswap(void)
59 {
60 struct config *cf;
61
62 for (cf = allcf; cf != NULL; cf = cf->cf_next)
63 if (cf->cf_root != NULL && mkoneswap(cf))
64 return (1);
65 return (0);
66 }
67
68 static char *
mkdevstr(dev_t d)69 mkdevstr(dev_t d)
70 {
71 static char buf[32];
72
73 if (d == NODEV)
74 (void)snprintf(buf, sizeof buf, "NODEV");
75 else
76 (void)snprintf(buf, sizeof buf, "makedev(%d, %d)",
77 major(d), minor(d));
78 return buf;
79 }
80
81 static int
mkoneswap(struct config * cf)82 mkoneswap(struct config *cf)
83 {
84 char fname[200];
85 const char *mountroot;
86 struct nvlist *nv;
87 FILE *fp;
88
89 (void)snprintf(fname, sizeof fname, "swap%s.c", cf->cf_name);
90 if ((fp = fopen(fname, "w")) == NULL) {
91 (void)fprintf(stderr, "config: cannot write %s: %s\n",
92 fname, strerror(errno));
93 return (1);
94 }
95 if (fputs("\
96 #include <sys/param.h>\n\
97 #include <sys/conf.h>\n\
98 #include <sys/systm.h>\n\n", fp) < 0)
99 goto wrerror;
100 nv = cf->cf_root;
101 if (fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n",
102 mkdevstr(nv->nv_int), nv->nv_str) < 0)
103 goto wrerror;
104 nv = cf->cf_dump;
105 if (fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n",
106 mkdevstr(nv->nv_int), nv->nv_str) < 0)
107 goto wrerror;
108 if (fputs("\nstruct\tswdevt swdevt[] = {\n", fp) < 0)
109 goto wrerror;
110 for (nv = cf->cf_swap; nv != NULL; nv = nv->nv_next)
111 if (fprintf(fp, "\t{ %s,\t0,\t0 },\t/* %s */\n",
112 mkdevstr(nv->nv_int), nv->nv_str) < 0)
113 goto wrerror;
114 if (fputs("\t{ NODEV, 0, 0 }\n};\n\n", fp) < 0)
115 goto wrerror;
116 mountroot =
117 cf->cf_root->nv_str == s_nfs ? "nfs_mountroot" : "dk_mountroot";
118 if (fprintf(fp, "int (*mountroot)(void) = %s;\n", mountroot) < 0)
119 goto wrerror;
120
121 if (fclose(fp)) {
122 fp = NULL;
123 goto wrerror;
124 }
125 return (0);
126 wrerror:
127 (void)fprintf(stderr, "config: error writing %s: %s\n",
128 fname, strerror(errno));
129 if (fp != NULL)
130 (void)fclose(fp);
131 /* (void)unlink(fname); */
132 return (1);
133 }
134