aboutsummaryrefslogtreecommitdiffstats
path: root/taper.c
blob: 66bdc571c84724d59a3c48951327a0d8f47f5177 (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
#include "config.h"
#include "conf.h"
#include "err.h"
#include "lock.h"
#include "tapeio.h"

#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mtio.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

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

#ifdef NEED_STRINGS
#include <strings.h>
#endif

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

struct tapelist {
	char filen[MAXPATHLEN];
	struct tapelist *next;
};

int canjmp = 0;
int taperunning = 0;
sigjmp_buf child_buf;

void
sig_child(int signo)
{
	int status;

	(void)wait(&status);

	taperunning = 0;
	if (canjmp)
		siglongjmp(child_buf, 1);
}

void
tapefile(int tapefd, const char *filen)
{
	char buffer[BUFFSIZE];
	char *vol;
	fileheader_t header;
	ssize_t total = 0, readb;
	time_t start;
	int fd;

	setproctitle("Dumping %s to tape", filen);

	fd = open(filen, O_RDONLY);
	if (fd == -1) {
		err("%s: couldn't open: %s.\n", filen, strerror(errno));
		return;
	}

	/* Write the header. */
	header.type = FILEMARK;
	header.date = time(NULL);

	/* Extract host and volume names from filename. */
	vol = (char *)rindex(filen, '/');
	if (vol)
		strncpy(buffer, vol+1, sizeof(buffer));
	else
		strncpy(buffer, filen, sizeof(buffer));

	vol = index(buffer, ':');
	if (vol) {
		*vol = '\0';
		strncpy(header.vol, vol+1, sizeof(header.vol));
	} else
		header.vol[0] = '\0';
	strncpy(header.host, buffer, sizeof(header.host));

	if (writeheader(tapefd, &header) == -1) {
		err("%s: couldn't write file header: %s.\n",
		    filen, strerror(errno));
		return;
	}

	/* Write the data. */
	start = time(NULL);
	while ((readb = read(fd, buffer, sizeof(buffer))) > 0) {
		ssize_t wrote = 0, n;

		total += readb;
		while (wrote < readb) {
			n = write(tapefd, buffer, readb);
			if (n == -1) {
				err("%s: couldn't write to tape: %s.\n",
				    filen, strerror(errno));
				close(fd);
				return;
			}
			wrote += n;
		}
	}
	if (readb == -1)
		err("%s: couldn't read from dump: %s.\n", filen,
		    strerror(errno));

	if (mt_weof(tapefd, 1) == -1)
		err("%s: couldn't write EOF marker: %s.\n",
		    filen, strerror(errno));

	printf("DEBUG: wrote %d bytes in %ld seconds.\n",
	       total, time(NULL)-start);

	close(fd);
}

int
main(int argc, char *argv[])
{
	struct tapelist *list = NULL;
	const config_t *option;
	char *labelstr, *tapedev;
	struct sigaction sa;
	fileheader_t header;
	tapelabel_t label;
	sigset_t set, oset;
	int tapefd;

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

	option = findopt(LABELSTR);
	if (!option) {
		err("Taper: " LABELSTR " hasn't been set.\n");
		return 1;
	}
	labelstr = option->strvalue;

	option = findopt(TAPEDEV);
	if (!option) {
		err("Taper: " TAPEDEV " hasn't been set.\n");
		return 1;
	}
	tapedev = option->strvalue;

	tapefd = open(tapedev, O_RDWR);
	if (tapefd == -1) {
		err("Taper: Couldn't open tape device %s: %s.\n",
		    tapedev, strerror(errno));
		return 1;
	}

	if (mt_rewind(tapefd) == -1) {
		err("Taper: couldn't rewind tape: %s.\n", strerror(errno));
		return 1;
	}

	if (readlabel(tapefd, &label) == -1) {
		close(tapefd);
		return 1;
	}
	if (memcmp(label.labelstr, labelstr, sizeof(labelstr))) {
		err("Taper: label `%s' doesn't match expected label `%s'.\n",
		    label.labelstr, labelstr);
		return 1;
	}

	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = sig_child;
	sigaction(SIGCHLD, &sa, NULL);

	/* Loop waiting for input from the dumper. */
	for (;;) {
		char buffer[MAXLINE];

		if (!canjmp && sigsetjmp(child_buf, 1)) {
			if (list) {
				struct tapelist *p = list;
				pid_t pid;

				taperunning = 1;
				pid = fork();
				if (pid == -1) {
					err("Taper: couldn't fork: %s.\n",
					    strerror(errno));
					return 1;
				} else if (!pid) {
					tapefile(tapefd, p->filen);
					exit(0);
				}
				list = list->next;
				free(p);
			}
		}
		canjmp = 1;

		if (!fgets(buffer, sizeof(buffer), stdin)) {
			err("Taper: dumper died abnormally.\n");
			break;
		}

		sigemptyset(&set);
		sigaddset(&set, SIGCHLD);
		sigprocmask(SIG_BLOCK, &set, &oset);

		if (!memcmp(buffer, DISK_REQ, sizeof(DISK_REQ)-1)) {
			char *index = buffer+sizeof(DISK_REQ)-1;

			/*
			 * Special case the event when the taper isn't
			 * running and the list is NULL so we can start
			 * the process and continue it when we've dumped
			 * everything so far to tape.
			 */
			index[strlen(index)-1] = '\0';
			if (!list && !taperunning) {
				pid_t pid;

				taperunning = 1;
				pid = fork();
				if (pid == -1) {
					err("Taper: couldn't fork: %s.\n",
					    strerror(errno));
					break;
				} else if (!pid) {
					tapefile(tapefd, index);
					exit(0);
				}
			} else {
				struct tapelist *p, *q = list;

				p = malloc(sizeof(struct tapelist));
				if (!p) {
					err("Taper: couldn't malloc: %s.\n",
					    strerror(errno));
					break;
				}
				strncpy(p->filen, index, sizeof(p->filen));
				p->next = NULL;

				if (q) {
					while (q->next)
						q = q->next;
					q->next = p;
				} else
					list = p;
			}
		}
		if (!memcmp(buffer, DUMP_DONE, sizeof(DUMP_DONE)-1))
			break;

		sigprocmask(SIG_SETMASK, &oset, NULL);
	}

	if (list || taperunning) {
		/*
		 * Make sig_child exit without a siglongjmp, then unblock
		 * the signal.
		 */
		canjmp = 0;
		sigprocmask(SIG_SETMASK, &oset, NULL);

		/* Wait for the first child to exit. */
		sigemptyset(&set);
		sigsuspend(&set);

		/* Then run through the pending files. */
		while (list) {
			struct tapelist *p = list;

			tapefile(tapefd, list->filen);
			list = list->next;
			free(p);
		}
	}

	header.type = STOPMARK;
	if (writeheader(tapefd, &header) == -1) {
		err("Taper: couldn't write end-of-tape marker: %s.\n",
		    strerror(errno));
		close(tapefd);
		return 1;
	}

	close(tapefd);
	return 0;
}