2 * Copyright 2008 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/siginfo.c,v 1.2 2008/05/22 21:08:36 cperciva Exp $");
37 /* Is there a pending SIGINFO or SIGUSR1? */
38 static volatile sig_atomic_t siginfo_received
= 0;
41 /* What sort of operation are we doing? */
44 /* What path are we handling? */
47 /* How large is the archive entry? */
50 /* Old signal handlers. */
52 void (*siginfo_old
)(int);
54 void (*sigusr1_old
)(int);
57 static void siginfo_handler(int sig
);
59 /* Handler for SIGINFO / SIGUSR1. */
61 siginfo_handler(int sig
)
64 (void)sig
; /* UNUSED */
66 /* Record that SIGINFO or SIGUSR1 has been received. */
71 siginfo_init(struct bsdtar
*bsdtar
)
74 /* Allocate space for internal structure. */
75 if ((bsdtar
->siginfo
= malloc(sizeof(struct siginfo_data
))) == NULL
)
76 bsdtar_errc(bsdtar
, 1, errno
, "malloc failed");
78 /* Set the strings to NULL so that free() is safe. */
79 bsdtar
->siginfo
->path
= bsdtar
->siginfo
->oper
= NULL
;
82 /* We want to catch SIGINFO, if it exists. */
83 bsdtar
->siginfo
->siginfo_old
= signal(SIGINFO
, siginfo_handler
);
85 /* ... and treat SIGUSR1 the same way as SIGINFO. */
86 bsdtar
->siginfo
->sigusr1_old
= signal(SIGUSR1
, siginfo_handler
);
90 siginfo_setinfo(struct bsdtar
*bsdtar
, const char * oper
, const char * path
,
94 /* Free old operation and path strings. */
95 free(bsdtar
->siginfo
->oper
);
96 free(bsdtar
->siginfo
->path
);
98 /* Duplicate strings and store entry size. */
99 if ((bsdtar
->siginfo
->oper
= strdup(oper
)) == NULL
)
100 bsdtar_errc(bsdtar
, 1, errno
, "Cannot strdup");
101 if ((bsdtar
->siginfo
->path
= strdup(path
)) == NULL
)
102 bsdtar_errc(bsdtar
, 1, errno
, "Cannot strdup");
103 bsdtar
->siginfo
->size
= size
;
107 siginfo_printinfo(struct bsdtar
*bsdtar
, off_t progress
)
110 /* If there's a signal to handle and we know what we're doing... */
111 if ((siginfo_received
== 1) &&
112 (bsdtar
->siginfo
->path
!= NULL
) &&
113 (bsdtar
->siginfo
->oper
!= NULL
)) {
115 fprintf(stderr
, "\n");
116 if (bsdtar
->siginfo
->size
> 0) {
117 safe_fprintf(stderr
, "%s %s (%ju / %" PRId64
")",
118 bsdtar
->siginfo
->oper
, bsdtar
->siginfo
->path
,
119 (uintmax_t)progress
, bsdtar
->siginfo
->size
);
121 safe_fprintf(stderr
, "%s %s",
122 bsdtar
->siginfo
->oper
, bsdtar
->siginfo
->path
);
124 if (!bsdtar
->verbose
)
125 fprintf(stderr
, "\n");
126 siginfo_received
= 0;
131 siginfo_done(struct bsdtar
*bsdtar
)
135 /* Restore old SIGINFO handler. */
136 signal(SIGINFO
, bsdtar
->siginfo
->siginfo_old
);
138 /* And the old SIGUSR1 handler, too. */
139 signal(SIGUSR1
, bsdtar
->siginfo
->sigusr1_old
);
142 free(bsdtar
->siginfo
->path
);
143 free(bsdtar
->siginfo
->oper
);
145 /* Free internal data structure. */
146 free(bsdtar
->siginfo
);