source: MondoRescue/trunk/mondo/mondo/common/libmondo-fifo.c@ 688

Last change on this file since 688 was 688, checked in by bcornec, 18 years ago

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

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