aboutsummaryrefslogtreecommitdiffstats
path: root/run-estimate.c
blob: f97761d01f6f7e3e1a12429d35254ca6d320ef2e (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
#include "config.h"
#include "conf.h"
#include "err.h"

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#ifdef NEED_LIBUTIL
#include <libutil.h>
#endif

RCSID("$Id: run-estimate.c,v 1.1.1.1 1999/02/02 23:29:39 shmit Exp $");

short exit_status = -1;

void
sig_pipe(int signo)
{
	err("SIGPIPE caught.\n");
	exit_status = -2;
}

void
sig_child(int signo)
{
	int status;

	if (wait(&status) == -1) {
		err("Couldn't clean up child: %s.\n", strerror(errno));
		return;
	}

	exit_status = WEXITSTATUS(status);
}

int
init_connection(disklist_t *disklist, long port)
{
	char buffer[MAXLINE];

	printf(PORT_REQ " %ld\n", port);
	fflush(stdout);

	if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
		err("NULL input waiting for ACK.\n");
		return -1;
	}

	if (!strcmp(buffer, REQ_NACK "\n")) {
		err("OK. I'm bailing.\n");
		return -1;
	} else if (strcmp(buffer, REQ_ACK "\n")) {
		err("Don't know what `%s' means, giving up.\n", buffer);
		return -1;
	}

	while (disklist) {
		char authtype[MAXLINE];

		switch (disklist->disk.auth) {
		case RSH:
			strcpy(authtype, AUTH_RSH);
			break;
		case NOAUTH:
		default:
			strcpy(authtype, AUTH_NOAUTH);
		}

		printf("%s %s %s %s %s\n", DISK_REQ,
		       DISK_NAME, disklist->disk.vol,
		       DISK_AUTH, authtype);

		fflush(stdout);

		disklist = disklist->next;

		if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
			err("NULL input waiting for ACK.\n");
			return -1;
		}

		if (!strcmp(buffer, REQ_NACK "\n"))
			/* TODO: remove item from disklist in this event. */
			continue;
		else if (strcmp(buffer, REQ_ACK "\n")) {
			err("Don't know what `%s' means, giving up.\n",
			     buffer);
			return -1;
		}
	}

	printf(DUMP_DONE "\n");
	return 0;
}

int
main(int argc, char *argv[])
{
	char buffer[MAXLINE];
	const config_t *option;
	disklist_t *disklist;
	long port;

	if (read_config(CLIENT_CONFIG_FILE) == -1)
		return 1;

	option = findopt(PORTOPT);
	if (!option) {
		err("Couldn't find " PORTOPT " in config file.\n");
		return 1;
	}
	port = option->numvalue;

	printf(INIT_REQ "\n");
	fflush(stdout);

	if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
		err("NULL input waiting for handshake ACK.\n");
		return 1;
	}

	if (strncmp(buffer, INIT_ACK "\n", sizeof(buffer))) {
		err("Recieved invalid handshake ACK: %s.", buffer);
		return 1;
	}

	if (read_dumptypes(DATADIR "/dumptypes") == -1)
		return 1;

	disklist = read_disklist(DATADIR "/disklist");
	if (!disklist)
		return 1;

	if (init_connection(disklist, port) == -1)
		return 1;

	return 0;
}