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