Home | History | Annotate | Line # | Download | only in config
mkswap.c revision 1.2
      1 /*	$NetBSD: mkswap.c,v 1.2 2006/10/04 20:34:48 dsl 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 "defs.h"
     53 #include "sem.h"
     54 
     55 static	char   *mkdevstr(dev_t);
     56 static	int	mkoneswap(struct config *);
     57 
     58 /*
     59  * Make the various swap*.c files.  Nothing to do for generic swap.
     60  */
     61 int
     62 mkswap(void)
     63 {
     64 	struct config *cf;
     65 
     66 	TAILQ_FOREACH(cf, &allcf, cf_next) {
     67 		if (mkoneswap(cf))
     68 			return (1);
     69 	}
     70 	return (0);
     71 }
     72 
     73 static char *
     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(%d, %d)",
     82 		    major(d), minor(d));
     83 	return buf;
     84 }
     85 
     86 static int
     87 mkoneswap(struct config *cf)
     88 {
     89 	struct nvlist *nv;
     90 	FILE *fp;
     91 	char fname[200], tname[200];
     92 	char specinfo[200];
     93 
     94 	(void)snprintf(fname, sizeof(fname), "swap%s.c", cf->cf_name);
     95 	(void)snprintf(tname, sizeof(tname), "swap%s.c.tmp", cf->cf_name);
     96 	if ((fp = fopen(tname, "w")) == NULL) {
     97 		(void)fprintf(stderr, "config: cannot write %s: %s\n",
     98 		    fname, strerror(errno));
     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 		(void)fprintf(stderr, "config: error renaming %s: %s\n",
    152 		    fname, strerror(errno));
    153 		return (1);
    154 	}
    155 	return (0);
    156 
    157  wrerror:
    158 	(void)fprintf(stderr, "config: error writing %s: %s\n",
    159 	    fname, strerror(errno));
    160 	if (fp != NULL)
    161 		(void)fclose(fp);
    162 	/* (void)unlink(fname); */
    163 	return (1);
    164 }
    165