[2030] | 1 | /*
|
---|
[128] | 2 | $Id: libmondo-fifo.c 3840 2024-03-06 00:20:15Z 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] | 34 | bool g_sigpipe = FALSE;
|
---|
[1] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * PID of the "main" process.
|
---|
| 38 | */
|
---|
[128] | 39 | pid_t g_mastermind_pid = 0;
|
---|
[1] | 40 |
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Command line with which @c buffer was invoked.
|
---|
| 44 | */
|
---|
[3840] | 45 | char *g_call_to_buffer = NULL;
|
---|
[1] | 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Size of the buffer used with @c buffer.
|
---|
| 49 | */
|
---|
[128] | 50 | int g_tape_buffer_size_MB = 0;
|
---|
[1] | 51 |
|
---|
| 52 | /* @} - end of globalGroup */
|
---|
| 53 |
|
---|
[792] | 54 | extern char *ps_options;
|
---|
[928] | 55 | extern char *ps_proc_id;
|
---|
[1316] | 56 | extern 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] | 69 | FILE *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) {
|
---|
[3840] | 108 | mr_asprintf(g_call_to_buffer,
|
---|
[128] | 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 {
|
---|
[3840] | 113 | mr_asprintf(g_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
[128] | 114 | internal_tape_block_size, keych, device);
|
---|
| 115 | }
|
---|
[3840] | 116 | log_msg(2, "Calling buffer --- command = '%s'", g_call_to_buffer);
|
---|
[3185] | 117 | mr_asprintf(sz_dir, "%c", direction);
|
---|
[3840] | 118 | fres = popen(g_call_to_buffer, sz_dir);
|
---|
[2519] | 119 | mr_free(sz_dir);
|
---|
| 120 |
|
---|
[128] | 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);
|
---|
[3840] | 129 | mr_asprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_call_to_buffer);
|
---|
[128] | 130 | if (run_program_and_log_output(tmp, 2)) {
|
---|
| 131 | log_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
| 132 | }
|
---|
[2519] | 133 | mr_free(tmp);
|
---|
[128] | 134 | g_tape_buffer_size_MB = bufsize;
|
---|
[3185] | 135 | mr_asprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
[128] | 136 | if (run_program_and_log_output(command, 1)) {
|
---|
| 137 | fres = NULL;
|
---|
[541] | 138 | log_to_screen("Failed to open tape streamer. Buffer error.");
|
---|
[128] | 139 | } else {
|
---|
[541] | 140 | log_to_screen("Buffer successfully started.");
|
---|
[128] | 141 | }
|
---|
[2519] | 142 | mr_free(command);
|
---|
[1] | 143 |
|
---|
[128] | 144 | return (fres);
|
---|
[1] | 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Kill @c buffer processes.
|
---|
[3840] | 150 | * Only called in mondoarchive at then before exiting thus freeing memory
|
---|
[1] | 151 | */
|
---|
| 152 | void kill_buffer()
|
---|
| 153 | {
|
---|
[2214] | 154 | char *tmp = NULL;
|
---|
| 155 | char *command = NULL;
|
---|
[1] | 156 |
|
---|
[3840] | 157 | if (g_call_to_buffer == NULL) {
|
---|
[941] | 158 | return;
|
---|
| 159 | }
|
---|
[3840] | 160 | if (strcmp(g_call_to_buffer,"") == 0) {
|
---|
[941] | 161 | return;
|
---|
| 162 | }
|
---|
[3191] | 163 | sync();
|
---|
[3840] | 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);
|
---|
[128] | 165 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[3610] | 166 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[2214] | 167 | mr_free(command);
|
---|
| 168 |
|
---|
[3185] | 169 | mr_asprintf(command, "kill %s", tmp);
|
---|
[128] | 170 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
| 171 | if (strlen(tmp) > 0) {
|
---|
| 172 | run_program_and_log_output(command, TRUE);
|
---|
| 173 | }
|
---|
[2214] | 174 | mr_free(tmp);
|
---|
| 175 | mr_free(command);
|
---|
[3840] | 176 | mr_free(g_call_to_buffer);
|
---|
[1] | 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * Handler for SIGPIPE.
|
---|
| 185 | * @param sig The signal that occurred (hopefully SIGPIPE).
|
---|
| 186 | */
|
---|
| 187 | void sigpipe_occurred(int sig)
|
---|
| 188 | {
|
---|
[128] | 189 | g_sigpipe = TRUE;
|
---|
[1] | 190 | }
|
---|
| 191 |
|
---|
| 192 | /* @} - end of fifoGroup */
|
---|