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

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

Fix sigpipe_occurred proto

  • Property svn:keywords set to Id
File size: 4.3 KB
RevLine 
[2030]1/*
[128]2 $Id: libmondo-fifo.c 3865 2024-03-07 10:57:00Z bruno $
[1]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>
[128]14#include <fcntl.h>
[1]15#include <stdio.h>
[128]16
[1]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"
[2217]23#include "mr_mem.h"
[1]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 */
[128]34bool g_sigpipe = FALSE;
[1]35
36/**
37 * PID of the "main" process.
38 */
[128]39pid_t g_mastermind_pid = 0;
[1]40
41
42/**
43 * Command line with which @c buffer was invoked.
44 */
[3840]45char *g_call_to_buffer = NULL;
[1]46
47/**
48 * Size of the buffer used with @c buffer.
49 */
[128]50int g_tape_buffer_size_MB = 0;
[1]51
52/* @} - end of globalGroup */
53
[792]54extern char *ps_options;
[928]55extern char *ps_proc_id;
[1316]56extern char *MONDO_LOGFILE;
[1]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 */
[128]69FILE *open_device_via_buffer(char *device, char direction,
70 long internal_tape_block_size)
[1]71{
[128]72 char keych;
[2519]73 char *sz_dir = NULL;
74 char *tmp = NULL;
75 char *command = NULL;
[128]76 FILE *fres;
77 int bufsize; // in megabytes
78 int res;
79 int wise_upper_limit;
80 int wise_lower_limit;
[1]81
[128]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;
[3191]86 sync();
[128]87 for (bufsize = wise_upper_limit, res = -1;
88 res != 0 && bufsize >= wise_lower_limit; bufsize--) {
[3191]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');
[128]90 res = run_program_and_log_output(tmp, 2);
[2519]91 mr_free(tmp);
[128]92 }
93 if (!res) {
94 bufsize++;
[3191]95 log_to_screen(tmp, "Negotiated max buffer of %d MB ", bufsize);
[128]96 } else {
97 bufsize = 0;
98 res = 0;
99 log_to_screen
[541]100 ("Cannot negotiate a buffer of ANY size. Using dd instead.");
[128]101 }
102 if (direction == 'r') {
103 keych = 'i';
104 } else {
105 keych = 'o';
106 }
107 if (bufsize) {
[3858]108 mr_asprintf(g_call_to_buffer, "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
[128]109 (direction == 'r') ? 20 : 75, internal_tape_block_size,
110 keych, device, MONDO_LOGFILE);
111 } else {
[3840]112 mr_asprintf(g_call_to_buffer, "dd bs=%ld %cf=%s",
[128]113 internal_tape_block_size, keych, device);
114 }
[3840]115 log_msg(2, "Calling buffer --- command = '%s'", g_call_to_buffer);
[3185]116 mr_asprintf(sz_dir, "%c", direction);
[3840]117 fres = popen(g_call_to_buffer, sz_dir);
[2519]118 mr_free(sz_dir);
119
[128]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);
[3840]128 mr_asprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_call_to_buffer);
[128]129 if (run_program_and_log_output(tmp, 2)) {
130 log_msg(2, "Warning - I think I failed to open tape, actually.");
131 }
[2519]132 mr_free(tmp);
[128]133 g_tape_buffer_size_MB = bufsize;
[3185]134 mr_asprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
[128]135 if (run_program_and_log_output(command, 1)) {
136 fres = NULL;
[541]137 log_to_screen("Failed to open tape streamer. Buffer error.");
[128]138 } else {
[541]139 log_to_screen("Buffer successfully started.");
[128]140 }
[2519]141 mr_free(command);
[1]142
[128]143 return (fres);
[1]144}
145
146
147/**
148 * Kill @c buffer processes.
[3840]149 * Only called in mondoarchive at then before exiting thus freeing memory
[1]150 */
151void kill_buffer()
152{
[2214]153 char *tmp = NULL;
154 char *command = NULL;
[1]155
[3840]156 if (g_call_to_buffer == NULL) {
[941]157 return;
158 }
[3840]159 if (strcmp(g_call_to_buffer,"") == 0) {
[941]160 return;
161 }
[3191]162 sync();
[3840]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);
[128]164 log_msg(2, "kill_buffer() --- command = %s", command);
[3610]165 tmp = call_program_and_get_last_line_of_output(command);
[2214]166 mr_free(command);
167
[3185]168 mr_asprintf(command, "kill %s", tmp);
[128]169 log_msg(2, "kill_buffer() --- command = %s", command);
170 if (strlen(tmp) > 0) {
171 run_program_and_log_output(command, TRUE);
172 }
[2214]173 mr_free(tmp);
174 mr_free(command);
[3840]175 mr_free(g_call_to_buffer);
[1]176}
177
178
179
180
181
182/**
183 * Handler for SIGPIPE.
184 */
[3865]185void sigpipe_occurred()
[1]186{
[128]187 g_sigpipe = TRUE;
[1]188}
189
190/* @} - end of fifoGroup */
Note: See TracBrowser for help on using the repository browser.