1 /* $NetBSD: recover.c,v 1.4 2013/11/30 14:54:29 christos Exp $ */
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: recover.c,v 10.31 2001/11/01 15:24:44 skimo Exp (Berkeley) Date: 2001/11/01 15:24:44 ";
17 #include <sys/param.h>
18 #include <sys/types.h> /* XXX: param.h may not have included types.h */
19 #include <sys/queue.h>
23 * We include <sys/file.h>, because the open #defines were found there
24 * on historical systems. We also include <fcntl.h> because the open(2)
25 * #defines are found there on newer systems.
29 #include <bitstring.h>
42 #include "pathnames.h"
47 * The basic scheme is as follows. In the EXF structure, we maintain full
48 * paths of a b+tree file and a mail recovery file. The former is the file
49 * used as backing store by the DB package. The latter is the file that
50 * contains an email message to be sent to the user if we crash. The two
51 * simple states of recovery are:
53 * + first starting the edit session:
54 * the b+tree file exists and is mode 700, the mail recovery
56 * + after the file has been modified:
57 * the b+tree file exists and is mode 600, the mail recovery
58 * file exists, and is exclusively locked.
60 * In the EXF structure we maintain a file descriptor that is the locked
61 * file descriptor for the mail recovery file. NOTE: we sometimes have to
62 * do locking with fcntl(2). This is a problem because if you close(2) any
63 * file descriptor associated with the file, ALL of the locks go away. Be
64 * sure to remember that if you have to modify the recovery code. (It has
65 * been rhetorically asked of what the designers could have been thinking
66 * when they did that interface. The answer is simple: they weren't.)
68 * To find out if a recovery file/backing file pair are in use, try to get
69 * a lock on the recovery file.
71 * To find out if a backing file can be deleted at boot time, check for an
72 * owner execute bit. (Yes, I know it's ugly, but it's either that or put
73 * special stuff into the backing file itself, or correlate the files at
74 * boot time, neither of which looks like fun.) Note also that there's a
75 * window between when the file is created and the X bit is set. It's small,
76 * but it's there. To fix the window, check for 0 length files as well.
78 * To find out if a file can be recovered, check the F_RCV_ON bit. Note,
79 * this DOES NOT mean that any initialization has been done, only that we
80 * haven't yet failed at setting up or doing recovery.
82 * To preserve a recovery file/backing file pair, set the F_RCV_NORM bit.
83 * If that bit is not set when ending a file session:
84 * If the EXF structure paths (rcv_path and rcv_mpath) are not NULL,
85 * they are unlink(2)'d, and free(3)'d.
86 * If the EXF file descriptor (rcv_fd) is not -1, it is closed.
88 * The backing b+tree file is set up when a file is first edited, so that
89 * the DB package can use it for on-disk caching and/or to snapshot the
90 * file. When the file is first modified, the mail recovery file is created,
91 * the backing file permissions are updated, the file is sync(2)'d to disk,
92 * and the timer is started. Then, at RCV_PERIOD second intervals, the
93 * b+tree file is synced to disk. RCV_PERIOD is measured using SIGALRM, which
94 * means that the data structures (SCR, EXF, the underlying tree structures)
95 * must be consistent when the signal arrives.
97 * The recovery mail file contains normal mail headers, with two additions,
98 * which occur in THIS order, as the FIRST TWO headers:
100 * X-vi-recover-file: file_name
101 * X-vi-recover-path: recover_path
103 * Since newlines delimit the headers, this means that file names cannot have
104 * newlines in them, but that's probably okay. As these files aren't intended
105 * to be long-lived, changing their format won't be too painful.
107 * Btree files are named "vi.XXXX" and recovery files are named "recover.XXXX".
110 #define VI_FHEADER "X-vi-recover-file: "
111 #define VI_PHEADER "X-vi-recover-path: "
113 static int rcv_copy
__P((SCR
*, int, char *));
114 static void rcv_email
__P((SCR
*, char *));
115 static char *rcv_gets
__P((char *, size_t, int));
116 static int rcv_mailfile
__P((SCR
*, int, char *));
117 static int rcv_mktemp
__P((SCR
*, char *, const char *, int));
121 * Build a file name that will be used as the recovery file.
123 * PUBLIC: int rcv_tmp __P((SCR *, EXF *, char *));
126 rcv_tmp(SCR
*sp
, EXF
*ep
, char *name
)
130 char path
[MAXPATHLEN
];
135 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
138 * If the recovery directory doesn't exist, try and create it. As
139 * the recovery files are themselves protected from reading/writing
140 * by other than the owner, the worst that can happen is that a user
141 * would have permission to remove other user's recovery files. If
142 * the sticky bit has the BSD semantics, that too will be impossible.
144 if (opts_empty(sp
, O_RECDIR
, 0))
146 dp
= O_STR(sp
, O_RECDIR
);
148 if (errno
!= ENOENT
|| mkdir(dp
, 0)) {
149 msgq(sp
, M_SYSERR
, "%s", dp
);
152 (void)chmod(dp
, S_IRWXU
| S_IRWXG
| S_IRWXO
| S_ISVTX
);
155 /* Newlines delimit the mail messages. */
156 if (strchr(name
, '\n')) {
158 "055|Files with newlines in the name are unrecoverable");
162 (void)snprintf(path
, sizeof(path
), "%s/vi.XXXXXX", dp
);
163 if ((fd
= rcv_mktemp(sp
, path
, dp
, S_IRWXU
)) == -1)
167 if ((ep
->rcv_path
= strdup(path
)) == NULL
) {
168 msgq(sp
, M_SYSERR
, NULL
);
171 "056|Modifications not recoverable if the session fails");
175 /* We believe the file is recoverable. */
182 * Force the file to be snapshotted for recovery.
184 * PUBLIC: int rcv_init __P((SCR *));
194 /* Only do this once. */
195 F_CLR(ep
, F_FIRSTMODIFY
);
197 /* If we already know the file isn't recoverable, we're done. */
198 if (!F_ISSET(ep
, F_RCV_ON
))
201 /* Turn off recoverability until we figure out if this will work. */
204 /* Test if we're recovering a file, not editing one. */
205 if (ep
->rcv_mpath
== NULL
) {
206 /* Build a file to mail to the user. */
207 if (rcv_mailfile(sp
, 0, NULL
))
210 /* Force a read of the entire file. */
211 if (db_last(sp
, &lno
))
214 /* Turn on a busy message, and sync it to backing store. */
216 "057|Copying file for recovery...", BUSY_ON
);
217 if (ep
->db
->sync(ep
->db
, 0)) {
218 msgq_str(sp
, M_SYSERR
, ep
->rcv_path
,
219 "058|Preservation failed: %s");
220 sp
->gp
->scr_busy(sp
, NULL
, BUSY_OFF
);
223 sp
->gp
->scr_busy(sp
, NULL
, BUSY_OFF
);
226 /* Turn off the owner execute bit. */
227 (void)chmod(ep
->rcv_path
, S_IRUSR
| S_IWUSR
);
229 /* We believe the file is recoverable. */
234 "059|Modifications not recoverable if the session fails");
240 * Sync the file, optionally:
241 * flagging the backup file to be preserved
242 * snapshotting the backup file and send email to the user
243 * sending email to the user if the file was modified
244 * ending the file session
246 * PUBLIC: int rcv_sync __P((SCR *, u_int));
249 rcv_sync(SCR
*sp
, u_int flags
)
256 /* Make sure that there's something to recover/sync. */
258 if (ep
== NULL
|| !F_ISSET(ep
, F_RCV_ON
))
261 /* Sync the file if it's been modified. */
262 if (F_ISSET(ep
, F_MODIFIED
)) {
264 * If we are using a db1 version of the database,
265 * we want to sync the underlying btree not the
266 * recno tree which is transient anyway.
269 #define R_RECNOSYNC 0
271 if (ep
->db
->sync(ep
->db
, R_RECNOSYNC
)) {
272 F_CLR(ep
, F_RCV_ON
| F_RCV_NORM
);
273 msgq_str(sp
, M_SYSERR
,
274 ep
->rcv_path
, "060|File backup failed: %s");
278 /* REQUEST: don't remove backing file on exit. */
279 if (LF_ISSET(RCV_PRESERVE
))
280 F_SET(ep
, F_RCV_NORM
);
282 /* REQUEST: send email. */
283 if (LF_ISSET(RCV_EMAIL
))
284 rcv_email(sp
, ep
->rcv_mpath
);
289 * Each time the user exec's :preserve, we have to snapshot all of
290 * the recovery information, i.e. it's like the user re-edited the
291 * file. We copy the DB(3) backing file, and then create a new mail
292 * recovery file, it's simpler than exiting and reopening all of the
295 * REQUEST: snapshot the file.
298 if (LF_ISSET(RCV_SNAPSHOT
)) {
299 if (opts_empty(sp
, O_RECDIR
, 0))
301 dp
= O_STR(sp
, O_RECDIR
);
302 (void)snprintf(buf
, sizeof(buf
), "%s/vi.XXXXXX", dp
);
303 if ((fd
= rcv_mktemp(sp
, buf
, dp
, S_IRUSR
| S_IWUSR
)) == -1)
306 "061|Copying file for recovery...", BUSY_ON
);
307 if (rcv_copy(sp
, fd
, ep
->rcv_path
) ||
308 close(fd
) || rcv_mailfile(sp
, 1, buf
)) {
313 sp
->gp
->scr_busy(sp
, NULL
, BUSY_OFF
);
319 /* REQUEST: end the file session. */
320 if (LF_ISSET(RCV_ENDSESSION
) && file_end(sp
, NULL
, 1))
328 * Build the file to mail to the user.
331 rcv_mailfile(SCR
*sp
, int issync
, char *cp_path
)
340 char *p
, *t
, buf
[4096], mpath
[MAXPATHLEN
];
346 * MAXHOSTNAMELEN is in various places on various systems, including
347 * <netdb.h> and <sys/socket.h>. If not found, use a large default.
349 #ifndef MAXHOSTNAMELEN
350 #define MAXHOSTNAMELEN 1024
352 char host
[MAXHOSTNAMELEN
];
355 if ((pw
= getpwuid(uid
= getuid())) == NULL
) {
357 "062|Information on user id %u not found", uid
);
361 if (opts_empty(sp
, O_RECDIR
, 0))
363 dp
= O_STR(sp
, O_RECDIR
);
364 (void)snprintf(mpath
, sizeof(mpath
), "%s/recover.XXXXXX", dp
);
365 if ((fd
= rcv_mktemp(sp
, mpath
, dp
, S_IRUSR
| S_IWUSR
)) == -1)
370 * We keep an open lock on the file so that the recover option can
371 * distinguish between files that are live and those that need to
372 * be recovered. There's an obvious window between the mkstemp call
373 * and the lock, but it's pretty small.
376 if (file_lock(sp
, NULL
, NULL
, fd
, 1) != LOCK_SUCCESS
)
377 msgq(sp
, M_SYSERR
, "063|Unable to lock recovery file");
379 /* Save the recover file descriptor, and mail path. */
381 if ((ep
->rcv_mpath
= strdup(mpath
)) == NULL
) {
382 msgq(sp
, M_SYSERR
, NULL
);
385 cp_path
= ep
->rcv_path
;
390 * We can't use stdio(3) here. The problem is that we may be using
391 * fcntl(2), so if ANY file descriptor into the file is closed, the
392 * lock is lost. So, we could never close the FILE *, even if we
393 * dup'd the fd first.
396 if ((p
= strrchr(t
, '/')) == NULL
)
401 (void)gethostname(host
, sizeof(host
));
402 len
= snprintf(buf
, sizeof(buf
),
403 "%s%s\n%s%s\n%s\n%s\n%s%s\n%s%s\n%s\n\n",
404 VI_FHEADER
, t
, /* Non-standard. */
405 VI_PHEADER
, cp_path
, /* Non-standard. */
407 "From: root (Nvi recovery program)",
409 "Subject: Nvi saved the file ", p
,
410 "Precedence: bulk"); /* For vacation(1). */
411 if (len
> sizeof(buf
) - 1)
413 if ((size_t)write(fd
, buf
, len
) != len
)
416 len
= snprintf(buf
, sizeof(buf
),
417 "%s%.24s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n\n",
418 "On ", ctime(&now
), ", the user ", pw
->pw_name
,
419 " was editing a file named ", t
, " on the machine ",
420 host
, ", when it was saved for recovery. ",
421 "You can recover most, if not all, of the changes ",
422 "to this file using the -r option to ", gp
->progname
, ":\n\n\t",
423 gp
->progname
, " -r ", t
);
424 if (len
> sizeof(buf
) - 1) {
425 lerr
: msgq(sp
, M_ERR
, "064|Recovery file buffer overrun");
430 * Format the message. (Yes, I know it's silly.)
431 * Requires that the message end in a <newline>.
434 for (t1
= buf
; len
> 0; len
-= t2
- t1
, t1
= t2
) {
435 /* Check for a short length. */
436 if (len
<= FMTCOLS
) {
441 /* Check for a required <newline>. */
442 t2
= strchr(t1
, '\n');
443 if (t2
- t1
<= FMTCOLS
)
446 /* Find the closest space, if any. */
447 for (t3
= t2
; t2
> t1
; --t2
)
449 if (t2
- t1
<= FMTCOLS
)
455 /* t2 points to the last character to display. */
458 /* t2 points one after the last character to display. */
459 if (write(fd
, t1
, t2
- t1
) != t2
- t1
)
464 rcv_email(sp
, mpath
);
466 werr
: msgq(sp
, M_SYSERR
, "065|Recovery file");
481 * never exactly the same
482 * just like a snowflake
485 * List the files that can be recovered by this user.
487 * PUBLIC: int rcv_list __P((SCR *));
499 char file
[MAXPATHLEN
], path
[MAXPATHLEN
];
501 /* Open the recovery directory for reading. */
502 if (opts_empty(sp
, O_RECDIR
, 0))
504 d
= O_STR(sp
, O_RECDIR
);
505 if (chdir(d
) || (dirp
= opendir(".")) == NULL
) {
506 msgq_str(sp
, M_SYSERR
, d
, "recdir: %s");
510 /* Read the directory. */
511 for (found
= 0; (dp
= readdir(dirp
)) != NULL
;) {
512 if (strncmp(dp
->d_name
, "recover.", 8))
516 * If it's readable, it's recoverable.
519 * Should be "r", we don't want to write the file. However,
520 * if we're using fcntl(2), there's no way to lock a file
521 * descriptor that's not open for writing.
523 if ((fp
= fopen(dp
->d_name
, "r+")) == NULL
)
526 switch (file_lock(sp
, NULL
, NULL
, fileno(fp
), 1)) {
530 * Assume that a lock can't be acquired, but that we
531 * should permit recovery anyway. If this is wrong,
532 * and someone else is using the file, we're going to
539 /* If it's locked, it's live. */
544 /* Check the headers. */
545 if (fgets(file
, sizeof(file
), fp
) == NULL
||
546 strncmp(file
, VI_FHEADER
, sizeof(VI_FHEADER
) - 1) ||
547 (p
= strchr(file
, '\n')) == NULL
||
548 fgets(path
, sizeof(path
), fp
) == NULL
||
549 strncmp(path
, VI_PHEADER
, sizeof(VI_PHEADER
) - 1) ||
550 (t
= strchr(path
, '\n')) == NULL
) {
551 msgq_str(sp
, M_ERR
, dp
->d_name
,
552 "066|%s: malformed recovery file");
558 * If the file doesn't exist, it's an orphaned recovery file,
562 * This can occur if the backup file was deleted and we crashed
563 * before deleting the email file.
566 if (stat(path
+ sizeof(VI_PHEADER
) - 1, &sb
) &&
568 (void)unlink(dp
->d_name
);
572 /* Get the last modification time and display. */
573 (void)fstat(fileno(fp
), &sb
);
574 (void)printf("%.24s: %s\n",
575 ctime(&sb
.st_mtime
), file
+ sizeof(VI_FHEADER
) - 1);
578 /* Close, discarding lock. */
579 next
: (void)fclose(fp
);
582 (void)printf("%s: No files to recover\n", sp
->gp
->progname
);
583 (void)closedir(dirp
);
589 * Start a recovered file as the file to edit.
591 * PUBLIC: int rcv_read __P((SCR *, FREF *));
594 rcv_read(SCR
*sp
, FREF
*frp
)
601 int fd
, found
, locked
= 0, requested
, sv_fd
;
602 char *name
, *p
, *t
, *recp
, *pathp
;
604 char file
[MAXPATHLEN
], path
[MAXPATHLEN
], recpath
[MAXPATHLEN
];
606 if (opts_empty(sp
, O_RECDIR
, 0))
608 rp
= O_STR(sp
, O_RECDIR
);
609 if ((dirp
= opendir(rp
)) == NULL
) {
610 msgq_str(sp
, M_ERR
, rp
, "%s");
618 for (found
= requested
= 0; (dp
= readdir(dirp
)) != NULL
;) {
619 if (strncmp(dp
->d_name
, "recover.", 8))
621 (void)snprintf(recpath
,
622 sizeof(recpath
), "%s/%s", rp
, dp
->d_name
);
625 * If it's readable, it's recoverable. It would be very
626 * nice to use stdio(3), but, we can't because that would
627 * require closing and then reopening the file so that we
628 * could have a lock and still close the FP. Another tip
629 * of the hat to fcntl(2).
632 * Should be O_RDONLY, we don't want to write it. However,
633 * if we're using fcntl(2), there's no way to lock a file
634 * descriptor that's not open for writing.
636 if ((fd
= open(recpath
, O_RDWR
, 0)) == -1)
639 switch (file_lock(sp
, NULL
, NULL
, fd
, 1)) {
643 * Assume that a lock can't be acquired, but that we
644 * should permit recovery anyway. If this is wrong,
645 * and someone else is using the file, we're going to
654 /* If it's locked, it's live. */
659 /* Check the headers. */
660 if (rcv_gets(file
, sizeof(file
), fd
) == NULL
||
661 strncmp(file
, VI_FHEADER
, sizeof(VI_FHEADER
) - 1) ||
662 (p
= strchr(file
, '\n')) == NULL
||
663 rcv_gets(path
, sizeof(path
), fd
) == NULL
||
664 strncmp(path
, VI_PHEADER
, sizeof(VI_PHEADER
) - 1) ||
665 (t
= strchr(path
, '\n')) == NULL
) {
666 msgq_str(sp
, M_ERR
, recpath
,
667 "067|%s: malformed recovery file");
674 * If the file doesn't exist, it's an orphaned recovery file,
678 * This can occur if the backup file was deleted and we crashed
679 * before deleting the email file.
682 if (stat(path
+ sizeof(VI_PHEADER
) - 1, &sb
) &&
684 (void)unlink(dp
->d_name
);
688 /* Check the file name. */
689 if (strcmp(file
+ sizeof(VI_FHEADER
) - 1, name
))
695 * If we've found more than one, take the most recent.
698 * Since we're using st_mtime, for portability reasons,
699 * we only get a single second granularity, instead of
702 (void)fstat(fd
, &sb
);
703 if (recp
== NULL
|| rec_mtime
< sb
.st_mtime
) {
706 if ((recp
= strdup(recpath
)) == NULL
) {
707 msgq(sp
, M_SYSERR
, NULL
);
711 if ((pathp
= strdup(path
)) == NULL
) {
712 msgq(sp
, M_SYSERR
, NULL
);
722 rec_mtime
= sb
.st_mtime
;
727 next
: (void)close(fd
);
729 (void)closedir(dirp
);
732 msgq_str(sp
, M_INFO
, name
,
733 "068|No files named %s, readable by you, to recover");
739 "069|There are older versions of this file for you to recover");
740 if (found
> requested
)
742 "070|There are other files for you to recover");
746 * Create the FREF structure, start the btree file.
749 * file_init() is going to set ep->rcv_path.
751 if (file_init(sp
, frp
, pathp
+ sizeof(VI_PHEADER
) - 1, 0)) {
759 * We keep an open lock on the file so that the recover option can
760 * distinguish between files that are live and those that need to
761 * be recovered. The lock is already acquired, just copy it.
764 ep
->rcv_mpath
= recp
;
767 F_SET(frp
, FR_UNLOCKED
);
769 /* We believe the file is recoverable. */
777 * Copy a recovery file.
780 rcv_copy(SCR
*sp
, int wfd
, char *fname
)
782 int nr
, nw
, off
, rfd
;
785 if ((rfd
= open(fname
, O_RDONLY
, 0)) == -1)
787 while ((nr
= read(rfd
, buf
, sizeof(buf
))) > 0)
788 for (off
= 0; nr
; nr
-= nw
, off
+= nw
)
789 if ((nw
= write(wfd
, buf
+ off
, nr
)) < 0)
794 err
: msgq_str(sp
, M_SYSERR
, fname
, "%s");
800 * Fgets(3) for a file descriptor.
803 rcv_gets(char *buf
, size_t len
, int fd
)
808 if ((nr
= read(fd
, buf
, len
- 1)) == -1)
810 if ((p
= strchr(buf
, '\n')) == NULL
)
812 (void)lseek(fd
, (off_t
)((p
- buf
) + 1), SEEK_SET
);
818 * Paranoid make temporary file routine.
821 rcv_mktemp(SCR
*sp
, char *path
, const char *dname
, int perms
)
827 * We expect mkstemp(3) to set the permissions correctly. On
828 * historic System V systems, mkstemp didn't. Do it here, on
832 * The variable perms should really be a mode_t, and it would
833 * be nice to use fchmod(2) instead of chmod(2), here.
835 if ((fd
= mkstemp(path
)) == -1)
836 msgq_str(sp
, M_SYSERR
, dname
, "%s");
838 (void)chmod(path
, perms
);
847 rcv_email(SCR
*sp
, char *fname
)
850 char buf
[MAXPATHLEN
* 2 + 20];
852 if (_PATH_SENDMAIL
[0] != '/' || stat(_PATH_SENDMAIL
, &sb
))
853 msgq_str(sp
, M_SYSERR
,
854 _PATH_SENDMAIL
, "071|not sending email: %s");
858 * If you need to port this to a system that doesn't have
859 * sendmail, the -t flag causes sendmail to read the message
860 * for the recipients instead of specifying them some other
863 (void)snprintf(buf
, sizeof(buf
),
864 "%s -t < %s", _PATH_SENDMAIL
, fname
);