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

Last change on this file was 3879, checked in by Bruno Cornec, 3 months ago

Fix all remaining compiler errors

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/*
2 $Id: libmondo-fifo.c 3879 2024-03-09 02:10:04Z 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-devices-EXT.h"
26#include "libmondo-fork-EXT.h"
27#include "newt-specific-EXT.h"
28
29
30/**
31 * @addtogroup globalGroup
32 * @{
33 */
34/**
35 * The SIGPIPE handler sets this to TRUE.
36 */
37bool g_sigpipe = FALSE;
38
39/**
40 * PID of the "main" process.
41 */
42pid_t g_mastermind_pid = 0;
43
44
45/**
46 * Command line with which @c buffer was invoked.
47 */
48char *g_call_to_buffer = NULL;
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
57extern char *ps_options;
58extern char *ps_proc_id;
59extern char *MONDO_LOGFILE;
60
61/**
62 * @addtogroup fifoGroup
63 * @{
64 */
65/**
66 * Open a pipe to/from @c buffer.
67 * If buffer does not work at all, we use `dd'.
68 * @param device The device to read from/write to.
69 * @param direction @c 'r' (reading) or @c 'w' (writing).
70 * @return A file pointer to/from the @c buffer process.
71 */
72FILE *open_device_via_buffer(char *device, char direction,
73 long internal_tape_block_size)
74{
75 char keych;
76 char *sz_dir = NULL;
77 char *tmp = NULL;
78 char *command = NULL;
79 FILE *fres;
80 int bufsize; // in megabytes
81 int res;
82 int wise_upper_limit;
83 int wise_lower_limit;
84
85 assert_string_is_neither_NULL_nor_zerolength(device);
86 assert(direction == 'w' || direction == 'r');
87 wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
88 wise_lower_limit = 1; // wise_upper_limit/2 + 1;
89 sync();
90 for (bufsize = wise_upper_limit, res = -1;
91 res != 0 && bufsize >= wise_lower_limit; bufsize--) {
92 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');
93 res = run_program_and_log_output(tmp, 2);
94 mr_free(tmp);
95 }
96 if (!res) {
97 bufsize++;
98 log_to_screen(tmp, "Negotiated max buffer of %d MB ", bufsize);
99 } else {
100 bufsize = 0;
101 res = 0;
102 log_to_screen
103 ("Cannot negotiate a buffer of ANY size. Using dd instead.");
104 }
105 if (direction == 'r') {
106 keych = 'i';
107 } else {
108 keych = 'o';
109 }
110 if (bufsize) {
111 mr_asprintf(g_call_to_buffer, "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
112 (direction == 'r') ? 20 : 75, internal_tape_block_size,
113 keych, device, MONDO_LOGFILE);
114 } else {
115 mr_asprintf(g_call_to_buffer, "dd bs=%ld %cf=%s",
116 internal_tape_block_size, keych, device);
117 }
118 log_msg(2, "Calling buffer --- command = '%s'", g_call_to_buffer);
119 mr_asprintf(sz_dir, "%c", direction);
120 fres = popen(g_call_to_buffer, sz_dir);
121 mr_free(sz_dir);
122
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 mr_asprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_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 mr_free(tmp);
136 g_tape_buffer_size_MB = bufsize;
137 mr_asprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
138 if (run_program_and_log_output(command, 1)) {
139 fres = NULL;
140 log_to_screen("Failed to open tape streamer. Buffer error.");
141 } else {
142 log_to_screen("Buffer successfully started.");
143 }
144 mr_free(command);
145
146 return (fres);
147}
148
149
150/**
151 * Kill @c buffer processes.
152 * Only called in mondoarchive at then before exiting thus freeing memory
153 */
154void kill_buffer()
155{
156 char *tmp = NULL;
157 char *command = NULL;
158
159 if (g_call_to_buffer == NULL) {
160 return;
161 }
162 if (strcmp(g_call_to_buffer,"") == 0) {
163 return;
164 }
165 sync();
166 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);
167 log_msg(2, "kill_buffer() --- command = %s", command);
168 tmp = call_program_and_get_last_line_of_output(command);
169 mr_free(command);
170
171 mr_asprintf(command, "kill %s", tmp);
172 log_msg(2, "kill_buffer() --- command = %s", command);
173 if (strlen(tmp) > 0) {
174 run_program_and_log_output(command, TRUE);
175 }
176 mr_free(tmp);
177 mr_free(command);
178 mr_free(g_call_to_buffer);
179}
180
181
182
183
184
185/**
186 * Handler for SIGPIPE.
187 */
188void sigpipe_occurred()
189{
190 g_sigpipe = TRUE;
191}
192
193/* @} - end of fifoGroup */
Note: See TracBrowser for help on using the repository browser.