Home | History | Annotate | Line # | Download | only in config
      1 /*	$NetBSD: mkswap.c,v 1.10 2015/09/03 13:53:36 uebayasi 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/cdefs.h>
     48 __RCSID("$NetBSD: mkswap.c,v 1.10 2015/09/03 13:53:36 uebayasi Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <errno.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <err.h>
     56 #include "defs.h"
     57 #include "sem.h"
     58 
     59 static	char   *mkdevstr(dev_t);
     60 static	int	mkoneswap(struct config *);
     61 
     62 /*
     63  * Make the various swap*.c files.  Nothing to do for generic swap.
     64  */
     65 int
     66 mkswap(void)
     67 {
     68 	struct config *cf;
     69 
     70 	TAILQ_FOREACH(cf, &allcf, cf_next) {
     71 		if (mkoneswap(cf))
     72 			return (1);
     73 	}
     74 	return (0);
     75 }
     76 
     77 static char *
     78 mkdevstr(dev_t d)
     79 {
     80 	static char buf[32];
     81 
     82 	if (d == NODEV)
     83 		(void)snprintf(buf, sizeof(buf), "NODEV");
     84 	else
     85 		(void)snprintf(buf, sizeof(buf), "makedev(%d, %d)",
     86 		    major(d), minor(d));
     87 	return buf;
     88 }
     89 
     90 static int
     91 mkoneswap(struct config *cf)
     92 {
     93 	struct nvlist *nv;
     94 	FILE *fp;
     95 	char fname[200], tname[200];
     96 	char specinfo[200];
     97 
     98 	(void)snprintf(fname, sizeof(fname), "swap%s.c", cf->cf_name);
     99 	(void)snprintf(tname, sizeof(tname), "swap%s.c.tmp", cf->cf_name);
    100 	if ((fp = fopen(tname, "w")) == NULL) {
    101 		warn("cannot open %s", fname);
    102 		return (1);
    103 	}
    104 
    105 	autogen_comment(fp, fname);
    106 
    107 	fputs("#include <sys/param.h>\n"
    108 		"#include <sys/conf.h>\n\n", fp);
    109 	/*
    110 	 * Emit the root device.
    111 	 */
    112 	nv = cf->cf_root;
    113 	if (cf->cf_root->nv_str == s_qmark)
    114 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    115 	else
    116 		snprintf(specinfo, sizeof(specinfo), "\"%s\"",
    117 		    cf->cf_root->nv_str);
    118 	fprintf(fp, "const char *rootspec = %s;\n", specinfo);
    119 	fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n\n",
    120 		mkdevstr((dev_t)nv->nv_num),
    121 		nv->nv_str == s_qmark ? "wildcarded" : nv->nv_str);
    122 
    123 	/*
    124 	 * Emit the dump device.
    125 	 */
    126 	nv = cf->cf_dump;
    127 	if (cf->cf_dump == NULL)
    128 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    129 	else
    130 		snprintf(specinfo, sizeof(specinfo), "\"%s\"", cf->cf_dump->nv_str);
    131 	fprintf(fp, "const char *dumpspec = %s;\n", specinfo);
    132 	fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n\n",
    133 		nv ? mkdevstr((dev_t)nv->nv_num) : "NODEV",
    134 		nv ? nv->nv_str : "unspecified");
    135 
    136 	/*
    137 	 * Emit the root file system.
    138 	 */
    139 	fprintf(fp, "const char *rootfstype = \"%s\";\n",
    140 		cf->cf_fstype ? cf->cf_fstype : "?");
    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