[42] | 1 | /* $Id: libmondo-fifo.c 1086 2007-02-02 22:21:27Z bruno $ */
|
---|
[1] | 2 | /**
|
---|
| 3 | * @file
|
---|
| 4 | * Functions to handle buffering of tape archives as they are read/written.
|
---|
| 5 | * This used the external program @c buffer mostly.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <unistd.h>
|
---|
| 9 | #include <stdio.h>
|
---|
[142] | 10 | #ifndef S_SPLINT_S
|
---|
[1] | 11 | #include <signal.h>
|
---|
[142] | 12 | #endif
|
---|
[42] | 13 | #include <fcntl.h>
|
---|
| 14 |
|
---|
[1] | 15 | #include <errno.h>
|
---|
| 16 | #include <sys/types.h>
|
---|
| 17 | #include <sys/stat.h>
|
---|
| 18 | #include <sys/ipc.h>
|
---|
| 19 | #include <sys/shm.h>
|
---|
| 20 | #include <sys/sem.h>
|
---|
| 21 | #include <sys/wait.h>
|
---|
[142] | 22 | #ifndef S_SPLINT_S
|
---|
[1] | 23 | #include <pthread.h>
|
---|
[142] | 24 | #endif
|
---|
[1] | 25 |
|
---|
| 26 | #include "my-stuff.h"
|
---|
| 27 | #include "mondostructures.h"
|
---|
| 28 | #include "libmondo.h"
|
---|
[900] | 29 | #include "mr_mem.h"
|
---|
[1] | 30 |
|
---|
| 31 | /**
|
---|
| 32 | * @addtogroup globalGroup
|
---|
| 33 | * @{
|
---|
| 34 | */
|
---|
| 35 | /**
|
---|
| 36 | * The SIGPIPE handler sets this to TRUE.
|
---|
| 37 | */
|
---|
[42] | 38 | bool g_sigpipe = FALSE;
|
---|
[1] | 39 |
|
---|
| 40 | /**
|
---|
| 41 | * PID of the "main" process.
|
---|
| 42 | */
|
---|
[42] | 43 | pid_t g_mastermind_pid = 0;
|
---|
[1] | 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Command line with which @c buffer was invoked.
|
---|
| 47 | */
|
---|
[42] | 48 | char *g_sz_call_to_buffer;
|
---|
[1] | 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Size of the buffer used with @c buffer.
|
---|
| 52 | */
|
---|
[42] | 53 | int g_tape_buffer_size_MB = 0;
|
---|
[1] | 54 |
|
---|
| 55 | /* @} - end of globalGroup */
|
---|
| 56 |
|
---|
[794] | 57 | extern char *ps_options;
|
---|
[932] | 58 | extern char *ps_proc_id;
|
---|
[1] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * @addtogroup fifoGroup
|
---|
| 62 | * @{
|
---|
| 63 | */
|
---|
| 64 | /**
|
---|
| 65 | * Open a pipe to/from @c buffer.
|
---|
| 66 | * If buffer does not work at all, we use `dd'.
|
---|
| 67 | * @param device The device to read from/write to.
|
---|
| 68 | * @param direction @c 'r' (reading) or @c 'w' (writing).
|
---|
| 69 | * @return A file pointer to/from the @c buffer process.
|
---|
| 70 | */
|
---|
[42] | 71 | FILE *open_device_via_buffer(char *device, char direction,
|
---|
| 72 | long internal_tape_block_size)
|
---|
[1] | 73 | {
|
---|
[42] | 74 | char *sz_dir;
|
---|
| 75 | char keych;
|
---|
| 76 | char *tmp;
|
---|
| 77 | FILE *fres;
|
---|
| 78 | int bufsize; // in megabytes
|
---|
| 79 | int res;
|
---|
| 80 | int wise_upper_limit;
|
---|
| 81 | int wise_lower_limit;
|
---|
[1] | 82 |
|
---|
[42] | 83 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 84 | assert(direction == 'w' || direction == 'r');
|
---|
[900] | 85 | mr_asprintf(&sz_dir, "%c", direction);
|
---|
[42] | 86 | wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
|
---|
| 87 | wise_lower_limit = 1; // wise_upper_limit/2 + 1;
|
---|
[688] | 88 | sync();
|
---|
[42] | 89 | for (bufsize = wise_upper_limit, res = -1;
|
---|
| 90 | res != 0 && bufsize >= wise_lower_limit; bufsize--) {
|
---|
[900] | 91 | mr_asprintf(&tmp,
|
---|
[42] | 92 | "dd if=/dev/zero bs=1024 count=16k 2> /dev/null | buffer -o /dev/null -s %ld -m %d%c",
|
---|
| 93 | internal_tape_block_size, bufsize, 'm');
|
---|
| 94 | res = run_program_and_log_output(tmp, 2);
|
---|
[900] | 95 | mr_free(tmp);
|
---|
[42] | 96 | }
|
---|
| 97 | if (!res) {
|
---|
| 98 | bufsize++;
|
---|
[900] | 99 | mr_asprintf(&tmp, _("Negotiated max buffer of %d MB "), bufsize);
|
---|
[42] | 100 | log_to_screen(tmp);
|
---|
[900] | 101 | mr_free(tmp);
|
---|
[42] | 102 | } else {
|
---|
| 103 | bufsize = 0;
|
---|
| 104 | res = 0;
|
---|
| 105 | log_to_screen
|
---|
[507] | 106 | (_("Cannot negotiate a buffer of ANY size. Using dd instead."));
|
---|
[42] | 107 | }
|
---|
| 108 | if (direction == 'r') {
|
---|
| 109 | keych = 'i';
|
---|
| 110 | } else {
|
---|
| 111 | keych = 'o';
|
---|
| 112 | }
|
---|
| 113 | if (bufsize) {
|
---|
[900] | 114 | mr_asprintf(&g_sz_call_to_buffer,
|
---|
[42] | 115 | "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize,
|
---|
| 116 | 'm', (direction == 'r') ? 20 : 75,
|
---|
| 117 | internal_tape_block_size, keych, device, MONDO_LOGFILE);
|
---|
| 118 | } else {
|
---|
[900] | 119 | mr_asprintf(&g_sz_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
[42] | 120 | internal_tape_block_size, keych, device);
|
---|
| 121 | }
|
---|
[1086] | 122 | mr_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
|
---|
[42] | 123 | fres = popen(g_sz_call_to_buffer, sz_dir);
|
---|
[900] | 124 | mr_free(sz_dir);
|
---|
[42] | 125 | if (fres) {
|
---|
[1086] | 126 | mr_msg(2, "Successfully opened ('%c') tape device %s", direction,
|
---|
[42] | 127 | device);
|
---|
| 128 | } else {
|
---|
[1086] | 129 | mr_msg(2, "Failed to open ('%c') tape device %s", direction,
|
---|
[42] | 130 | device);
|
---|
| 131 | }
|
---|
| 132 | sleep(2);
|
---|
[900] | 133 | mr_asprintf(&tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
|
---|
[42] | 134 | if (run_program_and_log_output(tmp, 2)) {
|
---|
[1086] | 135 | mr_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
[42] | 136 | }
|
---|
[900] | 137 | mr_free(tmp);
|
---|
[42] | 138 | g_tape_buffer_size_MB = bufsize;
|
---|
[900] | 139 | mr_asprintf(&tmp, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
[42] | 140 | if (run_program_and_log_output(tmp, 1)) {
|
---|
| 141 | fres = NULL;
|
---|
[507] | 142 | log_to_screen(_("Failed to open tape streamer. Buffer error."));
|
---|
[42] | 143 | } else {
|
---|
[507] | 144 | log_to_screen(_("Buffer successfully started."));
|
---|
[42] | 145 | }
|
---|
[900] | 146 | mr_free(tmp);
|
---|
[42] | 147 | return (fres);
|
---|
[1] | 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Kill @c buffer processes.
|
---|
[932] | 153 | * Only called in mondoarchive
|
---|
[1] | 154 | */
|
---|
| 155 | void kill_buffer()
|
---|
| 156 | {
|
---|
[42] | 157 | char *tmp;
|
---|
| 158 | char *command;
|
---|
[1] | 159 |
|
---|
[688] | 160 | sync();
|
---|
[956] | 161 | if (g_sz_call_to_buffer == NULL) {
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
| 164 | if (strcmp(g_sz_call_to_buffer,"") == 0) {
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
[900] | 167 | mr_asprintf(&command,
|
---|
[956] | 168 | "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_sz_call_to_buffer);
|
---|
[900] | 169 | mr_free(g_sz_call_to_buffer);
|
---|
[1086] | 170 | mr_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[1] | 171 |
|
---|
[688] | 172 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[900] | 173 | mr_free(command);
|
---|
[1] | 174 |
|
---|
[900] | 175 | mr_asprintf(&command, "kill %s", tmp);
|
---|
[1086] | 176 | mr_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[1] | 177 |
|
---|
[42] | 178 | if (strlen(tmp) > 0) {
|
---|
| 179 | run_program_and_log_output(command, TRUE);
|
---|
| 180 | }
|
---|
[900] | 181 | mr_free(tmp);
|
---|
| 182 | mr_free(command);
|
---|
[42] | 183 | }
|
---|
[1] | 184 |
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Handler for SIGPIPE.
|
---|
| 188 | * @param sig The signal that occurred (hopefully SIGPIPE).
|
---|
| 189 | */
|
---|
| 190 | void sigpipe_occurred(int sig)
|
---|
| 191 | {
|
---|
[42] | 192 | g_sigpipe = TRUE;
|
---|
[1] | 193 | }
|
---|
| 194 |
|
---|
| 195 | /* @} - end of fifoGroup */
|
---|
| 196 |
|
---|
[42] | 197 | /* BERLIOS: useless ?
|
---|
[1] | 198 | int
|
---|
| 199 | extract_config_file_from_ramdisk( struct s_bkpinfo *bkpinfo,
|
---|
| 200 | char *ramdisk_fname,
|
---|
| 201 | char *output_cfg_file,
|
---|
| 202 | char *output_mountlist_file);
|
---|
[42] | 203 | */
|
---|