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