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