xref: /openbsd/usr.sbin/config/ukc.c (revision d485f761)
1 /*	$OpenBSD: ukc.c,v 1.5 2001/02/04 20:42:12 maja Exp $ */
2 
3 /*
4  * Copyright (c) 1999-2001 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Mats O Jansson.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef LINT
33 static char rcsid[] = "$OpenBSD: ukc.c,v 1.5 2001/02/04 20:42:12 maja Exp $";
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/device.h>
38 #include <sys/ioctl.h>
39 
40 #include <err.h>
41 #include <kvm.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <nlist.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #define UKC_MAIN
51 #include "ukc.h"
52 #include "exec.h"
53 
54 void	init __P((void));
55 void	usage __P((void));
56 
57 int	ukc_mod_kernel = 0;
58 
59 int
60 ukc(file, outfile, uflag, force)
61 	char *file;
62 	char *outfile;
63 	int uflag;
64 	int force;
65 {
66 	extern char *__progname;
67 	int ret, i;
68 	kvm_t *kd;
69 	char errbuf[_POSIX2_LINE_MAX];
70 	int histlen = 0, ok = 1;
71 	char history[1024], kversion[1024];
72 
73 	if (file == NULL) {
74 		fprintf(stderr, "%s: no file specified\n", __progname);
75 		usage();
76 	}
77 
78 	loadkernel(file);
79 
80 	ret = nlist(file, nl);
81 
82 	if (uflag) {
83 
84 		if ((kd = kvm_openfiles(NULL,NULL,NULL,O_RDONLY, errbuf)) == 0)
85 			errx(1, "kvm_openfiles: %s", errbuf);
86 
87 		if ((ret = kvm_nlist(kd, knl)) == -1)
88 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
89 
90 		i = 0;
91 		while (i < NLENTRIES) {
92 			if ((nl[i].n_type != knl[i].n_type) ||
93 			    (nl[i].n_desc != knl[i].n_desc) ||
94 			    (nl[i].n_value != knl[i].n_value))
95 				ok = 0;
96 			i++;
97 		}
98 
99 		if ((knl[I_HISTLEN].n_type != 0) && ok) {
100 			if (kvm_read(kd, knl[I_HISTLEN].n_value, &histlen,
101 				     sizeof(histlen)) != sizeof(histlen))
102 				warnx("cannot read %s: %s",
103 				      knl[I_HISTLEN].n_name,
104 				      kvm_geterr(kd));
105 		}
106 		if ((knl[CA_HISTORY].n_type != 0) && ok) {
107 			if (kvm_read(kd, knl[CA_HISTORY].n_value, history,
108 				     sizeof(history)) != sizeof(history))
109 				warnx("cannot read %s: %s",
110 				      knl[CA_HISTORY].n_name,
111 				      kvm_geterr(kd));
112 		}
113 		if ((knl[P_VERSION].n_type != 0) && ok) {
114 			if (kvm_read(kd, knl[P_VERSION].n_value, kversion,
115 				     sizeof(kversion)) != sizeof(kversion))
116 				warnx("cannot read %s: %s",
117 				      knl[P_VERSION].n_name,
118 				      kvm_geterr(kd));
119 		}
120 	}
121 
122 	printf("%s", adjust((caddr_t)nl[P_VERSION].n_value));
123 
124 	if (force == 0 && outfile == NULL)
125 		printf("warning: no output file specified\n");
126 
127 	if ((nl[IA_EXTRALOC].n_type == 0) ||
128 	    (nl[I_NEXTRALOC].n_type == 0) ||
129 	    (nl[I_UEXTRALOC].n_type == 0) ||
130 	    (nl[I_HISTLEN].n_type == 0) ||
131 	    (nl[CA_HISTORY].n_type == 0)) {
132 		printf("\
133 WARNING this kernel doesn't contain all information needed!\n\
134 WARNING the commands add and change might not work.\n");
135 		oldkernel = 1;
136 	}
137 
138 	if ((nl[P_PDEVNAMES].n_type == 0) ||
139 	    (nl[I_PDEVSIZE].n_type == 0) ||
140 	    (nl[S_PDEVINIT].n_type == 0)) {
141 		printf("\
142 WARNING this kernel doesn't support pseudo devices.\n");
143 		nopdev = 1;
144 	}
145 
146 	init();
147 
148 	if (uflag) {
149 		if (ok) {
150 			if (strcmp(adjust((caddr_t)nl[P_VERSION].n_value),
151 				   kversion) != 0)
152 				ok = 1;
153 		}
154 		if (!ok) {
155 			printf("WARNING kernel mismatch. -u ignored.\n");
156 			printf("WARNING the running kernel version:\n");
157 			printf("%s",kversion);
158 		} else {
159 			process_history(histlen,history);
160 		}
161 	}
162 
163 	if (config()) {
164 		if (force == 0 && outfile == NULL) {
165 			fprintf(stderr, "not forced\n");
166 			exit(1);
167 		}
168 		if (outfile == NULL)
169 			outfile = file;
170 		if (ukc_mod_kernel == 0) {
171 			fprintf(stderr, "Kernel not modified\n");
172 			exit(1);
173 		} else {
174 			printf ("Saving modified kernel.\n");
175 			savekernel(outfile);
176 		}
177 	}
178 
179 	return(0);
180 }
181 
182 void
183 init()
184 {
185 	int i = 0,fd;
186 	struct cfdata *cd;
187 	short	*ln;
188 	int	*p;
189 #ifdef NOTDEF
190 	struct winsize w;
191 #endif
192 
193 	cd = get_cfdata(0);			/* get first item */
194 
195 	while(cd->cf_attach != 0) {
196 		maxdev = i;
197 		totdev = i;
198 
199 		ln = get_locnamp(cd->cf_locnames);
200 		while (*ln != -1) {
201 			if (*ln > maxlocnames)
202 				maxlocnames = *ln;
203 			ln++;
204 		}
205 		i++;
206 		cd++;
207 	}
208 
209 	while(cd->cf_attach == 0) {
210 		totdev = i;
211 		i++;
212 		cd++;
213 	}
214 
215 	totdev = totdev - 1;
216 
217 	if (nopdev == 0) {
218 		p = (int *)adjust((caddr_t)nl[I_PDEVSIZE].n_value);
219 		maxpseudo = *p;
220 	}
221 
222 	if ((fd = open("/dev/tty", O_RDWR)) < 0)
223 		fd = 2;
224 
225 #ifdef NOTDEF
226 	if (ioctl(fd, TIOCGWINSZ, &w) == 0) {
227 		printf("row %d col %d\n",w.ws_row,w.ws_col);
228 	}
229 
230 	if ((s = getenv("LINES")) != NULL)
231 		sc_height = atoi(s);
232 	else
233  		sc_height = tgetnum("li");
234 
235 	if (sc_height <= 0)
236 		sc_height = 24;
237 
238 	if ((s = getenv("COLUMNS")) != NULL)
239 		sc_width = atoi(s);
240 	else
241  		sc_width = tgetnum("co");
242 
243  	if (sc_width <= 0)
244   		sc_width = 80;
245 #endif
246 }
247