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