1/* $OpenBSD: mkswap.c,v 1.19 2021/11/28 19:26:03 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/types.h> 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 55dev_t nodev = (dev_t)-1; 56 57static int mkoneswap(struct config *); 58 59/* 60 * Make the various swap*.c files. Nothing to do for generic swap. 61 */ 62int 63mkswap(void) 64{ 65 struct config *cf; 66 67 for (cf = allcf; cf != NULL; cf = cf->cf_next) 68 if (cf->cf_root != NULL && mkoneswap(cf)) 69 return (1); 70 return (0); 71} 72 73static char * 74mkdevstr(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(%u, %u)", 82 major(d), minor(d)); 83 return buf; 84} 85 86static int 87mkoneswap(struct config *cf) 88{ 89 char fname[200], *mountroot; 90 struct nvlist *nv; 91 FILE *fp; 92 93 (void)snprintf(fname, sizeof fname, "swap%s.c", cf->cf_name); 94 if ((fp = fopen(fname, "w")) == NULL) { 95 warn("cannot write %s", fname); 96 return (1); 97 } 98 if (fputs("\ 99#include <sys/param.h>\n\ 100#include <sys/conf.h>\n\ 101#include <sys/systm.h>\n\n", fp) == EOF) 102 goto wrerror; 103 nv = cf->cf_root; 104 if (fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n", 105 mkdevstr(nv->nv_int), nv->nv_str) < 0) 106 goto wrerror; 107 nv = cf->cf_dump; 108 if (fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n", 109 mkdevstr(nv->nv_int), nv->nv_str) < 0) 110 goto wrerror; 111 if (fputs("\nstruct\tswdevt swdevt[] = {\n", fp) == EOF) 112 goto wrerror; 113 for (nv = cf->cf_swap; nv != NULL; nv = nv->nv_next) 114 if (fprintf(fp, "\t{ %s,\t0 },\t/* %s */\n", 115 mkdevstr(nv->nv_int), nv->nv_str) < 0) 116 goto wrerror; 117 if (fputs("\t{ NODEV, 0 }\n};\n\n", fp) == EOF) 118 goto wrerror; 119 mountroot = 120 cf->cf_root->nv_str == s_nfs ? "nfs_mountroot" : "dk_mountroot"; 121 if (fprintf(fp, "int (*mountroot)(void) = %s;\n", mountroot) < 0) 122 goto wrerror; 123 124 if (fclose(fp)) { 125 fp = NULL; 126 goto wrerror; 127 } 128 return (0); 129wrerror: 130 warn("error writing %s", fname); 131 if (fp != NULL) 132 (void)fclose(fp); 133 /* (void)unlink(fname); */ 134 return (1); 135} 136