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