source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-fifo.c@ 2230

Last change on this file since 2230 was 2217, checked in by Bruno Cornec, 15 years ago

Fix a linker error - mondo builds again

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