summaryrefslogtreecommitdiff
path: root/usr.sbin/config/ukc.c
blob: 1ff7be614f15f14b44ebb28765fa885040ad5892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*	$OpenBSD: ukc.c,v 1.17 2011/10/02 10:10:30 edd Exp $ */

/*
 * Copyright (c) 1999-2001 Mats O Jansson.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/device.h>
#include <sys/ioctl.h>

#include <err.h>
#include <kvm.h>
#include <fcntl.h>
#include <limits.h>
#include <nlist.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define UKC_MAIN
#include "ukc.h"
#include "exec.h"

void	init(void);
void	usage(void);

int	ukc_mod_kernel = 0;

static void
check_int(int idx, const char *name)
{
	if (nl[idx].n_type == 0)
		printf("WARNING this kernel doesn't support modification "
		    "of %s.\n", name);
}

int
ukc(char *file, char *outfile, int uflag, int force)
{
	extern char *__progname;
	int i;
	kvm_t *kd;
	char errbuf[_POSIX2_LINE_MAX];
	int histlen = 0, ok = 1;
	char history[1024], kversion[1024];

	if (file == NULL) {
		fprintf(stderr, "%s: no file specified\n", __progname);
		usage();
	}

	loadkernel(file);

	if (nlist(file, nl) == -1)
		errx(1, "nlist: %s", file);

	if (uflag) {
		if ((kd = kvm_openfiles(NULL,NULL,NULL,O_RDONLY, errbuf)) == 0)
			errx(1, "kvm_openfiles: %s", errbuf);

		if (kvm_nlist(kd, knl) == -1)
			errx(1, "kvm_nlist: %s", kvm_geterr(kd));

		i = 0;
		while (i < NLENTRIES) {
			if (nl[i].n_type != knl[i].n_type ||
			    nl[i].n_desc != knl[i].n_desc ||
			    nl[i].n_value != knl[i].n_value)
				ok = 0;
			i++;
		}

		if (knl[I_HISTLEN].n_type != 0 && ok) {
			if (kvm_read(kd, knl[I_HISTLEN].n_value, &histlen,
			    sizeof(histlen)) != sizeof(histlen))
				warnx("cannot read %s: %s",
				    knl[I_HISTLEN].n_name,
				    kvm_geterr(kd));
		}
		if (knl[CA_HISTORY].n_type != 0 && ok) {
			if (kvm_read(kd, knl[CA_HISTORY].n_value, history,
			    sizeof(history)) != sizeof(history))
				warnx("cannot read %s: %s",
				    knl[CA_HISTORY].n_name,
				    kvm_geterr(kd));
		}
		if (knl[P_VERSION].n_type != 0 && ok) {
			if (kvm_read(kd, knl[P_VERSION].n_value, kversion,
			    sizeof(kversion)) != sizeof(kversion))
				warnx("cannot read %s: %s",
				    knl[P_VERSION].n_name,
				    kvm_geterr(kd));
		}
	}

	if (force == 0 && outfile == NULL)
		printf("WARNING no output file specified\n");

	if (nl[IA_EXTRALOC].n_type == 0 || nl[I_NEXTRALOC].n_type == 0 ||
	    nl[I_UEXTRALOC].n_type == 0 || nl[I_HISTLEN].n_type == 0 ||
	    nl[CA_HISTORY].n_type == 0) {
		printf("\
WARNING this kernel doesn't contain all information needed!\n\
WARNING the commands add and change might not work.\n");
		oldkernel = 1;
	}

	if (nl[P_PDEVNAMES].n_type == 0 ||
	    nl[I_PDEVSIZE].n_type == 0 ||
	    nl[S_PDEVINIT].n_type == 0) {
		printf("\
WARNING this kernel doesn't support pseudo devices.\n");
		nopdev = 1;
	}

	check_int(I_BUFCACHEPCT, "BUFCACHEPERCENT");
	check_int(I_NKMEMPG, "NKMEMPAGES");

	init();

	if (uflag) {
		if (ok) {
			if (strcmp(adjust((caddr_t)nl[P_VERSION].n_value),
			    kversion) != 0)
				ok = 1;
		}
		if (!ok) {
			printf("WARNING kernel mismatch. -u ignored.\n");
			printf("WARNING the running kernel version:\n");
			printf("%s", kversion);
		} else
			process_history(histlen, history);
	}

	printf("%s", adjust((caddr_t)nl[P_VERSION].n_value));

	if (config()) {
		if (force == 0 && outfile == NULL) {
			fprintf(stderr, "not forced\n");
			return (1);
		}
		if (outfile == NULL)
			outfile = file;
		if (ukc_mod_kernel == 0) {
			fprintf(stderr, "Kernel not modified\n");
			return (1);
		} else {
			printf ("Saving modified kernel.\n");
			savekernel(outfile);
		}
	}
	return(0);
}

void
init(void)
{
	int i = 0, fd;
	struct cfdata *cd;
	short	*ln;
	int	*p;
#ifdef NOTDEF
	struct winsize w;
#endif

	if ((cd = get_cfdata(0)) == NULL)	/* get first item */
		errx(1, "failed to get first cfdata");

	while (cd->cf_attach != 0) {
		maxdev = i;
		totdev = i;

		ln = get_locnamp(cd->cf_locnames);
		while (*ln != -1) {
			if (*ln > maxlocnames)
				maxlocnames = *ln;
			ln++;
		}
		i++;
		cd++;
	}

	while (cd->cf_attach == 0) {
		totdev = i;
		i++;
		cd++;
	}

	totdev = totdev - 1;

	if (nopdev == 0) {
		p = (int *)adjust((caddr_t)nl[I_PDEVSIZE].n_value);
		maxpseudo = *p;
	}

	if ((fd = open("/dev/tty", O_RDWR)) < 0)
		fd = 2;

#ifdef NOTDEF
	if (ioctl(fd, TIOCGWINSZ, &w) == 0)
		printf("row %d col %d\n", w.ws_row, w.ws_col);

	if ((s = getenv("LINES")) != NULL)
		sc_height = atoi(s);
	else
		sc_height = tgetnum("li");

	if (sc_height <= 0)
		sc_height = 24;

	if ((s = getenv("COLUMNS")) != NULL)
		sc_width = atoi(s);
	else
		sc_width = tgetnum("co");

	if (sc_width <= 0)
		sc_width = 80;
#endif
}