xref: /netbsd-src/usr.bin/config/mkswap.c (revision b5b4952daef3f2b9c13abb88d52d007656b9c01d)
1*b5b4952dSuebayasi /*	$NetBSD: mkswap.c,v 1.10 2015/09/03 13:53:36 uebayasi Exp $	*/
25ecc953bSthorpej 
35ecc953bSthorpej /*
45ecc953bSthorpej  * Copyright (c) 1992, 1993
55ecc953bSthorpej  *	The Regents of the University of California.  All rights reserved.
65ecc953bSthorpej  *
75ecc953bSthorpej  * This software was developed by the Computer Systems Engineering group
85ecc953bSthorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
95ecc953bSthorpej  * contributed to Berkeley.
105ecc953bSthorpej  *
115ecc953bSthorpej  * All advertising materials mentioning features or use of this software
125ecc953bSthorpej  * must display the following acknowledgement:
135ecc953bSthorpej  *	This product includes software developed by the University of
145ecc953bSthorpej  *	California, Lawrence Berkeley Laboratories.
155ecc953bSthorpej  *
165ecc953bSthorpej  * Redistribution and use in source and binary forms, with or without
175ecc953bSthorpej  * modification, are permitted provided that the following conditions
185ecc953bSthorpej  * are met:
195ecc953bSthorpej  * 1. Redistributions of source code must retain the above copyright
205ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer.
215ecc953bSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
225ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer in the
235ecc953bSthorpej  *    documentation and/or other materials provided with the distribution.
245ecc953bSthorpej  * 3. Neither the name of the University nor the names of its contributors
255ecc953bSthorpej  *    may be used to endorse or promote products derived from this software
265ecc953bSthorpej  *    without specific prior written permission.
275ecc953bSthorpej  *
285ecc953bSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
295ecc953bSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
305ecc953bSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
315ecc953bSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
325ecc953bSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
335ecc953bSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
345ecc953bSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
355ecc953bSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
365ecc953bSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
375ecc953bSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
385ecc953bSthorpej  * SUCH DAMAGE.
395ecc953bSthorpej  *
405ecc953bSthorpej  *	from: @(#)mkswap.c	8.1 (Berkeley) 6/6/93
415ecc953bSthorpej  */
425ecc953bSthorpej 
435ecc953bSthorpej #if HAVE_NBTOOL_CONFIG_H
445ecc953bSthorpej #include "nbtool_config.h"
455ecc953bSthorpej #endif
465ecc953bSthorpej 
47d12b0036Schristos #include <sys/cdefs.h>
48*b5b4952dSuebayasi __RCSID("$NetBSD: mkswap.c,v 1.10 2015/09/03 13:53:36 uebayasi Exp $");
49d12b0036Schristos 
505ecc953bSthorpej #include <sys/param.h>
515ecc953bSthorpej #include <errno.h>
525ecc953bSthorpej #include <stdio.h>
535ecc953bSthorpej #include <stdlib.h>
545ecc953bSthorpej #include <string.h>
55c7295a4cSchristos #include <err.h>
565ecc953bSthorpej #include "defs.h"
575ecc953bSthorpej #include "sem.h"
585ecc953bSthorpej 
595ecc953bSthorpej static	char   *mkdevstr(dev_t);
605ecc953bSthorpej static	int	mkoneswap(struct config *);
615ecc953bSthorpej 
625ecc953bSthorpej /*
635ecc953bSthorpej  * Make the various swap*.c files.  Nothing to do for generic swap.
645ecc953bSthorpej  */
655ecc953bSthorpej int
mkswap(void)665ecc953bSthorpej mkswap(void)
675ecc953bSthorpej {
685ecc953bSthorpej 	struct config *cf;
695ecc953bSthorpej 
705ecc953bSthorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
715ecc953bSthorpej 		if (mkoneswap(cf))
725ecc953bSthorpej 			return (1);
735ecc953bSthorpej 	}
745ecc953bSthorpej 	return (0);
755ecc953bSthorpej }
765ecc953bSthorpej 
775ecc953bSthorpej static char *
mkdevstr(dev_t d)785ecc953bSthorpej mkdevstr(dev_t d)
795ecc953bSthorpej {
805ecc953bSthorpej 	static char buf[32];
815ecc953bSthorpej 
825ecc953bSthorpej 	if (d == NODEV)
835ecc953bSthorpej 		(void)snprintf(buf, sizeof(buf), "NODEV");
845ecc953bSthorpej 	else
85d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "makedev(%d, %d)",
86d767912bSdrochner 		    major(d), minor(d));
875ecc953bSthorpej 	return buf;
885ecc953bSthorpej }
895ecc953bSthorpej 
905ecc953bSthorpej static int
mkoneswap(struct config * cf)915ecc953bSthorpej mkoneswap(struct config *cf)
925ecc953bSthorpej {
935ecc953bSthorpej 	struct nvlist *nv;
945ecc953bSthorpej 	FILE *fp;
955ecc953bSthorpej 	char fname[200], tname[200];
965ecc953bSthorpej 	char specinfo[200];
975ecc953bSthorpej 
985ecc953bSthorpej 	(void)snprintf(fname, sizeof(fname), "swap%s.c", cf->cf_name);
995ecc953bSthorpej 	(void)snprintf(tname, sizeof(tname), "swap%s.c.tmp", cf->cf_name);
1005ecc953bSthorpej 	if ((fp = fopen(tname, "w")) == NULL) {
101c7295a4cSchristos 		warn("cannot open %s", fname);
102*b5b4952dSuebayasi 		return (1);
1035ecc953bSthorpej 	}
10482d7cb83Slukem 
10582d7cb83Slukem 	autogen_comment(fp, fname);
10682d7cb83Slukem 
107342d3579Sdsl 	fputs("#include <sys/param.h>\n"
108342d3579Sdsl 		"#include <sys/conf.h>\n\n", fp);
1095ecc953bSthorpej 	/*
1105ecc953bSthorpej 	 * Emit the root device.
1115ecc953bSthorpej 	 */
1125ecc953bSthorpej 	nv = cf->cf_root;
1135ecc953bSthorpej 	if (cf->cf_root->nv_str == s_qmark)
1145ecc953bSthorpej 		strlcpy(specinfo, "NULL", sizeof(specinfo));
1155ecc953bSthorpej 	else
1165ecc953bSthorpej 		snprintf(specinfo, sizeof(specinfo), "\"%s\"",
1175ecc953bSthorpej 		    cf->cf_root->nv_str);
118342d3579Sdsl 	fprintf(fp, "const char *rootspec = %s;\n", specinfo);
119342d3579Sdsl 	fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n\n",
120d12b0036Schristos 		mkdevstr((dev_t)nv->nv_num),
121342d3579Sdsl 		nv->nv_str == s_qmark ? "wildcarded" : nv->nv_str);
1225ecc953bSthorpej 
1235ecc953bSthorpej 	/*
1245ecc953bSthorpej 	 * Emit the dump device.
1255ecc953bSthorpej 	 */
1265ecc953bSthorpej 	nv = cf->cf_dump;
1275ecc953bSthorpej 	if (cf->cf_dump == NULL)
1285ecc953bSthorpej 		strlcpy(specinfo, "NULL", sizeof(specinfo));
1295ecc953bSthorpej 	else
1305ecc953bSthorpej 		snprintf(specinfo, sizeof(specinfo), "\"%s\"", cf->cf_dump->nv_str);
131342d3579Sdsl 	fprintf(fp, "const char *dumpspec = %s;\n", specinfo);
132342d3579Sdsl 	fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n\n",
133d12b0036Schristos 		nv ? mkdevstr((dev_t)nv->nv_num) : "NODEV",
134342d3579Sdsl 		nv ? nv->nv_str : "unspecified");
1355ecc953bSthorpej 
1365ecc953bSthorpej 	/*
1375ecc953bSthorpej 	 * Emit the root file system.
1385ecc953bSthorpej 	 */
1399c683319Spgoyette 	fprintf(fp, "const char *rootfstype = \"%s\";\n",
1409c683319Spgoyette 		cf->cf_fstype ? cf->cf_fstype : "?");
141342d3579Sdsl 
142342d3579Sdsl 	fflush(fp);
143342d3579Sdsl 	if (ferror(fp))
1445ecc953bSthorpej 		goto wrerror;
1455ecc953bSthorpej 
1465ecc953bSthorpej 	if (fclose(fp)) {
1475ecc953bSthorpej 		fp = NULL;
1485ecc953bSthorpej 		goto wrerror;
1495ecc953bSthorpej 	}
1505ecc953bSthorpej 	if (moveifchanged(tname, fname) != 0) {
151c7295a4cSchristos 		warn("error renaming %s", fname);
152*b5b4952dSuebayasi 		return (1);
1535ecc953bSthorpej 	}
1545ecc953bSthorpej 	return (0);
155342d3579Sdsl 
1565ecc953bSthorpej  wrerror:
157c7295a4cSchristos 	warn("error writing %s", fname);
1585ecc953bSthorpej 	if (fp != NULL)
1595ecc953bSthorpej 		(void)fclose(fp);
160c7295a4cSchristos #if 0
161c7295a4cSchristos 	(void)unlink(fname);
162c7295a4cSchristos #endif
1635ecc953bSthorpej 	return (1);
1645ecc953bSthorpej }
165