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