GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/config/ukc.c Lines: 0 99 0.0 %
Date: 2017-11-13 Branches: 0 78 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ukc.c,v 1.23 2017/09/27 15:14:52 deraadt 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
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#include <sys/types.h>
28
#include <sys/device.h>
29
30
#include <err.h>
31
#include <fcntl.h>
32
#include <kvm.h>
33
#include <limits.h>
34
#include <nlist.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h>
39
40
#define UKC_MAIN
41
#include "ukc.h"
42
#include "exec.h"
43
44
void		init(void);
45
__dead void	usage(void);
46
47
int	ukc_mod_kernel = 0;
48
49
static void
50
check_int(int idx, const char *name)
51
{
52
	if (nl[idx].n_type == 0)
53
		printf("WARNING this kernel doesn't support modification "
54
		    "of %s.\n", name);
55
}
56
57
int
58
ukc(char *file, char *outfile, int uflag, int force)
59
{
60
	int i;
61
	kvm_t *kd;
62
	char errbuf[_POSIX2_LINE_MAX];
63
	int histlen = 0, ok = 1;
64
	char history[1024], kversion[1024];
65
66
	if (file == NULL) {
67
		warnx("no file specified");
68
		usage();
69
	}
70
71
	loadkernel(file);
72
73
	if (nlist(file, nl) == -1)
74
		errx(1, "nlist: %s", file);
75
76
	if (uflag) {
77
		if ((kd = kvm_openfiles(NULL,NULL,NULL,O_RDONLY, errbuf)) == 0)
78
			errx(1, "kvm_openfiles: %s", errbuf);
79
80
		if (kvm_nlist(kd, knl) == -1)
81
			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
82
83
		i = 0;
84
		while (i < NLENTRIES) {
85
			if (nl[i].n_type != knl[i].n_type ||
86
			    nl[i].n_desc != knl[i].n_desc ||
87
			    nl[i].n_value != knl[i].n_value)
88
				ok = 0;
89
			i++;
90
		}
91
92
		if (knl[I_HISTLEN].n_type != 0 && ok) {
93
			if (kvm_read(kd, knl[I_HISTLEN].n_value, &histlen,
94
			    sizeof(histlen)) != sizeof(histlen))
95
				warnx("cannot read %s: %s",
96
				    knl[I_HISTLEN].n_name,
97
				    kvm_geterr(kd));
98
		}
99
		if (knl[CA_HISTORY].n_type != 0 && ok) {
100
			if (kvm_read(kd, knl[CA_HISTORY].n_value, history,
101
			    sizeof(history)) != sizeof(history))
102
				warnx("cannot read %s: %s",
103
				    knl[CA_HISTORY].n_name,
104
				    kvm_geterr(kd));
105
		}
106
		if (knl[P_VERSION].n_type != 0 && ok) {
107
			if (kvm_read(kd, knl[P_VERSION].n_value, kversion,
108
			    sizeof(kversion)) != sizeof(kversion))
109
				warnx("cannot read %s: %s",
110
				    knl[P_VERSION].n_name,
111
				    kvm_geterr(kd));
112
		}
113
	}
114
115
	if (force == 0 && outfile == NULL)
116
		printf("WARNING no output file specified\n");
117
118
	if (nl[IA_EXTRALOC].n_type == 0 || nl[I_REXTRALOC].n_type == 0 ||
119
	    nl[I_TEXTRALOC].n_type == 0 ||
120
	    nl[I_HISTLEN].n_type == 0 || nl[CA_HISTORY].n_type == 0) {
121
		printf("\
122
WARNING this kernel doesn't contain all information needed!\n\
123
WARNING the commands add and change might not work.\n");
124
		oldkernel = 1;
125
	}
126
127
	if (nl[P_PDEVNAMES].n_type == 0 ||
128
	    nl[I_PDEVSIZE].n_type == 0 ||
129
	    nl[S_PDEVINIT].n_type == 0) {
130
		printf("\
131
WARNING this kernel doesn't support pseudo devices.\n");
132
		nopdev = 1;
133
	}
134
135
	check_int(I_BUFCACHEPCT, "BUFCACHEPERCENT");
136
	check_int(I_NKMEMPG, "NKMEMPAGES");
137
138
	init();
139
140
	if (uflag) {
141
		if (ok) {
142
			if (strcmp(adjust((caddr_t)nl[P_VERSION].n_value),
143
			    kversion) != 0)
144
				ok = 1;
145
		}
146
		if (!ok) {
147
			printf("WARNING kernel mismatch. -u ignored.\n");
148
			printf("WARNING the running kernel version:\n");
149
			printf("%s", kversion);
150
		} else
151
			process_history(histlen, history);
152
	}
153
154
	printf("%s", adjust((caddr_t)nl[P_VERSION].n_value));
155
156
	if (config()) {
157
		if (force == 0 && outfile == NULL) {
158
			fprintf(stderr, "not forced\n");
159
			return (1);
160
		}
161
		if (outfile == NULL)
162
			outfile = file;
163
		if (ukc_mod_kernel == 0) {
164
			fprintf(stderr, "Kernel not modified\n");
165
			return (1);
166
		} else {
167
			printf ("Saving modified kernel.\n");
168
			savekernel(outfile);
169
		}
170
	}
171
	return(0);
172
}
173
174
void
175
init(void)
176
{
177
	int i = 0;
178
	struct cfdata *cd;
179
	short	*ln;
180
	int	*p;
181
182
	if ((cd = get_cfdata(0)) == NULL)	/* get first item */
183
		errx(1, "failed to get first cfdata");
184
185
	while (cd->cf_attach != 0) {
186
		maxdev = i;
187
		totdev = i;
188
189
		ln = get_locnamp(cd->cf_locnames);
190
		while (*ln != -1) {
191
			if (*ln > maxlocnames)
192
				maxlocnames = *ln;
193
			ln++;
194
		}
195
		i++;
196
		cd++;
197
	}
198
199
	while (cd->cf_attach == 0) {
200
		totdev = i;
201
		i++;
202
		cd++;
203
	}
204
205
	totdev = totdev - 1;
206
207
	if (nopdev == 0) {
208
		p = (int *)adjust((caddr_t)nl[I_PDEVSIZE].n_value);
209
		maxpseudo = *p;
210
	}
211
}