1 | /* $Id: libmondo-fifo.c 1161 2007-02-14 12:13:49Z bruno $ */
|
---|
2 | /**
|
---|
3 | * @file
|
---|
4 | * Functions to handle buffering of tape archives as they are read/written.
|
---|
5 | * This used the external program @c buffer mostly.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <unistd.h>
|
---|
9 | #include <stdio.h>
|
---|
10 | #ifndef S_SPLINT_S
|
---|
11 | #include <signal.h>
|
---|
12 | #endif
|
---|
13 | #include <fcntl.h>
|
---|
14 |
|
---|
15 | #include <errno.h>
|
---|
16 | #include <sys/types.h>
|
---|
17 | #include <sys/stat.h>
|
---|
18 | #include <sys/ipc.h>
|
---|
19 | #include <sys/shm.h>
|
---|
20 | #include <sys/sem.h>
|
---|
21 | #include <sys/wait.h>
|
---|
22 | #ifndef S_SPLINT_S
|
---|
23 | #include <pthread.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #include "my-stuff.h"
|
---|
27 | #include "mondostructures.h"
|
---|
28 | #include "libmondo.h"
|
---|
29 | #include "mr_mem.h"
|
---|
30 | #include "mr_msg.h"
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @addtogroup globalGroup
|
---|
34 | * @{
|
---|
35 | */
|
---|
36 | /**
|
---|
37 | * The SIGPIPE handler sets this to TRUE.
|
---|
38 | */
|
---|
39 | bool g_sigpipe = FALSE;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * PID of the "main" process.
|
---|
43 | */
|
---|
44 | pid_t g_mastermind_pid = 0;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Command line with which @c buffer was invoked.
|
---|
48 | */
|
---|
49 | char *g_sz_call_to_buffer;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Size of the buffer used with @c buffer.
|
---|
53 | */
|
---|
54 | int g_tape_buffer_size_MB = 0;
|
---|
55 |
|
---|
56 | /* @} - end of globalGroup */
|
---|
57 |
|
---|
58 | extern char *ps_options;
|
---|
59 | extern char *ps_proc_id;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * @addtogroup fifoGroup
|
---|
63 | * @{
|
---|
64 | */
|
---|
65 | /**
|
---|
66 | * Open a pipe to/from @c buffer.
|
---|
67 | * If buffer does not work at all, we use `dd'.
|
---|
68 | * @param device The device to read from/write to.
|
---|
69 | * @param direction @c 'r' (reading) or @c 'w' (writing).
|
---|
70 | * @return A file pointer to/from the @c buffer process.
|
---|
71 | */
|
---|
72 | FILE *open_device_via_buffer(char *device, char direction,
|
---|
73 | long internal_tape_block_size)
|
---|
74 | {
|
---|
75 | char *sz_dir;
|
---|
76 | char keych;
|
---|
77 | char *tmp;
|
---|
78 | FILE *fres;
|
---|
79 | int bufsize; // in megabytes
|
---|
80 | int res;
|
---|
81 | int wise_upper_limit;
|
---|
82 | int wise_lower_limit;
|
---|
83 |
|
---|
84 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
85 | assert(direction == 'w' || direction == 'r');
|
---|
86 | mr_asprintf(&sz_dir, "%c", direction);
|
---|
87 | wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
|
---|
88 | wise_lower_limit = 1; // wise_upper_limit/2 + 1;
|
---|
89 | sync();
|
---|
90 | for (bufsize = wise_upper_limit, res = -1;
|
---|
91 | res != 0 && bufsize >= wise_lower_limit; bufsize--) {
|
---|
92 | mr_asprintf(&tmp,
|
---|
93 | "dd if=/dev/zero bs=1024 count=16k 2> /dev/null | buffer -o /dev/null -s %ld -m %d%c",
|
---|
94 | internal_tape_block_size, bufsize, 'm');
|
---|
95 | res = run_program_and_log_output(tmp, 2);
|
---|
96 | mr_free(tmp);
|
---|
97 | }
|
---|
98 | if (!res) {
|
---|
99 | bufsize++;
|
---|
100 | mr_asprintf(&tmp, _("Negotiated max buffer of %d MB "), bufsize);
|
---|
101 | log_to_screen(tmp);
|
---|
102 | mr_free(tmp);
|
---|
103 | } else {
|
---|
104 | bufsize = 0;
|
---|
105 | res = 0;
|
---|
106 | log_to_screen
|
---|
107 | (_("Cannot negotiate a buffer of ANY size. Using dd instead."));
|
---|
108 | }
|
---|
109 | if (direction == 'r') {
|
---|
110 | keych = 'i';
|
---|
111 | } else {
|
---|
112 | keych = 'o';
|
---|
113 | }
|
---|
114 | if (bufsize) {
|
---|
115 | mr_asprintf(&g_sz_call_to_buffer,
|
---|
116 | "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize,
|
---|
117 | 'm', (direction == 'r') ? 20 : 75,
|
---|
118 | internal_tape_block_size, keych, device, MONDO_LOGFILE);
|
---|
119 | } else {
|
---|
120 | mr_asprintf(&g_sz_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
121 | internal_tape_block_size, keych, device);
|
---|
122 | }
|
---|
123 | mr_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
|
---|
124 | fres = popen(g_sz_call_to_buffer, sz_dir);
|
---|
125 | mr_free(sz_dir);
|
---|
126 | if (fres) {
|
---|
127 | mr_msg(2, "Successfully opened ('%c') tape device %s", direction,
|
---|
128 | device);
|
---|
129 | } else {
|
---|
130 | mr_msg(2, "Failed to open ('%c') tape device %s", direction,
|
---|
131 | device);
|
---|
132 | }
|
---|
133 | sleep(2);
|
---|
134 | mr_asprintf(&tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
|
---|
135 | if (run_program_and_log_output(tmp, 2)) {
|
---|
136 | mr_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
137 | }
|
---|
138 | mr_free(tmp);
|
---|
139 | g_tape_buffer_size_MB = bufsize;
|
---|
140 | mr_asprintf(&tmp, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
141 | if (run_program_and_log_output(tmp, 1)) {
|
---|
142 | fres = NULL;
|
---|
143 | log_to_screen(_("Failed to open tape streamer. Buffer error."));
|
---|
144 | } else {
|
---|
145 | log_to_screen(_("Buffer successfully started."));
|
---|
146 | }
|
---|
147 | mr_free(tmp);
|
---|
148 | return (fres);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Kill @c buffer processes.
|
---|
154 | * Only called in mondoarchive
|
---|
155 | */
|
---|
156 | void kill_buffer()
|
---|
157 | {
|
---|
158 | char *tmp = NULL;
|
---|
159 | char *command = NULL;
|
---|
160 |
|
---|
161 | sync();
|
---|
162 | if (g_sz_call_to_buffer == NULL) {
|
---|
163 | return;
|
---|
164 | }
|
---|
165 | if (strcmp(g_sz_call_to_buffer,"") == 0) {
|
---|
166 | return;
|
---|
167 | }
|
---|
168 | mr_asprintf(&command,
|
---|
169 | "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_sz_call_to_buffer);
|
---|
170 | mr_free(g_sz_call_to_buffer);
|
---|
171 | mr_msg(2, "kill_buffer() --- command = %s", command);
|
---|
172 |
|
---|
173 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
174 | mr_free(command);
|
---|
175 |
|
---|
176 | mr_asprintf(&command, "kill %s", tmp);
|
---|
177 | mr_msg(2, "kill_buffer() --- command = %s", command);
|
---|
178 | if (strlen(tmp) > 0) {
|
---|
179 | run_program_and_log_output(command, TRUE);
|
---|
180 | }
|
---|
181 | mr_free(tmp);
|
---|
182 | mr_free(command);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Handler for SIGPIPE.
|
---|
188 | * @param sig The signal that occurred (hopefully SIGPIPE).
|
---|
189 | */
|
---|
190 | void sigpipe_occurred(int sig)
|
---|
191 | {
|
---|
192 | g_sigpipe = TRUE;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /* @} - end of fifoGroup */
|
---|
196 |
|
---|
197 | /* BERLIOS: useless ?
|
---|
198 | int
|
---|
199 | extract_config_file_from_ramdisk( struct s_bkpinfo *bkpinfo,
|
---|
200 | char *ramdisk_fname,
|
---|
201 | char *output_cfg_file,
|
---|
202 | char *output_mountlist_file);
|
---|
203 | */
|
---|