Home | History | Annotate | Line # | Download | only in config
mkswap.c revision 1.1
      1 /*	$NetBSD: mkswap.c,v 1.1 2005/06/05 18:19:53 thorpej 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 	if (fputs("\
    102 #include <sys/param.h>\n\
    103 #include <sys/conf.h>\n\n", fp) < 0)
    104 		goto wrerror;
    105 
    106 	/*
    107 	 * Emit the root device.
    108 	 */
    109 	nv = cf->cf_root;
    110 	if (cf->cf_root->nv_str == s_qmark)
    111 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    112 	else
    113 		snprintf(specinfo, sizeof(specinfo), "\"%s\"",
    114 		    cf->cf_root->nv_str);
    115 	if (fprintf(fp, "const char *rootspec = %s;\n", specinfo) < 0)
    116 		goto wrerror;
    117 	if (fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n\n",
    118 	    mkdevstr(nv->nv_int),
    119 	    nv->nv_str == s_qmark ? "wildcarded" : nv->nv_str) < 0)
    120 		goto wrerror;
    121 
    122 	/*
    123 	 * Emit the dump device.
    124 	 */
    125 	nv = cf->cf_dump;
    126 	if (cf->cf_dump == NULL)
    127 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    128 	else
    129 		snprintf(specinfo, sizeof(specinfo), "\"%s\"", cf->cf_dump->nv_str);
    130 	if (fprintf(fp, "const char *dumpspec = %s;\n", specinfo) < 0)
    131 		goto wrerror;
    132 	if (fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n\n",
    133 	    nv ? mkdevstr(nv->nv_int) : "NODEV",
    134 	    nv ? nv->nv_str : "unspecified") < 0)
    135 		goto wrerror;
    136 
    137 	/*
    138 	 * Emit the root file system.
    139 	 */
    140 	if (cf->cf_fstype == NULL)
    141 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    142 	else {
    143 		snprintf(specinfo, sizeof(specinfo), "%s_mountroot",
    144 		    cf->cf_fstype);
    145 		if (fprintf(fp, "int %s(void);\n", specinfo) < 0)
    146 			goto wrerror;
    147 	}
    148 	if (fprintf(fp, "int (*mountroot)(void) = %s;\n", specinfo) < 0)
    149 		goto wrerror;
    150 
    151 	if (fclose(fp)) {
    152 		fp = NULL;
    153 		goto wrerror;
    154 	}
    155 	if (moveifchanged(tname, fname) != 0) {
    156 		(void)fprintf(stderr, "config: error renaming %s: %s\n",
    157 		    fname, strerror(errno));
    158 		return (1);
    159 	}
    160 	return (0);
    161  wrerror:
    162 	(void)fprintf(stderr, "config: error writing %s: %s\n",
    163 	    fname, strerror(errno));
    164 	if (fp != NULL)
    165 		(void)fclose(fp);
    166 	/* (void)unlink(fname); */
    167 	return (1);
    168 }
    169