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