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

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

Fix a static memory allocation in fifo mngt

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/*
2 $Id: libmondo-fifo.c 3840 2024-03-06 00:20:15Z 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,
109 "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
110 (direction == 'r') ? 20 : 75, internal_tape_block_size,
111 keych, device, MONDO_LOGFILE);
112 } else {
113 mr_asprintf(g_call_to_buffer, "dd bs=%ld %cf=%s",
114 internal_tape_block_size, keych, device);
115 }
116 log_msg(2, "Calling buffer --- command = '%s'", g_call_to_buffer);
117 mr_asprintf(sz_dir, "%c", direction);
118 fres = popen(g_call_to_buffer, sz_dir);
119 mr_free(sz_dir);
120
121 if (fres) {
122 log_msg(2, "Successfully opened ('%c') tape device %s", direction,
123 device);
124 } else {
125 log_msg(2, "Failed to open ('%c') tape device %s", direction,
126 device);
127 }
128 sleep(2);
129 mr_asprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_call_to_buffer);
130 if (run_program_and_log_output(tmp, 2)) {
131 log_msg(2, "Warning - I think I failed to open tape, actually.");
132 }
133 mr_free(tmp);
134 g_tape_buffer_size_MB = bufsize;
135 mr_asprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
136 if (run_program_and_log_output(command, 1)) {
137 fres = NULL;
138 log_to_screen("Failed to open tape streamer. Buffer error.");
139 } else {
140 log_to_screen("Buffer successfully started.");
141 }
142 mr_free(command);
143
144 return (fres);
145}
146
147
148/**
149 * Kill @c buffer processes.
150 * Only called in mondoarchive at then before exiting thus freeing memory
151 */
152void kill_buffer()
153{
154 char *tmp = NULL;
155 char *command = NULL;
156
157 if (g_call_to_buffer == NULL) {
158 return;
159 }
160 if (strcmp(g_call_to_buffer,"") == 0) {
161 return;
162 }
163 sync();
164 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);
165 log_msg(2, "kill_buffer() --- command = %s", command);
166 tmp = call_program_and_get_last_line_of_output(command);
167 mr_free(command);
168
169 mr_asprintf(command, "kill %s", tmp);
170 log_msg(2, "kill_buffer() --- command = %s", command);
171 if (strlen(tmp) > 0) {
172 run_program_and_log_output(command, TRUE);
173 }
174 mr_free(tmp);
175 mr_free(command);
176 mr_free(g_call_to_buffer);
177}
178
179
180
181
182
183/**
184 * Handler for SIGPIPE.
185 * @param sig The signal that occurred (hopefully SIGPIPE).
186 */
187void sigpipe_occurred(int sig)
188{
189 g_sigpipe = TRUE;
190}
191
192/* @} - end of fifoGroup */
Note: See TracBrowser for help on using the repository browser.