Home | History | Annotate | Line # | Download | only in config
mkswap.c revision 1.4
      1  1.4     lukem /*	$NetBSD: mkswap.c,v 1.4 2007/12/12 00:03:34 lukem 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.3  christos #include <err.h>
     53  1.1   thorpej #include "defs.h"
     54  1.1   thorpej #include "sem.h"
     55  1.1   thorpej 
     56  1.1   thorpej static	char   *mkdevstr(dev_t);
     57  1.1   thorpej static	int	mkoneswap(struct config *);
     58  1.1   thorpej 
     59  1.1   thorpej /*
     60  1.1   thorpej  * Make the various swap*.c files.  Nothing to do for generic swap.
     61  1.1   thorpej  */
     62  1.1   thorpej int
     63  1.1   thorpej mkswap(void)
     64  1.1   thorpej {
     65  1.1   thorpej 	struct config *cf;
     66  1.1   thorpej 
     67  1.1   thorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
     68  1.1   thorpej 		if (mkoneswap(cf))
     69  1.1   thorpej 			return (1);
     70  1.1   thorpej 	}
     71  1.1   thorpej 	return (0);
     72  1.1   thorpej }
     73  1.1   thorpej 
     74  1.1   thorpej static char *
     75  1.1   thorpej mkdevstr(dev_t d)
     76  1.1   thorpej {
     77  1.1   thorpej 	static char buf[32];
     78  1.1   thorpej 
     79  1.1   thorpej 	if (d == NODEV)
     80  1.1   thorpej 		(void)snprintf(buf, sizeof(buf), "NODEV");
     81  1.1   thorpej 	else
     82  1.1   thorpej 		(void)snprintf(buf, sizeof(buf), "makedev(%d, %d)",
     83  1.1   thorpej 		    major(d), minor(d));
     84  1.1   thorpej 	return buf;
     85  1.1   thorpej }
     86  1.1   thorpej 
     87  1.1   thorpej static int
     88  1.1   thorpej mkoneswap(struct config *cf)
     89  1.1   thorpej {
     90  1.1   thorpej 	struct nvlist *nv;
     91  1.1   thorpej 	FILE *fp;
     92  1.1   thorpej 	char fname[200], tname[200];
     93  1.1   thorpej 	char specinfo[200];
     94  1.1   thorpej 
     95  1.1   thorpej 	(void)snprintf(fname, sizeof(fname), "swap%s.c", cf->cf_name);
     96  1.1   thorpej 	(void)snprintf(tname, sizeof(tname), "swap%s.c.tmp", cf->cf_name);
     97  1.1   thorpej 	if ((fp = fopen(tname, "w")) == NULL) {
     98  1.3  christos 		warn("cannot open %s", fname);
     99  1.1   thorpej 		return (1);
    100  1.1   thorpej 	}
    101  1.4     lukem 
    102  1.4     lukem 	autogen_comment(fp, fname);
    103  1.4     lukem 
    104  1.2       dsl 	fputs("#include <sys/param.h>\n"
    105  1.2       dsl 		"#include <sys/conf.h>\n\n", fp);
    106  1.1   thorpej 	/*
    107  1.1   thorpej 	 * Emit the root device.
    108  1.1   thorpej 	 */
    109  1.1   thorpej 	nv = cf->cf_root;
    110  1.1   thorpej 	if (cf->cf_root->nv_str == s_qmark)
    111  1.1   thorpej 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    112  1.1   thorpej 	else
    113  1.1   thorpej 		snprintf(specinfo, sizeof(specinfo), "\"%s\"",
    114  1.1   thorpej 		    cf->cf_root->nv_str);
    115  1.2       dsl 	fprintf(fp, "const char *rootspec = %s;\n", specinfo);
    116  1.2       dsl 	fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n\n",
    117  1.2       dsl 		mkdevstr(nv->nv_int),
    118  1.2       dsl 		nv->nv_str == s_qmark ? "wildcarded" : nv->nv_str);
    119  1.1   thorpej 
    120  1.1   thorpej 	/*
    121  1.1   thorpej 	 * Emit the dump device.
    122  1.1   thorpej 	 */
    123  1.1   thorpej 	nv = cf->cf_dump;
    124  1.1   thorpej 	if (cf->cf_dump == NULL)
    125  1.1   thorpej 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    126  1.1   thorpej 	else
    127  1.1   thorpej 		snprintf(specinfo, sizeof(specinfo), "\"%s\"", cf->cf_dump->nv_str);
    128  1.2       dsl 	fprintf(fp, "const char *dumpspec = %s;\n", specinfo);
    129  1.2       dsl 	fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n\n",
    130  1.2       dsl 		nv ? mkdevstr(nv->nv_int) : "NODEV",
    131  1.2       dsl 		nv ? nv->nv_str : "unspecified");
    132  1.1   thorpej 
    133  1.1   thorpej 	/*
    134  1.1   thorpej 	 * Emit the root file system.
    135  1.1   thorpej 	 */
    136  1.1   thorpej 	if (cf->cf_fstype == NULL)
    137  1.1   thorpej 		strlcpy(specinfo, "NULL", sizeof(specinfo));
    138  1.1   thorpej 	else {
    139  1.1   thorpej 		snprintf(specinfo, sizeof(specinfo), "%s_mountroot",
    140  1.1   thorpej 		    cf->cf_fstype);
    141  1.2       dsl 		fprintf(fp, "int %s(void);\n", specinfo);
    142  1.1   thorpej 	}
    143  1.2       dsl 	fprintf(fp, "int (*mountroot)(void) = %s;\n", specinfo);
    144  1.2       dsl 
    145  1.2       dsl 	fflush(fp);
    146  1.2       dsl 	if (ferror(fp))
    147  1.1   thorpej 		goto wrerror;
    148  1.1   thorpej 
    149  1.1   thorpej 	if (fclose(fp)) {
    150  1.1   thorpej 		fp = NULL;
    151  1.1   thorpej 		goto wrerror;
    152  1.1   thorpej 	}
    153  1.1   thorpej 	if (moveifchanged(tname, fname) != 0) {
    154  1.3  christos 		warn("error renaming %s", fname);
    155  1.1   thorpej 		return (1);
    156  1.1   thorpej 	}
    157  1.1   thorpej 	return (0);
    158  1.2       dsl 
    159  1.1   thorpej  wrerror:
    160  1.3  christos 	warn("error writing %s", fname);
    161  1.1   thorpej 	if (fp != NULL)
    162  1.1   thorpej 		(void)fclose(fp);
    163  1.3  christos #if 0
    164  1.3  christos 	(void)unlink(fname);
    165  1.3  christos #endif
    166  1.1   thorpej 	return (1);
    167  1.1   thorpej }
    168