1 | /* libmondo-fifo.c
|
---|
2 | $Id: libmondo-fifo.c 1645 2007-09-24 01:04:43Z bruno $
|
---|
3 |
|
---|
4 |
|
---|
5 | 04/17
|
---|
6 | - replaced INTERNAL_TAPE_BLK_SIZE with bkpinfo->internal_tape_block_size
|
---|
7 |
|
---|
8 | 04/04/2004
|
---|
9 | - ps wax is now ps wwax
|
---|
10 |
|
---|
11 | 09/10/2003
|
---|
12 | - p75 is now p80 in outopening tape call
|
---|
13 | - negotiate largest buffer possible when opening tape drive
|
---|
14 | - fall back to dd if buffer fails
|
---|
15 |
|
---|
16 | 09/09
|
---|
17 | - better logging for tape users
|
---|
18 | - working on try_hard_to_fwrite(), try_hard_to_fread()
|
---|
19 | - replaced internal w/ EXTERNAL 'buffer' exe.
|
---|
20 |
|
---|
21 | 08/30
|
---|
22 | - tweaked error msgs in try_hard_to_fwrite()
|
---|
23 |
|
---|
24 | 08/02
|
---|
25 | - updated is_incoming_block_valid() to make it
|
---|
26 | return end-of-tape if >300 flotsam blocks
|
---|
27 |
|
---|
28 | 05/02
|
---|
29 | - when logging tape errors, don't repeat self
|
---|
30 |
|
---|
31 | 04/24
|
---|
32 | - added lots of log_OS_error()'s and assert()'s
|
---|
33 |
|
---|
34 | 04/22
|
---|
35 | - copy_file_from_here_to_there() --- added a bit of fault tolerance;
|
---|
36 | if write fails, retry a few times before reporting error
|
---|
37 |
|
---|
38 | 04/07/2003
|
---|
39 | - line 866 --- set block_size used by internal buffer to 32768;
|
---|
40 | was INTERNAL_TAPE_BLK_SIZE/2
|
---|
41 |
|
---|
42 | 10/01 - 11/30/2002
|
---|
43 | - is_incoming_block_valid() --- always make
|
---|
44 | checksums %65536, just in case int size is
|
---|
45 | odd (GRRR, ArkLinux)
|
---|
46 | - disabled rotor-related superfluous warnings
|
---|
47 | - added INTERNAL_TAPE_BLK_SIZE
|
---|
48 | - do_sem() now returns int explicitly
|
---|
49 | - changed internal_block_size
|
---|
50 | - some irregularities (e.g. bad 'type'-ing) found by Kylix; fixed by Hugo
|
---|
51 |
|
---|
52 | 09/01 - 09/30
|
---|
53 | - change 64k to TAPE_BLOCK_SIZE
|
---|
54 | - added internal_block_size; set it to TAPE_BLOCK_SIZE*2
|
---|
55 | - if data is flowing FROM tape TO hard disk then set the threshold to 10 (not 75)
|
---|
56 | - lots of multitape-related fixes
|
---|
57 | - finally caught & fixed the 'won't finish unzipping last bigfile' bug
|
---|
58 | - added WIFEXITED() after waitpid(), to improve multi-tape support
|
---|
59 |
|
---|
60 | 08/01 - 08/31
|
---|
61 | - trying to catch & stop 'won't finish unzipping last bigfile' bug by
|
---|
62 | changing the copy_file_rom_here_to_there code
|
---|
63 | - changed % threshold from 95 back to 75
|
---|
64 | - don't insist on full 256K write of last block to tape
|
---|
65 | - if >25 secs go by & all data (AFAIK) has been copied thru by FIFO wrapper
|
---|
66 | and g_tape_stream is _still_ not closed then shrug shoulders & pthread_exit
|
---|
67 | anyway...
|
---|
68 | - change fprintf()'s to log_it()'s
|
---|
69 | - added a header+footer to each block as it is read/written to/from tape
|
---|
70 | by copy_file_from_here_to_there
|
---|
71 | - wrote workaround to allow >2GB of archives w/buffering
|
---|
72 | - changed % threshold from 75 to 95
|
---|
73 | - added calls to set_signals(); 'buffer' was killing mondoarchive as
|
---|
74 | it terminated
|
---|
75 | - cleaned up struct-passing, to improve reliability and eliminate
|
---|
76 | some race conditions
|
---|
77 | - changed some forks to pthreads
|
---|
78 | - added some comments
|
---|
79 | - created libfifo{.c,.h,-EXT.h}
|
---|
80 | - copied core of 'buffer' here
|
---|
81 | - added some other, Mondo-specific functions
|
---|
82 | - hacked 'buffer' into user-friendliness
|
---|
83 | */
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * @file
|
---|
87 | * Functions to handle buffering of tape archives as they are read/written.
|
---|
88 | * This used the external program @c buffer mostly.
|
---|
89 | */
|
---|
90 |
|
---|
91 | #include <unistd.h>
|
---|
92 | #include <stdio.h>
|
---|
93 | #include <signal.h>
|
---|
94 | #include <fcntl.h>
|
---|
95 | #include <stdio.h>
|
---|
96 |
|
---|
97 | #include <errno.h>
|
---|
98 | #include <sys/types.h>
|
---|
99 | #include <sys/stat.h>
|
---|
100 | #include <sys/ipc.h>
|
---|
101 | #include <sys/shm.h>
|
---|
102 | #include <sys/sem.h>
|
---|
103 | #include <sys/wait.h>
|
---|
104 | #include <pthread.h>
|
---|
105 |
|
---|
106 | #include "my-stuff.h"
|
---|
107 | #include "mondostructures.h"
|
---|
108 | #include "libmondo.h"
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * @addtogroup globalGroup
|
---|
112 | * @{
|
---|
113 | */
|
---|
114 | /**
|
---|
115 | * The SIGPIPE handler sets this to TRUE.
|
---|
116 | */
|
---|
117 | bool g_sigpipe = FALSE;
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * PID of the "main" process.
|
---|
121 | */
|
---|
122 | pid_t g_mastermind_pid = 0;
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Command line with which @c buffer was invoked.
|
---|
128 | */
|
---|
129 | char g_sz_call_to_buffer[MAX_STR_LEN];
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Size of the buffer used with @c buffer.
|
---|
133 | */
|
---|
134 | int g_tape_buffer_size_MB = 0;
|
---|
135 |
|
---|
136 | /* @} - end of globalGroup */
|
---|
137 |
|
---|
138 | extern char *ps_options;
|
---|
139 | extern char *ps_proc_id;
|
---|
140 | extern char *MONDO_LOGFILE;
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * @addtogroup fifoGroup
|
---|
144 | * @{
|
---|
145 | */
|
---|
146 | /**
|
---|
147 | * Open a pipe to/from @c buffer.
|
---|
148 | * If buffer does not work at all, we use `dd'.
|
---|
149 | * @param device The device to read from/write to.
|
---|
150 | * @param direction @c 'r' (reading) or @c 'w' (writing).
|
---|
151 | * @return A file pointer to/from the @c buffer process.
|
---|
152 | */
|
---|
153 | FILE *open_device_via_buffer(char *device, char direction,
|
---|
154 | long internal_tape_block_size)
|
---|
155 | {
|
---|
156 | char sz_dir[32];
|
---|
157 | char keych;
|
---|
158 | char *tmp;
|
---|
159 | char *command;
|
---|
160 | FILE *fres;
|
---|
161 | int bufsize; // in megabytes
|
---|
162 | int res;
|
---|
163 | int wise_upper_limit;
|
---|
164 | int wise_lower_limit;
|
---|
165 |
|
---|
166 | malloc_string(tmp);
|
---|
167 | malloc_string(command);
|
---|
168 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
169 | assert(direction == 'w' || direction == 'r');
|
---|
170 | sprintf(sz_dir, "%c", direction);
|
---|
171 | wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
|
---|
172 | wise_lower_limit = 1; // wise_upper_limit/2 + 1;
|
---|
173 | paranoid_system("sync");
|
---|
174 | for (bufsize = wise_upper_limit, res = -1;
|
---|
175 | res != 0 && bufsize >= wise_lower_limit; bufsize--) {
|
---|
176 | sprintf(tmp,
|
---|
177 | "dd if=/dev/zero bs=1024 count=16k 2> /dev/null | buffer -o /dev/null -s %ld -m %d%c",
|
---|
178 | internal_tape_block_size, bufsize, 'm');
|
---|
179 | res = run_program_and_log_output(tmp, 2);
|
---|
180 | }
|
---|
181 | if (!res) {
|
---|
182 | bufsize++;
|
---|
183 | sprintf(tmp, "Negotiated max buffer of %d MB ", bufsize);
|
---|
184 | log_to_screen(tmp);
|
---|
185 | } else {
|
---|
186 | bufsize = 0;
|
---|
187 | res = 0;
|
---|
188 | log_to_screen
|
---|
189 | ("Cannot negotiate a buffer of ANY size. Using dd instead.");
|
---|
190 | }
|
---|
191 | if (direction == 'r') {
|
---|
192 | keych = 'i';
|
---|
193 | } else {
|
---|
194 | keych = 'o';
|
---|
195 | }
|
---|
196 | if (bufsize) {
|
---|
197 | sprintf(g_sz_call_to_buffer,
|
---|
198 | "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
|
---|
199 | (direction == 'r') ? 20 : 75, internal_tape_block_size,
|
---|
200 | keych, device, MONDO_LOGFILE);
|
---|
201 | } else {
|
---|
202 | sprintf(g_sz_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
203 | internal_tape_block_size, keych, device);
|
---|
204 | }
|
---|
205 | log_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
|
---|
206 | fres = popen(g_sz_call_to_buffer, sz_dir);
|
---|
207 | if (fres) {
|
---|
208 | log_msg(2, "Successfully opened ('%c') tape device %s", direction,
|
---|
209 | device);
|
---|
210 | } else {
|
---|
211 | log_msg(2, "Failed to open ('%c') tape device %s", direction,
|
---|
212 | device);
|
---|
213 | }
|
---|
214 | sleep(2);
|
---|
215 | sprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
|
---|
216 | if (run_program_and_log_output(tmp, 2)) {
|
---|
217 | log_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
218 | }
|
---|
219 | g_tape_buffer_size_MB = bufsize;
|
---|
220 | tmp[30] = '\0';
|
---|
221 | sprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
222 | if (run_program_and_log_output(command, 1)) {
|
---|
223 | fres = NULL;
|
---|
224 | log_to_screen("Failed to open tape streamer. Buffer error.");
|
---|
225 | } else {
|
---|
226 | log_to_screen("Buffer successfully started.");
|
---|
227 | }
|
---|
228 |
|
---|
229 | paranoid_free(command);
|
---|
230 | paranoid_free(tmp);
|
---|
231 | return (fres);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Kill @c buffer processes.
|
---|
237 | * Only called in mondoarchive
|
---|
238 | */
|
---|
239 | void kill_buffer()
|
---|
240 | {
|
---|
241 | char *tmp;
|
---|
242 | char *command;
|
---|
243 |
|
---|
244 | if (g_sz_call_to_buffer == NULL) {
|
---|
245 | return;
|
---|
246 | }
|
---|
247 | if (strcmp(g_sz_call_to_buffer,"") == 0) {
|
---|
248 | return;
|
---|
249 | }
|
---|
250 | malloc_string(tmp);
|
---|
251 | malloc_string(command);
|
---|
252 | paranoid_system("sync");
|
---|
253 | sprintf(command,
|
---|
254 | "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_sz_call_to_buffer);
|
---|
255 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
256 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
257 | sprintf(command, "kill %s", tmp);
|
---|
258 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
259 | if (strlen(tmp) > 0) {
|
---|
260 | run_program_and_log_output(command, TRUE);
|
---|
261 | }
|
---|
262 | paranoid_free(tmp);
|
---|
263 | paranoid_free(command);
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 |
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Handler for SIGPIPE.
|
---|
272 | * @param sig The signal that occurred (hopefully SIGPIPE).
|
---|
273 | */
|
---|
274 | void sigpipe_occurred(int sig)
|
---|
275 | {
|
---|
276 | g_sigpipe = TRUE;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /* @} - end of fifoGroup */
|
---|
280 |
|
---|
281 | int
|
---|
282 | extract_config_file_from_ramdisk(char *ramdisk_fname,
|
---|
283 | char *output_cfg_file,
|
---|
284 | char *output_mountlist_file);
|
---|