Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sbin/dump/optr.c
39476 views
1
/*-
2
* SPDX-License-Identifier: BSD-3-Clause
3
*
4
* Copyright (c) 1980, 1988, 1993
5
* The Regents of the University of California. All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
* 3. Neither the name of the University nor the names of its contributors
16
* may be used to endorse or promote products derived from this software
17
* without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
* SUCH DAMAGE.
30
*/
31
32
#include <sys/param.h>
33
#include <sys/queue.h>
34
#include <sys/wait.h>
35
36
#include <ufs/ufs/dinode.h>
37
38
#include <errno.h>
39
#include <fstab.h>
40
#include <grp.h>
41
#include <limits.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <stdarg.h>
46
#include <signal.h>
47
#include <time.h>
48
#include <unistd.h>
49
50
#include "dump.h"
51
#include "pathnames.h"
52
53
void alarmcatch(int);
54
int datesort(const void *, const void *);
55
56
/*
57
* Query the operator; This previously-fascist piece of code
58
* no longer requires an exact response.
59
* It is intended to protect dump aborting by inquisitive
60
* people banging on the console terminal to see what is
61
* happening which might cause dump to croak, destroying
62
* a large number of hours of work.
63
*
64
* Every 2 minutes we reprint the message, alerting others
65
* that dump needs attention.
66
*/
67
static int timeout;
68
static const char *attnmessage; /* attention message */
69
70
int
71
query(const char *question)
72
{
73
char replybuffer[64];
74
int back, errcount;
75
FILE *mytty;
76
77
if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
78
quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
79
attnmessage = question;
80
timeout = 0;
81
alarmcatch(0);
82
back = -1;
83
errcount = 0;
84
do {
85
if (fgets(replybuffer, 63, mytty) == NULL) {
86
clearerr(mytty);
87
if (++errcount > 30) /* XXX ugly */
88
quit("excessive operator query failures\n");
89
} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
90
back = 1;
91
} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
92
back = 0;
93
} else {
94
(void) fprintf(stderr,
95
" DUMP: \"Yes\" or \"No\"?\n");
96
(void) fprintf(stderr,
97
" DUMP: %s: (\"yes\" or \"no\") ", question);
98
}
99
} while (back < 0);
100
101
/*
102
* Turn off the alarm, and reset the signal to trap out..
103
*/
104
(void) alarm(0);
105
if (signal(SIGALRM, sig) == SIG_IGN)
106
signal(SIGALRM, SIG_IGN);
107
(void) fclose(mytty);
108
return(back);
109
}
110
111
char lastmsg[BUFSIZ];
112
113
/*
114
* Alert the console operator, and enable the alarm clock to
115
* sleep for 2 minutes in case nobody comes to satisfy dump
116
*/
117
void
118
alarmcatch(int sig __unused)
119
{
120
if (notify == 0) {
121
if (timeout == 0)
122
(void) fprintf(stderr,
123
" DUMP: %s: (\"yes\" or \"no\") ",
124
attnmessage);
125
else
126
msgtail("\a\a");
127
} else {
128
if (timeout) {
129
msgtail("\n");
130
broadcast(""); /* just print last msg */
131
}
132
(void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
133
attnmessage);
134
}
135
signal(SIGALRM, alarmcatch);
136
(void) alarm(120);
137
timeout = 1;
138
}
139
140
/*
141
* Here if an inquisitive operator interrupts the dump program
142
*/
143
void
144
interrupt(int signo __unused)
145
{
146
msg("Interrupt received.\n");
147
if (query("Do you want to abort dump?"))
148
dumpabort(0);
149
}
150
151
/*
152
* We now use wall(1) to do the actual broadcasting.
153
*/
154
void
155
broadcast(const char *message)
156
{
157
FILE *fp;
158
char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
159
160
if (!notify)
161
return;
162
163
snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
164
if ((fp = popen(buf, "w")) == NULL)
165
return;
166
167
(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
168
if (lastmsg[0])
169
(void) fputs(lastmsg, fp);
170
if (message[0])
171
(void) fputs(message, fp);
172
173
(void) pclose(fp);
174
}
175
176
/*
177
* Print out an estimate of the amount of time left to do the dump
178
*/
179
180
time_t tschedule = 0;
181
182
void
183
timeest(void)
184
{
185
double percent;
186
time_t tnow, tdone;
187
char *tdone_str;
188
int deltat, hours, mins;
189
190
(void)time(&tnow);
191
if (blockswritten > tapesize) {
192
setproctitle("%s: 99.99%% done, finished soon", disk);
193
if (tnow >= tschedule) {
194
tschedule = tnow + 300;
195
msg("99.99%% done, finished soon\n");
196
}
197
} else {
198
deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
199
(double)(tnow - tstart_writing) / blockswritten * tapesize;
200
tdone = tnow + deltat;
201
percent = (blockswritten * 100.0) / tapesize;
202
hours = deltat / 3600;
203
mins = (deltat % 3600) / 60;
204
205
tdone_str = ctime(&tdone);
206
tdone_str[strlen(tdone_str) - 1] = '\0';
207
setproctitle(
208
"%s: pass %d: %3.2f%% done, finished in %d:%02d at %s",
209
disk, passno, percent, hours, mins, tdone_str);
210
if (tnow >= tschedule) {
211
tschedule = tnow + 300;
212
if (blockswritten < 500)
213
return;
214
msg("%3.2f%% done, finished in %d:%02d at %s\n", percent,
215
hours, mins, tdone_str);
216
}
217
}
218
}
219
220
/*
221
* Schedule a printout of the estimate in the next call to timeest().
222
*/
223
void
224
infosch(int signal __unused)
225
{
226
tschedule = 0;
227
}
228
229
void
230
msg(const char *fmt, ...)
231
{
232
va_list ap;
233
234
(void) fprintf(stderr," DUMP: ");
235
#ifdef TDEBUG
236
(void) fprintf(stderr, "pid=%d ", getpid());
237
#endif
238
va_start(ap, fmt);
239
(void) vfprintf(stderr, fmt, ap);
240
va_end(ap);
241
(void) fflush(stdout);
242
(void) fflush(stderr);
243
va_start(ap, fmt);
244
(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
245
va_end(ap);
246
}
247
248
void
249
msgtail(const char *fmt, ...)
250
{
251
va_list ap;
252
253
va_start(ap, fmt);
254
(void) vfprintf(stderr, fmt, ap);
255
va_end(ap);
256
}
257
258
void
259
quit(const char *fmt, ...)
260
{
261
va_list ap;
262
263
(void) fprintf(stderr," DUMP: ");
264
#ifdef TDEBUG
265
(void) fprintf(stderr, "pid=%d ", getpid());
266
#endif
267
va_start(ap, fmt);
268
(void) vfprintf(stderr, fmt, ap);
269
va_end(ap);
270
(void) fflush(stdout);
271
(void) fflush(stderr);
272
dumpabort(0);
273
}
274
275
/*
276
* Tell the operator what has to be done;
277
* we don't actually do it
278
*/
279
280
struct fstab *
281
allocfsent(const struct fstab *fs)
282
{
283
struct fstab *new;
284
285
new = (struct fstab *)malloc(sizeof (*fs));
286
if (new == NULL ||
287
(new->fs_file = strdup(fs->fs_file)) == NULL ||
288
(new->fs_type = strdup(fs->fs_type)) == NULL ||
289
(new->fs_spec = strdup(fs->fs_spec)) == NULL)
290
quit("%s\n", strerror(errno));
291
new->fs_passno = fs->fs_passno;
292
new->fs_freq = fs->fs_freq;
293
return (new);
294
}
295
296
struct pfstab {
297
SLIST_ENTRY(pfstab) pf_list;
298
struct fstab *pf_fstab;
299
};
300
301
static SLIST_HEAD(, pfstab) table;
302
303
void
304
dump_getfstab(void)
305
{
306
struct fstab *fs;
307
struct pfstab *pf;
308
309
if (setfsent() == 0) {
310
msg("Can't open %s for dump table information: %s\n",
311
_PATH_FSTAB, strerror(errno));
312
return;
313
}
314
while ((fs = getfsent()) != NULL) {
315
if ((strcmp(fs->fs_type, FSTAB_RW) &&
316
strcmp(fs->fs_type, FSTAB_RO) &&
317
strcmp(fs->fs_type, FSTAB_RQ)) ||
318
strcmp(fs->fs_vfstype, "ufs"))
319
continue;
320
fs = allocfsent(fs);
321
if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
322
quit("%s\n", strerror(errno));
323
pf->pf_fstab = fs;
324
SLIST_INSERT_HEAD(&table, pf, pf_list);
325
}
326
(void) endfsent();
327
}
328
329
/*
330
* Search in the fstab for a file name.
331
* This file name can be either the special or the path file name.
332
*
333
* The file name can omit the leading '/'.
334
*/
335
struct fstab *
336
fstabsearch(const char *key)
337
{
338
struct pfstab *pf;
339
struct fstab *fs;
340
char *rn;
341
342
SLIST_FOREACH(pf, &table, pf_list) {
343
fs = pf->pf_fstab;
344
if (strcmp(fs->fs_file, key) == 0 ||
345
strcmp(fs->fs_spec, key) == 0)
346
return (fs);
347
rn = rawname(fs->fs_spec);
348
if (rn != NULL && strcmp(rn, key) == 0)
349
return (fs);
350
if (key[0] != '/') {
351
if (*fs->fs_spec == '/' &&
352
strcmp(fs->fs_spec + 1, key) == 0)
353
return (fs);
354
if (*fs->fs_file == '/' &&
355
strcmp(fs->fs_file + 1, key) == 0)
356
return (fs);
357
}
358
}
359
return (NULL);
360
}
361
362
/*
363
* Tell the operator what to do
364
*/
365
void
366
lastdump(int arg) /* w ==> just what to do; W ==> most recent dumps */
367
{
368
int i;
369
struct fstab *dt;
370
struct dumpdates *dtwalk;
371
char *lastname, *date;
372
int dumpme;
373
time_t tnow;
374
struct tm *tlast;
375
376
(void) time(&tnow);
377
dump_getfstab(); /* /etc/fstab input */
378
initdumptimes(); /* /etc/dumpdates input */
379
qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
380
381
if (arg == 'w')
382
(void) printf("Dump these file systems:\n");
383
else
384
(void) printf("Last dump(s) done (Dump '>' file systems):\n");
385
lastname = "??";
386
ITITERATE(i, dtwalk) {
387
if (strncmp(lastname, dtwalk->dd_name,
388
sizeof(dtwalk->dd_name)) == 0)
389
continue;
390
date = (char *)ctime(&dtwalk->dd_ddate);
391
date[16] = '\0'; /* blast away seconds and year */
392
lastname = dtwalk->dd_name;
393
dt = fstabsearch(dtwalk->dd_name);
394
dumpme = (dt != NULL && dt->fs_freq != 0);
395
if (dumpme) {
396
tlast = localtime(&dtwalk->dd_ddate);
397
dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
398
- (tlast->tm_min * 60) - tlast->tm_sec
399
+ (dt->fs_freq * 86400));
400
}
401
if (arg != 'w' || dumpme)
402
(void) printf(
403
"%c %8s\t(%6s) Last dump: Level %d, Date %s\n",
404
dumpme && (arg != 'w') ? '>' : ' ',
405
dtwalk->dd_name,
406
dt ? dt->fs_file : "",
407
dtwalk->dd_level,
408
date);
409
}
410
}
411
412
int
413
datesort(const void *a1, const void *a2)
414
{
415
struct dumpdates *d1 = *(struct dumpdates **)a1;
416
struct dumpdates *d2 = *(struct dumpdates **)a2;
417
int diff;
418
419
diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
420
if (diff == 0)
421
return (d2->dd_ddate - d1->dd_ddate);
422
return (diff);
423
}
424
425