summaryrefslogtreecommitdiffstats
path: root/sbin/sysinstall/utils.c
blob: 243b1912ca74dae8f453950256c92084573e39d6 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * $Id: utils.c,v 1.35 1995/01/14 10:31:29 jkh Exp $
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <dialog.h>
#include <errno.h>

#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/dkbad.h>
#include <sys/disklabel.h>

#include "sysinstall.h"

void
strip_trailing_newlines(char *p)
{
	int len = strlen(p);
	while (len > 0 && p[len-1] == '\n')
		p[--len] = '\0';
}

void
Debug(char *fmt, ...)
{
	char *p;
	va_list ap;
	p = Malloc(2048);
	va_start(ap,fmt);
	vsnprintf(p, 2048, fmt, ap);
	va_end(ap);
	write(debug_fd,"Debug <",7);
	write(debug_fd,p,strlen(p));
	write(debug_fd,">\n\r",3);
	free(p);
}

void
TellEm(char *fmt, ...)
{
	char *p;
	va_list ap;
	p = Malloc(2048);
	va_start(ap,fmt);
	vsnprintf(p, 2048, fmt, ap);
	va_end(ap);
	strip_trailing_newlines(p);
	write(debug_fd,"Progress <",10);
	write(debug_fd,p,strlen(p));
	write(debug_fd,">\n\r",3);
	if(!on_serial) {
		dialog_clear_norefresh();
		dialog_msgbox("Progress", p, -1, -1, 0);
	}
	free(p);
}

void
Fatal(char *fmt, ...)
{
	char *p;
	va_list ap;
	p = Malloc(2048);
	va_start(ap,fmt);
	vsnprintf(p, 2048, fmt, ap);
	va_end(ap);
	strip_trailing_newlines(p);
	if (dialog_active && !on_serial)
		dialog_msgbox("Fatal", p, -1, -1, 1);
	else
		fprintf(stderr, "Fatal -- %s\n", p);
	free(p);
	ExitSysinstall();
}

void
AskAbort(char *fmt, ...)
{
	char *p;
	va_list ap;

	p = Malloc(2048);
	va_start(ap,fmt);
	vsnprintf(p, 2048, fmt, ap);
	va_end(ap);
	strcat(p, "\n\nDo you wish to abort the installation?");
	if (!dialog_yesno("Abort", p, -1, -1)) {
		dialog_clear_norefresh();
		Abort();
	}
	dialog_clear();
	free(p);
}

void
Abort()
{
	if (dialog_yesno("Exit sysinstall","\n\nAre you sure you want to quit?",
						  -1, -1)) {
		dialog_clear();
		return;
	}
	ExitSysinstall();
}

void
ExitSysinstall()
{
	if (dialog_active) {
		clear();
		dialog_update();
	}
	if (getpid() == 1) {
		if (reboot(RB_AUTOBOOT) == -1)
			if (dialog_active) {
				clear();
				dialog_msgbox(TITLE, "\n\nCan't reboot machine -- hit reset button",
						 -1,-1,0);
			} else
				fprintf(stderr, "Can't reboot the machine -- hit the reset button");
			while(1);
	} else {
		if (dialog_active) {
			end_dialog();
			dialog_active = 0;
		}
		exit(0);
	}
}

void *
Malloc(size_t size)
{
	void *p = malloc(size);
	if (!p) {
		exit(7); /* XXX longjmp bad */
	}
	return p;
}

char *
StrAlloc(char *str) 
{
	char *p;

	p = (char *)Malloc(strlen(str) + 1);
	strcpy(p,str);
	return p;
}

void
MountUfs(char *device, char *mountpoint, int do_mkdir, int flags)
{
	struct ufs_args ufsargs;
	char dbuf[90];

	memset(&ufsargs,0,sizeof ufsargs);

	if(do_mkdir && access(mountpoint,R_OK)) {
		Mkdir(mountpoint, TRUE);
	}

	strcpy(dbuf,"/dev/");
	strcat(dbuf,device);
	
	TellEm("mount %s %s",dbuf,mountpoint); 
	ufsargs.fspec = dbuf;
	if (mount(MOUNT_UFS, mountpoint, flags, (caddr_t) &ufsargs) == -1) {
		Fatal("Error mounting %s on %s : %s\n",
			dbuf, mountpoint, strerror(errno));
	}
}

void
Mkdir(char *ipath, int die)
{
	struct stat sb;
	int final=0;
	char *p,*path=StrAlloc(ipath);

	Debug("mkdir(%s)",path);
	p = path;
	if (p[0] == '/')		/* Skip leading '/'. */
		++p;
	for (;!final; ++p) {
		if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
			final++;
		else if (p[0] != '/')
			continue;
		*p = '\0';
		if (stat(path, &sb)) {
			if (errno != ENOENT && die)
				Fatal("Couldn't stat directory %s: %s\n",
					path,strerror(errno));
			Debug("mkdir(%s..)",path);
		        if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 &&
			    die)
				Fatal("Couldn't create directory %s: %s\n",
					path,strerror(errno));
		}
		*p = '/';
	}
	free(path);
	return;
}

void
Link(char *from, char *to)
{
	TellEm("ln %s %s", from, to);
	if(fixit)
	    unlink(to);
	if (link(from, to) == -1)
		Fatal("Couldn't create link: %s -> %s\n", from, to);
}

void
CopyFile(char *p1, char *p2)
{
	char buf[BUFSIZ];
	int fd1,fd2;
	int i;
	struct stat st;

	TellEm("Copy %s to %s",p1,p2);
	fd1 = open(p1,O_RDONLY);
	if (fd1 < 0) Fatal("Couldn't open %s: %s\n",p1,strerror(errno));
	fd2 = open(p2,O_TRUNC|O_CREAT|O_WRONLY,0200);
	if (fd2 < 0) Fatal("Couldn't open %s: %s\n",p2,strerror(errno));
	for(;;) {
		i = read(fd1,buf,sizeof buf);
		if (i > 0)
			if (i != write(fd2,buf,i)) {
				Fatal("Write errror on %s: %s\n",
					p2,strerror(errno));
			}
		if (i != sizeof buf)
			break;
	}
	fstat(fd1,&st);
	fchmod(fd2,st.st_mode & 07777);
	fchown(fd2,st.st_uid,st.st_gid);
	close(fd1);
	close(fd2);
}

u_long
PartMb(struct disklabel *lbl,int part)
{
	u_long l;
	l = 1024*1024/lbl->d_secsize;
	return (lbl->d_partitions[part].p_size + l/2)/l;
}

void
CleanMount(int disk, int part)
{
    int i = MP[disk][part];
    Faction[i] = 0;
    if (Fmount[i]) {
        free(Fmount[i]);
        Fmount[i] = 0;
    }           
    if (Fname[i]) {
        free(Fname[i]);
        Fname[i] = 0;
    }       
    if (Ftype[i]) {
        free(Ftype[i]);
        Ftype[i] = 0;
    }
    MP[disk][part] = 0;
}

char *
SetMount(int disk, int part, char *path)
{
    int k;
    char buf[80];

    CleanMount(disk,part);
    for (k = 1; k < MAX_NO_FS; k++)
	if (!Fmount[k])
	    break;

    if (k >= MAX_NO_FS) 
	return "Maximum number of filesystems exceeded";

    Fmount[k] = StrAlloc(path);
    sprintf(buf, "%s%c", Dname[disk], part + 'a');
    Fname[k] = StrAlloc(buf);
    switch (Dlbl[disk]->d_partitions[part].p_fstype) {
	case FS_BSDFFS:
	    Ftype[k] = StrAlloc("ufs");
	    if(!fixit)
		Faction[k] = 1;
	    break;
	case FS_MSDOS:
	    Ftype[k] = StrAlloc("msdos");
	    Faction[k] = 0;
	    break;
	case FS_SWAP:
	    Ftype[k] = StrAlloc("swap");
	    Faction[k] = 1;
	    break;
	default:
	    CleanMount(disk,part);
	    return "Unknown filesystem-type";
    }
    Fsize[k] = (Dlbl[disk]->d_partitions[part].p_size+1024)/2048;
    
    MP[disk][part] = k;
    return NULL;
}

void
enable_label(int fd)
{ 
	int flag = 1;
	if (ioctl(fd, DIOCWLABEL, &flag) < 0) 
	    Fatal("ioctl(DIOCWLABEL,1) failed: %s",strerror(errno));
}

void
disable_label(int fd)
{  
	int flag = 0;
	if (ioctl(fd, DIOCWLABEL, &flag) < 0) 
	    Fatal("ioctl(DIOCWLABEL,0) failed: %s",strerror(errno));
}

OpenPOWER on IntegriCloud