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