[42] | 1 | /* $Id: libmondo-fifo.c 795 2006-09-13 23:07:21Z bruno $ */
|
---|
[1] | 2 |
|
---|
| 3 | /**
|
---|
| 4 | * @file
|
---|
| 5 | * Functions to handle buffering of tape archives as they are read/written.
|
---|
| 6 | * This used the external program @c buffer mostly.
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | #include <unistd.h>
|
---|
| 10 | #include <stdio.h>
|
---|
[142] | 11 | #ifndef S_SPLINT_S
|
---|
[1] | 12 | #include <signal.h>
|
---|
[142] | 13 | #endif
|
---|
[42] | 14 | #include <fcntl.h>
|
---|
| 15 |
|
---|
[1] | 16 | #include <errno.h>
|
---|
| 17 | #include <sys/types.h>
|
---|
| 18 | #include <sys/stat.h>
|
---|
| 19 | #include <sys/ipc.h>
|
---|
| 20 | #include <sys/shm.h>
|
---|
| 21 | #include <sys/sem.h>
|
---|
| 22 | #include <sys/wait.h>
|
---|
[142] | 23 | #ifndef S_SPLINT_S
|
---|
[1] | 24 | #include <pthread.h>
|
---|
[142] | 25 | #endif
|
---|
[1] | 26 |
|
---|
| 27 | #include "my-stuff.h"
|
---|
| 28 | #include "mondostructures.h"
|
---|
| 29 | #include "libmondo.h"
|
---|
| 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;
|
---|
[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 | */
|
---|
[42] | 70 | FILE *open_device_via_buffer(char *device, char direction,
|
---|
| 71 | long internal_tape_block_size)
|
---|
[1] | 72 | {
|
---|
[42] | 73 | char *sz_dir;
|
---|
| 74 | char keych;
|
---|
| 75 | char *tmp;
|
---|
| 76 | FILE *fres;
|
---|
| 77 | int bufsize; // in megabytes
|
---|
| 78 | int res;
|
---|
| 79 | int wise_upper_limit;
|
---|
| 80 | int wise_lower_limit;
|
---|
[1] | 81 |
|
---|
[42] | 82 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 83 | assert(direction == 'w' || direction == 'r');
|
---|
| 84 | asprintf(&sz_dir, "%c", direction);
|
---|
| 85 | wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
|
---|
| 86 | wise_lower_limit = 1; // wise_upper_limit/2 + 1;
|
---|
[688] | 87 | sync();
|
---|
[42] | 88 | for (bufsize = wise_upper_limit, res = -1;
|
---|
| 89 | res != 0 && bufsize >= wise_lower_limit; bufsize--) {
|
---|
| 90 | asprintf(&tmp,
|
---|
| 91 | "dd if=/dev/zero bs=1024 count=16k 2> /dev/null | buffer -o /dev/null -s %ld -m %d%c",
|
---|
| 92 | internal_tape_block_size, bufsize, 'm');
|
---|
| 93 | res = run_program_and_log_output(tmp, 2);
|
---|
| 94 | paranoid_free(tmp);
|
---|
| 95 | }
|
---|
| 96 | if (!res) {
|
---|
| 97 | bufsize++;
|
---|
[688] | 98 | asprintf(&tmp, _("Negotiated max buffer of %d MB "), bufsize);
|
---|
[42] | 99 | log_to_screen(tmp);
|
---|
| 100 | paranoid_free(tmp);
|
---|
| 101 | } else {
|
---|
| 102 | bufsize = 0;
|
---|
| 103 | res = 0;
|
---|
| 104 | log_to_screen
|
---|
[507] | 105 | (_("Cannot negotiate a buffer of ANY size. Using dd instead."));
|
---|
[42] | 106 | }
|
---|
| 107 | if (direction == 'r') {
|
---|
| 108 | keych = 'i';
|
---|
| 109 | } else {
|
---|
| 110 | keych = 'o';
|
---|
| 111 | }
|
---|
| 112 | if (bufsize) {
|
---|
| 113 | asprintf(&g_sz_call_to_buffer,
|
---|
| 114 | "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize,
|
---|
| 115 | 'm', (direction == 'r') ? 20 : 75,
|
---|
| 116 | internal_tape_block_size, keych, device, MONDO_LOGFILE);
|
---|
| 117 | } else {
|
---|
| 118 | asprintf(&g_sz_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
| 119 | internal_tape_block_size, keych, device);
|
---|
| 120 | }
|
---|
| 121 | log_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
|
---|
| 122 | fres = popen(g_sz_call_to_buffer, sz_dir);
|
---|
| 123 | paranoid_free(sz_dir);
|
---|
| 124 | if (fres) {
|
---|
| 125 | log_msg(2, "Successfully opened ('%c') tape device %s", direction,
|
---|
| 126 | device);
|
---|
| 127 | } else {
|
---|
| 128 | log_msg(2, "Failed to open ('%c') tape device %s", direction,
|
---|
| 129 | device);
|
---|
| 130 | }
|
---|
| 131 | sleep(2);
|
---|
[794] | 132 | asprintf(&tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
|
---|
[42] | 133 | if (run_program_and_log_output(tmp, 2)) {
|
---|
| 134 | log_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
| 135 | }
|
---|
| 136 | paranoid_free(tmp);
|
---|
| 137 | g_tape_buffer_size_MB = bufsize;
|
---|
[794] | 138 | asprintf(&tmp, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
[42] | 139 | if (run_program_and_log_output(tmp, 1)) {
|
---|
| 140 | fres = NULL;
|
---|
[507] | 141 | log_to_screen(_("Failed to open tape streamer. Buffer error."));
|
---|
[42] | 142 | } else {
|
---|
[507] | 143 | log_to_screen(_("Buffer successfully started."));
|
---|
[42] | 144 | }
|
---|
| 145 | paranoid_free(tmp);
|
---|
| 146 | return (fres);
|
---|
[1] | 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * Kill @c buffer processes.
|
---|
| 152 | */
|
---|
| 153 | void kill_buffer()
|
---|
| 154 | {
|
---|
[42] | 155 | char *tmp;
|
---|
| 156 | char *command;
|
---|
[1] | 157 |
|
---|
[688] | 158 | sync();
|
---|
[42] | 159 | asprintf(&command,
|
---|
[794] | 160 | "ps | grep -F \"%s\" | grep -Fv grep | awk '{print $1;}' | grep -v PID | tr -s '\n' ' ' | awk '{ print $1; }'", ps_options,
|
---|
[687] | 161 | g_sz_call_to_buffer);
|
---|
[42] | 162 | paranoid_free(g_sz_call_to_buffer);
|
---|
| 163 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[1] | 164 |
|
---|
[688] | 165 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[42] | 166 | paranoid_free(command);
|
---|
[1] | 167 |
|
---|
[42] | 168 | asprintf(&command, "kill %s", tmp);
|
---|
| 169 | log_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[1] | 170 |
|
---|
[42] | 171 | if (strlen(tmp) > 0) {
|
---|
| 172 | run_program_and_log_output(command, TRUE);
|
---|
| 173 | }
|
---|
[688] | 174 | paranoid_free(tmp);
|
---|
[42] | 175 | paranoid_free(command);
|
---|
| 176 | }
|
---|
[1] | 177 |
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Handler for SIGPIPE.
|
---|
| 181 | * @param sig The signal that occurred (hopefully SIGPIPE).
|
---|
| 182 | */
|
---|
| 183 | void sigpipe_occurred(int sig)
|
---|
| 184 | {
|
---|
[42] | 185 | g_sigpipe = TRUE;
|
---|
[1] | 186 | }
|
---|
| 187 |
|
---|
| 188 | /* @} - end of fifoGroup */
|
---|
| 189 |
|
---|
[42] | 190 | /* BERLIOS: useless ?
|
---|
[1] | 191 | int
|
---|
| 192 | extract_config_file_from_ramdisk( struct s_bkpinfo *bkpinfo,
|
---|
| 193 | char *ramdisk_fname,
|
---|
| 194 | char *output_cfg_file,
|
---|
| 195 | char *output_mountlist_file);
|
---|
[42] | 196 | */
|
---|