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