#include <stdio.h>
#include <err.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define NEEDS_DFUI_STRUCTURE_DEFINITIONS
#define NEEDS_DISKUTIL_STRUCTURE_DEFINITIONS
#include "libdfui/dfui.h"
#include "libinstaller/functions.h"
#include "libinstaller/diskutil.h"
#include "libinstaller/commands.h"
#ifdef DEBUG
#include "libdfui/dump.h"
#endif
#include "libaura/fspred.h"
#define TMPDIR "/tmp/installer/temp"
void test_storage(struct i_fn_args *);
void libinstaller_backend(struct i_fn_args **);
void libinstaller_frontend(struct dfui_connection **);
void libinstaller_form_dump(struct dfui_form *);
void (*tstate)(struct i_fn_args *) = NULL;
void
check_user(void)
{
if (getuid() != 0) {
warnx(
"\n--------------------------------------------------\n"
"It is not recommended that you run this program\n"
"as a regular user since many of the tasks the\n"
"installer performs must be run as root\n"
"(such as fdisk, disklabel, ...)\n"
"Please run as root to get correct results.\n"
"--------------------------------------------------\n"
);
sleep(2);
}
}
void
test_storage(struct i_fn_args *a)
{
struct disk *dsk, *dsk_next;
int r;
r = survey_storage(a);
printf("survey_storage rc=%d\n", r);
printf("Found disks:\n");
dsk = a->s->disk_head;
while (dsk != NULL) {
dsk_next = dsk->next;
printf("%s %ld MB\n",
dsk->device, dsk->capacity);
dsk = dsk_next;
}
tstate = NULL;
}
void
libinstaller_backend(struct i_fn_args **a)
{
struct i_fn_args *ap = *a;
ap = i_fn_args_new("/", TMPDIR, "tmp/cmdnames.conf",
DFUI_TRANSPORT_TCP, "9999");
if (ap == NULL)
errx(1, "Failed to start the installer backend");
tstate = test_storage;
for (; tstate != NULL; ) {
tstate(ap);
}
i_fn_args_free(ap);
}
void
libinstaller_frontend(struct dfui_connection **c) {
struct dfui_connection *cp = *c;
struct dfui_response *r;
char msgtype;
void *payload;
int done = 0;
usleep(100000);
cp = dfui_connection_new(DFUI_TRANSPORT_TCP, "9999");
dfui_fe_connect(cp);
while (!done) {
dfui_fe_receive(cp, &msgtype, &payload);
switch (msgtype) {
case DFUI_BE_MSG_PRESENT:
#ifdef DEBUG
struct dfui_form *f;
f = (struct dfui_form *)payload;
dfui_form_dump(f);
#endif
r = dfui_response_new("dialog", "Cancel");
dfui_fe_submit(cp, r);
dfui_response_free(r);
sleep(1);
break;
case DFUI_BE_MSG_PROG_BEGIN:
case DFUI_BE_MSG_PROG_UPDATE:
case DFUI_BE_MSG_PROG_END:
dfui_fe_progress_continue(cp);
break;
case DFUI_BE_MSG_STOP:
dfui_fe_confirm_stop(cp);
done = 1;
break;
default:
printf("msgtype=%c\n", msgtype);
sleep(1);
}
}
dfui_fe_disconnect(cp);
}
int
main(int argc __unused, char **argv __unused)
{
struct dfui_connection *c;
struct i_fn_args *a;
int error;
char *path;
int r;
check_user();
asprintf(&path, "mkdir -p %s", TMPDIR);
system(path);
free(path);
r = fork();
switch(r) {
case -1:
err(1, "Failed to fork");
case 0:
libinstaller_backend(&a);
break;
default:
libinstaller_frontend(&c);
}
return(0);
}