summaryrefslogtreecommitdiffstats
path: root/sbin/sysinstall/utils.c
blob: d7f07787b3afe23e58177236be79521a05cebafe (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
/*
 * ----------------------------------------------------------------------------
 * "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.9 1994/10/22 02:35:09 ache Exp $
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <dialog.h>
#include <errno.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 "sysinstall.h"

void
TellEm(char *fmt, ...)
{
	char *p;
	va_list ap;
	p = Malloc(2048);
	va_start(ap,fmt);
	vsnprintf(p, 2048, fmt, ap);
	va_end(ap);
	dialog_msgbox("Progress", p, 3, 75, 0);
	dialog_clear();
	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);
	if (dialog_active)
		dialog_msgbox("Fatal", p, 12, 75, 1);
	else
		fprintf(stderr, "Fatal -- %s", 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, 15, 60)) {
		dialog_clear();
		Abort();
	}
	dialog_clear();
	free(p);
}

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

void
ExitSysinstall()
{
	if (getpid() == 1) {
		if (dialog_active) {
			clear();
			dialog_update();
		}
		if (reboot(RB_AUTOBOOT) == -1)
			if (dialog_active) {
				clear();
				dialog_msgbox(TITLE, "\n\nCan't reboot machine -- hit reset button",
						  5,30,0);
			} else
				fprintf(stderr, "Can't reboot the machine -- hit the reset button");
			while(1);
	} else {
		if (dialog_active) {
			dialog_update();
			end_dialog();
		}
		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 *prefix, char *mountpoint, int do_mkdir)
{
	struct ufs_args ufsargs;
	char dbuf[90];
	char pbuf[90];

	memset(&ufsargs,0,sizeof ufsargs);

	if (prefix)
		strcpy(pbuf,prefix);
	else
		strcpy(pbuf,"");

	strcat(pbuf,mountpoint);

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

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

void
Mkdir(char *path)
{
	TellEm("mkdir %s",path);
	if (mkdir(path, S_IRWXU) == -1) {
		Fatal("Couldn't create directory %s: %s\n",
			path,strerror(errno));
	}
}
OpenPOWER on IntegriCloud