source: MondoRescue/branches/3.3/mondo/src/common/libmondo-fifo.c@ 3866

Last change on this file since 3866 was 3865, checked in by Bruno Cornec, 3 months ago

Fix sigpipe_occurred proto

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 $Id: libmondo-fifo.c 3865 2024-03-07 10:57:00Z 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 * Command line with which @c buffer was invoked.
44 */
45char *g_call_to_buffer = NULL;
46
47/**
48 * Size of the buffer used with @c buffer.
49 */
50int g_tape_buffer_size_MB = 0;
51
52/* @} - end of globalGroup */
53
54extern char *ps_options;
55extern char *ps_proc_id;
56extern char *MONDO_LOGFILE;
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 keych;
73 char *sz_dir = NULL;
74 char *tmp = NULL;
75 char *command = NULL;
76 FILE *fres;
77 int bufsize; // in megabytes
78 int res;
79 int wise_upper_limit;
80 int wise_lower_limit;
81
82 assert_string_is_neither_NULL_nor_zerolength(device);
83 assert(direction == 'w' || direction == 'r');
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 mr_asprintf(tmp,"dd if=/dev/zero bs=1024 count=16k 2> /dev/null | buffer -o /dev/null -s %ld -m %d%c", internal_tape_block_size, bufsize, 'm');
90 res = run_program_and_log_output(tmp, 2);
91 mr_free(tmp);
92 }
93 if (!res) {
94 bufsize++;
95 log_to_screen(tmp, "Negotiated max buffer of %d MB ", bufsize);
96 } else {
97 bufsize = 0;
98 res = 0;
99 log_to_screen
100 ("Cannot negotiate a buffer of ANY size. Using dd instead.");
101 }
102 if (direction == 'r') {
103 keych = 'i';
104 } else {
105 keych = 'o';
106 }
107 if (bufsize) {
108 mr_asprintf(g_call_to_buffer, "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
109 (direction == 'r') ? 20 : 75, internal_tape_block_size,
110 keych, device, MONDO_LOGFILE);
111 } else {
112 mr_asprintf(g_call_to_buffer, "dd bs=%ld %cf=%s",
113 internal_tape_block_size, keych, device);
114 }
115 log_msg(2, "Calling buffer --- command = '%s'", g_call_to_buffer);
116 mr_asprintf(sz_dir, "%c", direction);
117 fres = popen(g_call_to_buffer, sz_dir);
118 mr_free(sz_dir);
119
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 mr_asprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_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 mr_free(tmp);
133 g_tape_buffer_size_MB = bufsize;
134 mr_asprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
135 if (run_program_and_log_output(command, 1)) {
136 fres = NULL;
137 log_to_screen("Failed to open tape streamer. Buffer error.");
138 } else {
139 log_to_screen("Buffer successfully started.");
140 }
141 mr_free(command);
142
143 return (fres);
144}
145
146
147/**
148 * Kill @c buffer processes.
149 * Only called in mondoarchive at then before exiting thus freeing memory
150 */
151void kill_buffer()
152{
153 char *tmp = NULL;
154 char *command = NULL;
155
156 if (g_call_to_buffer == NULL) {
157 return;
158 }
159 if (strcmp(g_call_to_buffer,"") == 0) {
160 return;
161 }
162 sync();
163 mr_asprintf(command, "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_call_to_buffer);
164 log_msg(2, "kill_buffer() --- command = %s", command);
165 tmp = call_program_and_get_last_line_of_output(command);
166 mr_free(command);
167
168 mr_asprintf(command, "kill %s", tmp);
169 log_msg(2, "kill_buffer() --- command = %s", command);
170 if (strlen(tmp) > 0) {
171 run_program_and_log_output(command, TRUE);
172 }
173 mr_free(tmp);
174 mr_free(command);
175 mr_free(g_call_to_buffer);
176}
177
178
179
180
181
182/**
183 * Handler for SIGPIPE.
184 */
185void sigpipe_occurred()
186{
187 g_sigpipe = TRUE;
188}
189
190/* @} - end of fifoGroup */
Note: See TracBrowser for help on using the repository browser.