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