[2030] | 1 | /*
|
---|
[128] | 2 | $Id: libmondo-fifo.c 3610 2016-11-05 17:12:23Z 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 | /**
|
---|
| 44 | * Command line with which @c buffer was invoked.
|
---|
| 45 | */
|
---|
| 46 | char g_sz_call_to_buffer[MAX_STR_LEN];
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Size of the buffer used with @c buffer.
|
---|
| 50 | */
|
---|
[128] | 51 | int g_tape_buffer_size_MB = 0;
|
---|
[1] | 52 |
|
---|
| 53 | /* @} - end of globalGroup */
|
---|
| 54 |
|
---|
[792] | 55 | extern char *ps_options;
|
---|
[928] | 56 | extern char *ps_proc_id;
|
---|
[1316] | 57 | extern char *MONDO_LOGFILE;
|
---|
[1] | 58 |
|
---|
| 59 | /**
|
---|
| 60 | * @addtogroup fifoGroup
|
---|
| 61 | * @{
|
---|
| 62 | */
|
---|
| 63 | /**
|
---|
| 64 | * Open a pipe to/from @c buffer.
|
---|
| 65 | * If buffer does not work at all, we use `dd'.
|
---|
| 66 | * @param device The device to read from/write to.
|
---|
| 67 | * @param direction @c 'r' (reading) or @c 'w' (writing).
|
---|
| 68 | * @return A file pointer to/from the @c buffer process.
|
---|
| 69 | */
|
---|
[128] | 70 | FILE *open_device_via_buffer(char *device, char direction,
|
---|
| 71 | long internal_tape_block_size)
|
---|
[1] | 72 | {
|
---|
[128] | 73 | char keych;
|
---|
[2519] | 74 | char *sz_dir = NULL;
|
---|
| 75 | char *tmp = NULL;
|
---|
| 76 | char *command = NULL;
|
---|
[128] | 77 | FILE *fres;
|
---|
| 78 | int bufsize; // in megabytes
|
---|
| 79 | int res;
|
---|
| 80 | int wise_upper_limit;
|
---|
| 81 | int wise_lower_limit;
|
---|
[1] | 82 |
|
---|
[128] | 83 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 84 | assert(direction == 'w' || direction == 'r');
|
---|
| 85 | wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
|
---|
| 86 | wise_lower_limit = 1; // wise_upper_limit/2 + 1;
|
---|
[3191] | 87 | sync();
|
---|
[128] | 88 | for (bufsize = wise_upper_limit, res = -1;
|
---|
| 89 | res != 0 && bufsize >= wise_lower_limit; bufsize--) {
|
---|
[3191] | 90 | 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] | 91 | res = run_program_and_log_output(tmp, 2);
|
---|
[2519] | 92 | mr_free(tmp);
|
---|
[128] | 93 | }
|
---|
| 94 | if (!res) {
|
---|
| 95 | bufsize++;
|
---|
[3191] | 96 | log_to_screen(tmp, "Negotiated max buffer of %d MB ", bufsize);
|
---|
[128] | 97 | } else {
|
---|
| 98 | bufsize = 0;
|
---|
| 99 | res = 0;
|
---|
| 100 | log_to_screen
|
---|
[541] | 101 | ("Cannot negotiate a buffer of ANY size. Using dd instead.");
|
---|
[128] | 102 | }
|
---|
| 103 | if (direction == 'r') {
|
---|
| 104 | keych = 'i';
|
---|
| 105 | } else {
|
---|
| 106 | keych = 'o';
|
---|
| 107 | }
|
---|
| 108 | if (bufsize) {
|
---|
| 109 | sprintf(g_sz_call_to_buffer,
|
---|
| 110 | "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
|
---|
| 111 | (direction == 'r') ? 20 : 75, internal_tape_block_size,
|
---|
| 112 | keych, device, MONDO_LOGFILE);
|
---|
| 113 | } else {
|
---|
| 114 | sprintf(g_sz_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
| 115 | internal_tape_block_size, keych, device);
|
---|
| 116 | }
|
---|
| 117 | log_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
|
---|
[3185] | 118 | mr_asprintf(sz_dir, "%c", direction);
|
---|
[128] | 119 | fres = popen(g_sz_call_to_buffer, sz_dir);
|
---|
[2519] | 120 | mr_free(sz_dir);
|
---|
| 121 |
|
---|
[128] | 122 | if (fres) {
|
---|
| 123 | log_msg(2, "Successfully opened ('%c') tape device %s", direction,
|
---|
| 124 | device);
|
---|
| 125 | } else {
|
---|
| 126 | log_msg(2, "Failed to open ('%c') tape device %s", direction,
|
---|
| 127 | device);
|
---|
| 128 | }
|
---|
| 129 | sleep(2);
|
---|
[3185] | 130 | mr_asprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
|
---|
[128] | 131 | if (run_program_and_log_output(tmp, 2)) {
|
---|
| 132 | log_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
| 133 | }
|
---|
[2519] | 134 | mr_free(tmp);
|
---|
[128] | 135 | g_tape_buffer_size_MB = bufsize;
|
---|
[3185] | 136 | mr_asprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
[128] | 137 | if (run_program_and_log_output(command, 1)) {
|
---|
| 138 | fres = NULL;
|
---|
[541] | 139 | log_to_screen("Failed to open tape streamer. Buffer error.");
|
---|
[128] | 140 | } else {
|
---|
[541] | 141 | log_to_screen("Buffer successfully started.");
|
---|
[128] | 142 | }
|
---|
[2519] | 143 | mr_free(command);
|
---|
[1] | 144 |
|
---|
[128] | 145 | return (fres);
|
---|
[1] | 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * Kill @c buffer processes.
|
---|
[928] | 151 | * Only called in mondoarchive
|
---|
[1] | 152 | */
|
---|
| 153 | void kill_buffer()
|
---|
| 154 | {
|
---|
[2214] | 155 | char *tmp = NULL;
|
---|
| 156 | char *command = NULL;
|
---|
[1] | 157 |
|
---|
[941] | 158 | if (g_sz_call_to_buffer == NULL) {
|
---|
| 159 | return;
|
---|
| 160 | }
|
---|
| 161 | if (strcmp(g_sz_call_to_buffer,"") == 0) {
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
[3191] | 164 | sync();
|
---|
[3185] | 165 | mr_asprintf(command, "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_sz_call_to_buffer);
|
---|
[128] | 166 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[3610] | 167 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[2214] | 168 | mr_free(command);
|
---|
| 169 |
|
---|
[3185] | 170 | mr_asprintf(command, "kill %s", tmp);
|
---|
[128] | 171 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
| 172 | if (strlen(tmp) > 0) {
|
---|
| 173 | run_program_and_log_output(command, TRUE);
|
---|
| 174 | }
|
---|
[2214] | 175 | mr_free(tmp);
|
---|
| 176 | mr_free(command);
|
---|
[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 */
|
---|